Skip to content

Commit

Permalink
Merge pull request K0rdent#410 from Mirantis/release-controller
Browse files Browse the repository at this point in the history
Preload Templates associated with Release objects
  • Loading branch information
Kshatrix authored Oct 3, 2024
2 parents 7dca223 + 7694a19 commit 13138bd
Show file tree
Hide file tree
Showing 22 changed files with 292 additions and 165 deletions.
31 changes: 16 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"

hmcmirantiscomv1alpha1 "github.com/Mirantis/hmc/api/v1alpha1"
"github.com/Mirantis/hmc/internal/controller"
"github.com/Mirantis/hmc/internal/telemetry"
"github.com/Mirantis/hmc/internal/utils"
hmcwebhook "github.com/Mirantis/hmc/internal/webhook"
sveltosv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
// +kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -218,10 +219,11 @@ func main() {
os.Exit(1)
}
if err = (&controller.ManagementReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
SystemNamespace: currentNamespace,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
SystemNamespace: currentNamespace,
CreateTemplateManagement: createTemplateManagement,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Management")
os.Exit(1)
Expand Down Expand Up @@ -252,16 +254,15 @@ func main() {
os.Exit(1)
}

if err = mgr.Add(&controller.Poller{
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
CreateManagement: createManagement,
CreateTemplateManagement: createTemplateManagement,
CreateTemplates: createTemplates,
HMCTemplatesChartName: hmcTemplatesChartName,
SystemNamespace: currentNamespace,
}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ReleaseController")
if err = (&controller.ReleaseReconciler{
Client: mgr.GetClient(),
Config: mgr.GetConfig(),
CreateManagement: createManagement,
CreateTemplates: createTemplates,
HMCTemplatesChartName: hmcTemplatesChartName,
SystemNamespace: currentNamespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Release")
os.Exit(1)
}

Expand Down
2 changes: 2 additions & 0 deletions hack/templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ EOF
cat <<EOF >> $TEMPLATES_OUTPUT_DIR/$name.yaml
metadata:
name: $name
annotations:
helm.sh/resource-policy: keep
spec:
helm:
chartName: $name
Expand Down
52 changes: 48 additions & 4 deletions internal/controller/management_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
sourcev1 "github.com/fluxcd/source-controller/api/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
Expand All @@ -41,9 +42,10 @@ import (
// ManagementReconciler reconciles a Management object
type ManagementReconciler struct {
client.Client
Scheme *runtime.Scheme
Config *rest.Config
SystemNamespace string
Scheme *runtime.Scheme
Config *rest.Config
SystemNamespace string
CreateTemplateManagement bool
}

func (r *ManagementReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -75,11 +77,17 @@ 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: %w", management.Name, err)
l.Error(err, "Failed to update Management finalizers")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}

if err := r.ensureTemplateManagement(ctx, management); err != nil {
l.Error(err, "Failed to ensure TemplateManagement is created")
return ctrl.Result{}, err
}

release := &hmc.Release{}
if err := r.Client.Get(ctx, client.ObjectKey{Name: management.Spec.Release}, release); err != nil {
l.Error(err, "failed to get Release object")
Expand Down Expand Up @@ -145,6 +153,42 @@ func (r *ManagementReconciler) Update(ctx context.Context, management *hmc.Manag
return ctrl.Result{}, nil
}

func (r *ManagementReconciler) ensureTemplateManagement(ctx context.Context, mgmt *hmc.Management) error {
l := ctrl.LoggerFrom(ctx)
if !r.CreateTemplateManagement {
return nil
}
l.Info("Ensuring TemplateManagement is created")
tmObj := &hmc.TemplateManagement{
ObjectMeta: metav1.ObjectMeta{
Name: hmc.TemplateManagementName,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: hmc.GroupVersion.String(),
Kind: mgmt.Kind,
Name: mgmt.Name,
UID: mgmt.UID,
},
},
},
}
err := r.Get(ctx, client.ObjectKey{
Name: hmc.TemplateManagementName,
}, tmObj)
if err == nil {
return nil
}
if !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get %s TemplateManagement object: %w", hmc.TemplateManagementName, err)
}
err = r.Create(ctx, tmObj)
if err != nil {
return fmt.Errorf("failed to create %s TemplateManagement object: %w", hmc.TemplateManagementName, err)
}
l.Info("Successfully created TemplateManagement object")
return nil
}

func (r *ManagementReconciler) Delete(ctx context.Context, management *hmc.Management) (ctrl.Result, error) {
l := ctrl.LoggerFrom(ctx)
listOpts := &client.ListOptions{
Expand Down
Loading

0 comments on commit 13138bd

Please sign in to comment.