Skip to content

Commit

Permalink
add check for tenant existence
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanganh.Mai committed Sep 24, 2024
1 parent 0508ece commit 35dc096
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 14 additions & 8 deletions internal/controller/ipaddressclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
Namespace: o.Namespace,
}

// 2. check if tenant exists in the list of tenants
_, err = r.NetboxClient.GetTenantDetails(o.Spec.Tenant)
if err != nil {
return ctrl.Result{}, err
}

err = r.Client.Get(ctx, ipAddressLookupKey, ipAddress)
if err != nil {
// return error if not a notfound error
Expand All @@ -94,7 +100,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque

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

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

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

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

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

if ipAddressModel == nil {
// ip address cannot be restored from netbox
// 5.a assign new available ip address
// 6.a assign new available ip address
ipAddressModel, err = r.NetboxClient.GetAvailableIpAddressByClaim(
&models.IPAddressClaim{
ParentPrefix: o.Spec.ParentPrefix,
Expand All @@ -141,12 +147,12 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
debugLogger.Info(fmt.Sprintf("ip address is not reserved in netbox, assignined new ip address: %s", ipAddressModel.IpAddress))
} else {
// 5.b reassign reserved ip address from netbox
// 6.b reassign reserved ip address from netbox
// do nothing, ip address restored
debugLogger.Info(fmt.Sprintf("reassign reserved ip address from netbox, ip: %s", ipAddressModel.IpAddress))
}

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

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

// 7. update IPAddressClaim Ready status
// 8. update IPAddressClaim Ready status
debugLogger.Info("update ipaddressclaim status")

if apismeta.IsStatusConditionTrue(ipAddress.Status.Conditions, "Ready") {
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/ipaddressclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ var _ = Describe("IpAddressClaim Controller", Ordered, func() {
defaultIpAddressClaimCR(), defaultIpAddressCreatedByClaim(false), nil,
nil,
nil,
nil,
[]func(*mock_interfaces.MockTenancyInterface, chan error){
mockTenancyTenancyTenantsList,
},
false, false, netboxv1.IpAddressClaimStatus{}, true),
)
})

0 comments on commit 35dc096

Please sign in to comment.