Skip to content

Commit

Permalink
fix wait job bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dongbeiqing91 committed Feb 29, 2024
1 parent 583ef3a commit c55a311
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions pkg/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit c55a311

Please sign in to comment.