Skip to content

Commit

Permalink
Fix kubectl server-side dry-run flag
Browse files Browse the repository at this point in the history
If k8s client version < v1.18.0, the server-side dry-run flag is `--server-dry-run`.
If k8s client version >= v1.18.0, the server-side dry-run flag is `--dry-run=server`.

Signed-off-by: JenTing Hsiao <[email protected]>
  • Loading branch information
JenTing Hsiao committed Dec 14, 2020
1 parent c408d09 commit 1c5e415
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions internal/pkg/skuba/addons/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ func (addon Addon) applyPreflight(addonConfiguration AddonConfiguration, rootDir
"-f", preflightManifestPath,
}
if dryRun {
kubectlArgs = append(kubectlArgs, "--dry-run=server")
flag, err := kubectlServerSideDryRunFlag()
if err != nil {
return true, err
}
kubectlArgs = append(kubectlArgs, flag)
}
cmd := exec.Command("kubectl", kubectlArgs...)
var stderr bytes.Buffer
Expand Down Expand Up @@ -441,7 +445,11 @@ func (addon Addon) Apply(client clientset.Interface, addonConfiguration AddonCon
"-k", addon.addonDir(),
}
if dryRun {
kubectlArgs = append(kubectlArgs, "--dry-run=server")
flag, err := kubectlServerSideDryRunFlag()
if err != nil {
return err
}
kubectlArgs = append(kubectlArgs, flag)
}
cmd := exec.Command("kubectl", kubectlArgs...)
var stderr bytes.Buffer
Expand Down Expand Up @@ -509,3 +517,24 @@ func updateSkubaConfigMapWithAddonVersion(client clientset.Interface, addon kube
skubaConfiguration.AddonsVersion[addon] = addonVersion
return skuba.UpdateSkubaConfiguration(client, skubaConfiguration)
}

func kubectlServerSideDryRunFlag() (string, error) {
// If k8s client version < v1.18.0, the server-side dry-run flag is `--server-dry-run`
// If k8s client version >= v1.18.0, the server-side dry-run flag is `--dry-run=server`
cmd := exec.Command("kubectl", "version", "--client", "--short")
combinedOutput, err := cmd.CombinedOutput()
if err != nil {
return "", err
}

var clientVersion string
_, err = fmt.Sscanf(string(combinedOutput), "Client Version: %s", &clientVersion)
if err != nil {
return "", err
}

if version.MustParseSemantic(clientVersion).LessThan(version.MustParseSemantic("v1.18.0")) {
return "--server-dry-run", nil
}
return "--dry-run=server", nil
}

0 comments on commit 1c5e415

Please sign in to comment.