Skip to content

Commit

Permalink
add controller reference for inventory object (ironcore-dev#129)
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
aobort authored Jun 19, 2024
1 parent 86fdc7c commit 3f9eaff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
37 changes: 34 additions & 3 deletions internal/controller/inventory_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
metalv1alpha1 "github.com/ironcore-dev/metal/api/v1alpha1"
metalv1alpha1apply "github.com/ironcore-dev/metal/client/applyconfiguration/api/v1alpha1"
"github.com/ironcore-dev/metal/internal/ssa"
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1apply "k8s.io/client-go/applyconfigurations/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -57,6 +59,10 @@ func (r *InventoryReconciler) reconcile(ctx context.Context, inventory metalv1al
machine := machines.Items[idx].DeepCopy()
machineApply := metalv1alpha1apply.Machine(machine.Name, machine.Namespace)

if err := r.setControllerReference(ctx, machine, &inventory); err != nil {
return err
}

sizeLabels := make(map[string]string)
for k, v := range inventory.GetLabels() {
if !strings.HasPrefix(k, MachineSizeLabelPrefix) {
Expand All @@ -71,20 +77,45 @@ func (r *InventoryReconciler) reconcile(ctx context.Context, inventory metalv1al
if machine.Spec.InventoryRef == nil {
machineSpecApply := metalv1alpha1apply.MachineSpec().
WithPower(metalv1alpha1.PowerOff).
WithInventoryRef(v1.LocalObjectReference{Name: inventory.Name})
WithInventoryRef(corev1.LocalObjectReference{Name: inventory.Name})
machineApply = machineApply.WithSpec(machineSpecApply)
return r.Patch(
ctx, machine, ssa.Apply(machineApply), client.FieldOwner(InventoryFieldManager), client.ForceOwnership)
} else {
machineSpecApply := metalv1alpha1apply.MachineSpec().
WithPower(machine.Spec.Power).
WithInventoryRef(v1.LocalObjectReference{Name: inventory.Name})
WithInventoryRef(corev1.LocalObjectReference{Name: inventory.Name})
machineApply = machineApply.WithSpec(machineSpecApply)
return r.Patch(
ctx, machine, ssa.Apply(machineApply), client.FieldOwner(InventoryFieldManager), client.ForceOwnership)
}
}

func (r *InventoryReconciler) setControllerReference(ctx context.Context, machine, inventory client.Object) error {
owners := inventory.GetOwnerReferences()
if slices.ContainsFunc(owners, func(ref metav1.OwnerReference) bool {
return ref.Name == machine.GetName()
}) {
return nil
}

if err := ctrl.SetControllerReference(machine, inventory, r.Scheme()); err != nil {
return err
}
existing := metav1.GetControllerOf(inventory)
owner := metav1apply.OwnerReference().
WithAPIVersion(existing.APIVersion).
WithKind(existing.Kind).
WithName(existing.Name).
WithUID(existing.UID).
WithController(*existing.Controller).
WithBlockOwnerDeletion(*existing.BlockOwnerDeletion)
inventoryApply := metalv1alpha1apply.Inventory(inventory.GetName(), inventory.GetNamespace()).
WithOwnerReferences(owner)
return r.Patch(
ctx, inventory, ssa.Apply(inventoryApply), client.FieldOwner(InventoryFieldManager), client.ForceOwnership)
}

func (r *InventoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Client = mgr.GetClient()

Expand Down
27 changes: 2 additions & 25 deletions internal/controller/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,31 +550,8 @@ func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.Machine{}).
Watches(&metalv1alpha1.Inventory{}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
requests := make([]reconcile.Request, 0)
source, ok := object.(*metalv1alpha1.Inventory)
if !ok {
return requests
}
machineList := &metalv1alpha1.MachineList{}
if err := r.List(ctx, machineList); err != nil {
log.Error(ctx, err, "failed to list machines")
return requests
}
for _, machine := range machineList.Items {
if machine.Spec.InventoryRef == nil {
continue
}
if machine.Spec.InventoryRef.Name == source.Name {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: machine.GetName(),
}})
break
}
}
return requests
})).
Watches(&metalv1alpha1.Inventory{}, handler.EnqueueRequestForOwner(
mgr.GetScheme(), mgr.GetRESTMapper(), &metalv1alpha1.Machine{}, handler.OnlyControllerOwner())).
Watches(&metalv1alpha1.OOB{}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
requests := make([]reconcile.Request, 0)
source, ok := object.(*metalv1alpha1.OOB)
Expand Down

0 comments on commit 3f9eaff

Please sign in to comment.