Skip to content

Commit

Permalink
inventory controller
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
aobort committed Jun 13, 2024
1 parent d8b747a commit 75bc841
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
25 changes: 22 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/ironcore-dev/metal/internal/controller"
"github.com/ironcore-dev/metal/internal/log"
"github.com/ironcore-dev/metal/internal/namespace"
//+kubebuilder:scaffold:imports
// +kubebuilder:scaffold:imports
)

type params struct {
Expand All @@ -49,6 +49,7 @@ type params struct {
enableMachineController bool
enableMachineClaimController bool
enableOOBController bool
enableInventoryController bool
oobIpLabelSelector string
oobMacDB string
oobCredsRenewalBeforeExpiry time.Duration
Expand All @@ -73,6 +74,7 @@ func parseCmdLine() params {
pflag.Bool("enable-machine-controller", true, "Enable the Machine controller.")
pflag.Bool("enable-machineclaim-controller", true, "Enable the MachineClaim controller.")
pflag.Bool("enable-oob-controller", true, "Enable the OOB controller.")
pflag.Bool("enable-inventory-controller", true, "Enable the Inventory controller")
pflag.String("oob-ip-label-selector", "", "OOB: Filter IP objects by labels.")
pflag.String("oob-mac-db", "", "OOB: Load MAC DB from file.")
pflag.Duration("oob-creds-renewal-before-expiry", time.Hour*24*7, "OOB: Renew expiring credentials this long before they expire.")
Expand Down Expand Up @@ -171,7 +173,7 @@ func main() {
exitCode = 1
return
}
//+kubebuilder:scaffold:scheme
// +kubebuilder:scaffold:scheme

var kcfg *rest.Config
kcfg, err = ctrl.GetConfig()
Expand Down Expand Up @@ -286,7 +288,24 @@ func main() {
}
}

//+kubebuilder:scaffold:builder
if p.enableInventoryController {
var inventoryReconciler *controller.InventoryReconciler
inventoryReconciler, err = controller.NewInventoryReconciler()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "Inventory")
exitCode = 1
return
}

err = inventoryReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "Inventory")
exitCode = 1
return
}
}

// +kubebuilder:scaffold:builder

err = mgr.AddHealthzCheck("health", healthz.Ping)
if err != nil {
Expand Down
61 changes: 61 additions & 0 deletions internal/controller/inventory_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package controller

import (
"context"
"fmt"
"slices"

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"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func NewInventoryReconciler() (*InventoryReconciler, error) {
return &InventoryReconciler{}, nil
}

type InventoryReconciler struct {
client.Client
}

func (r *InventoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var inventory metalv1alpha1.Inventory
if err := r.Get(ctx, req.NamespacedName, &inventory); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("cannot get Inventory: %w", err))
}
if !inventory.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
}
return ctrl.Result{}, r.reconcile(ctx, inventory)
}

func (r *InventoryReconciler) reconcile(ctx context.Context, inventory metalv1alpha1.Inventory) error {
machines := &metalv1alpha1.MachineList{}
if err := r.List(ctx, machines); err != nil {
return err
}
idx := slices.IndexFunc(machines.Items, func(machine metalv1alpha1.Machine) bool {
return machine.Spec.UUID == inventory.Name
})
if idx == -1 {
return nil
}
machine := machines.Items[idx].DeepCopy()
machineApply := metalv1alpha1apply.Machine(machine.Name, machine.Namespace)
machineSpecApply := metalv1alpha1apply.MachineSpec().
WithPower(metalv1alpha1.PowerOff).
WithInventoryRef(v1.LocalObjectReference{Name: inventory.Name})
machineApply = machineApply.WithSpec(machineSpecApply)
return r.Patch(ctx, machine, ssa.Apply(machineApply), client.FieldOwner(MachineFieldOwner), client.ForceOwnership)
}

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

return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.Inventory{}).
Complete(r)
}
5 changes: 4 additions & 1 deletion internal/controller/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,10 @@ func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
return requests
}
for _, machine := range machineList.Items {
if machine.Spec.UUID == source.Name {
if machine.Spec.InventoryRef == nil {
continue
}
if machine.Spec.InventoryRef.Name == source.Name {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: machine.GetName(),
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ var _ = BeforeSuite(func() {
Expect(oobReconciler).NotTo(BeNil())
Expect(oobReconciler.SetupWithManager(mgr)).To(Succeed())

var inventoryReconciler *InventoryReconciler
inventoryReconciler, err = NewInventoryReconciler()
Expect(err).NotTo(HaveOccurred())
Expect(inventoryReconciler).NotTo(BeNil())
Expect(inventoryReconciler.SetupWithManager(mgr)).To(Succeed())

mgrCtx, mgrCancel := context.WithCancel(ctx)
DeferCleanup(mgrCancel)

Expand Down

0 comments on commit 75bc841

Please sign in to comment.