Skip to content

Commit

Permalink
Refactor skipvalidations to include create (#6189)
Browse files Browse the repository at this point in the history
  • Loading branch information
pokearu authored Jul 17, 2023
1 parent 46181b7 commit c7cb412
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 102 deletions.
30 changes: 17 additions & 13 deletions cmd/eksctl-anywhere/cmd/upgradecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -49,7 +50,7 @@ func init() {
applyTinkerbellHardwareFlag(upgradeClusterCmd.Flags(), &uc.hardwareCSVPath)
upgradeClusterCmd.Flags().StringVarP(&uc.wConfig, "w-config", "w", "", "Kubeconfig file to use when upgrading a workload cluster")
upgradeClusterCmd.Flags().BoolVar(&uc.forceClean, "force-cleanup", false, "Force deletion of previously created bootstrap cluster")
upgradeClusterCmd.Flags().StringArrayVar(&uc.skipValidations, "skip-validations", []string{}, "Bypass upgrade validations by name. Valid arguments you can pass are --skip-validations=pod-disruption")
upgradeClusterCmd.Flags().StringArrayVar(&uc.skipValidations, "skip-validations", []string{}, fmt.Sprintf("Bypass upgrade validations by name. Valid arguments you can pass are --skip-validations=%s", strings.Join(upgradevalidations.SkippableValidations[:], ",")))

if err := upgradeClusterCmd.MarkFlagRequired("filename"); err != nil {
log.Fatalf("Error marking flag as required: %v", err)
Expand Down Expand Up @@ -98,6 +99,14 @@ func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command) error {
return fmt.Errorf("failed to build cluster manager opts: %v", err)
}

var skippedValidations map[string]bool
if len(uc.skipValidations) != 0 {
skippedValidations, err = validations.ValidateSkippableValidation(uc.skipValidations, upgradevalidations.SkippableValidations)
if err != nil {
return err
}
}

factory := dependencies.ForSpec(ctx, clusterSpec).WithExecutableMountDirs(dirs...).
WithBootstrapper().
WithCliConfig(cliConfig).
Expand Down Expand Up @@ -147,20 +156,15 @@ func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command) error {
}

validationOpts := &validations.Opts{
Kubectl: deps.UnAuthKubectlClient,
Spec: clusterSpec,
WorkloadCluster: workloadCluster,
ManagementCluster: managementCluster,
Provider: deps.Provider,
CliConfig: cliConfig,
Kubectl: deps.UnAuthKubectlClient,
Spec: clusterSpec,
WorkloadCluster: workloadCluster,
ManagementCluster: managementCluster,
Provider: deps.Provider,
CliConfig: cliConfig,
SkippedValidations: skippedValidations,
}

if len(uc.skipValidations) != 0 {
validationOpts.SkippedValidations, err = upgradevalidations.ValidateSkippableUpgradeValidation(uc.skipValidations)
if err != nil {
return err
}
}
upgradeValidations := upgradevalidations.New(validationOpts)

err = upgradeCluster.Run(ctx, clusterSpec, managementCluster, workloadCluster, upgradeValidations, uc.forceClean)
Expand Down
10 changes: 10 additions & 0 deletions pkg/validations/createvalidations/createvalidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import (
"github.com/aws/eks-anywhere/pkg/validations"
)

// string values of supported validation names that can be skipped.
const (
VSphereUserPriv = "vsphere-user-privilege"
)

// SkippableValidations represents all the validations we offer for users to skip.
var SkippableValidations = []string{
VSphereUserPriv,
}

func New(opts *validations.Opts) *CreateValidations {
opts.SetDefaults()
return &CreateValidations{Opts: opts}
Expand Down
34 changes: 34 additions & 0 deletions pkg/validations/skipvalidations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package validations

import (
"fmt"
"strings"
)

// ValidSkippableValidationsMap returns a map for all valid skippable validations as keys, defaulting values to false.
// Defaulting to False means these validations won't be skipped unless set to True.
func validSkippableValidationsMap(skippableValidations []string) map[string]bool {
validationsMap := make(map[string]bool, len(skippableValidations))

for i := range skippableValidations {
validationsMap[skippableValidations[i]] = false
}

return validationsMap
}

// ValidateSkippableValidation validates if provided validations are supported by EKSA to skip for upgrades.
func ValidateSkippableValidation(skippedValidations []string, skippableValidations []string) (map[string]bool, error) {
svMap := validSkippableValidationsMap(skippableValidations)

for i := range skippedValidations {
validationName := skippedValidations[i]
_, ok := svMap[validationName]
if !ok {
return nil, fmt.Errorf("invalid validation name to be skipped. The supported validations that can be skipped using --skip-validations are %s", strings.Join(skippableValidations[:], ","))
}
svMap[validationName] = true
}

return svMap, nil
}
60 changes: 60 additions & 0 deletions pkg/validations/skipvalidations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package validations_test

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/aws/eks-anywhere/pkg/validations"
"github.com/aws/eks-anywhere/pkg/validations/createvalidations"
"github.com/aws/eks-anywhere/pkg/validations/upgradevalidations"
)

func TestValidateSkippableValidation(t *testing.T) {
tests := []struct {
name string
want map[string]bool
wantErr error
skippedValidations []string
skippableValidations []string
}{
{
name: "invalid upgrade validation param",
want: nil,
wantErr: fmt.Errorf("invalid validation name to be skipped. The supported validations that can be skipped using --skip-validations are %s", strings.Join(upgradevalidations.SkippableValidations[:], ",")),
skippedValidations: []string{"test"},
skippableValidations: []string{upgradevalidations.PDB},
},
{
name: "valid upgrade validation param",
want: map[string]bool{
upgradevalidations.PDB: true,
},
wantErr: nil,
skippedValidations: []string{upgradevalidations.PDB},
skippableValidations: []string{upgradevalidations.PDB},
},
{
name: "valid create validation param",
want: map[string]bool{
createvalidations.VSphereUserPriv: true,
},
wantErr: nil,
skippedValidations: []string{createvalidations.VSphereUserPriv},
skippableValidations: []string{createvalidations.VSphereUserPriv},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := validations.ValidateSkippableValidation(tt.skippedValidations, tt.skippableValidations)
if !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("ValidateSkippableValidation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ValidateSkippableValidation() = %v, want %v", got, tt.want)
}
})
}
}
43 changes: 0 additions & 43 deletions pkg/validations/upgradevalidations/upgradeskipvalidations.go

This file was deleted.

46 changes: 0 additions & 46 deletions pkg/validations/upgradevalidations/upgradeskipvalidations_test.go

This file was deleted.

10 changes: 10 additions & 0 deletions pkg/validations/upgradevalidations/upgradevalidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import (
"github.com/aws/eks-anywhere/pkg/validations"
)

// string values of supported validation names that can be skipped.
const (
PDB = "pod-disruption"
)

// SkippableValidations represents all the validations we offer for users to skip.
var SkippableValidations = []string{
PDB,
}

func New(opts *validations.Opts) *UpgradeValidations {
opts.SetDefaults()
return &UpgradeValidations{Opts: opts}
Expand Down

0 comments on commit c7cb412

Please sign in to comment.