diff --git a/common/common.go b/common/common.go index 0ea13a79c59..459ca8fcb44 100644 --- a/common/common.go +++ b/common/common.go @@ -29,6 +29,8 @@ const ( DefaultConsoleImage = "quay.io/redhat-developer/gitops-console-plugin" // Default console plugin version DefaultConsoleVersion = "v0.1.0" + // Default console plugin installation OCP version + DefaultDynamicPluginStartOCPVersion = "4.15.0" ) // InfraNodeSelector returns openshift label for infrastructure nodes diff --git a/controllers/gitopsservice_controller.go b/controllers/gitopsservice_controller.go index afefb995bfb..7a475bf01fb 100644 --- a/controllers/gitopsservice_controller.go +++ b/controllers/gitopsservice_controller.go @@ -19,6 +19,7 @@ package controllers import ( "context" "fmt" + "log" "os" "reflect" "strings" @@ -28,6 +29,7 @@ import ( argocdcontroller "github.com/argoproj-labs/argocd-operator/controllers/argocd" argocdutil "github.com/argoproj-labs/argocd-operator/controllers/argoutil" "github.com/go-logr/logr" + version "github.com/hashicorp/go-version" routev1 "github.com/openshift/api/route/v1" pipelinesv1alpha1 "github.com/redhat-developer/gitops-operator/api/v1alpha1" "github.com/redhat-developer/gitops-operator/common" @@ -57,15 +59,16 @@ var logs = logf.Log.WithName("controller_gitopsservice") // defaults must some somewhere else.. var ( - port int32 = 8080 - portTLS int32 = 8443 - backendImage string = "quay.io/redhat-developer/gitops-backend:v0.0.1" - backendImageEnvName = "BACKEND_IMAGE" - serviceName = "cluster" - insecureEnvVar = "INSECURE" - insecureEnvVarValue = "true" - serviceNamespace = "openshift-gitops" - deprecatedServiceNamespace = "openshift-pipelines-app-delivery" + port int32 = 8080 + portTLS int32 = 8443 + backendImage string = "quay.io/redhat-developer/gitops-backend:v0.0.1" + backendImageEnvName = "BACKEND_IMAGE" + serviceName = "cluster" + insecureEnvVar = "INSECURE" + insecureEnvVarValue = "true" + serviceNamespace = "openshift-gitops" + deprecatedServiceNamespace = "openshift-pipelines-app-delivery" + dynamicPluginStartOCPVersionEnv = "DYNAMIC_PLUGIN_START_OCP_VERSION" ) const ( @@ -224,7 +227,40 @@ func (r *ReconcileGitopsService) Reconcile(ctx context.Context, request reconcil return result, err } - return r.reconcilePlugin(instance, request) + dynamicPluginStartOCPVersion := os.Getenv(dynamicPluginStartOCPVersionEnv) + if dynamicPluginStartOCPVersion == "" { + dynamicPluginStartOCPVersion = common.DefaultDynamicPluginStartOCPVersion + } + + OCPVersion, err := util.GetClusterVersion(r.Client) + if err != nil { + log.Printf("Unable to get cluster version: %v", err) + return reconcile.Result{}, nil + } + + v1, err := version.NewVersion(OCPVersion) + if err != nil { + log.Printf("Unable to retrieve current OCP version: %v", err) + return reconcile.Result{}, nil + } + realVersion := v1.Segments() + realMajorVersion := realVersion[0] + realMinorVersion := realVersion[1] + + v2, err := version.NewVersion(dynamicPluginStartOCPVersion) + if err != nil { + return reconcile.Result{}, nil + } + startVersion := v2.Segments() + startMajorVersion := startVersion[0] + startMinorVersion := startVersion[1] + + if realMajorVersion < startMajorVersion || (realMajorVersion == startMajorVersion && realMinorVersion < startMinorVersion) { + // Skip plugin reconciliation if real OCP version is less than dynamic plugin start OCP version + return reconcile.Result{}, nil + } else { + return r.reconcilePlugin(instance, request) + } } func (r *ReconcileGitopsService) ensureDefaultArgoCDInstanceDoesntExist(instance *pipelinesv1alpha1.GitopsService, reqLogger logr.Logger) error { diff --git a/controllers/gitopsservice_controller_test.go b/controllers/gitopsservice_controller_test.go index 35ec5d48972..e6d3e3994a4 100644 --- a/controllers/gitopsservice_controller_test.go +++ b/controllers/gitopsservice_controller_test.go @@ -188,7 +188,7 @@ func TestReconcile(t *testing.T) { s := scheme.Scheme addKnownTypesToScheme(s) - fakeClient := fake.NewFakeClient(newGitopsService()) + fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.15.1"), newGitopsService()) reconciler := newReconcileGitOpsService(fakeClient, s) _, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test")) @@ -327,6 +327,38 @@ func TestReconcile_consoleAPINotFound(t *testing.T) { assert.Error(t, err, "configmaps \"httpd-cfg\" not found") } +func TestReconcile_ocpVersionLowerThan4_15(t *testing.T) { + defer util.SetConsoleAPIFound(util.IsConsoleAPIFound()) + util.SetConsoleAPIFound(false) + + logf.SetLogger(argocd.ZapLogger(true)) + s := scheme.Scheme + addKnownTypesToScheme(s) + + fakeClient := fake.NewFakeClient(util.NewClusterVersion("4.11.1"), newGitopsService()) + reconciler := newReconcileGitOpsService(fakeClient, s) + + _, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test")) + assertNoError(t, err) + + // Check consolePlugin and other resources are not created + consolePlugin := &consolepluginv1.ConsolePlugin{} + err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName}, consolePlugin) + assert.Error(t, err, "consoleplugins.console.openshift.io \"gitops-plugin\" not found") + + pluginDeploy := &appsv1.Deployment{} + err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginDeploy) + assert.Error(t, err, "deployments.apps \"gitops-plugin\" not found") + + pluginService := &corev1.Service{} + err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: gitopsPluginName, Namespace: serviceNamespace}, pluginService) + assert.Error(t, err, "services \"gitops-plugin\" not found") + + pluginConfigMap := &corev1.ConfigMap{} + err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: httpdConfigMapName, Namespace: serviceNamespace}, pluginConfigMap) + assert.Error(t, err, "configmaps \"httpd-cfg\" not found") +} + func TestReconcile_GitOpsNamespace(t *testing.T) { logf.SetLogger(argocd.ZapLogger(true)) s := scheme.Scheme diff --git a/go.mod b/go.mod index 195c261aed3..8f861db2a50 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/coreos/prometheus-operator v0.40.0 github.com/go-logr/logr v1.2.3 github.com/google/go-cmp v0.5.8 + github.com/hashicorp/go-version v1.2.1 github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.17.0 github.com/openshift/api v3.9.1-0.20190916204813-cdbe64fb0c91+incompatible diff --git a/go.sum b/go.sum index bf78679dcef..1f9805054b5 100644 --- a/go.sum +++ b/go.sum @@ -687,6 +687,8 @@ github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= +github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=