Skip to content

Commit 3703d8d

Browse files
Merge pull request #403 from iam-veeramalla/pickConditionalLogicForPlugin
[cherry-pick]: Add reconcile plugin condition (#402)
2 parents d82ced1 + 5af8c2e commit 3703d8d

5 files changed

Lines changed: 84 additions & 11 deletions

File tree

common/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const (
2929
DefaultConsoleImage = "quay.io/redhat-developer/gitops-console-plugin"
3030
// Default console plugin version
3131
DefaultConsoleVersion = "v0.1.0"
32+
// Default console plugin installation OCP version
33+
DefaultDynamicPluginStartOCPVersion = "4.15.0"
3234
)
3335

3436
// InfraNodeSelector returns openshift label for infrastructure nodes

controllers/gitopsservice_controller.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22+
"log"
2223
"os"
2324
"reflect"
2425
"strings"
@@ -28,6 +29,7 @@ import (
2829
argocdcontroller "github.com/argoproj-labs/argocd-operator/controllers/argocd"
2930
argocdutil "github.com/argoproj-labs/argocd-operator/controllers/argoutil"
3031
"github.com/go-logr/logr"
32+
version "github.com/hashicorp/go-version"
3133
routev1 "github.com/openshift/api/route/v1"
3234
pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1"
3335
"github.com/redhat-developer/gitops-operator/common"
@@ -57,15 +59,16 @@ var logs = logf.Log.WithName("controller_gitopsservice")
5759

5860
// defaults must some somewhere else..
5961
var (
60-
port int32 = 8080
61-
portTLS int32 = 8443
62-
backendImage string = "quay.io/redhat-developer/gitops-backend:v0.0.1"
63-
backendImageEnvName = "BACKEND_IMAGE"
64-
serviceName = "cluster"
65-
insecureEnvVar = "INSECURE"
66-
insecureEnvVarValue = "true"
67-
serviceNamespace = "openshift-gitops"
68-
deprecatedServiceNamespace = "openshift-pipelines-app-delivery"
62+
port int32 = 8080
63+
portTLS int32 = 8443
64+
backendImage string = "quay.io/redhat-developer/gitops-backend:v0.0.1"
65+
backendImageEnvName = "BACKEND_IMAGE"
66+
serviceName = "cluster"
67+
insecureEnvVar = "INSECURE"
68+
insecureEnvVarValue = "true"
69+
serviceNamespace = "openshift-gitops"
70+
deprecatedServiceNamespace = "openshift-pipelines-app-delivery"
71+
dynamicPluginStartOCPVersionEnv = "DYNAMIC_PLUGIN_START_OCP_VERSION"
6972
)
7073

7174
const (
@@ -224,7 +227,40 @@ func (r *ReconcileGitopsService) Reconcile(ctx context.Context, request reconcil
224227
return result, err
225228
}
226229

227-
return r.reconcilePlugin(instance, request)
230+
dynamicPluginStartOCPVersion := os.Getenv(dynamicPluginStartOCPVersionEnv)
231+
if dynamicPluginStartOCPVersion == "" {
232+
dynamicPluginStartOCPVersion = common.DefaultDynamicPluginStartOCPVersion
233+
}
234+
235+
OCPVersion, err := util.GetClusterVersion(r.Client)
236+
if err != nil {
237+
log.Printf("Unable to get cluster version: %v", err)
238+
return reconcile.Result{}, nil
239+
}
240+
241+
v1, err := version.NewVersion(OCPVersion)
242+
if err != nil {
243+
log.Printf("Unable to retrieve current OCP version: %v", err)
244+
return reconcile.Result{}, nil
245+
}
246+
realVersion := v1.Segments()
247+
realMajorVersion := realVersion[0]
248+
realMinorVersion := realVersion[1]
249+
250+
v2, err := version.NewVersion(dynamicPluginStartOCPVersion)
251+
if err != nil {
252+
return reconcile.Result{}, nil
253+
}
254+
startVersion := v2.Segments()
255+
startMajorVersion := startVersion[0]
256+
startMinorVersion := startVersion[1]
257+
258+
if realMajorVersion < startMajorVersion || (realMajorVersion == startMajorVersion && realMinorVersion < startMinorVersion) {
259+
// Skip plugin reconciliation if real OCP version is less than dynamic plugin start OCP version
260+
return reconcile.Result{}, nil
261+
} else {
262+
return r.reconcilePlugin(instance, request)
263+
}
228264
}
229265

230266
func (r *ReconcileGitopsService) ensureDefaultArgoCDInstanceDoesntExist(instance *pipelinesv1alpha1.GitopsService, reqLogger logr.Logger) error {

controllers/gitopsservice_controller_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestReconcile(t *testing.T) {
188188
s := scheme.Scheme
189189
addKnownTypesToScheme(s)
190190

191-
fakeClient := fake.NewFakeClient(newGitopsService())
191+
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.15.1"), newGitopsService())
192192
reconciler := newReconcileGitOpsService(fakeClient, s)
193193

194194
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
@@ -327,6 +327,38 @@ func TestReconcile_consoleAPINotFound(t *testing.T) {
327327
assert.Error(t, err, "configmaps \"httpd-cfg\" not found")
328328
}
329329

330+
func TestReconcile_ocpVersionLowerThan4_15(t *testing.T) {
331+
defer util.SetConsoleAPIFound(util.IsConsoleAPIFound())
332+
util.SetConsoleAPIFound(false)
333+
334+
logf.SetLogger(argocd.ZapLogger(true))
335+
s := scheme.Scheme
336+
addKnownTypesToScheme(s)
337+
338+
fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.11.1"), newGitopsService())
339+
reconciler := newReconcileGitOpsService(fakeClient, s)
340+
341+
_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
342+
assertNoError(t, err)
343+
344+
// Check consolePlugin and other resources are not created
345+
consolePlugin := &consolepluginv1.ConsolePlugin{}
346+
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName}, consolePlugin)
347+
assert.Error(t, err, "consoleplugins.console.openshift.io \"gitops-plugin\" not found")
348+
349+
pluginDeploy := &appsv1.Deployment{}
350+
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginDeploy)
351+
assert.Error(t, err, "deployments.apps \"gitops-plugin\" not found")
352+
353+
pluginService := &corev1.Service{}
354+
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginService)
355+
assert.Error(t, err, "services \"gitops-plugin\" not found")
356+
357+
pluginConfigMap := &corev1.ConfigMap{}
358+
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: httpdConfigMapName, Namespace: serviceNamespace}, pluginConfigMap)
359+
assert.Error(t, err, "configmaps \"httpd-cfg\" not found")
360+
}
361+
330362
func TestReconcile_GitOpsNamespace(t *testing.T) {
331363
logf.SetLogger(argocd.ZapLogger(true))
332364
s := scheme.Scheme

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/coreos/prometheus-operator v0.40.0
88
github.com/go-logr/logr v1.2.3
99
github.com/google/go-cmp v0.5.8
10+
github.com/hashicorp/go-version v1.2.1
1011
github.com/onsi/ginkgo v1.16.5
1112
github.com/onsi/gomega v1.17.0
1213
github.com/openshift/api v3.9.1-0.20190916204813-cdbe64fb0c91+incompatible

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
687687
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
688688
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
689689
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
690+
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
691+
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
690692
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
691693
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
692694
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

0 commit comments

Comments
 (0)