Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherrypick(v0.35): fix: Adjust duration validation to include format for 1h10m #1136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -165,7 +165,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 @@ -177,6 +177,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
Loading