Skip to content

Commit 5ee9279

Browse files
committed
Ensure IPAddress has a ClusterName label as CAPI resources
Signed-off-by: Gong Zhang <[email protected]>
1 parent fdc0071 commit 5ee9279

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/controllers/ipaddressclaim_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ var _ = Describe("IPAddressClaimReconciler", func() {
9191
})
9292

9393
When("the referenced namespaced pool exists", func() {
94-
const poolName = "test-pool"
94+
const (
95+
clusterName = "test-cluster"
96+
poolName = "test-pool"
97+
)
9598

9699
BeforeEach(func() {
97100
pool := v1alpha2.InClusterIPPool{
@@ -116,6 +119,7 @@ var _ = Describe("IPAddressClaimReconciler", func() {
116119

117120
It("should allocate an Address from the Pool", func() {
118121
claim := newClaim("test", namespace, "InClusterIPPool", poolName)
122+
claim.Labels = map[string]string{clusterv1.ClusterNameLabel: clusterName}
119123
expectedIPAddress := ipamv1.IPAddress{
120124
ObjectMeta: metav1.ObjectMeta{
121125
Name: "test",
@@ -137,6 +141,7 @@ var _ = Describe("IPAddressClaimReconciler", func() {
137141
Name: poolName,
138142
},
139143
},
144+
Labels: map[string]string{clusterv1.ClusterNameLabel: clusterName},
140145
},
141146
Spec: ipamv1.IPAddressSpec{
142147
ClaimRef: corev1.LocalObjectReference{

pkg/ipamutil/reconciler.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (r *ClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ct
198198
// The handler will complete it with the ip address.
199199
address := NewIPAddress(claim, pool)
200200

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

211+
if val, ok := claim.Labels[clusterv1.ClusterNameLabel]; ok {
212+
if address.Labels == nil {
213+
address.Labels = make(map[string]string)
214+
}
215+
address.Labels[clusterv1.ClusterNameLabel] = val
216+
}
217+
211218
_ = controllerutil.AddFinalizer(&address, ProtectAddressFinalizer)
212219

213220
return nil

0 commit comments

Comments
 (0)