Skip to content

Commit 01ca641

Browse files
authored
Merge pull request #34 from jkyros/nodeclaim-labels-disruption-flapping
Pass labels from machineDeployment to NodeClaim to avoid continuous drift
2 parents 00df49a + c779d4d commit 01ca641

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

pkg/cloudprovider/cloudprovider.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ func (c *CloudProvider) machineToNodeClaim(ctx context.Context, machine *capiv1b
382382
return nil, fmt.Errorf("unable to convert Machine %q to a NodeClaim, no memory capacity found on MachineDeployment %q", machine.GetName(), machineDeployment.Name)
383383
}
384384

385-
// TODO (elmiko) add labels, and taints
385+
// Set NodeClaim labels from the MachineDeployment
386+
nodeClaim.Labels = nodeLabelsFromMachineDeployment(machineDeployment)
387+
388+
// TODO (elmiko) add taints
386389

387390
nodeClaim.Status.Capacity = capacity
388391

@@ -532,7 +535,10 @@ func createNodeClaimFromMachineDeployment(machineDeployment *capiv1beta1.Machine
532535
nodeClaim.Status.Capacity = instanceType.Capacity
533536
nodeClaim.Status.Allocatable = instanceType.Allocatable()
534537

535-
// TODO (elmiko) we might need to also convey the labels and annotations on to the NodeClaim
538+
// Set NodeClaim labels from the MachineDeployment
539+
nodeClaim.Labels = nodeLabelsFromMachineDeployment(machineDeployment)
540+
541+
// TODO (elmiko) add taints
536542

537543
return nodeClaim
538544
}
@@ -593,9 +599,15 @@ func machineDeploymentToInstanceType(machineDeployment *capiv1beta1.MachineDeplo
593599
}
594600

595601
instanceType.Offerings = offerings
596-
// TODO (elmiko) this may not be correct given the code comment in the InstanceType struct about the name corresponding
602+
603+
// TODO (elmiko) find a better way to learn the instance type. The instance name needs to be the same
604+
// as the well-known `node.kubernetes.io/instance-type` that will be on resulting nodes. Usually, a
605+
// cloud controller, or similar, mechanism is used to apply this label.
606+
// For now, we check the labels we know about from the MachineDeployment, if the instance type label
607+
// is not there, leave it blank.
597608
// to the v1.LabelInstanceTypeStable. if karpenter expects this to match the node, then we need to get this value through capi.
598-
instanceType.Name = machineDeployment.Name
609+
// TODO (jkyros) Add a test case to test this behavior
610+
instanceType.Name = labels[corev1.LabelInstanceTypeStable]
599611

600612
// TODO (elmiko) add the proper overhead information, not sure where we will harvest this information.
601613
// perhaps it needs to be a configurable option somewhere.

pkg/cloudprovider/cloudprovider_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
349349
Expect(instanceType.Capacity).Should(HaveKeyWithValue(corev1.ResourceCPU, resource.MustParse("1")))
350350
Expect(instanceType.Capacity).Should(HaveKeyWithValue(corev1.ResourceMemory, resource.MustParse("16Gi")))
351351
Expect(instanceType.Capacity).Should(HaveKeyWithValue(corev1.ResourceName("nvidia.com/gpu"), resource.MustParse("1")))
352-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
352+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
353353
})
354354

355355
It("adds nothing to requirements when no managed labels or scale from zero annotations are present", func() {
@@ -358,7 +358,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
358358
instanceType := machineDeploymentToInstanceType(machineDeployment)
359359

360360
Expect(instanceType.Requirements).To(HaveLen(0))
361-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
361+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
362362
})
363363

364364
It("adds labels to the requirements from the Cluster API propagation rules", func() {
@@ -381,7 +381,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
381381
Expect(instanceType.Requirements).Should(HaveKey("prefixed.node-restriction.kubernetes.io/some-other-thing"))
382382
Expect(instanceType.Requirements).Should(HaveKey("node.cluster.x-k8s.io/another-thing"))
383383
Expect(instanceType.Requirements).Should(HaveKey("prefixed.node.cluster.x-k8s.io/another-thing"))
384-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
384+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
385385
})
386386

387387
It("adds labels to the requirements from the scale from zero annotations", func() {
@@ -395,7 +395,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
395395
Expect(instanceType.Requirements).To(HaveLen(2))
396396
Expect(instanceType.Requirements).Should(HaveKey(corev1.LabelTopologyZone))
397397
Expect(instanceType.Requirements).Should(HaveKey(InstanceSizeLabelKey))
398-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
398+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
399399
})
400400

401401
It("adds labels to the requirements from the propagation rules and the scale from zero annotations", func() {
@@ -424,7 +424,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
424424
Expect(instanceType.Requirements).Should(HaveKey("prefixed.node-restriction.kubernetes.io/some-other-thing"))
425425
Expect(instanceType.Requirements).Should(HaveKey("node.cluster.x-k8s.io/another-thing"))
426426
Expect(instanceType.Requirements).Should(HaveKey("prefixed.node.cluster.x-k8s.io/another-thing"))
427-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
427+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
428428
})
429429

430430
It("adds a single available on-demand offering with price 0 and empty zone", func() {
@@ -438,7 +438,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
438438
Expect(offering.Requirements[karpv1.CapacityTypeLabelKey].Values()).Should(ContainElement(karpv1.CapacityTypeOnDemand))
439439
Expect(offering.Requirements).Should(HaveKey(corev1.LabelTopologyZone))
440440
Expect(offering.Requirements[corev1.LabelTopologyZone].Values()).Should(ContainElement(""))
441-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
441+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
442442
})
443443

444444
It("adds the correct zone to offering when the well known zone label is present", func() {
@@ -453,7 +453,7 @@ var _ = Describe("machineDeploymentToInstanceType function", func() {
453453
offering := instanceType.Offerings[0]
454454
Expect(offering.Requirements).Should(HaveKey(corev1.LabelTopologyZone))
455455
Expect(offering.Requirements[corev1.LabelTopologyZone].Values()).Should(ContainElement(zone))
456-
Expect(instanceType.Name).To(Equal(machineDeployment.Name))
456+
Expect(instanceType.MachineDeploymentName).To(Equal(machineDeployment.Name))
457457
})
458458
})
459459

0 commit comments

Comments
 (0)