Skip to content

Commit d2569de

Browse files
Connor Kuehlbrandond
Connor Kuehl
authored andcommitted
Allow Plan to specify Job deadline
If a workload is known to be slow in excess of the system-upgrade-controller deployment's default deadline, allow the Plan creator to specify a job deadline with the plan so that the entailing job may run to completion. Signed-off-by: Connor Kuehl <[email protected]>
1 parent 9e7e45c commit d2569de

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

pkg/apis/upgrade.cattle.io/v1/types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ type Plan struct {
3333

3434
// PlanSpec represents the user-configurable details of a Plan.
3535
type PlanSpec struct {
36-
Concurrency int64 `json:"concurrency,omitempty"`
37-
NodeSelector *metav1.LabelSelector `json:"nodeSelector,omitempty"`
38-
ServiceAccountName string `json:"serviceAccountName,omitempty"`
36+
Concurrency int64 `json:"concurrency,omitempty"`
37+
JobActiveDeadlineSecs int64 `json:"jobActiveDeadlineSecs,omitempty"`
38+
NodeSelector *metav1.LabelSelector `json:"nodeSelector,omitempty"`
39+
ServiceAccountName string `json:"serviceAccountName,omitempty"`
3940

4041
Channel string `json:"channel,omitempty"`
4142
Version string `json:"version,omitempty"`

pkg/upgrade/job/job.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ var (
4141
return defaultValue
4242
}(defaultActiveDeadlineSeconds)
4343

44+
ActiveDeadlineSecondsMax = func(defaultValue int64) int64 {
45+
if str, ok := os.LookupEnv("SYSTEM_UPGRADE_JOB_ACTIVE_DEADLINE_SECONDS_MAX"); ok {
46+
if i, err := strconv.ParseInt(str, 10, 64); err != nil {
47+
logrus.Errorf("failed to parse $%s: %v", "SYSTEM_UPGRADE_JOB_ACTIVE_DEADLINE_SECONDS_MAX", err)
48+
} else {
49+
return i
50+
}
51+
}
52+
return defaultValue
53+
}(0 /* no maximum */)
54+
4455
BackoffLimit = func(defaultValue int32) int32 {
4556
if str, ok := os.LookupEnv("SYSTEM_UPGRADE_JOB_BACKOFF_LIMIT"); ok {
4657
if i, err := strconv.ParseInt(str, 10, 32); err != nil {
@@ -327,8 +338,20 @@ func New(plan *upgradeapiv1.Plan, node *corev1.Node, controllerName string) *bat
327338
),
328339
}
329340

330-
if ActiveDeadlineSeconds > 0 {
331-
job.Spec.ActiveDeadlineSeconds = &ActiveDeadlineSeconds
341+
activeDeadlineSeconds := ActiveDeadlineSeconds
342+
343+
if plan.Spec.JobActiveDeadlineSecs > 0 {
344+
activeDeadlineSeconds = plan.Spec.JobActiveDeadlineSecs
345+
}
346+
347+
// If configured with a maximum deadline via "SYSTEM_UPGRADE_JOB_ACTIVE_DEADLINE_SECONDS_MAX",
348+
// clamp the Plan's given deadline to the maximum.
349+
if ActiveDeadlineSecondsMax > 0 && activeDeadlineSeconds > ActiveDeadlineSecondsMax {
350+
activeDeadlineSeconds = ActiveDeadlineSecondsMax
351+
}
352+
353+
if activeDeadlineSeconds > 0 {
354+
job.Spec.ActiveDeadlineSeconds = &activeDeadlineSeconds
332355
if drain != nil && drain.Timeout != nil && drain.Timeout.Milliseconds() > ActiveDeadlineSeconds*1000 {
333356
logrus.Warnf("drain timeout exceeds active deadline seconds")
334357
}

pkg/upgrade/job/job_suite_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,87 @@ import (
55

66
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
8+
9+
upgradev1 "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io/v1"
10+
sucjob "github.com/rancher/system-upgrade-controller/pkg/upgrade/job"
11+
corev1 "k8s.io/api/core/v1"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
813
)
914

1015
func TestJob(t *testing.T) {
1116
RegisterFailHandler(Fail)
1217
RunSpecs(t, "Job Suite")
1318
}
19+
20+
var _ = Describe("Jobs", func() {
21+
var plan *upgradev1.Plan
22+
var node *corev1.Node
23+
24+
BeforeEach(func() {
25+
plan = &upgradev1.Plan{
26+
ObjectMeta: metav1.ObjectMeta{
27+
Name: "test-1",
28+
Namespace: "default",
29+
},
30+
Spec: upgradev1.PlanSpec{
31+
Concurrency: 1,
32+
ServiceAccountName: "system-upgrade-controller-foo",
33+
Upgrade: &upgradev1.ContainerSpec{
34+
Image: "test-image:latest",
35+
},
36+
},
37+
}
38+
39+
node = &corev1.Node{
40+
ObjectMeta: metav1.ObjectMeta{
41+
Name: "prod.test.local",
42+
},
43+
}
44+
})
45+
46+
Describe("Setting the batchv1.Job ActiveDeadlineSeconds field", func() {
47+
Context("When the Plan has a positive non-zero value for deadline", func() {
48+
It("Constructs the batchv1.Job with the Plan's given value", func() {
49+
plan.Spec.JobActiveDeadlineSecs = 12345
50+
job := sucjob.New(plan, node, "foo")
51+
Expect(*job.Spec.ActiveDeadlineSeconds).To(Equal(int64(12345)))
52+
})
53+
})
54+
55+
Context("When the Plan has a zero-value given as its deadline", func() {
56+
It("Constructs the batchv1.Job with a global default", func() {
57+
oldActiveDeadlineSeconds := sucjob.ActiveDeadlineSeconds
58+
sucjob.ActiveDeadlineSeconds = 300
59+
defer func() { sucjob.ActiveDeadlineSeconds = oldActiveDeadlineSeconds }()
60+
61+
plan.Spec.JobActiveDeadlineSecs = 0
62+
job := sucjob.New(plan, node, "bar")
63+
Expect(*job.Spec.ActiveDeadlineSeconds).To(Equal(int64(300)))
64+
})
65+
})
66+
67+
Context("When the Plan has a negative value given as its deadline", func() {
68+
It("Constructs the batchv1.Job with a global default", func() {
69+
oldActiveDeadlineSeconds := sucjob.ActiveDeadlineSeconds
70+
sucjob.ActiveDeadlineSeconds = 3600
71+
defer func() { sucjob.ActiveDeadlineSeconds = oldActiveDeadlineSeconds }()
72+
73+
plan.Spec.JobActiveDeadlineSecs = -1
74+
job := sucjob.New(plan, node, "baz")
75+
Expect(*job.Spec.ActiveDeadlineSeconds).To(Equal(int64(3600)))
76+
})
77+
})
78+
79+
Context("When cluster has a maximum deadline and the Plan deadline exceeds that value", func() {
80+
It("Constructs the batchv1.Job with the cluster's maximum deadline value", func() {
81+
oldActiveDeadlineSecondsMax := sucjob.ActiveDeadlineSecondsMax
82+
sucjob.ActiveDeadlineSecondsMax = 300
83+
defer func() { sucjob.ActiveDeadlineSecondsMax = oldActiveDeadlineSecondsMax }()
84+
85+
plan.Spec.JobActiveDeadlineSecs = 600
86+
job := sucjob.New(plan, node, "foobar")
87+
Expect(*job.Spec.ActiveDeadlineSeconds).To(Equal(int64(300)))
88+
})
89+
})
90+
})
91+
})

0 commit comments

Comments
 (0)