From 3e1f2e3c79eecf1175f60bfcfef28493b7faabf3 Mon Sep 17 00:00:00 2001 From: Francesco Cheinasso Date: Wed, 2 Aug 2023 16:40:44 +0200 Subject: [PATCH] Liqoctl: install warnings --- pkg/liqoctl/install/handler.go | 9 ++++ pkg/liqoctl/install/warning.go | 86 ++++++++++++++++++++++++++++++++++ pkg/liqoctl/output/output.go | 14 ++++++ 3 files changed, 109 insertions(+) create mode 100644 pkg/liqoctl/install/warning.go diff --git a/pkg/liqoctl/install/handler.go b/pkg/liqoctl/install/handler.go index 7497f6a0c5..b7b4344fa2 100644 --- a/pkg/liqoctl/install/handler.go +++ b/pkg/liqoctl/install/handler.go @@ -170,6 +170,15 @@ func (o *Options) Run(ctx context.Context, provider Provider) error { } } + warnings, err := ValuesWarning(values) + if err != nil { + s.Fail("Error generating installation warnings: ", output.PrettyErr(err)) + return err + } + for _, warning := range warnings { + s = o.Printer.SpinnerRunningWarning(s, warning) + } + rawValues, err := yaml.Marshal(values) if err != nil { s.Fail("Error generating values file: ", output.PrettyErr(err)) diff --git a/pkg/liqoctl/install/warning.go b/pkg/liqoctl/install/warning.go new file mode 100644 index 0000000000..215f1ebb36 --- /dev/null +++ b/pkg/liqoctl/install/warning.go @@ -0,0 +1,86 @@ +// Copyright 2019-2023 The Liqo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package install + +import ( + "fmt" + + "github.com/pterm/pterm" + corev1 "k8s.io/api/core/v1" + + "github.com/liqotech/liqo/pkg/liqoctl/util" +) + +type warner interface { + name() string + check(values map[string]interface{}) (bool, error) + warn() []string +} + +type serviceWarner struct { + warnings []string +} + +var _ warner = &serviceWarner{} + +func (sw *serviceWarner) name() string { + return "Service type" +} + +func (sw *serviceWarner) check(values map[string]interface{}) (bool, error) { + var value interface{} + var svctype string + var err error + var ok bool + + components := []string{"gateway", "auth"} + + for _, component := range components { + if value, err = util.ExtractValuesFromNestedMaps(values, component, "service", "type"); err != nil { + return false, err + } + + if svctype, ok = value.(string); !ok { + return false, fmt.Errorf("cannot cast %v to string", value) + } + + if corev1.ServiceType(svctype) == corev1.ServiceTypeClusterIP { + sw.warnings = append(sw.warnings, fmt.Sprintf( + "Service type of %s is %s. It will not be reachable from outside the cluster", + pterm.Bold.Sprintf("liqo-%s", component), pterm.Bold.Sprintf("%s", svctype))) + } + } + return len(sw.warnings) == 0, nil +} + +func (sw *serviceWarner) warn() []string { + return sw.warnings +} + +// ValuesWarning checks the values map and returns a list of warnings. +func ValuesWarning(values map[string]interface{}) ([]string, error) { + warners := []warner{&serviceWarner{}} + warnings := []string{} + for i := range warners { + ok, err := warners[i].check(values) + if err != nil { + return warnings, fmt.Errorf("cannot check %s: %w", warners[i].name(), err) + } + if !ok { + warnings = append(warnings, warners[i].warn()...) + } + } + return warnings, nil +} diff --git a/pkg/liqoctl/output/output.go b/pkg/liqoctl/output/output.go index 4071c34f24..766a2eb862 100644 --- a/pkg/liqoctl/output/output.go +++ b/pkg/liqoctl/output/output.go @@ -98,6 +98,20 @@ type Printer struct { verbose bool } +// SpinnerRunningWarning prints a warning message while a spinner is running. +// It returns a new spinner printer which must be used instead of the one passed in the arguments. +func (p *Printer) SpinnerRunningWarning(spinner *pterm.SpinnerPrinter, message ...interface{}) *pterm.SpinnerPrinter { + spinner.Warning(message...) + return p.StartSpinner(spinner.Text) +} + +// SpinnerRunningSuccess prints a success message while a spinner is running. +// It returns a new spinner printer which must be used instead of the one passed in the arguments. +func (p *Printer) SpinnerRunningSuccess(spinner *pterm.SpinnerPrinter, message ...interface{}) *pterm.SpinnerPrinter { + spinner.Success(message...) + return p.StartSpinner(spinner.Text) +} + // AskConfirm asks the user to confirm an action. func (p *Printer) AskConfirm(cmdName string, skip bool) error { if skip {