Skip to content

Commit

Permalink
cherrypick(v0.34): fix: Adjust duration validation to include format …
Browse files Browse the repository at this point in the history
…for `1h10m` (#1137)
  • Loading branch information
engedaam committed Mar 26, 2024
1 parent bee2bf9 commit 3f66cef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/crds/karpenter.sh_nodepools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ spec:
This is required if Schedule is set.
This regex has an optional 0s at the end since the duration.String() always adds
a 0s at the end.
pattern: ^([0-9]+(m|h)+(0s)?)$
pattern: ^((([0-9]+(h|m))|([0-9]+h[0-9]+m))(0s)?)$
type: string
nodes:
default: 10%
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/v1beta1/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type Budget struct {
// This is required if Schedule is set.
// This regex has an optional 0s at the end since the duration.String() always adds
// a 0s at the end.
// +kubebuilder:validation:Pattern=`^([0-9]+(m|h)+(0s)?)$`
// +kubebuilder:validation:Pattern=`^((([0-9]+(h|m))|([0-9]+h[0-9]+m))(0s)?)$`
// +kubebuilder:validation:Type="string"
// +optional
Duration *metav1.Duration `json:"duration,omitempty" hash:"ignore"`
Expand Down
10 changes: 9 additions & 1 deletion pkg/apis/v1beta1/nodepool_validation_cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ var _ = Describe("CEL/Validation", func() {
It("should fail when creating a budget with a duration but no cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("-20m"))},
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
Expand All @@ -174,6 +174,14 @@ var _ = Describe("CEL/Validation", func() {
}}
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
})
It("should succeed when creating a budget with hours and minutes in duration", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("2h20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
})
It("should succeed when creating a budget with neither duration nor cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Expand Down
10 changes: 9 additions & 1 deletion pkg/apis/v1beta1/nodepool_validation_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var _ = Describe("Webhook/Validation", func() {
It("should fail to validate a budget with a duration but no cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("-20m"))},
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
})
Expand All @@ -131,6 +131,14 @@ var _ = Describe("Webhook/Validation", func() {
}}
Expect(nodePool.Validate(ctx)).To(Succeed())
})
It("should succeed when creating a budget with hours and minutes in duration", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("2h20m"))},
}}
Expect(nodePool.Validate(ctx)).To(Succeed())
})
It("should fail when creating two budgets where one has an invalid crontab", func() {
nodePool.Spec.Disruption.Budgets = []Budget{
{
Expand Down

0 comments on commit 3f66cef

Please sign in to comment.