Skip to content

Commit

Permalink
Fail the job instead of restarting if the exit code indicates unsuppo…
Browse files Browse the repository at this point in the history
…rted chart version

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Jul 31, 2024
1 parent 9e978aa commit 06843c9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
16 changes: 14 additions & 2 deletions pkg/controllers/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const (
var (
commaRE = regexp.MustCompile(`\\*,`)
deletePolicy = metav1.DeletePropagationForeground
DefaultJobImage = "rancher/klipper-helm:v0.9.0-build20240730"
DefaultJobImage = "rancher/klipper-helm:v0.9.1-build20240731"
DefaultFailurePolicy = FailurePolicyReinstall
defaultBackOffLimit = pointer.Int32(1000)

Expand Down Expand Up @@ -409,6 +409,18 @@ func job(chart *v1.HelmChart, apiServerPort string) (*batch.Job, *corev1.Secret,
},
},
Spec: batch.JobSpec{
PodFailurePolicy: &batch.PodFailurePolicy{
Rules: []batch.PodFailurePolicyRule{
{
Action: batch.PodFailurePolicyActionFailJob,
OnExitCodes: &batch.PodFailurePolicyOnExitCodesRequirement{
ContainerName: pointer.String("helm"),
Operator: batch.PodFailurePolicyOnExitCodesOpIn,
Values: []int32{64},
},
},
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Expand All @@ -417,7 +429,7 @@ func job(chart *v1.HelmChart, apiServerPort string) (*batch.Job, *corev1.Secret,
},
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyOnFailure,
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "helm",
Expand Down
10 changes: 10 additions & 0 deletions test/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,13 @@ func (f *Framework) GetChartContent(url string) (string, error) {
}
return string(b.Bytes()), nil
}

// GetJobCondition returns true if there is a condition on the job matching the selected type and status
func (f *Framework) GetJobCondition(job *batchv1.Job, condition batchv1.JobConditionType, status corev1.ConditionStatus) bool {
for _, v := range job.Status.Conditions {
if v.Type == condition && v.Status == status {
return true
}
}
return false
}
22 changes: 16 additions & 6 deletions test/suite/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package suite_test

import (
"context"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -376,12 +377,21 @@ var _ = Describe("Helm Tests", Ordered, func() {
chart, err = framework.CreateHelmChart(chart, framework.Namespace)
Expect(err).ToNot(HaveOccurred())
})
It("Should return error status", func() {
chart, err = framework.GetHelmChart(chart.Name, chart.Namespace)
Eventually(err, 120*time.Second).ShouldNot(HaveOccurred())
job, err = framework.GetJob(chart)
Eventually(err, 120*time.Second).ShouldNot(HaveOccurred())
Eventually(job.Status.Failed, 120*time.Second).Should(BeNumerically(">", 0))
It("Job should have failed condition", func() {
Eventually(func() error {
chart, err = framework.GetHelmChart(chart.Name, chart.Namespace)
if err != nil {
return err
}
job, err = framework.GetJob(chart)
if err != nil {
return err
}
if !framework.GetJobCondition(job, batchv1.JobFailed, corev1.ConditionTrue) {
return fmt.Errorf("expected condition %v=%v not found", batchv1.JobFailed, corev1.ConditionTrue)
}
return nil
}, 120*time.Second).ShouldNot(HaveOccurred())
})
})

Expand Down

0 comments on commit 06843c9

Please sign in to comment.