Skip to content

Commit d3cbc1a

Browse files
committed
fix: enable infra label to console-plugin deployment
Signed-off-by: saumeya <[email protected]>
1 parent 9093ffb commit d3cbc1a

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

controllers/consoleplugin.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"reflect"
88

9+
argocommon "github.com/argoproj-labs/argocd-operator/common"
10+
argocdutil "github.com/argoproj-labs/argocd-operator/controllers/argoutil"
911
consolev1 "github.com/openshift/api/console/v1"
1012
pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1"
1113
"github.com/redhat-developer/gitops-operator/controllers/util"
@@ -241,16 +243,30 @@ func pluginConfigMap() *corev1.ConfigMap {
241243
}
242244
}
243245

244-
func (r *ReconcileGitopsService) reconcileDeployment(instance *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) {
246+
func (r *ReconcileGitopsService) reconcileDeployment(cr *pipelinesv1alpha1.GitopsService, request reconcile.Request) (reconcile.Result, error) {
245247
reqLogger := logs.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
246248
newPluginDeployment := pluginDeployment()
247249

248-
if err := controllerutil.SetControllerReference(instance, newPluginDeployment, r.Scheme); err != nil {
250+
if err := controllerutil.SetControllerReference(cr, newPluginDeployment, r.Scheme); err != nil {
249251
return reconcile.Result{}, err
250252
}
251253

254+
newPluginDeployment.Spec.Template.Spec.NodeSelector = argocommon.DefaultNodeSelector()
255+
256+
if cr.Spec.RunOnInfra {
257+
newPluginDeployment.Spec.Template.Spec.NodeSelector[common.InfraNodeLabelSelector] = ""
258+
}
259+
if len(cr.Spec.NodeSelector) > 0 {
260+
newPluginDeployment.Spec.Template.Spec.NodeSelector = argocdutil.AppendStringMap(newPluginDeployment.Spec.Template.Spec.NodeSelector, cr.Spec.NodeSelector)
261+
}
262+
263+
if len(cr.Spec.Tolerations) > 0 {
264+
newPluginDeployment.Spec.Template.Spec.Tolerations = cr.Spec.Tolerations
265+
}
266+
252267
// Check if this Deployment already exists
253268
existingPluginDeployment := &appsv1.Deployment{}
269+
254270
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: newPluginDeployment.Name, Namespace: newPluginDeployment.Namespace}, existingPluginDeployment)
255271
if err != nil {
256272
if errors.IsNotFound(err) {
@@ -272,7 +288,9 @@ func (r *ReconcileGitopsService) reconcileDeployment(instance *pipelinesv1alpha1
272288
!reflect.DeepEqual(existingSpecTemplate.Spec.Containers, newSpecTemplate.Spec.Containers) ||
273289
!reflect.DeepEqual(existingSpecTemplate.Spec.Volumes, newSpecTemplate.Spec.Volumes) ||
274290
!reflect.DeepEqual(existingSpecTemplate.Spec.RestartPolicy, newSpecTemplate.Spec.RestartPolicy) ||
275-
!reflect.DeepEqual(existingSpecTemplate.Spec.DNSPolicy, newSpecTemplate.Spec.DNSPolicy)
291+
!reflect.DeepEqual(existingSpecTemplate.Spec.DNSPolicy, newSpecTemplate.Spec.DNSPolicy) ||
292+
!reflect.DeepEqual(existingPluginDeployment.Spec.Template.Spec.NodeSelector, newPluginDeployment.Spec.Template.Spec.NodeSelector) ||
293+
!reflect.DeepEqual(existingPluginDeployment.Spec.Template.Spec.Tolerations, newPluginDeployment.Spec.Template.Spec.Tolerations)
276294

277295
if changed {
278296
reqLogger.Info("Reconciling plugin deployment", "Namespace", existingPluginDeployment.Namespace, "Name", existingPluginDeployment.Name)
@@ -284,6 +302,8 @@ func (r *ReconcileGitopsService) reconcileDeployment(instance *pipelinesv1alpha1
284302
existingSpecTemplate.Spec.Volumes = newSpecTemplate.Spec.Volumes
285303
existingSpecTemplate.Spec.RestartPolicy = newSpecTemplate.Spec.RestartPolicy
286304
existingSpecTemplate.Spec.DNSPolicy = newSpecTemplate.Spec.DNSPolicy
305+
existingPluginDeployment.Spec.Template.Spec.NodeSelector = newPluginDeployment.Spec.Template.Spec.NodeSelector
306+
existingPluginDeployment.Spec.Template.Spec.Tolerations = newPluginDeployment.Spec.Template.Spec.Tolerations
287307
return reconcile.Result{}, r.Client.Update(context.TODO(), newPluginDeployment)
288308
}
289309
}

controllers/consoleplugin_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package controllers
22

33
import (
44
"context"
5+
"maps"
56
"testing"
67

8+
argocommon "github.com/argoproj-labs/argocd-operator/common"
79
consolev1 "github.com/openshift/api/console/v1"
810
pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1"
11+
"github.com/redhat-developer/gitops-operator/common"
912
"gotest.tools/assert"
1013
appsv1 "k8s.io/api/apps/v1"
1114
corev1 "k8s.io/api/core/v1"
@@ -1063,6 +1066,36 @@ func TestPlugin_reconcileDeployment_changedDNSPolicy(t *testing.T) {
10631066
}
10641067
}
10651068

1069+
func TestPlugin_reconcileDeployment_changedInfraNodeSelector(t *testing.T) {
1070+
1071+
gitopsService := &pipelinesv1alpha1.GitopsService{
1072+
ObjectMeta: metav1.ObjectMeta{
1073+
Name: serviceName,
1074+
},
1075+
Spec: pipelinesv1alpha1.GitopsServiceSpec{
1076+
RunOnInfra: true,
1077+
Tolerations: deploymentDefaultTolerations(),
1078+
},
1079+
}
1080+
s := scheme.Scheme
1081+
addKnownTypesToScheme(s)
1082+
1083+
fakeClient := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(gitopsService).Build()
1084+
reconciler := newReconcileGitOpsService(fakeClient, s)
1085+
1086+
_, err := reconciler.reconcileDeployment(gitopsService, newRequest(serviceNamespace, gitopsPluginName))
1087+
assertNoError(t, err)
1088+
1089+
deployment := &appsv1.Deployment{}
1090+
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, deployment)
1091+
assertNoError(t, err)
1092+
1093+
nSelector := common.InfraNodeSelector()
1094+
maps.Copy(nSelector, argocommon.DefaultNodeSelector())
1095+
assert.DeepEqual(t, deployment.Spec.Template.Spec.NodeSelector, nSelector)
1096+
assert.DeepEqual(t, deployment.Spec.Template.Spec.Tolerations, deploymentDefaultTolerations())
1097+
}
1098+
10661099
func TestPlugin_reconcileDeployment(t *testing.T) {
10671100
s := scheme.Scheme
10681101
addKnownTypesToScheme(s)

0 commit comments

Comments
 (0)