Skip to content

Commit

Permalink
Cleanup code and use boundless-system namespace for addon CRDs BOP-14…
Browse files Browse the repository at this point in the history
…/BOP-40
  • Loading branch information
tppolkow committed Nov 2, 2023
1 parent e438d61 commit 7e6af6c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
8 changes: 1 addition & 7 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
resources:
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: ghcr.io/mirantis/boundless-operator
newTag: tpt
- manager.yaml
8 changes: 3 additions & 5 deletions controllers/addon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"context"
"github.com/mirantis/boundless-operator/pkg/helm"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -12,6 +11,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

boundlessv1alpha1 "github.com/mirantis/boundless-operator/api/v1alpha1"
"github.com/mirantis/boundless-operator/pkg/helm"
)

// AddonReconciler reconciles a Addon object
Expand Down Expand Up @@ -44,7 +44,6 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
err = r.Get(ctx, req.NamespacedName, instance)
if err != nil {
msg := "failed to get MkeAddon instance"
logger.Info("err is ", "err", err)
if errors.IsNotFound(err) {
// Ignore request.
logger.Info(msg, "Name", req.Name, "Requeue", false)
Expand Down Expand Up @@ -80,7 +79,7 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// The object is being deleted
if controllerutil.ContainsFinalizer(instance, addonFinalizerName) {
// our finalizer is present, so lets delete the helm chart
if err := hc.DeleteHelmChart(chart, req.Namespace); err != nil {
if err := hc.DeleteHelmChart(chart, instance.Spec.Namespace); err != nil {
// if fail to delete the helm chart here, return with error
// so that it can be retried
return ctrl.Result{}, err
Expand All @@ -98,8 +97,7 @@ func (r *AddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}

logger.Info("Creating Addon HelmChart resource", "Name", chart.Name, "Version", chart.Version)
logger.Info("req Namespace", "req.Namespace", req.Namespace)
if err2 := hc.CreateHelmChart(chart, req.Namespace); err2 != nil {
if err2 := hc.CreateHelmChart(chart, instance.Spec.Namespace); err2 != nil {
logger.Error(err, "failed to install addon", "Name", chart.Name, "Version", chart.Version)
return ctrl.Result{Requeue: true}, err2
}
Expand Down
31 changes: 13 additions & 18 deletions controllers/blueprint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
boundlessv1alpha1 "github.com/mirantis/boundless-operator/api/v1alpha1"
)

const boundlessSystemNamespace = "boundless-system"

// BlueprintReconciler reconciles a Blueprint object
type BlueprintReconciler struct {
client.Client
Expand Down Expand Up @@ -73,9 +75,7 @@ func (r *BlueprintReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

for _, addon := range instance.Spec.Components.Addons {
logger.Info("namespaces", "Addon namespace", addon.Namespace, "instance namespace", instance.Namespace)
if addon.Namespace == "" {
logger.Info("settingz addon namespace ot instance namespace")
addon.Namespace = instance.Namespace
}

Expand Down Expand Up @@ -128,14 +128,14 @@ func (r *BlueprintReconciler) deleteAddons(ctx context.Context, logger logr.Logg
return nil
}

func (r *BlueprintReconciler) createOrUpdateAddon(ctx context.Context, logger logr.Logger, obj client.Object) error {
err := r.createNamespaceIfNotExist(ctx, logger, obj.GetNamespace())
func (r *BlueprintReconciler) createOrUpdateAddon(ctx context.Context, logger logr.Logger, addon *boundlessv1alpha1.Addon) error {
err := r.createNamespaceIfNotExist(ctx, logger, addon.Spec.Namespace)
if err != nil {
return err
}

existing := &boundlessv1alpha1.Addon{}
err = r.Get(ctx, client.ObjectKey{Name: obj.GetName(), Namespace: obj.GetNamespace()}, existing)
err = r.Get(ctx, client.ObjectKey{Name: addon.GetName(), Namespace: addon.GetNamespace()}, existing)
if err != nil {
if client.IgnoreNotFound(err) != nil {
return err
Expand All @@ -144,30 +144,25 @@ func (r *BlueprintReconciler) createOrUpdateAddon(ctx context.Context, logger lo

if existing.Name != "" {
logger.Info("Add-on already exists. Updating", "Name", existing.Name)
obj.SetResourceVersion(existing.GetResourceVersion())
err = r.Update(ctx, obj)
addon.SetResourceVersion(existing.GetResourceVersion())
err = r.Update(ctx, addon)
if err != nil {
return fmt.Errorf("failed to update add-on %s: %w", existing.Name, err)
}
return nil
}

logger.Info("Creating add-on", "Name", obj.GetName())
err = r.Create(ctx, obj)
logger.Info("Creating add-on", "Name", addon.GetName())
err = r.Create(ctx, addon)
if err != nil {
return fmt.Errorf("failed to create add-on %s: %w", obj.GetName(), err)
return fmt.Errorf("failed to create add-on %s: %w", addon.GetName(), err)
}
return nil
}

func (r *BlueprintReconciler) createOrUpdateIngress(ctx context.Context, logger logr.Logger, obj client.Object) error {
err := r.createNamespaceIfNotExist(ctx, logger, obj.GetNamespace())
if err != nil {
return err
}

existing := &boundlessv1alpha1.Ingress{}
err = r.Get(ctx, client.ObjectKey{Name: obj.GetName(), Namespace: obj.GetNamespace()}, existing)
err := r.Get(ctx, client.ObjectKey{Name: obj.GetName(), Namespace: obj.GetNamespace()}, existing)
if err != nil {
if client.IgnoreNotFound(err) != nil {
return err
Expand Down Expand Up @@ -197,7 +192,7 @@ func ingressResource(spec *boundlessv1alpha1.IngressSpec, namespace string) *bou
return &boundlessv1alpha1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Namespace: boundlessSystemNamespace,
},
Spec: boundlessv1alpha1.IngressSpec{
Enabled: spec.Enabled,
Expand All @@ -213,7 +208,7 @@ func addonResource(spec *boundlessv1alpha1.AddonSpec) *boundlessv1alpha1.Addon {
return &boundlessv1alpha1.Addon{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: spec.Namespace,
Namespace: boundlessSystemNamespace,
},
Spec: boundlessv1alpha1.AddonSpec{
Name: spec.Name,
Expand Down
2 changes: 1 addition & 1 deletion controllers/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

hc := helm.NewHelmChartController(r.Client, logger)
logger.Info("Creating HelmChart resource", "Name", chart.Name, "Version", chart.Version)
if err2 := hc.CreateHelmChart(chart, req.Namespace); err2 != nil {
if err2 := hc.CreateHelmChart(chart, instance.Namespace); err2 != nil {
logger.Error(err, "failed to install ingress controller", "Controller Type", instance.Spec.Provider, "Version", "v1alpha1")
return ctrl.Result{Requeue: true}, err2
}
Expand Down

0 comments on commit 7e6af6c

Please sign in to comment.