diff --git a/cmd/flux/bootstrap.go b/cmd/flux/bootstrap.go index 1cef362e..c0d8d4de 100644 --- a/cmd/flux/bootstrap.go +++ b/cmd/flux/bootstrap.go @@ -135,13 +135,13 @@ func generateInstallManifests(targetPath, namespace, tmpDir string, localManifes func applyInstallManifests(ctx context.Context, manifestPath string, components []string) error { kubectlArgs := []string{"apply", "-f", manifestPath} - if _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubectlArgs...); err != nil { + if _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubeconfig, kubecontext, kubectlArgs...); err != nil { return fmt.Errorf("install failed") } for _, deployment := range components { kubectlArgs = []string{"-n", namespace, "rollout", "status", "deployment", deployment, "--timeout", timeout.String()} - if _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubectlArgs...); err != nil { + if _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubeconfig, kubecontext, kubectlArgs...); err != nil { return fmt.Errorf("install failed") } } @@ -177,7 +177,7 @@ func generateSyncManifests(url, branch, name, namespace, targetPath, tmpDir stri func applySyncManifests(ctx context.Context, kubeClient client.Client, name, namespace, targetPath, tmpDir string) error { kubectlArgs := []string{"apply", "-k", filepath.Join(tmpDir, targetPath, namespace)} - if _, err := utils.ExecKubectlCommand(ctx, utils.ModeStderrOS, kubectlArgs...); err != nil { + if _, err := utils.ExecKubectlCommand(ctx, utils.ModeStderrOS, kubeconfig, kubecontext, kubectlArgs...); err != nil { return err } diff --git a/cmd/flux/check.go b/cmd/flux/check.go index 499f4a18..4b553d22 100644 --- a/cmd/flux/check.go +++ b/cmd/flux/check.go @@ -104,7 +104,7 @@ func kubectlCheck(ctx context.Context, version string) bool { } kubectlArgs := []string{"version", "--client", "--output", "json"} - output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubectlArgs...) + output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubeconfig, kubecontext, kubectlArgs...) if err != nil { logger.Failuref("kubectl version can't be determined") return false @@ -174,14 +174,14 @@ func componentsCheck() bool { ok := true for _, deployment := range checkComponents { kubectlArgs := []string{"-n", namespace, "rollout", "status", "deployment", deployment, "--timeout", timeout.String()} - if output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubectlArgs...); err != nil { + if output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubeconfig, kubecontext, kubectlArgs...); err != nil { logger.Failuref("%s: %s", deployment, strings.TrimSuffix(output, "\n")) ok = false } else { logger.Successf("%s is healthy", deployment) } kubectlArgs = []string{"-n", namespace, "get", "deployment", deployment, "-o", "jsonpath=\"{..image}\""} - if output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubectlArgs...); err == nil { + if output, err := utils.ExecKubectlCommand(ctx, utils.ModeCapture, kubeconfig, kubecontext, kubectlArgs...); err == nil { logger.Actionf(strings.TrimPrefix(strings.TrimSuffix(output, "\""), "\"")) } } diff --git a/cmd/flux/install.go b/cmd/flux/install.go index 5eabd159..9c150a77 100644 --- a/cmd/flux/install.go +++ b/cmd/flux/install.go @@ -155,7 +155,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { kubectlArgs = append(kubectlArgs, "--dry-run=client") applyOutput = utils.ModeOS } - if _, err := utils.ExecKubectlCommand(ctx, applyOutput, kubectlArgs...); err != nil { + if _, err := utils.ExecKubectlCommand(ctx, applyOutput, kubeconfig, kubecontext, kubectlArgs...); err != nil { return fmt.Errorf("install failed") } @@ -169,7 +169,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { logger.Waitingf("verifying installation") for _, deployment := range installComponents { kubectlArgs = []string{"-n", namespace, "rollout", "status", "deployment", deployment, "--timeout", timeout.String()} - if _, err := utils.ExecKubectlCommand(ctx, applyOutput, kubectlArgs...); err != nil { + if _, err := utils.ExecKubectlCommand(ctx, applyOutput, kubeconfig, kubecontext, kubectlArgs...); err != nil { return fmt.Errorf("install failed") } else { logger.Successf("%s ready", deployment) diff --git a/cmd/flux/uninstall.go b/cmd/flux/uninstall.go index 35a615d2..bbe6a90c 100644 --- a/cmd/flux/uninstall.go +++ b/cmd/flux/uninstall.go @@ -144,7 +144,7 @@ func uninstallCmdRun(cmd *cobra.Command, args []string) error { if uninstallDryRun { kubectlArgs = append(kubectlArgs, dryRun) } - if _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubectlArgs...); err != nil { + if _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubeconfig, kubecontext, kubectlArgs...); err != nil { return fmt.Errorf("uninstall failed: %w", err) } } @@ -164,6 +164,6 @@ func deleteAll(ctx context.Context, kind string, dryRun bool) error { kubectlArgs = append(kubectlArgs, "--dry-run=server") } - _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubectlArgs...) + _, err := utils.ExecKubectlCommand(ctx, utils.ModeOS, kubeconfig, kubecontext, kubectlArgs...) return err } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index e51ee718..094f88b4 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -60,9 +60,17 @@ const ( ModeCapture ExecMode = "capture.stderr|stdout" ) -func ExecKubectlCommand(ctx context.Context, mode ExecMode, args ...string) (string, error) { +func ExecKubectlCommand(ctx context.Context, mode ExecMode, kubeConfigPath string, kubeContext string, args ...string) (string, error) { var stdoutBuf, stderrBuf bytes.Buffer + if kubeConfigPath != "" { + args = append(args, "--kubeconfig="+kubeConfigPath) + } + + if kubeContext != "" { + args = append(args, "--context="+kubeContext) + } + c := exec.CommandContext(ctx, "kubectl", args...) if mode == ModeStderrOS {