Skip to content

Commit 11e9866

Browse files
authored
Merge pull request #220 from zhanggbj/add_label
Ensure IPAddress has a ClusterName label as CAPI resources
2 parents 72541a4 + 8bd716c commit 11e9866

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

internal/controllers/ipaddressclaim_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,24 @@ 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() {
100+
cluster := clusterv1.Cluster{
101+
ObjectMeta: metav1.ObjectMeta{
102+
Name: clusterName,
103+
Namespace: namespace,
104+
},
105+
Spec: clusterv1.ClusterSpec{
106+
Paused: false,
107+
},
108+
}
109+
Expect(k8sClient.Create(context.Background(), &cluster)).To(Succeed())
110+
Eventually(Get(&cluster)).Should(Succeed())
111+
97112
pool := v1alpha2.InClusterIPPool{
98113
ObjectMeta: metav1.ObjectMeta{
99114
Name: poolName,
@@ -112,10 +127,20 @@ var _ = Describe("IPAddressClaimReconciler", func() {
112127
AfterEach(func() {
113128
deleteClaim("test", namespace)
114129
deleteNamespacedPool(poolName, namespace)
130+
131+
cluster := clusterv1.Cluster{
132+
ObjectMeta: metav1.ObjectMeta{
133+
Name: clusterName,
134+
Namespace: namespace,
135+
},
136+
}
137+
ExpectWithOffset(1, k8sClient.Delete(context.Background(), &cluster)).To(Succeed())
138+
EventuallyWithOffset(1, Get(&cluster)).Should(Not(Succeed()))
115139
})
116140

117141
It("should allocate an Address from the Pool", func() {
118142
claim := newClaim("test", namespace, "InClusterIPPool", poolName)
143+
claim.Labels = map[string]string{clusterv1.ClusterNameLabel: clusterName}
119144
expectedIPAddress := ipamv1.IPAddress{
120145
ObjectMeta: metav1.ObjectMeta{
121146
Name: "test",
@@ -137,6 +162,7 @@ var _ = Describe("IPAddressClaimReconciler", func() {
137162
Name: poolName,
138163
},
139164
},
165+
Labels: map[string]string{clusterv1.ClusterNameLabel: clusterName},
140166
},
141167
Spec: ipamv1.IPAddressSpec{
142168
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)