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"
|
||||
|
||||
"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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue