Skip to content

Commit

Permalink
Merge pull request #220 from zhanggbj/add_label
Browse files Browse the repository at this point in the history
Ensure IPAddress has a ClusterName label as CAPI resources
  • Loading branch information
k8s-ci-robot authored Feb 8, 2024
2 parents 72541a4 + 8bd716c commit 11e9866
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 27 additions & 1 deletion internal/controllers/ipaddressclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,24 @@ var _ = Describe("IPAddressClaimReconciler", func() {
})

When("the referenced namespaced pool exists", func() {
const poolName = "test-pool"
const (
clusterName = "test-cluster"
poolName = "test-pool"
)

BeforeEach(func() {
cluster := clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Namespace: namespace,
},
Spec: clusterv1.ClusterSpec{
Paused: false,
},
}
Expect(k8sClient.Create(context.Background(), &cluster)).To(Succeed())
Eventually(Get(&cluster)).Should(Succeed())

pool := v1alpha2.InClusterIPPool{
ObjectMeta: metav1.ObjectMeta{
Name: poolName,
Expand All @@ -112,10 +127,20 @@ var _ = Describe("IPAddressClaimReconciler", func() {
AfterEach(func() {
deleteClaim("test", namespace)
deleteNamespacedPool(poolName, namespace)

cluster := clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Namespace: namespace,
},
}
ExpectWithOffset(1, k8sClient.Delete(context.Background(), &cluster)).To(Succeed())
EventuallyWithOffset(1, Get(&cluster)).Should(Not(Succeed()))
})

It("should allocate an Address from the Pool", func() {
claim := newClaim("test", namespace, "InClusterIPPool", poolName)
claim.Labels = map[string]string{clusterv1.ClusterNameLabel: clusterName}
expectedIPAddress := ipamv1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand All @@ -137,6 +162,7 @@ var _ = Describe("IPAddressClaimReconciler", func() {
Name: poolName,
},
},
Labels: map[string]string{clusterv1.ClusterNameLabel: clusterName},
},
Spec: ipamv1.IPAddressSpec{
ClaimRef: corev1.LocalObjectReference{
Expand Down
9 changes: 8 additions & 1 deletion pkg/ipamutil/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (r *ClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ct
// The handler will complete it with the ip address.
address := NewIPAddress(claim, pool)

// Patch or create the address, ensuring necessary owner references are set.
// Patch or create the address, ensuring necessary owner references and labels are set
operationResult, err := controllerutil.CreateOrPatch(ctx, r.Client, &address, func() error {
if res, err = handler.EnsureAddress(ctx, &address); err != nil {
return err
Expand All @@ -208,6 +208,13 @@ func (r *ClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ct
return errors.Wrap(err, "failed to ensure owner references on address")
}

if val, ok := claim.Labels[clusterv1.ClusterNameLabel]; ok {
if address.Labels == nil {
address.Labels = make(map[string]string)
}
address.Labels[clusterv1.ClusterNameLabel] = val
}

_ = controllerutil.AddFinalizer(&address, ProtectAddressFinalizer)

return nil
Expand Down

0 comments on commit 11e9866

Please sign in to comment.