From 19d5a36c7d4965a20968f2f76c61a52e042faad3 Mon Sep 17 00:00:00 2001 From: Nick Tran <10810510+njtran@users.noreply.github.com> Date: Thu, 12 Sep 2024 14:07:12 -0700 Subject: [PATCH] fix: add default disruption stanza (#1662) --- kwok/charts/crds/karpenter.sh_nodepools.yaml | 2 +- pkg/apis/crds/karpenter.sh_nodepools.yaml | 2 + pkg/apis/v1/nodepool.go | 1 + pkg/apis/v1/nodepool_default_test.go | 71 ++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 pkg/apis/v1/nodepool_default_test.go diff --git a/kwok/charts/crds/karpenter.sh_nodepools.yaml b/kwok/charts/crds/karpenter.sh_nodepools.yaml index 36d2d1dd91..9fd232212d 100644 --- a/kwok/charts/crds/karpenter.sh_nodepools.yaml +++ b/kwok/charts/crds/karpenter.sh_nodepools.yaml @@ -1 +1 @@ -../../../pkg/apis/crds/karpenter.sh_nodepools.yaml \ No newline at end of file +../../../pkg/apis/crds/karpenter.sh_nodepools.yaml diff --git a/pkg/apis/crds/karpenter.sh_nodepools.yaml b/pkg/apis/crds/karpenter.sh_nodepools.yaml index eb30a4d5cd..4c200f2a9b 100644 --- a/pkg/apis/crds/karpenter.sh_nodepools.yaml +++ b/pkg/apis/crds/karpenter.sh_nodepools.yaml @@ -71,6 +71,8 @@ spec: from a combination of nodepool and pod scheduling constraints. properties: disruption: + default: + consolidateAfter: 0s description: Disruption contains the parameters that relate to Karpenter's disruption logic properties: budgets: diff --git a/pkg/apis/v1/nodepool.go b/pkg/apis/v1/nodepool.go index 9f6d06dcc3..a5717c9ccc 100644 --- a/pkg/apis/v1/nodepool.go +++ b/pkg/apis/v1/nodepool.go @@ -43,6 +43,7 @@ type NodePoolSpec struct { // +required Template NodeClaimTemplate `json:"template"` // Disruption contains the parameters that relate to Karpenter's disruption logic + // +kubebuilder:default:={consolidateAfter: "0s"} // +optional Disruption Disruption `json:"disruption"` // Limits define a set of bounds for provisioning capacity. diff --git a/pkg/apis/v1/nodepool_default_test.go b/pkg/apis/v1/nodepool_default_test.go new file mode 100644 index 0000000000..96d6954713 --- /dev/null +++ b/pkg/apis/v1/nodepool_default_test.go @@ -0,0 +1,71 @@ +/* +Copyright The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1_test + +import ( + "strings" + "time" + + "github.com/Pallinder/go-randomdata" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/samber/lo" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "sigs.k8s.io/controller-runtime/pkg/client" + + . "sigs.k8s.io/karpenter/pkg/apis/v1" +) + +var _ = Describe("CEL/Default", func() { + var nodePool *NodePool + + BeforeEach(func() { + nodePool = &NodePool{ + ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(randomdata.SillyName())}, + Spec: NodePoolSpec{ + Template: NodeClaimTemplate{ + Spec: NodeClaimTemplateSpec{ + NodeClassRef: &NodeClassReference{ + Kind: "NodeClaim", + Name: "default", + }, + Requirements: []NodeSelectorRequirementWithMinValues{ + { + NodeSelectorRequirement: v1.NodeSelectorRequirement{ + Key: CapacityTypeLabelKey, + Operator: v1.NodeSelectorOpExists, + }, + }, + }, + }, + }, + }, + } + }) + Context("Defaults/TopLevel", func() { + It("should default the disruption stanza when undefined", func() { + Expect(env.Client.Create(ctx, nodePool)).To(Succeed()) + Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(nodePool), nodePool)).To(Succeed()) + Expect(nodePool.Spec.Disruption).ToNot(BeNil()) + Expect(lo.FromPtr(nodePool.Spec.Disruption.ConsolidateAfter.Duration)).To(Equal(0 * time.Second)) + Expect(nodePool.Spec.Disruption.ConsolidationPolicy).To(Equal(ConsolidationPolicyWhenEmptyOrUnderutilized)) + Expect(nodePool.Spec.Disruption.Budgets).To(Equal([]Budget{{Nodes: "10%"}})) + }) + }) +})