Skip to content

Commit

Permalink
Apply Management defaults by the admission controller
Browse files Browse the repository at this point in the history
Still keeping the same code in the management controller
to handle the case when the admission is not yet started

Closes #140
  • Loading branch information
eromanova committed Aug 13, 2024
1 parent ddc7e55 commit a8296f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 8 additions & 0 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func (in *Component) HelmReleaseName() string {
return in.Template
}

func (m *ManagementSpec) SetDefaults() bool {
if m.Core != nil {
return false
}
m.Core = &DefaultCoreConfiguration
return true
}

func (m *ManagementSpec) SetProvidersDefaults() {
m.Providers = []Component{
{
Expand Down
14 changes: 3 additions & 11 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
return ctrl.Result{}, nil
}

// TODO: this should be implemented in admission controller instead
if changed := applyDefaultCoreConfiguration(management); changed {
// TODO: this is also implemented in admission but we have to keep it in controller as well
// to set defaults before the admission is started
if changed := management.Spec.SetDefaults(); changed {
l.Info("Applying default core configuration")
return ctrl.Result{}, r.Client.Update(ctx, management)
}
Expand Down Expand Up @@ -261,15 +262,6 @@ func (r *ManagementReconciler) enableAdmissionWebhook(ctx context.Context, mgmt
return nil
}

func applyDefaultCoreConfiguration(mgmt *hmc.Management) (changed bool) {
if mgmt.Spec.Core != nil {
// Only apply defaults when there's no configuration provided
return false
}
mgmt.Spec.Core = &hmc.DefaultCoreConfiguration
return true
}

func updateComponentsStatus(
components map[string]hmc.ComponentStatus,
providers *hmc.Providers,
Expand Down
9 changes: 8 additions & 1 deletion internal/webhook/management_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package webhook // nolint:dupl
import (
"context"
"errors"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -73,6 +75,11 @@ func (v *ManagementValidator) ValidateDelete(ctx context.Context, _ runtime.Obje
}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (*ManagementValidator) Default(_ context.Context, _ runtime.Object) error {
func (*ManagementValidator) Default(_ context.Context, obj runtime.Object) error {
management, ok := obj.(*v1alpha1.Management)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected Management but got a %T", obj))
}
management.Spec.SetDefaults()
return nil
}

0 comments on commit a8296f4

Please sign in to comment.