Skip to content

Commit a8d5a37

Browse files
committed
Allow specifying human-readable duration string for drain timeout
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
1 parent 745300b commit a8d5a37

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

doc/plan.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ _Appears in:_
6363

6464
| Field | Description | Default | Validation |
6565
| --- | --- | --- | --- |
66-
| `timeout` _[Duration](#duration)_ | | | |
66+
| `timeout` _[IntOrString](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#intorstring-intstr-util)_ | If a string, this is passed through directly to the `kubectl drain` command.<br />If an int, this represents the duration as a count of nanoseconds, and will be converted to a duration string when passed to the `kubectl drain` command. | | |
6767
| `gracePeriod` _integer_ | | | |
6868
| `deleteLocalData` _boolean_ | | | |
6969
| `deleteEmptydirData` _boolean_ | | | |

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/rancher/wrangler/v3/pkg/genericcondition"
1212
corev1 "k8s.io/api/core/v1"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/util/intstr"
1415
)
1516

1617
var (
@@ -137,7 +138,9 @@ type VolumeSpec struct {
137138
// - https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/
138139
// - https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#drain
139140
type DrainSpec struct {
140-
Timeout *time.Duration `json:"timeout,omitempty"`
141+
// If a string, this is passed through directly to the `kubectl drain` command.
142+
// If an int, this represents the duration as a count of nanoseconds, and will be converted to a duration string when passed to the `kubectl drain` command.
143+
Timeout *intstr.IntOrString `json:"timeout,omitempty"`
141144
GracePeriod *int32 `json:"gracePeriod,omitempty"`
142145
DeleteLocalData *bool `json:"deleteLocalData,omitempty"`
143146
DeleteEmptydirData *bool `json:"deleteEmptydirData,omitempty"`

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

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crds/yaml/generated/upgrade.cattle.io_plans.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ spec:
145145
skipWaitForDeleteTimeout:
146146
type: integer
147147
timeout:
148+
anyOf:
149+
- type: integer
150+
- type: string
148151
description: |-
149-
A Duration represents the elapsed time between two instants
150-
as an int64 nanosecond count. The representation limits the
151-
largest representable duration to approximately 290 years.
152-
format: int64
153-
type: integer
152+
If a string, this is passed through directly to the `kubectl drain` command.
153+
If an int, this represents the duration as a count of nanoseconds, and will be converted to a duration string when passed to the `kubectl drain` command.
154+
x-kubernetes-int-or-string: true
154155
type: object
155156
exclusive:
156157
description: Jobs for exclusive plans cannot be run alongside any

pkg/upgrade/job/job.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020
"k8s.io/apimachinery/pkg/labels"
2121
"k8s.io/apimachinery/pkg/selection"
22+
"k8s.io/apimachinery/pkg/util/intstr"
2223
"k8s.io/utils/pointer"
2324
)
2425

@@ -354,7 +355,13 @@ func New(plan *upgradeapiv1.Plan, node *corev1.Node, controllerName string) *bat
354355
args = append(args, "--force")
355356
}
356357
if drain.Timeout != nil {
357-
args = append(args, "--timeout", drain.Timeout.String())
358+
switch drain.Timeout.Type {
359+
case intstr.Int:
360+
timeout := time.Duration(drain.Timeout.IntVal)
361+
args = append(args, "--timeout", timeout.String())
362+
case intstr.String:
363+
args = append(args, "--timeout", drain.Timeout.StrVal)
364+
}
358365
}
359366
if drain.GracePeriod != nil {
360367
args = append(args, "--grace-period", strconv.FormatInt(int64(*drain.GracePeriod), 10))
@@ -448,8 +455,17 @@ func New(plan *upgradeapiv1.Plan, node *corev1.Node, controllerName string) *bat
448455
}
449456
}
450457

451-
if drain != nil && drain.Timeout != nil && job.Spec.ActiveDeadlineSeconds != nil && drain.Timeout.Milliseconds() > *job.Spec.ActiveDeadlineSeconds*1000 {
452-
logrus.Warnf("Plan %s/%s drain timeout exceeds active deadline seconds", plan.Namespace, plan.Name)
458+
if drain != nil && drain.Timeout != nil && job.Spec.ActiveDeadlineSeconds != nil {
459+
var timeout time.Duration
460+
switch drain.Timeout.Type {
461+
case intstr.Int:
462+
timeout = time.Duration(drain.Timeout.IntVal)
463+
case intstr.String:
464+
timeout, _ = time.ParseDuration(drain.Timeout.StrVal)
465+
}
466+
if timeout.Milliseconds() > *job.Spec.ActiveDeadlineSeconds*1000 {
467+
logrus.Warnf("Plan %s/%s drain timeout exceeds active deadline seconds", plan.Namespace, plan.Name)
468+
}
453469
}
454470

455471
return job

0 commit comments

Comments
 (0)