From c55a311b48555c15b0e0adf70c07282597507762 Mon Sep 17 00:00:00 2001 From: bedong Date: Thu, 29 Feb 2024 16:29:49 +0800 Subject: [PATCH] fix wait job bugs --- pkg/kubectl/kubectl.go | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/pkg/kubectl/kubectl.go b/pkg/kubectl/kubectl.go index 6fbc58d..d9d6a22 100644 --- a/pkg/kubectl/kubectl.go +++ b/pkg/kubectl/kubectl.go @@ -14,10 +14,11 @@ limitations under the License. */ import ( - "github.com/gemalto/helm-spray/v4/internal/log" "os" "os/exec" "strings" + + "github.com/gemalto/helm-spray/v4/internal/log" ) func GetDeployments(namespace string) ([]string, error) { @@ -41,26 +42,24 @@ func AreStatefulSetsReady(names []string, namespace string, debug bool) (bool, e } func AreJobsReady(names []string, namespace string, debug bool) (bool, error) { - if len(names) == 0 { - return true, nil - } - template := generateTemplate(names, "{{if .status.succeeded}}{{if lt .status.succeeded 1}}{{printf \"%s \" .metadata.name}}{{end}}{{end}}") - if debug { - log.Info(3, "kubectl template: %s", template) - } - cmd := exec.Command("kubectl", "--namespace", namespace, "get", "jobs", "-o", "go-template="+template) - cmd.Stderr = os.Stderr - result, err := cmd.Output() - if err != nil { - // Cannot make the difference between an error when calling kubectl and no corresponding resource found. Return "" in any case. - return false, err - } - strResult := string(result) - if debug { - log.Info(3, "kubectl output: %s", strResult) - } - if len(strResult) > 0 { - return false, nil + for _, name := range names { + cmd := exec.Command("kubectl", "--namespace", namespace, "get", "job", name, "--output=jsonpath={.status.succeeded}") + cmd.Stderr = os.Stderr + result, err := cmd.Output() + if err != nil { + // Cannot make the difference between an error when calling kubectl and no corresponding resource found. Return "" in any case. + return false, err + } + strResult := string(result) + if debug { + log.Info(3, "kubectl output: %s", strResult) + } + if strResult != "1" { + if debug { + log.Info(3, "job %s is not completed", name) + } + return false, nil + } } return true, nil }