Skip to content

Commit

Permalink
chore: change knative ptr to lo ptr (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
njtran authored May 21, 2024
1 parent 9b145a6 commit 7d8492a
Show file tree
Hide file tree
Showing 25 changed files with 284 additions and 305 deletions.
5 changes: 2 additions & 3 deletions pkg/apis/v1beta1/nodeclaim_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"
"knative.dev/pkg/apis"
"knative.dev/pkg/ptr"
)

var (
Expand Down Expand Up @@ -268,7 +267,7 @@ func validateEvictionThresholds(m map[string]string, fieldName string) (errs *ap

// Validate validateImageGCHighThresholdPercent
func (in *KubeletConfiguration) validateImageGCHighThresholdPercent() (errs *apis.FieldError) {
if in.ImageGCHighThresholdPercent != nil && ptr.Int32Value(in.ImageGCHighThresholdPercent) < ptr.Int32Value(in.ImageGCLowThresholdPercent) {
if in.ImageGCHighThresholdPercent != nil && lo.FromPtr(in.ImageGCHighThresholdPercent) < lo.FromPtr(in.ImageGCLowThresholdPercent) {
return errs.Also(apis.ErrInvalidValue("must be greater than imageGCLowThresholdPercent", "imageGCHighThresholdPercent"))
}

Expand All @@ -277,7 +276,7 @@ func (in *KubeletConfiguration) validateImageGCHighThresholdPercent() (errs *api

// Validate imageGCLowThresholdPercent
func (in *KubeletConfiguration) validateImageGCLowThresholdPercent() (errs *apis.FieldError) {
if in.ImageGCHighThresholdPercent != nil && ptr.Int32Value(in.ImageGCLowThresholdPercent) > ptr.Int32Value(in.ImageGCHighThresholdPercent) {
if in.ImageGCHighThresholdPercent != nil && lo.FromPtr(in.ImageGCLowThresholdPercent) > lo.FromPtr(in.ImageGCHighThresholdPercent) {
return errs.Also(apis.ErrInvalidValue("must be less than imageGCHighThresholdPercent", "imageGCLowThresholdPercent"))
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/apis/v1beta1/nodeclaim_validation_cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
"knative.dev/pkg/ptr"

. "sigs.k8s.io/karpenter/pkg/apis/v1beta1"

Expand Down Expand Up @@ -359,21 +358,21 @@ var _ = Describe("Validation", func() {
Context("GCThresholdPercent", func() {
It("should succeed on a valid imageGCHighThresholdPercent", func() {
nodeClaim.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(10),
ImageGCHighThresholdPercent: lo.ToPtr(int32(10)),
}
Expect(env.Client.Create(ctx, nodeClaim)).To(Succeed())
})
It("should fail when imageGCHighThresholdPercent is less than imageGCLowThresholdPercent", func() {
nodeClaim.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
It("should fail when imageGCLowThresholdPercent is greather than imageGCHighThresheldPercent", func() {
nodeClaim.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(env.Client.Create(ctx, nodeClaim)).ToNot(Succeed())
})
Expand Down
11 changes: 5 additions & 6 deletions pkg/apis/v1beta1/nodeclaim_validation_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
"knative.dev/pkg/ptr"

. "sigs.k8s.io/karpenter/pkg/apis/v1beta1"

Expand Down Expand Up @@ -318,21 +317,21 @@ var _ = Describe("Validation", func() {
Context("GCThresholdPercent", func() {
It("should succeed on a valid imageGCHighThresholdPercent", func() {
nodeClaim.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(10),
ImageGCHighThresholdPercent: lo.ToPtr(int32(10)),
}
Expect(nodeClaim.Validate(ctx)).To(Succeed())
})
It("should fail when imageGCHighThresholdPercent is less than imageGCLowThresholdPercent", func() {
nodeClaim.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(nodeClaim.Validate(ctx)).ToNot(Succeed())
})
It("should fail when imageGCLowThresholdPercent is greather than imageGCHighThresheldPercent", func() {
nodeClaim.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(nodeClaim.Validate(ctx)).ToNot(Succeed())
})
Expand Down
5 changes: 2 additions & 3 deletions pkg/apis/v1beta1/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/clock"
"knative.dev/pkg/ptr"
)

// NodePoolSpec is the top level nodepool specification. Nodepools
Expand Down Expand Up @@ -218,8 +217,8 @@ type NodePoolList struct {
// 2. If two NodePools have the same weight, then the NodePool with the name later in the alphabet will come first
func (nl *NodePoolList) OrderByWeight() {
sort.Slice(nl.Items, func(a, b int) bool {
weightA := ptr.Int32Value(nl.Items[a].Spec.Weight)
weightB := ptr.Int32Value(nl.Items[b].Spec.Weight)
weightA := lo.FromPtr(nl.Items[a].Spec.Weight)
weightB := lo.FromPtr(nl.Items[b].Spec.Weight)

if weightA == weightB {
// Order NodePools by name for a consistent ordering when sorting equal weight
Expand Down
35 changes: 17 additions & 18 deletions pkg/apis/v1beta1/nodepool_validation_cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/ptr"

. "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
)
Expand Down Expand Up @@ -108,31 +107,31 @@ var _ = Describe("CEL/Validation", func() {
It("should fail when creating a budget with an invalid cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("*"),
Schedule: lo.ToPtr("*"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
It("should fail when creating a schedule with less than 5 entries", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * "),
Schedule: lo.ToPtr("* * * * "),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
It("should fail when creating a budget with a negative duration", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("-20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
It("should fail when creating a budget with a seconds duration", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("30s"))},
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
Expand All @@ -158,7 +157,7 @@ var _ = Describe("CEL/Validation", func() {
It("should fail when creating a budget with a cron but no duration", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
Expand All @@ -172,15 +171,15 @@ var _ = Describe("CEL/Validation", func() {
It("should succeed when creating a budget with both duration and cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
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("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("2h20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
Expand All @@ -194,7 +193,7 @@ var _ = Describe("CEL/Validation", func() {
It("should succeed when creating a budget with special cased crons", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("@annually"),
Schedule: lo.ToPtr("@annually"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
Expand All @@ -203,12 +202,12 @@ var _ = Describe("CEL/Validation", func() {
nodePool.Spec.Disruption.Budgets = []Budget{
{
Nodes: "10",
Schedule: ptr.String("@annually"),
Schedule: lo.ToPtr("@annually"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
},
{
Nodes: "10",
Schedule: ptr.String("*"),
Schedule: lo.ToPtr("*"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
Expand All @@ -221,7 +220,7 @@ var _ = Describe("CEL/Validation", func() {
},
{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
},
{
Expand Down Expand Up @@ -426,29 +425,29 @@ var _ = Describe("CEL/Validation", func() {
Context("ImageGCHighThresholdPercent", func() {
It("should succeed on a imageGCHighThresholdPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(10),
ImageGCHighThresholdPercent: lo.ToPtr(int32(10)),
}
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
})
It("should fail when imageGCHighThresholdPercent is less than imageGCLowThresholdPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
})
Context("ImageGCLowThresholdPercent", func() {
It("should succeed on a imageGCLowThresholdPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCLowThresholdPercent: ptr.Int32(10),
ImageGCLowThresholdPercent: lo.ToPtr(int32(10)),
}
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
})
It("should fail when imageGCLowThresholdPercent is greather than imageGCHighThresheldPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(env.Client.Create(ctx, nodePool)).ToNot(Succeed())
})
Expand Down
31 changes: 15 additions & 16 deletions pkg/apis/v1beta1/nodepool_validation_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/ptr"

. "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
)
Expand Down Expand Up @@ -82,23 +81,23 @@ var _ = Describe("Webhook/Validation", func() {
It("should fail to validate a budget with an invalid cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("*"),
Schedule: lo.ToPtr("*"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
})
It("should fail to validate a schedule with less than 5 entries", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * "),
Schedule: lo.ToPtr("* * * * "),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
})
It("should fail to validate a budget with a cron but no duration", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
}}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
})
Expand All @@ -112,7 +111,7 @@ var _ = Describe("Webhook/Validation", func() {
It("should succeed to validate a budget with both duration and cron", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(nodePool.Validate(ctx)).To(Succeed())
Expand All @@ -126,15 +125,15 @@ var _ = Describe("Webhook/Validation", func() {
It("should succeed to validate a budget with special cased crons", func() {
nodePool.Spec.Disruption.Budgets = []Budget{{
Nodes: "10",
Schedule: ptr.String("@annually"),
Schedule: lo.ToPtr("@annually"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
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("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("2h20m"))},
}}
Expect(nodePool.Validate(ctx)).To(Succeed())
Expand All @@ -143,12 +142,12 @@ var _ = Describe("Webhook/Validation", func() {
nodePool.Spec.Disruption.Budgets = []Budget{
{
Nodes: "10",
Schedule: ptr.String("@annually"),
Schedule: lo.ToPtr("@annually"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
},
{
Nodes: "10",
Schedule: ptr.String("*"),
Schedule: lo.ToPtr("*"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
}}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
Expand All @@ -161,7 +160,7 @@ var _ = Describe("Webhook/Validation", func() {
},
{
Nodes: "10",
Schedule: ptr.String("* * * * *"),
Schedule: lo.ToPtr("* * * * *"),
Duration: &metav1.Duration{Duration: lo.Must(time.ParseDuration("20m"))},
},
{
Expand Down Expand Up @@ -534,29 +533,29 @@ var _ = Describe("Webhook/Validation", func() {
Context("ImageGCHighThresholdPercent", func() {
It("should succeed on a imageGCHighThresholdPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(10),
ImageGCHighThresholdPercent: lo.ToPtr(int32(10)),
}
Expect(nodePool.Validate(ctx)).To(Succeed())
})
It("should fail when imageGCHighThresholdPercent is less than imageGCLowThresholdPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
})
})
Context("ImageGCLowThresholdPercent", func() {
It("should succeed on a imageGCLowThresholdPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCLowThresholdPercent: ptr.Int32(10),
ImageGCLowThresholdPercent: lo.ToPtr(int32(10)),
}
Expect(nodePool.Validate(ctx)).To(Succeed())
})
It("should fail when imageGCLowThresholdPercent is greather than imageGCHighThresheldPercent", func() {
nodePool.Spec.Template.Spec.Kubelet = &KubeletConfiguration{
ImageGCHighThresholdPercent: ptr.Int32(50),
ImageGCLowThresholdPercent: ptr.Int32(60),
ImageGCHighThresholdPercent: lo.ToPtr(int32(50)),
ImageGCLowThresholdPercent: lo.ToPtr(int32(60)),
}
Expect(nodePool.Validate(ctx)).ToNot(Succeed())
})
Expand Down
Loading

0 comments on commit 7d8492a

Please sign in to comment.