mirror of
https://github.com/portainer/portainer.git
synced 2025-07-18 21:09:40 +02:00
fix: kubectl
sdk - capture fatal error and return instead of exiting 1 [r7s-371] (#841)
This commit is contained in:
parent
c20a8b5a68
commit
0a36d4fbfd
4 changed files with 47 additions and 4 deletions
|
@ -6,18 +6,30 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/cmd/apply"
|
"k8s.io/kubectl/pkg/cmd/apply"
|
||||||
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) Apply(ctx context.Context, manifests []string) (string, error) {
|
func (c *Client) Apply(ctx context.Context, manifests []string) (string, error) {
|
||||||
buf := new(bytes.Buffer)
|
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 := apply.NewCmdApply("kubectl", c.factory, c.streams)
|
||||||
cmd.SetArgs(resourcesToArgs(manifests))
|
cmd.SetArgs(resourcesToArgs(manifests))
|
||||||
cmd.SetOut(buf)
|
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 "", fmt.Errorf("error applying resources: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package libkubectl
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/cli-runtime/pkg/genericiooptions"
|
"k8s.io/cli-runtime/pkg/genericiooptions"
|
||||||
|
@ -61,3 +62,7 @@ func generateConfigFlags(token, server, namespace, kubeconfigPath string, insecu
|
||||||
|
|
||||||
return configFlags, nil
|
return configFlags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newKubectlFatalError(code int, msg string) error {
|
||||||
|
return fmt.Errorf("kubectl fatal error (exit code %d): %s", code, msg)
|
||||||
|
}
|
||||||
|
|
|
@ -6,17 +6,30 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/cmd/delete"
|
"k8s.io/kubectl/pkg/cmd/delete"
|
||||||
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) Delete(ctx context.Context, manifests []string) (string, error) {
|
func (c *Client) Delete(ctx context.Context, manifests []string) (string, error) {
|
||||||
buf := new(bytes.Buffer)
|
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 := delete.NewCmdDelete(c.factory, c.streams)
|
||||||
cmd.SetArgs(resourcesToArgs(manifests))
|
cmd.SetArgs(resourcesToArgs(manifests))
|
||||||
cmd.Flags().Set("ignore-not-found", "true")
|
cmd.Flags().Set("ignore-not-found", "true")
|
||||||
cmd.SetOut(buf)
|
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)
|
return "", fmt.Errorf("error deleting resources: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,18 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/cmd/rollout"
|
"k8s.io/kubectl/pkg/cmd/rollout"
|
||||||
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string, error) {
|
func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string, error) {
|
||||||
buf := new(bytes.Buffer)
|
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)
|
cmd := rollout.NewCmdRollout(c.factory, c.streams)
|
||||||
args := []string{"restart"}
|
args := []string{"restart"}
|
||||||
args = append(args, resourcesToArgs(manifests)...)
|
args = append(args, resourcesToArgs(manifests)...)
|
||||||
|
@ -18,7 +25,13 @@ func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string
|
||||||
cmd.SetArgs(args)
|
cmd.SetArgs(args)
|
||||||
cmd.SetOut(buf)
|
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)
|
return "", fmt.Errorf("error restarting resources: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue