From af05acc60fcb2f606b60e5503e08b6849fd5c797 Mon Sep 17 00:00:00 2001
From: Konstantinos Samaras-Tsakiris <ksamtsak@gmail.com>
Date: Mon, 22 Mar 2021 20:41:35 +0100
Subject: [PATCH] Add SiteURL to DrupalSite spec and default value without
 clustername

partial https://gitlab.cern.ch/webservices/webframeworks-planning/-/issues/298
---
 .gitlab-ci.yml                                      |  4 +---
 api/v1alpha1/drupalsite_types.go                    |  6 ++++++
 .../templates/manager-deploy.yaml                   |  4 ++--
 chart/drupalsite-operator/values.yaml               |  2 +-
 .../drupal.webservices.cern.ch_drupalsites.yaml     |  6 ++++++
 controllers/drupalsite_controller.go                | 13 ++++++++++---
 controllers/drupalsite_resources.go                 |  7 +------
 7 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 722d4c53..b37dd136 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -7,13 +7,11 @@ stages:
   - go_test
 
 variables:
-  CLUSTER_NAME: drupalsite-operator-ci
+  DEFAULT_DOMAIN: "webtest.cern.ch"
   RUNTIME_REPO: "https://gitlab.cern.ch/drupal/paas/drupal-runtime.git"
 
 GoTest:
   stage: go_test
   image: golang:1.15
   script:
-    - export CLUSTER_NAME=test
-    - export RUNTIME_REPO=https://gitlab.cern.ch/drupal/paas/drupal-runtime.git
     - make test
\ No newline at end of file
diff --git a/api/v1alpha1/drupalsite_types.go b/api/v1alpha1/drupalsite_types.go
index db570f81..8c618bfe 100644
--- a/api/v1alpha1/drupalsite_types.go
+++ b/api/v1alpha1/drupalsite_types.go
@@ -34,6 +34,12 @@ type DrupalSiteSpec struct {
 	// +kubebuilder:validation:Required
 	Publish bool `json:"publish"`
 
+	// SiteURL is the URL where the site should be made available.
+	// Defaults to <envName>-<projectname>.<defaultDomain>, where <defaultDomain> is configured per cluster (typically `web.cern.ch`)
+	// +kubebuilder:validation:Pattern=`[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)`
+	// +optional
+	SiteURL string `json:"siteUrl,omitempty"`
+
 	// DrupalVersion defines the version of the Drupal to install
 	// +kubebuilder:validation:Required
 	// +kubebuilder:validation:MinLength=1
diff --git a/chart/drupalsite-operator/templates/manager-deploy.yaml b/chart/drupalsite-operator/templates/manager-deploy.yaml
index 07542c6b..6e1947a4 100644
--- a/chart/drupalsite-operator/templates/manager-deploy.yaml
+++ b/chart/drupalsite-operator/templates/manager-deploy.yaml
@@ -30,8 +30,8 @@ spec:
         env:
         - name: RUNTIME_REPO
           value: {{ .Values.drupalsiteOperator.drupalRuntimeRepo }}
-        - name: CLUSTER_NAME
-          value: {{ .Values.clusterName }}
+        - name: DEFAULT_DOMAIN
+          value: {{ .Values.drupalsiteOperator.defaultDomain }}
         livenessProbe:
           httpGet:
             path: /healthz
diff --git a/chart/drupalsite-operator/values.yaml b/chart/drupalsite-operator/values.yaml
index 58df607c..0a89b068 100644
--- a/chart/drupalsite-operator/values.yaml
+++ b/chart/drupalsite-operator/values.yaml
@@ -1,5 +1,4 @@
 # Name of the k8s cluster where the operator is deployed. Used in the `ApplicationRegistration` naming convention.
-clusterName: {}
 
 image: {}
 imagePullPolicy: Always
@@ -17,4 +16,5 @@ resources:
 
 # Operator-specific configuration
 drupalsiteOperator:
+  defaultDoamin: "web.cern.ch"
   drupalRuntimeRepo: "https://gitlab.cern.ch/drupal/paas/drupal-runtime.git"
diff --git a/config/crd/bases/drupal.webservices.cern.ch_drupalsites.yaml b/config/crd/bases/drupal.webservices.cern.ch_drupalsites.yaml
index 8e670281..ac0301dc 100644
--- a/config/crd/bases/drupal.webservices.cern.ch_drupalsites.yaml
+++ b/config/crd/bases/drupal.webservices.cern.ch_drupalsites.yaml
@@ -83,6 +83,12 @@ spec:
               publish:
                 description: Publish defines if the site has to be published or not
                 type: boolean
+              siteUrl:
+                description: SiteURL is the URL where the site should be made available.
+                  Defaults to <envName>-<projectname>.<defaultDomain>, where <defaultDomain>
+                  is configured per cluster (typically `web.cern.ch`)
+                pattern: '[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)'
+                type: string
             required:
             - drupalVersion
             - environment
diff --git a/controllers/drupalsite_controller.go b/controllers/drupalsite_controller.go
index f1b7f409..a0e06699 100644
--- a/controllers/drupalsite_controller.go
+++ b/controllers/drupalsite_controller.go
@@ -64,8 +64,8 @@ var (
 	// and other config data to build the images
 	// Example: "s2i"
 	ImageRecipesRepoRef string
-	// ClusterName is used in the Route's Host field
-	ClusterName string
+	// DefaultDomain is used in the Route's Host field
+	DefaultDomain string
 )
 
 type strFlagList []string
@@ -213,7 +213,7 @@ func (r *DrupalSiteReconciler) initEnv() {
 	} else {
 		ImageRecipesRepoRef = "master"
 	}
-	ClusterName = getenvOrDie("CLUSTER_NAME", log)
+	DefaultDomain = getenvOrDie("DEFAULT_DOMAIN", log)
 
 	ImageRecipesRepoDownload := strings.Trim(runtimeRepo[0], ".git") + "/repository/archive.tar?path=configuration&ref=" + ImageRecipesRepoRef
 	directoryName := downloadFile(ImageRecipesRepoDownload, "/tmp/repo.tar", log)
@@ -294,6 +294,13 @@ func ensureSpecFinalizer(drp *webservicesv1a1.DrupalSite, log logr.Logger) (upda
 		controllerutil.AddFinalizer(drp, finalizerStr)
 		update = true
 	}
+	if drp.Spec.SiteURL == "" {
+		if drp.Spec.Environment.Name == productionEnvironment {
+			drp.Spec.SiteURL = drp.Namespace + "." + DefaultDomain
+
+		}
+		drp.Spec.SiteURL = drp.Spec.Environment.Name + "-" + drp.Namespace + "." + DefaultDomain
+	}
 	return
 }
 
diff --git a/controllers/drupalsite_resources.go b/controllers/drupalsite_resources.go
index 0a4c5390..66733617 100644
--- a/controllers/drupalsite_resources.go
+++ b/controllers/drupalsite_resources.go
@@ -837,16 +837,11 @@ func routeForDrupalSite(currentobject *routev1.Route, d *webservicesv1a1.DrupalS
 	ls := labelsForDrupalSite(d.Name)
 	ls["app"] = "drupal"
 
-	env := ""
-	if d.Spec.Environment.Name != productionEnvironment {
-		env = d.Spec.Environment.Name + "."
-	}
-
 	for k, v := range ls {
 		currentobject.Labels[k] = v
 	}
 	currentobject.Spec = routev1.RouteSpec{
-		Host: env + d.Name + "." + ClusterName + ".cern.ch",
+		Host: d.Spec.SiteURL,
 		To: routev1.RouteTargetReference{
 			Kind:   "Service",
 			Name:   "drupal-" + d.Name,
-- 
GitLab