Skip to content

Commit 3e1f2e3

Browse files
cheina97adamjensenbot
authored andcommitted
Liqoctl: install warnings
1 parent a0e3f1b commit 3e1f2e3

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

pkg/liqoctl/install/handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ func (o *Options) Run(ctx context.Context, provider Provider) error {
170170
}
171171
}
172172

173+
warnings, err := ValuesWarning(values)
174+
if err != nil {
175+
s.Fail("Error generating installation warnings: ", output.PrettyErr(err))
176+
return err
177+
}
178+
for _, warning := range warnings {
179+
s = o.Printer.SpinnerRunningWarning(s, warning)
180+
}
181+
173182
rawValues, err := yaml.Marshal(values)
174183
if err != nil {
175184
s.Fail("Error generating values file: ", output.PrettyErr(err))

pkg/liqoctl/install/warning.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2019-2023 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package install
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/pterm/pterm"
21+
corev1 "k8s.io/api/core/v1"
22+
23+
"github.com/liqotech/liqo/pkg/liqoctl/util"
24+
)
25+
26+
type warner interface {
27+
name() string
28+
check(values map[string]interface{}) (bool, error)
29+
warn() []string
30+
}
31+
32+
type serviceWarner struct {
33+
warnings []string
34+
}
35+
36+
var _ warner = &serviceWarner{}
37+
38+
func (sw *serviceWarner) name() string {
39+
return "Service type"
40+
}
41+
42+
func (sw *serviceWarner) check(values map[string]interface{}) (bool, error) {
43+
var value interface{}
44+
var svctype string
45+
var err error
46+
var ok bool
47+
48+
components := []string{"gateway", "auth"}
49+
50+
for _, component := range components {
51+
if value, err = util.ExtractValuesFromNestedMaps(values, component, "service", "type"); err != nil {
52+
return false, err
53+
}
54+
55+
if svctype, ok = value.(string); !ok {
56+
return false, fmt.Errorf("cannot cast %v to string", value)
57+
}
58+
59+
if corev1.ServiceType(svctype) == corev1.ServiceTypeClusterIP {
60+
sw.warnings = append(sw.warnings, fmt.Sprintf(
61+
"Service type of %s is %s. It will not be reachable from outside the cluster",
62+
pterm.Bold.Sprintf("liqo-%s", component), pterm.Bold.Sprintf("%s", svctype)))
63+
}
64+
}
65+
return len(sw.warnings) == 0, nil
66+
}
67+
68+
func (sw *serviceWarner) warn() []string {
69+
return sw.warnings
70+
}
71+
72+
// ValuesWarning checks the values map and returns a list of warnings.
73+
func ValuesWarning(values map[string]interface{}) ([]string, error) {
74+
warners := []warner{&serviceWarner{}}
75+
warnings := []string{}
76+
for i := range warners {
77+
ok, err := warners[i].check(values)
78+
if err != nil {
79+
return warnings, fmt.Errorf("cannot check %s: %w", warners[i].name(), err)
80+
}
81+
if !ok {
82+
warnings = append(warnings, warners[i].warn()...)
83+
}
84+
}
85+
return warnings, nil
86+
}

pkg/liqoctl/output/output.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ type Printer struct {
9898
verbose bool
9999
}
100100

101+
// SpinnerRunningWarning prints a warning message while a spinner is running.
102+
// It returns a new spinner printer which must be used instead of the one passed in the arguments.
103+
func (p *Printer) SpinnerRunningWarning(spinner *pterm.SpinnerPrinter, message ...interface{}) *pterm.SpinnerPrinter {
104+
spinner.Warning(message...)
105+
return p.StartSpinner(spinner.Text)
106+
}
107+
108+
// SpinnerRunningSuccess prints a success message while a spinner is running.
109+
// It returns a new spinner printer which must be used instead of the one passed in the arguments.
110+
func (p *Printer) SpinnerRunningSuccess(spinner *pterm.SpinnerPrinter, message ...interface{}) *pterm.SpinnerPrinter {
111+
spinner.Success(message...)
112+
return p.StartSpinner(spinner.Text)
113+
}
114+
101115
// AskConfirm asks the user to confirm an action.
102116
func (p *Printer) AskConfirm(cmdName string, skip bool) error {
103117
if skip {

0 commit comments

Comments
 (0)