From e5bcefd77d5c7cbe30faf902332df7f94440e448 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Mon, 7 Oct 2024 11:54:05 +0200 Subject: [PATCH] link consoleplugin deployment lifecycle with topolocy configmap lifecycle Signed-off-by: Eguzki Astiz Lezaun --- controllers/consoleplugin_reconciler.go | 33 ++++++++++++++++++++++++- controllers/state_of_the_world.go | 1 - controllers/topology_reconciler.go | 6 ++++- go.mod | 1 - 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/controllers/consoleplugin_reconciler.go b/controllers/consoleplugin_reconciler.go index 8e8b88bce..0c80e656e 100644 --- a/controllers/consoleplugin_reconciler.go +++ b/controllers/consoleplugin_reconciler.go @@ -16,6 +16,7 @@ import ( "github.com/kuadrant/policy-machinery/machinery" "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" + "github.com/kuadrant/kuadrant-operator/pkg/library/utils" "github.com/kuadrant/kuadrant-operator/pkg/log" "github.com/kuadrant/kuadrant-operator/pkg/openshift" "github.com/kuadrant/kuadrant-operator/pkg/openshift/consoleplugin" @@ -49,17 +50,38 @@ func (r *ConsolePluginReconciler) Subscription() *controller.Subscription { ReconcileFunc: r.Run, Events: []controller.ResourceEventMatcher{ {Kind: ptr.To(openshift.ConsolePluginGVK.GroupKind())}, + { + Kind: ptr.To(ConfigMapGroupKind), + ObjectNamespace: r.namespace, + ObjectName: TopologyConfigMapName, + EventType: ptr.To(controller.CreateEvent), + }, + { + Kind: ptr.To(ConfigMapGroupKind), + ObjectNamespace: r.namespace, + ObjectName: TopologyConfigMapName, + EventType: ptr.To(controller.DeleteEvent), + }, }, } } -func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.ResourceEvent, _ *machinery.Topology, _ error, _ *sync.Map) error { +func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, _ *sync.Map) error { logger := r.Logger() logger.V(1).Info("task started") ctx := logr.NewContext(eventCtx, logger) + existingTopologyConfigMaps := topology.Objects().Items(func(object machinery.Object) bool { + return object.GetName() == TopologyConfigMapName && object.GetNamespace() == r.namespace && object.GroupVersionKind().Kind == ConfigMapGroupKind.Kind + }) + + topologyExists := len(existingTopologyConfigMaps) > 0 + // Service service := consoleplugin.Service(r.namespace) + if !topologyExists { + utils.TagObjectToDelete(service) + } err := r.ReconcileResource(ctx, &corev1.Service{}, service, reconcilers.CreateOnlyMutator) if err != nil { logger.Error(err, "reconciling service") @@ -70,6 +92,9 @@ func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.R deployment := consoleplugin.Deployment(r.namespace, ConsolePluginImageURL) deploymentMutators := make([]reconcilers.DeploymentMutateFn, 0) deploymentMutators = append(deploymentMutators, reconcilers.DeploymentImageMutator) + if !topologyExists { + utils.TagObjectToDelete(deployment) + } err = r.ReconcileResource(ctx, &appsv1.Deployment{}, deployment, reconcilers.DeploymentMutator(deploymentMutators...)) if err != nil { logger.Error(err, "reconciling deployment") @@ -78,6 +103,9 @@ func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.R // Nginx ConfigMap nginxConfigMap := consoleplugin.NginxConfigMap(r.namespace) + if !topologyExists { + utils.TagObjectToDelete(nginxConfigMap) + } err = r.ReconcileResource(ctx, &corev1.ConfigMap{}, nginxConfigMap, reconcilers.CreateOnlyMutator) if err != nil { logger.Error(err, "reconciling nginx configmap") @@ -86,6 +114,9 @@ func (r *ConsolePluginReconciler) Run(eventCtx context.Context, _ []controller.R // ConsolePlugin consolePlugin := consoleplugin.ConsolePlugin(r.namespace) + if !topologyExists { + utils.TagObjectToDelete(consolePlugin) + } err = r.ReconcileResource(ctx, &consolev1.ConsolePlugin{}, consolePlugin, reconcilers.CreateOnlyMutator) if err != nil { logger.Error(err, "reconciling consoleplugin") diff --git a/controllers/state_of_the_world.go b/controllers/state_of_the_world.go index d70a9b358..ae200864a 100644 --- a/controllers/state_of_the_world.go +++ b/controllers/state_of_the_world.go @@ -145,7 +145,6 @@ func NewPolicyMachineryController(manager ctrlruntime.Manager, client *dynamic.D &consolev1.ConsolePlugin{}, openshift.ConsolePluginsResource, metav1.NamespaceAll, controller.FilterResourcesByLabel[*consolev1.ConsolePlugin](fmt.Sprintf("%s=%s", consoleplugin.AppLabelKey, consoleplugin.AppLabelValue)))), controller.WithObjectKinds(openshift.ConsolePluginGVK.GroupKind()), - // TODO: add object links ) } diff --git a/controllers/topology_reconciler.go b/controllers/topology_reconciler.go index e61655717..f68931f8d 100644 --- a/controllers/topology_reconciler.go +++ b/controllers/topology_reconciler.go @@ -15,6 +15,10 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant" ) +const ( + TopologyConfigMapName = "topology" +) + type TopologyReconciler struct { Client *dynamic.DynamicClient Namespace string @@ -32,7 +36,7 @@ func (r *TopologyReconciler) Reconcile(ctx context.Context, _ []controller.Resou cm := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "topology", + Name: TopologyConfigMapName, Namespace: r.Namespace, Labels: map[string]string{kuadrant.TopologyLabel: "true"}, }, diff --git a/go.mod b/go.mod index 1f51fe6c6..347ade174 100644 --- a/go.mod +++ b/go.mod @@ -127,7 +127,6 @@ require ( github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0 // indirect - github.com/openshift/api v0.0.0-20240926211938-f89ab92f1597 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect