From 3620b76139df9f4adc75215e29ce69015979e932 Mon Sep 17 00:00:00 2001
From: stefanprodan <stefan.prodan@gmail.com>
Date: Wed, 22 Jul 2020 14:30:39 +0300
Subject: [PATCH] Make notification component optional

---
 cmd/tk/install.go | 25 ++++++++++++++++++++++++-
 cmd/tk/main.go    |  7 ++++---
 cmd/tk/utils.go   |  9 +++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/cmd/tk/install.go b/cmd/tk/install.go
index 5817dc9c..3305de23 100644
--- a/cmd/tk/install.go
+++ b/cmd/tk/install.go
@@ -118,6 +118,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
 		} else if installExport {
 			fmt.Println("---")
 			fmt.Println("# GitOps Toolkit revision", installVersion, time.Now().Format(time.RFC3339))
+			fmt.Println("# Components:", strings.Join(installComponents, ","))
 			fmt.Print(yaml)
 			fmt.Println("---")
 			return nil
@@ -183,7 +184,7 @@ fieldSpecs:
 `
 
 var kustomizationTmpl = `---
-{{- $version := .Version }}
+{{- $eventsAddr := .EventsAddr }}
 apiVersion: kustomize.config.k8s.io/v1beta1
 kind: Kustomization
 namespace: {{.Namespace}}
@@ -196,6 +197,21 @@ resources:
 {{- range .Components }}
   - {{.}}.yaml
 {{- end }}
+
+patchesJson6902:
+{{- range $i, $v := .Components }}
+{{- if ne $v "notification-controller" }}
+- target:
+    group: apps
+    version: v1
+    kind: Deployment
+    name: {{$v}}
+  patch: |-
+    - op: replace
+      path: /spec/template/spec/containers/0/args/0
+      value: --events-addr={{$eventsAddr}}
+{{- end }}
+{{- end }}
 `
 
 var kustomizationRolesTmpl = `---
@@ -241,14 +257,21 @@ func downloadManifests(version string, tmpDir string) error {
 }
 
 func genInstallManifests(version string, namespace string, components []string, tmpDir string) error {
+	eventsAddr := ""
+	if utils.containsItemString(components, defaultNotification) {
+		eventsAddr = fmt.Sprintf("http://%s/", defaultNotification)
+	}
+
 	model := struct {
 		Version    string
 		Namespace  string
 		Components []string
+		EventsAddr string
 	}{
 		Version:    version,
 		Namespace:  namespace,
 		Components: components,
+		EventsAddr: eventsAddr,
 	}
 
 	if err := downloadManifests(version, tmpDir); err != nil {
diff --git a/cmd/tk/main.go b/cmd/tk/main.go
index aaaa64a0..b3cf6f82 100644
--- a/cmd/tk/main.go
+++ b/cmd/tk/main.go
@@ -104,9 +104,10 @@ var (
 )
 
 var (
-	defaultComponents = []string{"source-controller", "kustomize-controller", "helm-controller", "notification-controller"}
-	defaultVersion    = "latest"
-	defaultNamespace  = "gitops-system"
+	defaultComponents   = []string{"source-controller", "kustomize-controller", "helm-controller", "notification-controller"}
+	defaultVersion      = "latest"
+	defaultNamespace    = "gitops-system"
+	defaultNotification = "notification-controller"
 )
 
 func init() {
diff --git a/cmd/tk/utils.go b/cmd/tk/utils.go
index d2441106..e1dae097 100644
--- a/cmd/tk/utils.go
+++ b/cmd/tk/utils.go
@@ -166,3 +166,12 @@ func (*Utils) copyFile(src, dst string) error {
 	}
 	return out.Close()
 }
+
+func (*Utils) containsItemString(s []string, e string) bool {
+	for _, a := range s {
+		if a == e {
+			return true
+		}
+	}
+	return false
+}
-- 
GitLab