Skip to content

Commit

Permalink
Added changes for error handling in chaosctl apply manifest logic (#97)
Browse files Browse the repository at this point in the history
Signed-off-by: Saranya-jena <saranya.jena@harness.io>

Signed-off-by: Saranya-jena <saranya.jena@harness.io>
  • Loading branch information
Saranya-jena committed Sep 13, 2022
1 parent dd2a89e commit 6c8aea4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func UpgradeAgent(c context.Context, cred types.Credentials, projectID string, c
}, kubeconfig, true)

if err != nil {
return yamlOutput, err
return "", err
}
utils.White.Print("\n", yamlOutput)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connect/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ var agentCmd = &cobra.Command{
YamlPath: utils.ChaosYamlPath,
}, kubeconfig, false)
if err != nil {
utils.White_B.Print("\n❌ Failed in applying connection yaml: \n" + yamlOutput)
utils.Red.Print("\n❌ Failed in applying connection yaml: \n" + err.Error() + "\n")
utils.White_B.Print("\n Error: \n" + err.Error())
os.Exit(1)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/upgrade/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package upgrade
import (
"context"
"fmt"
"os"

"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/utils"
Expand Down Expand Up @@ -53,11 +54,10 @@ var agentCmd = &cobra.Command{

output, err := apis.UpgradeAgent(context.Background(), credentials, projectID, cluster_id, kubeconfig)
if err != nil {
utils.Red.Print(output)
utils.PrintError(err)
} else {
utils.White.Print(output)
utils.Red.Print("\n❌ Failed upgrading Chaos Delegate: \n" + err.Error() + "\n")
os.Exit(1)
}
utils.White_B.Print("\n", output)
},
}

Expand Down
23 changes: 20 additions & 3 deletions pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package k8s

import (
"bytes"
"context"
"flag"
"fmt"
Expand Down Expand Up @@ -306,14 +307,30 @@ func ApplyYaml(params ApplyYamlPrams, kubeconfig string, isLocal bool) (output s
args := []string{"kubectl", "apply", "-f", path}
if kubeconfig != "" {
args = append(args, []string{"--kubeconfig", kubeconfig}...)
} else {
args = []string{"kubectl", "apply", "-f", path}
}

stdout, err := exec.Command(args[0], args[1:]...).CombinedOutput()
cmd := exec.Command(args[0], args[1:]...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
outStr, errStr := stdout.String(), stderr.String()

// err, can have exit status 1
if err != nil {
return string(stdout), err
// if we get standard error then, return the same
if errStr != "" {
return "", fmt.Errorf(errStr)
}

// if not standard error found, return error
return "", err
}

return string(stdout), err
// If no error found, return standard output
return outStr, nil
}

// GetConfigMap returns config map for a given name and namespace
Expand Down

0 comments on commit 6c8aea4

Please sign in to comment.