|
| 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 | +} |
0 commit comments