Skip to content

Commit

Permalink
Add field indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gchbg committed Apr 2, 2024
1 parent 695c049 commit 6156d9f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 34 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/oob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ const (
type OOBState string

const (
OOBStateReady OOBState = "Ready"
OOBStateError OOBState = "Error"
OOBStateReady OOBState = "Ready"
OOBStateUnready OOBState = "Unready"
OOBStateError OOBState = "Error"
)

//+kubebuilder:object:root=true
Expand Down
47 changes: 43 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,23 @@ func main() {
return
}

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

if p.enableMachineController {
err = controller.NewMachineReconciler().SetupWithManager(mgr)
var machineReconciler *controller.MachineReconciler
machineReconciler, err = controller.NewMachineReconciler()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "Machine")
exitCode = 1
return
}

err = machineReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "Machine")
exitCode = 1
Expand All @@ -194,7 +209,15 @@ func main() {
}

if p.enableMachineClaimController {
err = controller.NewMachineClaimReconciler().SetupWithManager(mgr)
var machineClaimReconciler *controller.MachineClaimReconciler
machineClaimReconciler, err = controller.NewMachineClaimReconciler()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "MachineClaim")
exitCode = 1
return
}

err = machineClaimReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "MachineClaim")
exitCode = 1
Expand All @@ -203,7 +226,15 @@ func main() {
}

if p.enableOOBController {
err = controller.NewOOBReconciler().SetupWithManager(mgr)
var oobReconciler *controller.OOBReconciler
oobReconciler, err = controller.NewOOBReconciler()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "OOB")
exitCode = 1
return
}

err = oobReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "OOB")
exitCode = 1
Expand All @@ -212,7 +243,15 @@ func main() {
}

if p.enableOOBSecretController {
err = controller.NewOOBSecretReconciler().SetupWithManager(mgr)
var oobSecretReconciler *controller.OOBSecretReconciler
oobSecretReconciler, err = controller.NewOOBSecretReconciler()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "OOBSecret")
exitCode = 1
return
}

err = oobSecretReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "OOBSecret")
exitCode = 1
Expand Down
31 changes: 31 additions & 0 deletions internal/controller/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package controller

import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

metalv1alpha1 "github.com/ironcore-dev/metal/api/v1alpha1"
)

func CreateIndexes(ctx context.Context, mgr manager.Manager) error {
indexer := mgr.GetFieldIndexer()

err := indexer.IndexField(ctx, &metalv1alpha1.MachineClaim{}, MachineClaimSpecMachineRef, func(obj client.Object) []string {
claim := obj.(*metalv1alpha1.MachineClaim)
if claim.Spec.MachineRef == nil || claim.Spec.MachineRef.Name == "" {
return nil
}
return []string{claim.Spec.MachineRef.Name}
})
if err != nil {
return fmt.Errorf("cannot index field %s: %w", MachineClaimSpecMachineRef, err)
}

return nil
}
6 changes: 4 additions & 2 deletions internal/controller/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=machines/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=machines/finalizers,verbs=update

func NewMachineReconciler() *MachineReconciler {
return &MachineReconciler{}
func NewMachineReconciler() (*MachineReconciler, error) {
return &MachineReconciler{}, nil
}

// MachineReconciler reconciles a Machine object
Expand All @@ -36,6 +36,8 @@ func (r *MachineReconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl

// SetupWithManager sets up the controller with the Manager.
func (r *MachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Client = mgr.GetClient()

return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.Machine{}).
Complete(r)
Expand Down
33 changes: 16 additions & 17 deletions internal/controller/machineclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import (
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=machines/finalizers,verbs=update

const (
MachineClaimFieldOwner string = "metal.ironcore.dev/machineclaim"
MachineClaimFinalizer string = "metal.ironcore.dev/machineclaim"
MachineClaimFieldOwner string = "metal.ironcore.dev/machineclaim"
MachineClaimFinalizer string = "metal.ironcore.dev/machineclaim"
MachineClaimSpecMachineRef string = ".spec.machineRef.Name"
)

func NewMachineClaimReconciler() *MachineClaimReconciler {
return &MachineClaimReconciler{}
func NewMachineClaimReconciler() (*MachineClaimReconciler, error) {
return &MachineClaimReconciler{}, nil
}

// MachineClaimReconciler reconciles a MachineClaim object
Expand Down Expand Up @@ -145,12 +146,13 @@ func (r *MachineClaimReconciler) reconcile(ctx context.Context, claim *metalv1al
machine := &m
ctx = log.WithValues(ctx, "machine", machine.Name)

machineApply := metalv1alpha1apply.Machine(machine.Name, machine.Namespace).WithFinalizers(MachineClaimFinalizer).
WithSpec(metalv1alpha1apply.MachineSpec().WithMachineClaimRef(v1.ObjectReference{
machineApply := metalv1alpha1apply.Machine(machine.Name, machine.Namespace).WithFinalizers(MachineClaimFinalizer).WithSpec(metalv1alpha1apply.MachineSpec().
WithMachineClaimRef(v1.ObjectReference{
Namespace: claim.Namespace,
Name: claim.Name,
UID: claim.UID,
}).WithPower(claim.Spec.Power))
}).
WithPower(claim.Spec.Power))
if !controllerutil.ContainsFinalizer(machine, MachineClaimFinalizer) ||
!internal.NilOrEqual(machine.Spec.MachineClaimRef, machineApply.Spec.MachineClaimRef) ||
machine.Spec.Power != *machineApply.Spec.Power {
Expand Down Expand Up @@ -192,7 +194,8 @@ func (r *MachineClaimReconciler) reconcile(ctx context.Context, claim *metalv1al

// SetupWithManager sets up the controller with the Manager.
func (r *MachineClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
// TODO: Make an index for claim.spec.machineref.
r.Client = mgr.GetClient()

return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.MachineClaim{}).
Watches(&metalv1alpha1.Machine{}, r.enqueueMachineClaimsFromMachine()).
Expand All @@ -203,9 +206,8 @@ func (r *MachineClaimReconciler) enqueueMachineClaimsFromMachine() handler.Event
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
machine := obj.(*metalv1alpha1.Machine)

// TODO: Filter this list with a field selector.
claimList := &metalv1alpha1.MachineClaimList{}
if err := r.List(ctx, claimList); err != nil {
if err := r.List(ctx, claimList, client.MatchingFields{MachineClaimSpecMachineRef: machine.Name}); err != nil {
log.Error(ctx, fmt.Errorf("cannot list MachineClaims: %w", err))
return nil
}
Expand All @@ -217,13 +219,10 @@ func (r *MachineClaimReconciler) enqueueMachineClaimsFromMachine() handler.Event
}

// TODO: Also watch for machines matching the label selector.
ref := c.Spec.MachineRef
if ref != nil && ref.Name == machine.Name {
req = append(req, reconcile.Request{NamespacedName: types.NamespacedName{
Namespace: c.Namespace,
Name: c.Name,
}})
}
req = append(req, reconcile.Request{NamespacedName: types.NamespacedName{
Namespace: c.Namespace,
Name: c.Name,
}})
}
return req
})
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/oob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=oobs/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=oobs/finalizers,verbs=update

func NewOOBReconciler() *OOBReconciler {
return &OOBReconciler{}
func NewOOBReconciler() (*OOBReconciler, error) {
return &OOBReconciler{}, nil
}

// OOBReconciler reconciles a OOB object
Expand All @@ -36,6 +36,8 @@ func (r *OOBReconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Res

// SetupWithManager sets up the controller with the Manager.
func (r *OOBReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Client = mgr.GetClient()

return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.OOB{}).
Complete(r)
Expand Down
6 changes: 4 additions & 2 deletions internal/controller/oobsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=oobsecrets/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=metal.ironcore.dev,resources=oobsecrets/finalizers,verbs=update

func NewOOBSecretReconciler() *OOBSecretReconciler {
return &OOBSecretReconciler{}
func NewOOBSecretReconciler() (*OOBSecretReconciler, error) {
return &OOBSecretReconciler{}, nil
}

// OOBSecretReconciler reconciles a OOBSecret object
Expand All @@ -36,6 +36,8 @@ func (r *OOBSecretReconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *OOBSecretReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Client = mgr.GetClient()

return ctrl.NewControllerManagedBy(mgr).
For(&metalv1alpha1.OOBSecret{}).
Complete(r)
Expand Down
31 changes: 26 additions & 5 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ var _ = BeforeSuite(func() {
Expect(k8sClient).NotTo(BeNil())
SetClient(k8sClient)

ctx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)

var mgr manager.Manager
mgr, err = ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Expand All @@ -72,13 +75,31 @@ var _ = BeforeSuite(func() {
})
Expect(err).NotTo(HaveOccurred())
Expect(mgr).NotTo(BeNil())
Expect(CreateIndexes(ctx, mgr)).To(Succeed())

Expect((&MachineClaimReconciler{
Client: mgr.GetClient(),
}).SetupWithManager(mgr)).To(Succeed())
var machineReconciler *MachineReconciler
machineReconciler, err = NewMachineReconciler()
Expect(err).NotTo(HaveOccurred())
Expect(machineReconciler).NotTo(BeNil())
Expect(machineReconciler.SetupWithManager(mgr)).To(Succeed())

ctx, cancel := context.WithCancel(context.Background())
DeferCleanup(cancel)
var machineClaimReconciler *MachineClaimReconciler
machineClaimReconciler, err = NewMachineClaimReconciler()
Expect(err).NotTo(HaveOccurred())
Expect(machineClaimReconciler).NotTo(BeNil())
Expect(machineClaimReconciler.SetupWithManager(mgr)).To(Succeed())

var oobReconciler *OOBReconciler
oobReconciler, err = NewOOBReconciler()
Expect(err).NotTo(HaveOccurred())
Expect(oobReconciler).NotTo(BeNil())
Expect(oobReconciler.SetupWithManager(mgr)).To(Succeed())

var oobSecretReconciler *OOBSecretReconciler
oobSecretReconciler, err = NewOOBSecretReconciler()
Expect(err).NotTo(HaveOccurred())
Expect(oobSecretReconciler).NotTo(BeNil())
Expect(oobSecretReconciler.SetupWithManager(mgr)).To(Succeed())

go func() {
defer GinkgoRecover()
Expand Down

0 comments on commit 6156d9f

Please sign in to comment.