Skip to content

Commit

Permalink
Update error msgs for OSImageURL validation:
Browse files Browse the repository at this point in the history
As the error message is provided to Users,
including the expected Kubernetes version format
to the error message allows to User to clearly
understand how to resolve the validation error.

Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
  • Loading branch information
jacobweinstock committed Mar 2, 2024
1 parent 57c91bc commit 482e07d
Showing 1 changed file with 74 additions and 6 deletions.
80 changes: 74 additions & 6 deletions pkg/providers/tinkerbell/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func validateK8sVersionInOSImageURLs(spec *ClusterSpec) error {
kvs := spec.Cluster.KubernetesVersions()
for _, v := range kvs {
if !containsK8sVersion(spec.DatacenterConfig.Spec.OSImageURL, string(v)) {
return fmt.Errorf("missing kube version from OSImageURL: url=%v, version=%v",
spec.DatacenterConfig.Spec.OSImageURL, v)
return fmt.Errorf("missing kube version from OSImageURL, the image url should include %v: url=%v, version=%v",
permutations(v, []string{"-", "_", ""}), spec.DatacenterConfig.Spec.OSImageURL, v)

Check failure on line 136 in pkg/providers/tinkerbell/validate.go

View workflow job for this annotation

GitHub Actions / build

cannot use v (variable of type "github.com/aws/eks-anywhere/pkg/api/v1alpha1".KubernetesVersion) as string value in argument to permutations

Check failure on line 136 in pkg/providers/tinkerbell/validate.go

View workflow job for this annotation

GitHub Actions / govulncheck

cannot use v (variable of type "github.com/aws/eks-anywhere/pkg/api/v1alpha1".KubernetesVersion) as string value in argument to permutations
}
}
} else {
Expand All @@ -150,8 +150,8 @@ func validateK8sVersionInOSImageURLs(spec *ClusterSpec) error {
}

if !containsK8sVersion(spec.ControlPlaneMachineConfig().Spec.OSImageURL, string(spec.Cluster.Spec.KubernetesVersion)) {
return fmt.Errorf("missing kube version from control plane machine config OSImageURL: url=%v, version=%v",
spec.ControlPlaneMachineConfig().Spec.OSImageURL, spec.Cluster.Spec.KubernetesVersion)
return fmt.Errorf("missing kube version from control plane machine config OSImageURL, the image url should include %v: url=%v, version=%v",
permutations(spec.Cluster.Spec.KubernetesVersion, []string{"-", "_", ""}), spec.ControlPlaneMachineConfig().Spec.OSImageURL, spec.Cluster.Spec.KubernetesVersion)

Check failure on line 154 in pkg/providers/tinkerbell/validate.go

View workflow job for this annotation

GitHub Actions / build

cannot use spec.Cluster.Spec.KubernetesVersion (variable of type "github.com/aws/eks-anywhere/pkg/api/v1alpha1".KubernetesVersion) as string value in argument to permutations

Check failure on line 154 in pkg/providers/tinkerbell/validate.go

View workflow job for this annotation

GitHub Actions / govulncheck

cannot use spec.Cluster.Spec.KubernetesVersion (variable of type "github.com/aws/eks-anywhere/pkg/api/v1alpha1".KubernetesVersion) as string value in argument to permutations
}

for _, wng := range spec.WorkerNodeGroupConfigurations() {
Expand All @@ -162,14 +162,82 @@ func validateK8sVersionInOSImageURLs(spec *ClusterSpec) error {
}

if !containsK8sVersion(url, string(version)) {
return fmt.Errorf("missing kube version from worker node group machine config OSImageURL: url=%v, version=%v",
url, version)
return fmt.Errorf("missing kube version from worker node group machine config OSImageURL, the image url should include %v: url=%v, version=%v",
permutations(version, []string{"-", "_", ""}), url, version)

Check failure on line 166 in pkg/providers/tinkerbell/validate.go

View workflow job for this annotation

GitHub Actions / build

cannot use version (variable of type "github.com/aws/eks-anywhere/pkg/api/v1alpha1".KubernetesVersion) as string value in argument to permutations

Check failure on line 166 in pkg/providers/tinkerbell/validate.go

View workflow job for this annotation

GitHub Actions / govulncheck

cannot use version (variable of type "github.com/aws/eks-anywhere/pkg/api/v1alpha1".KubernetesVersion) as string value in argument to permutations
}
}
}
return nil
}

// permutations takes a version in dot format and
// returns a human readable string with the permutations of the version
// using the passed in separators.
//
// For example, when v = 1.29 and separators = []string{"-", "_", ""}
// the result will be "1.29, 1-29, 1_29, or 129"
func permutations(v string, separators []string) string {
result := []string{v}
split := strings.Split(v, ".")
if len(split) == 1 {
return v
}

seps := remove(".", deduplicate(separators))
for idx, sep := range seps {
var elem string
for idx, s := range split {
if idx != len(split)-1 {
elem = elem + s + sep
} else {
elem = elem + s
}
}

if idx == len(seps)-1 {
result = append(result, "or")
} else if idx != len(seps)-2 {
elem = elem + ","
}
result = append(result, elem)
}

if len(seps) > 1 {
result[0] = result[0] + ","
}

return strings.Join(result, " ")
}

// deduplicate returns a new slice with duplicates values removed.
func deduplicate(s []string) []string {
if len(s) <= 1 {
return s
}
result := []string{}
seen := make(map[string]struct{})
for _, val := range s {
val := strings.ToLower(val)
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = struct{}{}
}
}
return result
}

// removes all values equal to val from in.
func remove(val string, in []string) []string {
result := []string{}
for _, i := range in {
if i != val {
result = append(result, i)
}
}

return result
}

func defaultBottlerocketOSImageURLs(spec *ClusterSpec) {
if spec.ControlPlaneMachineConfig().Spec.OSImageURL == "" {
spec.ControlPlaneMachineConfig().Spec.OSImageURL = spec.RootVersionsBundle().EksD.Raw.Bottlerocket.URI
Expand Down

0 comments on commit 482e07d

Please sign in to comment.