diff --git a/pkg/libkubectl/apply.go b/pkg/libkubectl/apply.go index 5dad5adc5..23619e532 100644 --- a/pkg/libkubectl/apply.go +++ b/pkg/libkubectl/apply.go @@ -6,18 +6,30 @@ import ( "fmt" "k8s.io/kubectl/pkg/cmd/apply" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func (c *Client) Apply(ctx context.Context, manifests []string) (string, error) { buf := new(bytes.Buffer) + var fatalErr error + cmdutil.BehaviorOnFatal(func(msg string, code int) { + fatalErr = newKubectlFatalError(code, msg) + }) + defer cmdutil.DefaultBehaviorOnFatal() + cmd := apply.NewCmdApply("kubectl", c.factory, c.streams) cmd.SetArgs(resourcesToArgs(manifests)) cmd.SetOut(buf) - if err := cmd.ExecuteContext(ctx); err != nil { + err := cmd.ExecuteContext(ctx) + // check for the fatal error first so we don't return the error from the command execution + if fatalErr != nil { + return "", fatalErr + } + // if there is no fatal error, return the error from the command execution + if err != nil { return "", fmt.Errorf("error applying resources: %w", err) } - return buf.String(), nil } diff --git a/pkg/libkubectl/client.go b/pkg/libkubectl/client.go index f9d4137f6..0229c8167 100644 --- a/pkg/libkubectl/client.go +++ b/pkg/libkubectl/client.go @@ -3,6 +3,7 @@ package libkubectl import ( "bytes" "errors" + "fmt" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" @@ -61,3 +62,7 @@ func generateConfigFlags(token, server, namespace, kubeconfigPath string, insecu return configFlags, nil } + +func newKubectlFatalError(code int, msg string) error { + return fmt.Errorf("kubectl fatal error (exit code %d): %s", code, msg) +} diff --git a/pkg/libkubectl/delete.go b/pkg/libkubectl/delete.go index bb093ab4d..4544dfc76 100644 --- a/pkg/libkubectl/delete.go +++ b/pkg/libkubectl/delete.go @@ -6,17 +6,30 @@ import ( "fmt" "k8s.io/kubectl/pkg/cmd/delete" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func (c *Client) Delete(ctx context.Context, manifests []string) (string, error) { buf := new(bytes.Buffer) + var fatalErr error + cmdutil.BehaviorOnFatal(func(msg string, code int) { + fatalErr = newKubectlFatalError(code, msg) + }) + defer cmdutil.DefaultBehaviorOnFatal() + cmd := delete.NewCmdDelete(c.factory, c.streams) cmd.SetArgs(resourcesToArgs(manifests)) cmd.Flags().Set("ignore-not-found", "true") cmd.SetOut(buf) - if err := cmd.ExecuteContext(ctx); err != nil { + err := cmd.ExecuteContext(ctx) + // check for the fatal error first so we don't return the error from the command execution + if fatalErr != nil { + return "", fatalErr + } + // if there is no fatal error, return the error from the command execution + if err != nil { return "", fmt.Errorf("error deleting resources: %w", err) } diff --git a/pkg/libkubectl/restart.go b/pkg/libkubectl/restart.go index fcfa7fd15..9ce7149aa 100644 --- a/pkg/libkubectl/restart.go +++ b/pkg/libkubectl/restart.go @@ -6,11 +6,18 @@ import ( "fmt" "k8s.io/kubectl/pkg/cmd/rollout" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string, error) { buf := new(bytes.Buffer) + var fatalErr error + cmdutil.BehaviorOnFatal(func(msg string, code int) { + fatalErr = newKubectlFatalError(code, msg) + }) + defer cmdutil.DefaultBehaviorOnFatal() + cmd := rollout.NewCmdRollout(c.factory, c.streams) args := []string{"restart"} args = append(args, resourcesToArgs(manifests)...) @@ -18,7 +25,13 @@ func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string cmd.SetArgs(args) cmd.SetOut(buf) - if err := cmd.ExecuteContext(ctx); err != nil { + err := cmd.ExecuteContext(ctx) + // check for the fatal error first so we don't return the error from the command execution + if fatalErr != nil { + return "", fatalErr + } + // if there is no fatal error, return the error from the command execution + if err != nil { return "", fmt.Errorf("error restarting resources: %w", err) }