From 53f78260a99eff95f245c2fc238b6ff7cd37e5f4 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Thu, 22 Apr 2021 18:31:26 -0500 Subject: [PATCH] Add ability to set target namespace for helm chart template --- api/types/helmchartargs.go | 4 + .../HelmChartInflationGenerator.go | 3 + .../HelmChartInflationGenerator_test.go | 146 +++++++++++++++--- 3 files changed, 132 insertions(+), 21 deletions(-) diff --git a/api/types/helmchartargs.go b/api/types/helmchartargs.go index 7bb22f2e4..410601857 100644 --- a/api/types/helmchartargs.go +++ b/api/types/helmchartargs.go @@ -51,6 +51,10 @@ type HelmChart struct { // If omitted, the flag --generate-name is passed to 'helm template'. ReleaseName string `json:"releaseName,omitempty" yaml:"releaseName,omitempty"` + // Namespace set the target namespace for a release. It is .Release.Namespace + // in the helm template + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` + // ValuesFile is local file path to a values file to use _instead of_ // the default values that accompanied the chart. // The default values are in '{ChartHome}/{Name}/values.yaml'. diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go index b7830d420..84fbca807 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go @@ -272,6 +272,9 @@ func (p *HelmChartInflationGeneratorPlugin) templateCommand() []string { if p.ReleaseName != "" { args = append(args, p.ReleaseName) } + if p.Namespace != "" { + args = append(args, "--namespace", p.Namespace) + } args = append(args, filepath.Join(p.absChartHome(), p.Name)) if p.ValuesFile != "" { args = append(args, "--values", p.ValuesFile) diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go index 443d068f3..f187223e0 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go @@ -20,46 +20,150 @@ func TestHelmChartInflationGenerator(t *testing.T) { apiVersion: builtin kind: HelmChartInflationGenerator metadata: - name: myMc -name: minecraft -version: 3.1.3 -repo: https://itzg.github.io/minecraft-server-charts + name: myPipeline +name: ocp-pipeline +namespace: mynamespace +version: 0.1.16 +repo: https://bcgov.github.io/helm-charts releaseName: moria +valuesInline: + releaseNamespace: "" + rbac: + create: true + rules: + - apiGroups: [""] + verbs: ["*"] + resouces: ["*"] `) th.AssertActualEqualsExpected(rm, ` apiVersion: v1 data: - rcon-password: Q0hBTkdFTUUh + config: eyJleGFtcGxlIjoidmFsdWUifQ== kind: Secret metadata: labels: - app: moria-minecraft - chart: minecraft-3.1.3 + chart: ocp-pipeline-0.1.16 heritage: Helm release: moria - name: moria-minecraft + name: moria-config type: Opaque --- apiVersion: v1 -kind: Service +data: + WebHookSecretKey: MTIzNDU2Nzg= +kind: Secret metadata: - annotations: {} labels: - app: moria-minecraft - chart: minecraft-3.1.3 + chart: ocp-pipeline-0.1.16 heritage: Helm release: moria - name: moria-minecraft + name: moria-git-webhook-secret +type: Opaque +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: moria-ocp-pipeline + namespace: mynamespace +rules: +- apiGroups: + - "" + resouces: + - '*' + verbs: + - '*' +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: moria-ocp-pipeline + namespace: mynamespace +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: moria-ocp-pipeline +subjects: +- kind: ServiceAccount + name: jenkins + namespace: mynamespace +--- +apiVersion: build.openshift.io/v1 +kind: BuildConfig +metadata: + labels: + app: ocp-pipeline + chart: ocp-pipeline-0.1.16 + heritage: Helm + release: moria + name: moria-ocp-pipeline-deploy + namespace: null spec: - ports: - - name: minecraft - port: 25565 - protocol: TCP - targetPort: minecraft - selector: - app: moria-minecraft - type: ClusterIP + nodeSelector: {} + resources: + limits: + cpu: 4000m + memory: 8G + requests: + cpu: 2000m + memory: 4G + strategy: + jenkinsPipelineStrategy: + jenkinsfile: |- + def helmName = "helm-v3.1.0-linux-amd64.tar.gz" + def chartName = "metadata-curator" + def chartRepo = "http://bcgov.github.io/helm-charts" + def releaseName = "mc" + def releaseNamespace = "" + def forceRecreate = "false" + def callAnotherPipe = "false" + def useEnv = "false" + def fromEnv = "commit" + def setFlag = "image.tag" + + node("nodejs") { + stage("deploy (it's already built)") { + sh """ + curl -L -O https://get.helm.sh/${helmName} + tar -zxvf ${helmName} + cd linux-amd64 + + curl -L -O https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux32 + chmod ugo+x ./jq-linux32 + npm install -g json2yaml + + export CONF1=`+"`"+`oc get secret moria-config -o json | ./jq-linux32 .data.config`+"`"+` + export CONF2=`+"`"+`sed -e 's/^"//' -e 's/"\$//' <<<"\$CONF1"`+"`"+` + export CONF3=`+"`"+`echo \$CONF2 | base64 -d -`+"`"+` + export CONF=`+"`"+`echo \$CONF3 | json2yaml`+"`"+` + + echo "\$CONF" > ./config.yaml + oc project ${releaseNamespace} + ./helm repo add chart ${chartRepo} + ./helm repo update + if [ "${forceRecreate}" = "true" ]; then + ./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install --set hashLabel="${releaseName}\$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)" + elif [ "${useEnv}" = "true" ]; then + ./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install --set ${setFlag}=${env[fromEnv]} + else + ./helm upgrade ${releaseName} chart/${chartName} -f ./config.yaml --install + fi + + if [ "${callAnotherPipe}" = "true" ]; then + curl -d '' http://otherwebhookUrl + fi + """ + } + } + type: JenkinsPipeline + triggers: + - generic: + allowEnv: true + secretReference: + name: moria-git-webhook-secret + type: generic +status: + lastVersion: 0 `) }