Skip to content

Commit

Permalink
Make Management object cluster-scoped
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Pavlov <[email protected]>
  • Loading branch information
Kshatrix committed Sep 6, 2024
1 parent d0fcb89 commit d247590
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 30 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ apiVersion: hmc.mirantis.com/v1alpha1
kind: Management
metadata:
name: hmc
namespace: hmc-system
spec:
core:
capi:
Expand All @@ -76,7 +75,7 @@ There are two options to override the default management configuration of HMC:
1. Update the `Management` object after the HMC installation using `kubectl`:

`kubectl --kubeconfig <path-to-management-kubeconfig> -n hmc-system edit management`
`kubectl --kubeconfig <path-to-management-kubeconfig> edit management`

2. Deploy HMC skipping the default `Management` object creation and provide your own `Management`
configuration:
Expand All @@ -90,9 +89,9 @@ configuration:

`--set="controller.createManagement=false"`

* Create `hmc-system/hmc` `Management` object after HMC installation:
* Create `hmc` `Management` object after HMC installation:

`kubectl --kubeconfig <path-to-management-kubeconfig> -n hmc-system create -f management.yaml`
`kubectl --kubeconfig <path-to-management-kubeconfig> create -f management.yaml`

## Deploy a managed cluster

Expand Down Expand Up @@ -243,7 +242,7 @@ spec:

1. Remove the Management object:

`kubectl delete management.hmc hmc -n hmc-system`
`kubectl delete management.hmc hmc`

> Note: make sure you have no HMC ManagedCluster objects left in the cluster prior to Management deletion

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/management_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type ComponentStatus struct {

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// +kubebuilder:resource:shortName=hmc-mgmt;mgmt
// +kubebuilder:resource:shortName=hmc-mgmt;mgmt,scope=Cluster

// Management is the Schema for the managements API
type Management struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *ManagedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque

if managedCluster.Status.ObservedGeneration == 0 {
mgmt := &hmc.Management{}
mgmtRef := types.NamespacedName{Namespace: hmc.ManagementNamespace, Name: hmc.ManagementName}
mgmtRef := types.NamespacedName{Name: hmc.ManagementName}
if err := r.Get(ctx, mgmtRef, mgmt); err != nil {
l.Error(err, "Failed to get Management object")
return ctrl.Result{}, err
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ var _ = Describe("ManagedCluster Controller", func() {
if err != nil && errors.IsNotFound(err) {
management = &hmc.Management{
ObjectMeta: metav1.ObjectMeta{
Name: hmc.ManagementName,
Namespace: hmc.ManagementNamespace,
Name: hmc.ManagementName,
},
Spec: hmc.ManagementSpec{},
}
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
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{}, fmt.Errorf("failed to update Management %s: %w", management.Name, err)
}
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -117,10 +117,10 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
continue
}

_, _, err = helm.ReconcileHelmRelease(ctx, r.Client, component.HelmReleaseName(), management.Namespace, component.Config,
_, _, err = helm.ReconcileHelmRelease(ctx, r.Client, component.HelmReleaseName(), hmc.ManagementNamespace, component.Config,
nil, template.Status.ChartRef, defaultReconcileInterval, component.dependsOn)
if err != nil {
errMsg := fmt.Sprintf("error reconciling HelmRelease %s/%s: %s", management.Namespace, component.Template, err)
errMsg := fmt.Sprintf("error reconciling HelmRelease %s/%s: %s", hmc.ManagementNamespace, component.Template, err)
updateComponentsStatus(detectedComponents, &detectedProviders, component.Template, template.Status, errMsg)
errs = errors.Join(errs, errors.New(errMsg))
continue
Expand All @@ -132,8 +132,8 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
management.Status.AvailableProviders = detectedProviders
management.Status.Components = detectedComponents
if err := r.Status().Update(ctx, management); err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to update status for Management %s/%s: %w",
management.Namespace, management.Name, err))
errs = errors.Join(errs, fmt.Errorf("failed to update status for Management %s: %w",
management.Name, err))
}
if errs != nil {
l.Error(errs, "Multiple errors during Management reconciliation")
Expand Down
4 changes: 1 addition & 3 deletions internal/controller/release_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ func (p *Poller) ensureManagement(ctx context.Context) error {
mgmtObj := &hmc.Management{
ObjectMeta: metav1.ObjectMeta{
Name: hmc.ManagementName,
Namespace: hmc.ManagementNamespace,
Finalizers: []string{hmc.ManagementFinalizer},
},
}
err := p.Get(ctx, client.ObjectKey{
Name: hmc.ManagementName,
Namespace: hmc.ManagementNamespace,
Name: hmc.ManagementName,
}, mgmtObj)
if err != nil {
if !apierrors.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion internal/telemetry/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (t *Tracker) Tick(ctx context.Context) {

func (t *Tracker) trackManagedClusterHeartbeat(ctx context.Context) error {
mgmt := &v1alpha1.Management{}
mgmtRef := types.NamespacedName{Namespace: v1alpha1.ManagementNamespace, Name: v1alpha1.ManagementName}
mgmtRef := types.NamespacedName{Name: v1alpha1.ManagementName}
err := t.Get(ctx, mgmtRef, mgmt)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/webhook/managedcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (v *ManagedClusterValidator) isTemplateValid(ctx context.Context, template
func (v *ManagedClusterValidator) verifyProviders(ctx context.Context, template *v1alpha1.Template) error {
requiredProviders := template.Status.Providers
management := &v1alpha1.Management{}
managementRef := types.NamespacedName{Name: v1alpha1.ManagementName, Namespace: v1alpha1.ManagementNamespace}
managementRef := types.NamespacedName{Name: v1alpha1.ManagementName}
if err := v.Get(ctx, managementRef, management); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
- hmc-mgmt
- mgmt
singular: management
scope: Namespaced
scope: Cluster
versions:
- name: v1alpha1
schema:
Expand Down
12 changes: 2 additions & 10 deletions test/objects/management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ import (
)

const (
DefaultName = "hmc"
DefaultNamespace = "hmc-system"
DefaultName = "hmc"
)

type Opt func(management *v1alpha1.Management)

func NewManagement(opts ...Opt) *v1alpha1.Management {
p := &v1alpha1.Management{
ObjectMeta: metav1.ObjectMeta{
Name: DefaultName,
Namespace: DefaultNamespace,
Name: DefaultName,
},
}

Expand All @@ -47,12 +45,6 @@ func WithName(name string) Opt {
}
}

func WithNamespace(namespace string) Opt {
return func(p *v1alpha1.Management) {
p.Namespace = namespace
}
}

func WithCoreComponents(core *v1alpha1.Core) Opt {
return func(p *v1alpha1.Management) {
p.Spec.Core = core
Expand Down

0 comments on commit d247590

Please sign in to comment.