Skip to content

Commit

Permalink
Fix flag checks
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Nov 2, 2023
1 parent 3bcf320 commit 54aecef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
44 changes: 24 additions & 20 deletions cli/internal/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ func (a *applyCmd) apply(cmd *cobra.Command, configFetcher attestationconfigapi.

// Upgrade NodeVersion object
// This can be skipped if we ran the init RPC, as the NodeVersion object is already up to date
if !(a.flags.skipPhases.contains(skipK8sPhase) && a.flags.skipPhases.contains(skipImagePhase)) &&
a.flags.skipPhases.contains(skipInitPhase) {
if !(a.flags.skipPhases.contains(skipK8sPhase) && a.flags.skipPhases.contains(skipImagePhase)) {
if err := a.runK8sUpgrade(cmd, conf, kubeUpgrader); err != nil {
return err
}
Expand Down Expand Up @@ -444,12 +443,14 @@ func (a *applyCmd) validateInputs(cmd *cobra.Command, configFetcher attestationc
// Check if we already have a running Kubernetes cluster
// by checking if the Kubernetes admin config file exists
// If it exist, we skip the init phase
// If it does not exist, we need to run the init RPC first
// If it does not exist, we need to run the init RPC first and skip image and k8s phases
a.log.Debugf("Checking if %s exists", a.flags.pathPrefixer.PrefixPrintablePath(constants.AdminConfFilename))
if _, err := a.fileHandler.Stat(constants.AdminConfFilename); err == nil {
a.flags.skipPhases.add(skipInitPhase)
} else if !errors.Is(err, os.ErrNotExist) {
return nil, nil, fmt.Errorf("checking for %s: %w", a.flags.pathPrefixer.PrefixPrintablePath(constants.AdminConfFilename), err)
} else {
a.flags.skipPhases.add(skipImagePhase, skipK8sPhase)
}

// Validate input arguments
Expand All @@ -460,28 +461,31 @@ func (a *applyCmd) validateInputs(cmd *cobra.Command, configFetcher attestationc
// We skip version validation if the user explicitly skips the Kubernetes phase
a.log.Debugf("Validating Kubernetes version %s", conf.KubernetesVersion)
validVersion, err := versions.NewValidK8sVersion(string(conf.KubernetesVersion), true)
if err != nil && !a.flags.skipPhases.contains(skipK8sPhase) {
if err != nil {
a.log.Debugf("Kubernetes version not valid: %s", err)
if !a.flags.skipPhases.contains(skipInitPhase) {
return nil, nil, err
}
a.log.Debugf("Checking if user wants to continue anyway")
if !a.flags.yes {
confirmed, err := askToConfirm(cmd,
fmt.Sprintf(
"WARNING: The Kubernetes patch version %s is not supported. If you continue, Kubernetes upgrades will be skipped. Do you want to continue anyway?",
validVersion,
),
)
if err != nil {
return nil, nil, fmt.Errorf("asking for confirmation: %w", err)
}
if !confirmed {
return nil, nil, fmt.Errorf("aborted by user")

if !a.flags.skipPhases.contains(skipK8sPhase) {
a.log.Debugf("Checking if user wants to continue anyway")
if !a.flags.yes {
confirmed, err := askToConfirm(cmd,
fmt.Sprintf(
"WARNING: The Kubernetes patch version %s is not supported. If you continue, Kubernetes upgrades will be skipped. Do you want to continue anyway?",
validVersion,
),
)
if err != nil {
return nil, nil, fmt.Errorf("asking for confirmation: %w", err)
}
if !confirmed {
return nil, nil, fmt.Errorf("aborted by user")
}
}
a.flags.skipPhases.add(skipK8sPhase)
a.log.Debugf("Outdated Kubernetes version accepted, Kubernetes upgrade will be skipped")
}
a.flags.skipPhases.add(skipK8sPhase)
a.log.Debugf("Outdated Kubernetes version accepted, Kubernetes upgrade will be skipped")
}
if versions.IsPreviewK8sVersion(validVersion) {
cmd.PrintErrf("Warning: Constellation with Kubernetes %s is still in preview. Use only for evaluation purposes.\n", validVersion)
Expand All @@ -501,7 +505,7 @@ func (a *applyCmd) validateInputs(cmd *cobra.Command, configFetcher attestationc
// If using one of those providers, print a warning when trying to upgrade the image
if !(conf.GetProvider() == cloudprovider.AWS || conf.GetProvider() == cloudprovider.Azure || conf.GetProvider() == cloudprovider.GCP) &&
!a.flags.skipPhases.contains(skipImagePhase) {
cmd.PrintErrf("Image upgrades are not supported for provider %s", conf.GetProvider())
cmd.PrintErrf("Image upgrades are not supported for provider %s\n", conf.GetProvider())
cmd.PrintErrln("Image phase will be skipped")
a.flags.skipPhases.add(skipImagePhase)
}
Expand Down
10 changes: 10 additions & 0 deletions cli/internal/cmd/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ func TestValidateInputs(t *testing.T) {
createAdminConfig: func(require *require.Assertions, fh file.Handler) {},
createTfState: defaultTfState,
flags: applyFlags{},
wantPhases: skipPhases{
skipImagePhase: struct{}{},
skipK8sPhase: struct{}{},
},
},
"no tf state, but admin config exists": {
createConfig: defaultConfig(cloudprovider.GCP),
Expand All @@ -321,6 +325,10 @@ func TestValidateInputs(t *testing.T) {
createAdminConfig: func(require *require.Assertions, fh file.Handler) {},
createTfState: func(require *require.Assertions, fh file.Handler) {},
flags: applyFlags{},
wantPhases: skipPhases{
skipImagePhase: struct{}{},
skipK8sPhase: struct{}{},
},
},
"skip terraform": {
createConfig: defaultConfig(cloudprovider.GCP),
Expand All @@ -335,6 +343,8 @@ func TestValidateInputs(t *testing.T) {
},
wantPhases: skipPhases{
skipInfrastructurePhase: struct{}{},
skipImagePhase: struct{}{},
skipK8sPhase: struct{}{},
},
},
}
Expand Down

0 comments on commit 54aecef

Please sign in to comment.