Skip to content

Commit

Permalink
Merge pull request #165 from Mirantis/mgmt
Browse files Browse the repository at this point in the history
Split Management reconcile into Update and Delete
  • Loading branch information
Kshatrix authored Aug 8, 2024
2 parents 6cb1380 + 7675a90 commit f330605
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (

ManagementName = "hmc"
ManagementNamespace = "hmc-system"

ManagementFinalizer = "hmc.mirantis.com/management"
)

// ManagementSpec defines the desired state of Management
Expand Down
30 changes: 27 additions & 3 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
certmanager "github.com/Mirantis/hmc/internal/certmanager"
"github.com/Mirantis/hmc/internal/certmanager"
"github.com/Mirantis/hmc/internal/helm"
)

Expand All @@ -44,18 +45,37 @@ type ManagementReconciler struct {

func (r *ManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx).WithValues("ManagementController", req.NamespacedName)
log.IntoContext(ctx, l)
l.Info("Reconciling Management")

management := &hmc.Management{}
if err := r.Get(ctx, req.NamespacedName, management); err != nil {
if apierrors.IsNotFound(err) {
l.Info("Management config not found, ignoring since object must be deleted")
l.Info("Management not found, ignoring since object must be deleted")
return ctrl.Result{}, nil
}
l.Error(err, "Failed to get Management")
return ctrl.Result{}, err
}

if !management.DeletionTimestamp.IsZero() {
l.Info("Deleting Management")
return r.Delete(ctx, management)
}

return r.Update(ctx, management)
}

func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Management) (ctrl.Result, error) {
l := log.FromContext(ctx)

finalizersUpdated := controllerutil.AddFinalizer(management, hmc.ManagementFinalizer)
if finalizersUpdated {
if err := r.Client.Update(ctx, management); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to update Management %s/%s: %w", management.Namespace, management.Name, err)
}
return ctrl.Result{}, nil
}

// TODO: this should be implemented in admission controller instead
if changed := applyDefaultCoreConfiguration(management); changed {
l.Info("Applying default core configuration")
Expand Down Expand Up @@ -123,6 +143,10 @@ func (r *ManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

func (r *ManagementReconciler) Delete(_ context.Context, _ *hmc.Management) (ctrl.Result, error) {
return ctrl.Result{}, nil
}

type component struct {
hmc.Component
// helm release dependencies
Expand Down
12 changes: 7 additions & 5 deletions internal/controller/release_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"fmt"
"time"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/build"
"github.com/Mirantis/hmc/internal/helm"
hcv2 "github.com/fluxcd/helm-controller/api/v2"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
Expand All @@ -31,6 +28,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

hmc "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/build"
"github.com/Mirantis/hmc/internal/helm"
)

const (
Expand Down Expand Up @@ -101,8 +102,9 @@ func (p *Poller) ensureManagement(ctx context.Context) error {
l := log.FromContext(ctx)
mgmtObj := &hmc.Management{
ObjectMeta: metav1.ObjectMeta{
Name: hmc.ManagementName,
Namespace: hmc.ManagementNamespace,
Name: hmc.ManagementName,
Namespace: hmc.ManagementNamespace,
Finalizers: []string{hmc.ManagementFinalizer},
},
}
err := p.Get(ctx, client.ObjectKey{
Expand Down

0 comments on commit f330605

Please sign in to comment.