Skip to content

Commit

Permalink
Improve BMCReconciler status patching
Browse files Browse the repository at this point in the history
  • Loading branch information
afritzler committed Apr 30, 2024
1 parent 310aa50 commit 3d171fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
25 changes: 14 additions & 11 deletions internal/controller/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ import (
)

const (
DefaultIgnitionSecretKeyName = "ignition"
ServerFinalizer = "metal.ironcore.dev/server"
DefaultIgnitionSecretKeyName = "ignition"
ServerFinalizer = "metal.ironcore.dev/server"
InternalAnnotationTypeKeyName = "metal.ironcore.dev/type"
InternalAnnotationTypeValue = "Internal"
)

const (
Expand Down Expand Up @@ -325,6 +327,9 @@ func (r *ServerReconciler) applyBootConfigurationAndIgnitionForDiscovery(ctx con
ObjectMeta: metav1.ObjectMeta{
Name: server.Name,
Namespace: r.ManagerNamespace,
Annotations: map[string]string{
InternalAnnotationTypeKeyName: InternalAnnotationTypeValue,
},
},
Spec: metalv1alpha1.ServerBootConfigurationSpec{
ServerRef: v1.LocalObjectReference{
Expand Down Expand Up @@ -538,19 +543,17 @@ func (r *ServerReconciler) ensureInitialBootConfigurationIsDeleted(ctx context.C
return nil
}

if server.Spec.BootConfigurationRef.Namespace != r.ManagerNamespace && server.Spec.BootConfigurationRef.Name != server.Name {
// hit a non initial boot config
return nil
config := &metalv1alpha1.ServerBootConfiguration{}
if err := r.Get(ctx, client.ObjectKey{Namespace: server.Spec.BootConfigurationRef.Namespace, Name: server.Spec.BootConfigurationRef.Name}, config); err != nil {
return err
}

config := &metalv1alpha1.ServerBootConfiguration{
ObjectMeta: metav1.ObjectMeta{
Namespace: server.Spec.BootConfigurationRef.Namespace,
Name: server.Spec.BootConfigurationRef.Name,
},
if val, ok := config.Annotations[InternalAnnotationTypeKeyName]; !ok || val != InternalAnnotationTypeValue {
// hit a non-initial boot config
return nil
}

if err := r.Delete(ctx, config); !apierrors.IsNotFound(err) {
if err := r.Delete(ctx, config); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion internal/controller/server_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
)

var _ = FDescribe("Server Controller", func() {
var _ = Describe("Server Controller", func() {
ns := SetupTest()

var endpoint *metalv1alpha1.Endpoint
Expand Down Expand Up @@ -75,6 +75,7 @@ var _ = FDescribe("Server Controller", func() {
},
}
Eventually(Object(bootConfig)).Should(SatisfyAll(
HaveField("Annotations", HaveKeyWithValue(InternalAnnotationTypeKeyName, InternalAnnotationTypeValue)),
HaveField("Spec.ServerRef", v1.LocalObjectReference{Name: server.Name}),
HaveField("Spec.Image", "fooOS:latest"),
HaveField("Spec.IgnitionSecretRef", &v1.LocalObjectReference{Name: server.Name}),
Expand Down
16 changes: 14 additions & 2 deletions internal/controller/serverclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (r *ServerClaimReconciler) reconcile(ctx context.Context, log logr.Logger,
return ctrl.Result{}, err
}

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

Expand All @@ -177,7 +177,7 @@ func (r *ServerClaimReconciler) patchServerClaimPhase(ctx context.Context, claim
return true, nil
}

func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, claim *metalv1alpha1.ServerClaim) error {
func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, server *metalv1alpha1.Server, claim *metalv1alpha1.ServerClaim) error {
config := &metalv1alpha1.ServerBootConfiguration{
TypeMeta: metav1.TypeMeta{
APIVersion: "metal.ironcore.dev/v1alpha1",
Expand All @@ -203,6 +203,18 @@ func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, clai
return fmt.Errorf("failed to apply boot configuration: %w", err)
}

serverBase := server.DeepCopy()
server.Spec.BootConfigurationRef = &v1.ObjectReference{
Namespace: config.Namespace,
Name: config.Name,
UID: config.UID,
APIVersion: "metal.ironcore.dev/v1alpha1",
Kind: "ServerBootConfiguration",
}
if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 3d171fd

Please sign in to comment.