Skip to content

Commit

Permalink
Watch OSNet CRs to reconcile OSMacAddr CR
Browse files Browse the repository at this point in the history
  • Loading branch information
stuggi committed Sep 13, 2021
1 parent 84b5066 commit c50ff1f
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions controllers/openstackmacaddress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

ospdirectorv1beta1 "github.com/openstack-k8s-operators/osp-director-operator/api/v1beta1"
Expand Down Expand Up @@ -168,6 +169,8 @@ func (r *OpenStackMACAddressReconciler) Reconcile(ctx context.Context, req ctrl.
// MAC reservation as deleted. If the node gets recreated with the
// same name, it reuses the MAC.
macNodeStatus.Deleted = true
r.Log.Info(fmt.Sprintf("Flag MAC reservation as deleted - node: %s", reservation.Hostname))

}

actualStatus.MACReservations[reservation.Hostname] = macNodeStatus
Expand Down Expand Up @@ -209,10 +212,40 @@ func (r *OpenStackMACAddressReconciler) Reconcile(ctx context.Context, req ctrl.

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackMACAddressReconciler) SetupWithManager(mgr ctrl.Manager) error {

namespacedFn := handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
result := []reconcile.Request{}

// get all CRs from the same namespace, right now there should only be one
crs := &ospdirectorv1beta1.OpenStackMACAddressList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), crs, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve CRs %v")
return nil
}

for _, cr := range crs.Items {
if o.GetNamespace() == cr.Namespace {
// return namespace and Name of CR
name := client.ObjectKey{
Namespace: cr.Namespace,
Name: cr.Name,
}
result = append(result, reconcile.Request{NamespacedName: name})
}
}
if len(result) > 0 {
return result
}
return nil
})

return ctrl.NewControllerManagedBy(mgr).
For(&ospdirectorv1beta1.OpenStackMACAddress{}).
Watches(&source.Kind{Type: &ospdirectorv1beta1.OpenStackIPSet{}}, &handler.EnqueueRequestForObject{}).
Watches(&source.Kind{Type: &ospdirectorv1beta1.OpenStackNet{}}, &handler.EnqueueRequestForObject{}).
// Watch OpenStackNet to create/delete MAC
Watches(&source.Kind{Type: &ospdirectorv1beta1.OpenStackNet{}}, namespacedFn).
Complete(r)
}

Expand Down

0 comments on commit c50ff1f

Please sign in to comment.