Skip to content

Commit

Permalink
Liqoctl: install warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 authored and adamjensenbot committed Aug 3, 2023
1 parent a0e3f1b commit 3e1f2e3
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/liqoctl/install/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
86 changes: 86 additions & 0 deletions pkg/liqoctl/install/warning.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions pkg/liqoctl/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3e1f2e3

Please sign in to comment.