Skip to content

Commit

Permalink
Use CreateOrUpdate in Server creation
Browse files Browse the repository at this point in the history
  • Loading branch information
afritzler committed Jul 4, 2024
1 parent 99c2bf5 commit 686ecf4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
11 changes: 7 additions & 4 deletions internal/controller/bmc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *BMCReconciler) reconcile(ctx context.Context, log logr.Logger, bmcObj *
}
log.V(1).Info("Updated BMC status")

if err := r.discoverServers(ctx, bmcObj); err != nil && !errors.IsNotFound(err) {
if err := r.discoverServers(ctx, log, bmcObj); err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to discover servers: %w", err)
}
log.V(1).Info("Discovered servers")
Expand Down Expand Up @@ -131,7 +131,7 @@ func (r *BMCReconciler) updateBMCStatusDetails(ctx context.Context, log logr.Log
return nil
}

func (r *BMCReconciler) discoverServers(ctx context.Context, bmcObj *metalv1alpha1.BMC) error {
func (r *BMCReconciler) discoverServers(ctx context.Context, log logr.Logger, bmcObj *metalv1alpha1.BMC) error {
bmcClient, err := GetBMCClientFromBMC(ctx, r.Client, bmcObj, r.Insecure)
if err != nil {
return fmt.Errorf("failed to create BMC client: %w", err)
Expand Down Expand Up @@ -160,9 +160,12 @@ func (r *BMCReconciler) discoverServers(ctx context.Context, bmcObj *metalv1alph
if err := controllerutil.SetControllerReference(bmcObj, server, r.Scheme); err != nil {
return fmt.Errorf("failed to set owner reference on Server: %w", err)
}
if err := r.Patch(ctx, server, client.Apply, fieldOwner); err != nil {
return fmt.Errorf("failed to apply Server: %w", err)

opResult, err := controllerutil.CreateOrPatch(ctx, r.Client, server, nil)
if err != nil {
return fmt.Errorf("failed to create or patched Server: %w", err)
}
log.V(1).Info("Created or patched Server", "Server", server.Name, "Operation", opResult)
}

return nil
Expand Down
24 changes: 14 additions & 10 deletions internal/controller/endpoint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ func (r *EndpointReconciler) reconcile(ctx context.Context, log logr.Logger, end
// TODO: ensure that BMC has the correct MACAddress

var bmcSecret *metalv1alpha1.BMCSecret
if bmcSecret, err = r.applyBMCSecret(ctx, endpoint, m); err != nil {
if bmcSecret, err = r.applyBMCSecret(ctx, log, endpoint, m); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply BMCSecret: %w", err)
}
log.V(1).Info("Applied BMC secret for endpoint")

if err := r.applyBMC(ctx, endpoint, bmcSecret, m); err != nil {
if err := r.applyBMC(ctx, log, endpoint, bmcSecret, m); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply BMC object: %w", err)
}
log.V(1).Info("Applied BMC object for endpoint")
Expand All @@ -116,12 +116,12 @@ func (r *EndpointReconciler) reconcile(ctx context.Context, log logr.Logger, end
defer bmcClient.Logout()

var bmcSecret *metalv1alpha1.BMCSecret
if bmcSecret, err = r.applyBMCSecret(ctx, endpoint, m); err != nil {
if bmcSecret, err = r.applyBMCSecret(ctx, log, endpoint, m); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply BMCSecret: %w", err)
}
log.V(1).Info("Applied local test BMC secret for endpoint")

if err := r.applyBMC(ctx, endpoint, bmcSecret, m); err != nil {
if err := r.applyBMC(ctx, log, endpoint, bmcSecret, m); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply BMC object: %w", err)
}
log.V(1).Info("Applied local test BMC object for endpoint")
Expand All @@ -142,7 +142,7 @@ func (r *EndpointReconciler) getProtocol() string {
return protocol
}

func (r *EndpointReconciler) applyBMC(ctx context.Context, endpoint *metalv1alpha1.Endpoint, secret *metalv1alpha1.BMCSecret, m macdb.MacPrefix) error {
func (r *EndpointReconciler) applyBMC(ctx context.Context, log logr.Logger, endpoint *metalv1alpha1.Endpoint, secret *metalv1alpha1.BMCSecret, m macdb.MacPrefix) error {
bmcObj := &metalv1alpha1.BMC{
TypeMeta: metav1.TypeMeta{
APIVersion: metalv1alpha1.GroupVersion.String(),
Expand Down Expand Up @@ -174,14 +174,16 @@ func (r *EndpointReconciler) applyBMC(ctx context.Context, endpoint *metalv1alph
return err
}

if err := r.Patch(ctx, bmcObj, client.Apply, fieldOwner); err != nil {
return err
opResult, err := controllerutil.CreateOrPatch(ctx, r.Client, bmcObj, nil)
if err != nil {
return fmt.Errorf("failed to create or patch BMC: %w", err)
}
log.V(1).Info("Created or patched BMC", "BMC", bmcObj.Name, "Operation", opResult)

return nil
}

func (r *EndpointReconciler) applyBMCSecret(ctx context.Context, endpoint *metalv1alpha1.Endpoint, m macdb.MacPrefix) (*metalv1alpha1.BMCSecret, error) {
func (r *EndpointReconciler) applyBMCSecret(ctx context.Context, log logr.Logger, endpoint *metalv1alpha1.Endpoint, m macdb.MacPrefix) (*metalv1alpha1.BMCSecret, error) {
bmcSecret := &metalv1alpha1.BMCSecret{
TypeMeta: metav1.TypeMeta{
APIVersion: metalv1alpha1.GroupVersion.String(),
Expand All @@ -200,9 +202,11 @@ func (r *EndpointReconciler) applyBMCSecret(ctx context.Context, endpoint *metal
return nil, err
}

if err := r.Patch(ctx, bmcSecret, client.Apply, fieldOwner); err != nil {
return nil, err
opResult, err := controllerutil.CreateOrPatch(ctx, r.Client, bmcSecret, nil)
if err != nil {
return nil, fmt.Errorf("failed to create or patch BMCSecret: %w", err)
}
log.V(1).Info("Created or patched BMSecret", "BMCSecret", bmcSecret.Name, "Operation", opResult)

return bmcSecret, nil
}
Expand Down
4 changes: 0 additions & 4 deletions internal/controller/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
fieldOwner = client.FieldOwner("metal.ironcore.dev/controller-manager")
)

func shouldIgnoreReconciliation(obj client.Object) bool {
val, found := obj.GetAnnotations()[metalv1alpha1.OperationAnnotation]
if !found {
Expand Down
25 changes: 12 additions & 13 deletions internal/controller/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (r *ServerReconciler) ensureServerStateTransition(ctx context.Context, log
switch server.Status.State {
case metalv1alpha1.ServerStateInitial:
// apply boot configuration
if err := r.applyBootConfigurationAndIgnitionForDiscovery(ctx, server); err != nil {
if err := r.applyBootConfigurationAndIgnitionForDiscovery(ctx, log, server); err != nil {
return false, fmt.Errorf("failed to apply server boot configuration: %w", err)
}
log.V(1).Info("Applied Server boot configuration")
Expand Down Expand Up @@ -315,7 +315,7 @@ func (r *ServerReconciler) updateServerStatus(ctx context.Context, log logr.Logg
return nil
}

func (r *ServerReconciler) applyBootConfigurationAndIgnitionForDiscovery(ctx context.Context, server *metalv1alpha1.Server) error {
func (r *ServerReconciler) applyBootConfigurationAndIgnitionForDiscovery(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server) error {
// apply server boot configuration
bootConfig := &metalv1alpha1.ServerBootConfiguration{
TypeMeta: metav1.TypeMeta{
Expand All @@ -340,27 +340,24 @@ func (r *ServerReconciler) applyBootConfigurationAndIgnitionForDiscovery(ctx con
},
}

if err := r.Patch(ctx, bootConfig, client.Apply, fieldOwner, client.ForceOwnership); err != nil {
return err
opResult, err := controllerutil.CreateOrPatch(ctx, r.Client, bootConfig, nil)
if err != nil {
return fmt.Errorf("failed to create or patch ServerBootConfiguration: %w", err)
}
log.V(1).Info("Created or patched", "ServerBootConfiguration", bootConfig.Name, "Operation", opResult)

if err := r.ensureServerBootConfigRef(ctx, server, bootConfig); err != nil {
return err
}

if err := r.applyDefaultIgnitionForServer(ctx, server, bootConfig, r.RegistryURL); err != nil {
if err := r.applyDefaultIgnitionForServer(ctx, log, server, bootConfig, r.RegistryURL); err != nil {
return err
}

return nil
}

func (r *ServerReconciler) applyDefaultIgnitionForServer(
ctx context.Context,
server *metalv1alpha1.Server,
bootConfig *metalv1alpha1.ServerBootConfiguration,
registryURL string,
) error {
func (r *ServerReconciler) applyDefaultIgnitionForServer(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server, bootConfig *metalv1alpha1.ServerBootConfiguration, registryURL string) error {
probeFlags := fmt.Sprintf("--registry-url=%s --server-uuid=%s", registryURL, server.Spec.UUID)
ignitionData, err := r.generateDefaultIgnitionDataForServer(probeFlags)
if err != nil {
Expand All @@ -385,9 +382,11 @@ func (r *ServerReconciler) applyDefaultIgnitionForServer(
return fmt.Errorf("failed to set controller reference for default ignitionSecret: %w", err)
}

if err := r.Patch(ctx, ignitionSecret, client.Apply, fieldOwner, client.ForceOwnership); err != nil {
return fmt.Errorf("failed to apply default ignitionSecret: %w", err)
opResult, err := controllerutil.CreateOrPatch(ctx, r.Client, ignitionSecret, nil)
if err != nil {
return fmt.Errorf("failed to create or patch Ignition Secret: %w", err)
}
log.V(1).Info("Created or patched Ignition Secret", "Secret", ignitionSecret.Name, "Operation", opResult)

return nil
}
Expand Down
12 changes: 8 additions & 4 deletions internal/controller/serverclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/go-logr/logr"
"github.com/ironcore-dev/controller-utils/clientutils"
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
Expand Down Expand Up @@ -149,7 +151,7 @@ func (r *ServerClaimReconciler) reconcile(ctx context.Context, log logr.Logger,
return ctrl.Result{}, nil
}

if err := r.applyBootConfiguration(ctx, server, claim); err != nil {
if err := r.applyBootConfiguration(ctx, log, server, claim); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply boot configuration: %w", err)
}

Expand Down Expand Up @@ -186,7 +188,7 @@ func (r *ServerClaimReconciler) patchServerClaimPhase(ctx context.Context, claim
return true, nil
}

func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, server *metalv1alpha1.Server, claim *metalv1alpha1.ServerClaim) error {
func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server, claim *metalv1alpha1.ServerClaim) error {
config := &metalv1alpha1.ServerBootConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "metal.ironcore.dev/v1alpha1",
Expand All @@ -208,9 +210,11 @@ func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, serv
}

// TODO: we might want to add a finalizer on the ignition secret
if err := r.Patch(ctx, config, client.Apply, fieldOwner, client.ForceOwnership); err != nil {
return fmt.Errorf("failed to apply boot configuration: %w", err)
opResult, err := controllerutil.CreateOrPatch(ctx, r.Client, config, nil)
if err != nil {
return fmt.Errorf("failed to create or patch ServerBootConfiguration: %w", err)
}
log.V(1).Info("Created or patched ServerBootConfiguration", "ServerBootConfiguration", config.Name, "Operation", opResult)

serverBase := server.DeepCopy()
server.Spec.BootConfigurationRef = &v1.ObjectReference{
Expand Down

0 comments on commit 686ecf4

Please sign in to comment.