Skip to content

Commit 35dc096

Browse files
author
Hoanganh.Mai
committed
add check for tenant existence
1 parent 0508ece commit 35dc096

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

internal/controller/ipaddressclaim_controller.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
8585
Namespace: o.Namespace,
8686
}
8787

88+
// 2. check if tenant exists in the list of tenants
89+
_, err = r.NetboxClient.GetTenantDetails(o.Spec.Tenant)
90+
if err != nil {
91+
return ctrl.Result{}, err
92+
}
93+
8894
err = r.Client.Get(ctx, ipAddressLookupKey, ipAddress)
8995
if err != nil {
9096
// return error if not a notfound error
@@ -94,7 +100,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
94100

95101
debugLogger.Info("ipaddress object matching ipaddress claim was not found, creating new ipaddress object")
96102

97-
// 2. check if lease for parent prefix is available
103+
// 3. check if lease for parent prefix is available
98104
parentPrefixName := strings.ReplaceAll(o.Spec.ParentPrefix, "/", "-")
99105

100106
leaseLockerNSN := types.NamespacedName{Name: parentPrefixName, Namespace: r.OperatorNamespace}
@@ -106,7 +112,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
106112
lockCtx, cancel := context.WithCancel(ctx)
107113
defer cancel()
108114

109-
// 3. try to lock lease for parent prefix
115+
// 4. try to lock lease for parent prefix
110116
locked := ll.TryLock(lockCtx)
111117
if !locked {
112118
// lock for parent prefix was not available, rescheduling
@@ -118,7 +124,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
118124
}
119125
debugLogger.Info(fmt.Sprintf("successfully locked parent prefix %s", parentPrefixName))
120126

121-
// 4. try to reclaim ip address
127+
// 5. try to reclaim ip address
122128
h := generateIpAddressRestorationHash(o)
123129
ipAddressModel, err := r.NetboxClient.RestoreExistingIpByHash(config.GetOperatorConfig().NetboxRestorationHashFieldName, h)
124130
if err != nil {
@@ -128,7 +134,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
128134

129135
if ipAddressModel == nil {
130136
// ip address cannot be restored from netbox
131-
// 5.a assign new available ip address
137+
// 6.a assign new available ip address
132138
ipAddressModel, err = r.NetboxClient.GetAvailableIpAddressByClaim(
133139
&models.IPAddressClaim{
134140
ParentPrefix: o.Spec.ParentPrefix,
@@ -141,12 +147,12 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
141147
}
142148
debugLogger.Info(fmt.Sprintf("ip address is not reserved in netbox, assignined new ip address: %s", ipAddressModel.IpAddress))
143149
} else {
144-
// 5.b reassign reserved ip address from netbox
150+
// 6.b reassign reserved ip address from netbox
145151
// do nothing, ip address restored
146152
debugLogger.Info(fmt.Sprintf("reassign reserved ip address from netbox, ip: %s", ipAddressModel.IpAddress))
147153
}
148154

149-
// 6.a create the IPAddress object
155+
// 7.a create the IPAddress object
150156
ipAddressResource := generateIpAddressFromIpAddressClaim(o, ipAddressModel.IpAddress, logger)
151157
err = controllerutil.SetControllerReference(o, ipAddressResource, r.Scheme)
152158
if err != nil {
@@ -168,7 +174,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
168174
}
169175

170176
} else {
171-
// 6.b update fields of IPAddress object
177+
// 7.b update fields of IPAddress object
172178
debugLogger.Info("update ipaddress resource")
173179
err := r.Client.Get(ctx, ipAddressLookupKey, ipAddress)
174180
if err != nil {
@@ -189,7 +195,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
189195
}
190196
}
191197

192-
// 7. update IPAddressClaim Ready status
198+
// 8. update IPAddressClaim Ready status
193199
debugLogger.Info("update ipaddressclaim status")
194200

195201
if apismeta.IsStatusConditionTrue(ipAddress.Status.Conditions, "Ready") {

internal/controller/ipaddressclaim_controller_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
201201
defaultIpAddressClaimCR(), defaultIpAddressCreatedByClaim(false), nil,
202202
nil,
203203
nil,
204-
nil,
204+
[]func(*mock_interfaces.MockTenancyInterface, chan error){
205+
mockTenancyTenancyTenantsList,
206+
},
205207
false, false, netboxv1.IpAddressClaimStatus{}, true),
206208
)
207209
})

0 commit comments

Comments
 (0)