Replace kubectl with Go server-side apply
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
@@ -18,9 +18,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
@@ -73,18 +71,11 @@ func init() {
|
||||
}
|
||||
|
||||
func runCheckCmd(cmd *cobra.Command, args []string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
|
||||
defer cancel()
|
||||
|
||||
logger.Actionf("checking prerequisites")
|
||||
checkFailed := false
|
||||
|
||||
fluxCheck()
|
||||
|
||||
if !kubectlCheck(ctx, ">=1.18.0-0") {
|
||||
checkFailed = true
|
||||
}
|
||||
|
||||
if !kubernetesCheck(">=1.16.0-0") {
|
||||
checkFailed = true
|
||||
}
|
||||
@@ -130,42 +121,6 @@ func fluxCheck() {
|
||||
}
|
||||
}
|
||||
|
||||
func kubectlCheck(ctx context.Context, constraint string) bool {
|
||||
_, err := exec.LookPath("kubectl")
|
||||
if err != nil {
|
||||
logger.Failuref("kubectl not found")
|
||||
return false
|
||||
}
|
||||
|
||||
kubectlArgs := []string{"version", "--client", "--output", "json"}
|
||||
output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...)
|
||||
if err != nil {
|
||||
logger.Failuref("kubectl version can't be determined")
|
||||
return false
|
||||
}
|
||||
|
||||
kv := &kubectlVersion{}
|
||||
if err = json.Unmarshal([]byte(output), kv); err != nil {
|
||||
logger.Failuref("kubectl version output can't be unmarshalled")
|
||||
return false
|
||||
}
|
||||
|
||||
v, err := version.ParseVersion(kv.ClientVersion.GitVersion)
|
||||
if err != nil {
|
||||
logger.Failuref("kubectl version can't be parsed")
|
||||
return false
|
||||
}
|
||||
|
||||
c, _ := semver.NewConstraint(constraint)
|
||||
if !c.Check(v) {
|
||||
logger.Failuref("kubectl version %s < %s", v.Original(), constraint)
|
||||
return false
|
||||
}
|
||||
|
||||
logger.Successf("kubectl %s %s", v.String(), constraint)
|
||||
return true
|
||||
}
|
||||
|
||||
func kubernetesCheck(constraint string) bool {
|
||||
cfg, err := utils.KubeConfig(rootArgs.kubeconfig, rootArgs.kubecontext)
|
||||
if err != nil {
|
||||
|
||||
@@ -23,13 +23,11 @@ func TestCheckPre(t *testing.T) {
|
||||
t.Fatalf("Error unmarshalling: %v", err.Error())
|
||||
}
|
||||
|
||||
clientVersion := strings.TrimPrefix(versions["clientVersion"].GitVersion, "v")
|
||||
serverVersion := strings.TrimPrefix(versions["serverVersion"].GitVersion, "v")
|
||||
|
||||
cmd := cmdTestCase{
|
||||
args: "check --pre",
|
||||
assert: assertGoldenTemplateFile("testdata/check/check_pre.golden", map[string]string{
|
||||
"clientVersion": clientVersion,
|
||||
"serverVersion": serverVersion,
|
||||
}),
|
||||
}
|
||||
|
||||
@@ -41,13 +41,13 @@ If a previous version is installed, then an in-place upgrade will be performed.`
|
||||
flux install --version=latest --namespace=flux-system
|
||||
|
||||
# Install a specific version and a series of components
|
||||
flux install --dry-run --version=v0.0.7 --components="source-controller,kustomize-controller"
|
||||
flux install --version=v0.0.7 --components="source-controller,kustomize-controller"
|
||||
|
||||
# Install Flux onto tainted Kubernetes nodes
|
||||
flux install --toleration-keys=node.kubernetes.io/dedicated-to-flux
|
||||
|
||||
# Dry-run install with manifests preview
|
||||
flux install --dry-run --verbose
|
||||
# Dry-run install
|
||||
flux install --export | kubectl apply --dry-run=client -f-
|
||||
|
||||
# Write install manifests to file
|
||||
flux install --export > flux-system.yaml`,
|
||||
@@ -102,6 +102,7 @@ func init() {
|
||||
"list of toleration keys used to schedule the components pods onto nodes with matching taints")
|
||||
installCmd.Flags().MarkHidden("manifests")
|
||||
installCmd.Flags().MarkDeprecated("arch", "multi-arch container image is now available for AMD64, ARMv7 and ARM64")
|
||||
installCmd.Flags().MarkDeprecated("dry-run", "use 'flux install --export | kubectl apply --dry-run=client -f-'")
|
||||
rootCmd.AddCommand(installCmd)
|
||||
}
|
||||
|
||||
@@ -188,25 +189,19 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
|
||||
|
||||
logger.Successf("manifests build completed")
|
||||
logger.Actionf("installing components in %s namespace", rootArgs.namespace)
|
||||
applyOutput := utils.ModeStderrOS
|
||||
if rootArgs.verbose {
|
||||
applyOutput = utils.ModeOS
|
||||
}
|
||||
|
||||
kubectlArgs := []string{"apply", "-f", filepath.Join(tmpDir, manifest.Path)}
|
||||
if installArgs.dryRun {
|
||||
kubectlArgs = append(kubectlArgs, "--dry-run=client")
|
||||
applyOutput = utils.ModeOS
|
||||
}
|
||||
if _, err := utils.ExecKubectlCommand(ctx, applyOutput, rootArgs.kubeconfig, rootArgs.kubecontext, kubectlArgs...); err != nil {
|
||||
return fmt.Errorf("install failed: %w", err)
|
||||
}
|
||||
|
||||
if installArgs.dryRun {
|
||||
logger.Successf("install dry-run finished")
|
||||
return nil
|
||||
}
|
||||
|
||||
applyOutput, err := utils.Apply(ctx, rootArgs.kubeconfig, rootArgs.kubecontext, filepath.Join(tmpDir, manifest.Path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("install failed: %w", err)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr, applyOutput)
|
||||
|
||||
kubeConfig, err := utils.KubeConfig(rootArgs.kubeconfig, rootArgs.kubecontext)
|
||||
if err != nil {
|
||||
return fmt.Errorf("install failed: %w", err)
|
||||
|
||||
1
cmd/flux/testdata/check/check_pre.golden
vendored
1
cmd/flux/testdata/check/check_pre.golden
vendored
@@ -1,4 +1,3 @@
|
||||
► checking prerequisites
|
||||
✔ kubectl {{ .clientVersion }} >=1.18.0-0
|
||||
✔ Kubernetes {{ .serverVersion }} >=1.16.0-0
|
||||
✔ prerequisites checks passed
|
||||
|
||||
Reference in New Issue
Block a user