Skip to content

Commit

Permalink
chore: Use defined tag variables in labels.go (aws#7380)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Nov 14, 2024
1 parent fdccc5f commit 569c06d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
15 changes: 8 additions & 7 deletions pkg/apis/v1/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ var (
// Adheres to cluster name pattern matching as specified in the API spec
// https://docs.aws.amazon.com/eks/latest/APIReference/API_CreateCluster.html
regexp.MustCompile(`^kubernetes\.io/cluster/[0-9A-Za-z][A-Za-z0-9\-_]*$`),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(karpv1.NodePoolLabelKey))),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(NodePoolTagKey))),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(EKSClusterNameTagKey))),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(LabelNodeClass))),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(TagNodeClaim))),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(NodeClassTagKey))),
regexp.MustCompile(fmt.Sprintf("^%s$", regexp.QuoteMeta(NodeClaimTagKey))),
}
AMIFamilyBottlerocket = "Bottlerocket"
AMIFamilyAL2 = "AL2"
Expand All @@ -96,8 +96,6 @@ var (
ResourcePrivateIPv4Address corev1.ResourceName = "vpc.amazonaws.com/PrivateIPv4Address"
ResourceEFA corev1.ResourceName = "vpc.amazonaws.com/efa"

EKSClusterNameTagKey = "eks:eks-cluster-name"

LabelNodeClass = apis.Group + "/ec2nodeclass"

LabelTopologyZoneID = "topology.k8s.aws/zone-id"
Expand Down Expand Up @@ -126,6 +124,9 @@ var (
AnnotationEC2NodeClassHashVersion = apis.Group + "/ec2nodeclass-hash-version"
AnnotationInstanceTagged = apis.Group + "/tagged"

TagNodeClaim = coreapis.Group + "/nodeclaim"
TagName = "Name"
NodeClaimTagKey = coreapis.Group + "/nodeclaim"
NameTagKey = "Name"
NodePoolTagKey = karpv1.NodePoolLabelKey
NodeClassTagKey = LabelNodeClass
EKSClusterNameTagKey = "eks:eks-cluster-name"
)
4 changes: 2 additions & 2 deletions pkg/controllers/nodeclaim/tagging/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (c *Controller) Register(_ context.Context, m manager.Manager) error {

func (c *Controller) tagInstance(ctx context.Context, nc *karpv1.NodeClaim, id string) error {
tags := map[string]string{
v1.TagName: nc.Status.NodeName,
v1.TagNodeClaim: nc.Name,
v1.NameTagKey: nc.Status.NodeName,
v1.NodeClaimTagKey: nc.Name,
v1.EKSClusterNameTagKey: options.FromContext(ctx).ClusterName,
}

Expand Down
22 changes: 11 additions & 11 deletions pkg/controllers/nodeclaim/tagging/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var _ = Describe("TaggingController", func() {
ExpectObjectReconciled(ctx, env.Client, taggingController, nodeClaim)
Expect(nodeClaim.Annotations).To(Not(HaveKey(v1.AnnotationInstanceTagged)))
Expect(lo.ContainsBy(ec2Instance.Tags, func(tag ec2types.Tag) bool {
return *tag.Key == v1.TagName
return *tag.Key == v1.NameTagKey
})).To(BeFalse())
})

Expand All @@ -134,7 +134,7 @@ var _ = Describe("TaggingController", func() {
ExpectObjectReconciled(ctx, env.Client, taggingController, nodeClaim)
Expect(nodeClaim.Annotations).To(Not(HaveKey(v1.AnnotationInstanceTagged)))
Expect(lo.ContainsBy(ec2Instance.Tags, func(tag ec2types.Tag) bool {
return tag.Key == &v1.TagName
return tag.Key == &v1.NameTagKey
})).To(BeFalse())
})

Expand Down Expand Up @@ -181,7 +181,7 @@ var _ = Describe("TaggingController", func() {
ExpectObjectReconciled(ctx, env.Client, taggingController, nodeClaim)
Expect(nodeClaim.Annotations).To(Not(HaveKey(v1.AnnotationInstanceTagged)))
Expect(lo.ContainsBy(ec2Instance.Tags, func(tag ec2types.Tag) bool {
return tag.Key == &v1.TagName
return tag.Key == &v1.NameTagKey
})).To(BeFalse())
})

Expand Down Expand Up @@ -209,8 +209,8 @@ var _ = Describe("TaggingController", func() {
Expect(nodeClaim.Annotations).To(HaveKey(v1.AnnotationInstanceTagged))

expectedTags := map[string]string{
v1.TagName: nodeClaim.Status.NodeName,
v1.TagNodeClaim: nodeClaim.Name,
v1.NameTagKey: nodeClaim.Status.NodeName,
v1.NodeClaimTagKey: nodeClaim.Name,
v1.EKSClusterNameTagKey: options.FromContext(ctx).ClusterName,
}
ec2Instance := lo.Must(awsEnv.EC2API.Instances.Load(*ec2Instance.InstanceId)).(ec2types.Instance)
Expand All @@ -223,12 +223,12 @@ var _ = Describe("TaggingController", func() {
Expect(instanceTags).To(HaveKeyWithValue(tag, value))
}
},
Entry("with the karpenter.sh/nodeclaim tag", v1.TagName, v1.EKSClusterNameTagKey),
Entry("with the eks:eks-cluster-name tag", v1.TagName, v1.TagNodeClaim),
Entry("with the Name tag", v1.TagNodeClaim, v1.EKSClusterNameTagKey),
Entry("with the karpenter.sh/nodeclaim and eks:eks-cluster-name tags", v1.TagName),
Entry("with the Name and eks:eks-cluster-name tags", v1.TagNodeClaim),
Entry("with the karpenter.sh/nodeclaim tag", v1.NameTagKey, v1.EKSClusterNameTagKey),
Entry("with the eks:eks-cluster-name tag", v1.NameTagKey, v1.NodeClaimTagKey),
Entry("with the Name tag", v1.NodeClaimTagKey, v1.EKSClusterNameTagKey),
Entry("with the karpenter.sh/nodeclaim and eks:eks-cluster-name tags", v1.NameTagKey),
Entry("with the Name and eks:eks-cluster-name tags", v1.NodeClaimTagKey),
Entry("with the karpenter.sh/nodeclaim and Name tags", v1.EKSClusterNameTagKey),
Entry("with nothing to tag", v1.TagNodeClaim, v1.EKSClusterNameTagKey, v1.TagName),
Entry("with nothing to tag", v1.NodeClaimTagKey, v1.EKSClusterNameTagKey, v1.NameTagKey),
)
})
8 changes: 4 additions & 4 deletions pkg/providers/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ func (p *DefaultProvider) List(ctx context.Context) ([]*Instance, error) {
Filters: []ec2types.Filter{
{
Name: aws.String("tag-key"),
Values: []string{karpv1.NodePoolLabelKey},
Values: []string{v1.NodePoolTagKey},
},
{
Name: aws.String("tag-key"),
Values: []string{v1.LabelNodeClass},
Values: []string{v1.NodeClassTagKey},
},
{
Name: aws.String("tag-key"),
Values: []string{fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName)},
Name: aws.String(v1.EKSClusterNameTagKey),
Values: []string{options.FromContext(ctx).ClusterName},
},
instanceStateFilter,
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/launchtemplate/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (p *DefaultProvider) DeleteAll(ctx context.Context, nodeClass *v1.EC2NodeCl
Values: []string{clusterName},
},
{
Name: aws.String(fmt.Sprintf("tag:%s", v1.LabelNodeClass)),
Name: aws.String(fmt.Sprintf("tag:%s", v1.NodeClassTagKey)),
Values: []string{nodeClass.Name},
},
},
Expand Down

0 comments on commit 569c06d

Please sign in to comment.