mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +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,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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue