diff --git a/controllers/consoleplugin.go b/controllers/consoleplugin.go index d2d7172aa..685ac1db8 100644 --- a/controllers/consoleplugin.go +++ b/controllers/consoleplugin.go @@ -6,6 +6,8 @@ import ( "os" "reflect" + argocommon "github.com/argoproj-labs/argocd-operator/common" + argocdutil "github.com/argoproj-labs/argocd-operator/controllers/argoutil" consolev1 "github.com/openshift/api/console/v1" pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1" "github.com/redhat-developer/gitops-operator/controllers/util" @@ -241,16 +243,30 @@ func pluginConfigMap() *corev1.ConfigMap { } } -func (r *ReconcileGitopsService) reconcileDeployment(instance *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) { +func (r *ReconcileGitopsService) reconcileDeployment(cr *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) { reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name) newPluginDeployment := pluginDeployment() - if err := controllerutil.SetControllerReference(instance, newPluginDeployment, r.Scheme); err != nil { + if err := controllerutil.SetControllerReference(cr, newPluginDeployment, r.Scheme); err != nil { return reconcile.Result{}, err } + newPluginDeployment.Spec.Template.Spec.NodeSelector = argocommon.DefaultNodeSelector() + + if cr.Spec.RunOnInfra { + newPluginDeployment.Spec.Template.Spec.NodeSelector[common.InfraNodeLabelSelector] = "" + } + if len(cr.Spec.NodeSelector) > 0 { + newPluginDeployment.Spec.Template.Spec.NodeSelector = argocdutil.AppendStringMap(newPluginDeployment.Spec.Template.Spec.NodeSelector, cr.Spec.NodeSelector) + } + + if len(cr.Spec.Tolerations) > 0 { + newPluginDeployment.Spec.Template.Spec.Tolerations = cr.Spec.Tolerations + } + // Check if this Deployment already exists existingPluginDeployment := &appsv1.Deployment{} + err := r.Client.Get(context.TODO(), types.NamespacedName{Name: newPluginDeployment.Name, Namespace: newPluginDeployment.Namespace}, existingPluginDeployment) if err != nil { if errors.IsNotFound(err) { @@ -272,7 +288,9 @@ func (r *ReconcileGitopsService) reconcileDeployment(instance *pipelinesv1alpha1 !reflect.DeepEqual(existingSpecTemplate.Spec.Containers, newSpecTemplate.Spec.Containers) || !reflect.DeepEqual(existingSpecTemplate.Spec.Volumes, newSpecTemplate.Spec.Volumes) || !reflect.DeepEqual(existingSpecTemplate.Spec.RestartPolicy, newSpecTemplate.Spec.RestartPolicy) || - !reflect.DeepEqual(existingSpecTemplate.Spec.DNSPolicy, newSpecTemplate.Spec.DNSPolicy) + !reflect.DeepEqual(existingSpecTemplate.Spec.DNSPolicy, newSpecTemplate.Spec.DNSPolicy) || + !reflect.DeepEqual(existingPluginDeployment.Spec.Template.Spec.NodeSelector, newPluginDeployment.Spec.Template.Spec.NodeSelector) || + !reflect.DeepEqual(existingPluginDeployment.Spec.Template.Spec.Tolerations, newPluginDeployment.Spec.Template.Spec.Tolerations) if changed { reqLogger.Info("Reconciling plugin deployment", "Namespace", existingPluginDeployment.Namespace, "Name", existingPluginDeployment.Name) @@ -284,6 +302,8 @@ func (r *ReconcileGitopsService) reconcileDeployment(instance *pipelinesv1alpha1 existingSpecTemplate.Spec.Volumes = newSpecTemplate.Spec.Volumes existingSpecTemplate.Spec.RestartPolicy = newSpecTemplate.Spec.RestartPolicy existingSpecTemplate.Spec.DNSPolicy = newSpecTemplate.Spec.DNSPolicy + existingPluginDeployment.Spec.Template.Spec.NodeSelector = newPluginDeployment.Spec.Template.Spec.NodeSelector + existingPluginDeployment.Spec.Template.Spec.Tolerations = newPluginDeployment.Spec.Template.Spec.Tolerations return reconcile.Result{}, r.Client.Update(context.TODO(), newPluginDeployment) } } diff --git a/controllers/consoleplugin_test.go b/controllers/consoleplugin_test.go index d815bce94..689200137 100644 --- a/controllers/consoleplugin_test.go +++ b/controllers/consoleplugin_test.go @@ -2,10 +2,13 @@ package controllers import ( "context" + "maps" "testing" + argocommon "github.com/argoproj-labs/argocd-operator/common" consolev1 "github.com/openshift/api/console/v1" pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1" + "github.com/redhat-developer/gitops-operator/common" "gotest.tools/assert" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -1063,6 +1066,36 @@ func TestPlugin_reconcileDeployment_changedDNSPolicy(t *testing.T) { } } +func TestPlugin_reconcileDeployment_changedInfraNodeSelector(t *testing.T) { + + gitopsService := &pipelinesv1alpha1.GitopsService{ + ObjectMeta: metav1.ObjectMeta{ + Name: serviceName, + }, + Spec: pipelinesv1alpha1.GitopsServiceSpec{ + RunOnInfra: true, + Tolerations: deploymentDefaultTolerations(), + }, + } + s := scheme.Scheme + addKnownTypesToScheme(s) + + fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(gitopsService).Build() + reconciler := newReconcileGitOpsService(fakeClient, s) + + _, err := reconciler.reconcileDeployment(gitopsService, newRequest(serviceNamespace, gitopsPluginName)) + assertNoError(t, err) + + deployment := &appsv1.Deployment{} + err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, deployment) + assertNoError(t, err) + + nSelector := common.InfraNodeSelector() + maps.Copy(nSelector, argocommon.DefaultNodeSelector()) + assert.DeepEqual(t, deployment.Spec.Template.Spec.NodeSelector, nSelector) + assert.DeepEqual(t, deployment.Spec.Template.Spec.Tolerations, deploymentDefaultTolerations()) +} + func TestPlugin_reconcileDeployment(t *testing.T) { s := scheme.Scheme addKnownTypesToScheme(s)