From 92c1facca97d8a729b868077bf48bf2b9ad239dd Mon Sep 17 00:00:00 2001 From: Tomas Coufal Date: Mon, 24 Oct 2022 22:25:29 +0200 Subject: [PATCH] feat: Add BuildConfig option to build the image in cluster Signed-off-by: Tomas Coufal --- charts/backstage/Chart.yaml | 2 +- charts/backstage/README.md | 9 +- charts/backstage/templates/_helpers.tpl | 10 +- .../templates/build/buildconfig.yaml | 54 +++++++++ .../templates/build/imagestream.yaml | 12 ++ charts/backstage/templates/deployment.yaml | 6 +- .../templates/postgresql/postgresql-ss.yaml | 2 +- charts/backstage/values.schema.json | 113 +++++++++++++++++- charts/backstage/values.yaml | 16 +++ 9 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 charts/backstage/templates/build/buildconfig.yaml create mode 100644 charts/backstage/templates/build/imagestream.yaml diff --git a/charts/backstage/Chart.yaml b/charts/backstage/Chart.yaml index d23af0fd..0308f730 100644 --- a/charts/backstage/Chart.yaml +++ b/charts/backstage/Chart.yaml @@ -1,7 +1,7 @@ apiVersion: v2 name: backstage description: A helm chart for deploying Backstage -version: 0.1.4 +version: 0.1.5 appVersion: "v1.7.0" kubeVersion: ">= 1.19.0-0" home: https://github.com/redhat-developer/helm-backstage diff --git a/charts/backstage/README.md b/charts/backstage/README.md index aac1e6ae..3f8722a4 100644 --- a/charts/backstage/README.md +++ b/charts/backstage/README.md @@ -1,6 +1,6 @@ # backstage -![Version: 0.1.3](https://img.shields.io/badge/Version-0.1.3-informational?style=flat-square) ![AppVersion: v1.7.0](https://img.shields.io/badge/AppVersion-v1.7.0-informational?style=flat-square) +![Version: 0.1.5](https://img.shields.io/badge/Version-0.1.5-informational?style=flat-square) ![AppVersion: v1.7.0](https://img.shields.io/badge/AppVersion-v1.7.0-informational?style=flat-square) A helm chart for deploying Backstage @@ -41,6 +41,13 @@ Replace `` with the name of the Helm release that was used when install | backstage.catalog.rules[0].allow[3] | string | `"Resource"` | | | backstage.catalog.rules[0].allow[4] | string | `"Location"` | | | backstage.companyname | string | `"Red Hat Backstage Helm Chart"` | | +| build.contextDir | string | `""` | | +| build.dockerfilePath | string | `""` | | +| build.enabled | bool | `true` | | +| build.mode | string | `"source"` | | +| build.ref | string | `"master"` | | +| build.sourceScripts | string | `"https://raw.githubusercontent.com/redhat-developer/redhat-backstage-build/main/.s2i/bin/"` | | +| build.uri | string | `"https://github.com/backstage/demo.git"` | | | fullnameOverride | string | `""` | | | image.pullPolicy | string | `"Always"` | | | image.registry | string | `"ghcr.io"` | | diff --git a/charts/backstage/templates/_helpers.tpl b/charts/backstage/templates/_helpers.tpl index 479a3815..44d44db2 100644 --- a/charts/backstage/templates/_helpers.tpl +++ b/charts/backstage/templates/_helpers.tpl @@ -147,10 +147,18 @@ Expand the name of the chart. {{- default .Values.backstage.baseUrl | trimPrefix "https://" | trimPrefix "http://" }} {{- end }} +{{- define "backstage.image" -}} +{{- if .Values.build.enabled -}} +{{ include "backstage.fullname" . }}:latest +{{- else -}} +{{ template "parseImage" .Values.image }} +{{- end -}} +{{- end -}} + {{/* Create the image path for the passed in image field */}} -{{- define "backstage.image" -}} +{{- define "parseImage" -}} {{- if eq (substr 0 7 .version) "sha256:" -}} {{- printf "%s/%s@%s" .registry .repository .version -}} {{- else -}} diff --git a/charts/backstage/templates/build/buildconfig.yaml b/charts/backstage/templates/build/buildconfig.yaml new file mode 100644 index 00000000..4427bee9 --- /dev/null +++ b/charts/backstage/templates/build/buildconfig.yaml @@ -0,0 +1,54 @@ +{{- if .Values.build.enabled }} +apiVersion: build.openshift.io/v1 +kind: BuildConfig +metadata: + labels: + {{- include "backstage.labels" . | nindent 4 }} + name: {{ include "backstage.fullname" . }} + namespace: {{ .Release.Namespace }} +spec: + resources: + limits: + cpu: "500m" + memory: "2Gi" + failedBuildsHistoryLimit: 5 + output: + to: + kind: ImageStreamTag + name: "{{ include "backstage.image" . }}" + runPolicy: Serial + source: + type: Git + git: + uri: {{ .Values.build.uri }} + ref: {{ .Values.build.ref }} + {{- if .Values.build.contextDir }} + contextDir: {{ .Values.build.contextDir }} + {{- end }} + strategy: + {{- if not (has .Values.build.mode (list "docker" "source")) }} + {{- fail "value 'build.mode' must be either 'docker' or 'source'" }} + {{- end }} + {{- if eq .Values.build.mode "docker" }} + type: Docker + dockerStrategy: + dockerfilePath: {{ .Values.build.dockerfilePath | default "./Dockerfile "}} + {{- if .Values.build.pullSecret }} + pullSecret: + name: {{ .Values.build.pullSecret }} + {{- end }} + {{ else }} + type: Source + sourceStrategy: + from: + kind: ImageStreamTag + name: "nodejs:latest" + namespace: openshift + {{- if .Values.build.sourceScripts }} + scripts: {{ .Values.build.sourceScripts }} + {{- end }} + {{- end }} + successfulBuildsHistoryLimit: 5 + triggers: + - type: ConfigChange +{{- end }} diff --git a/charts/backstage/templates/build/imagestream.yaml b/charts/backstage/templates/build/imagestream.yaml new file mode 100644 index 00000000..5f5272bf --- /dev/null +++ b/charts/backstage/templates/build/imagestream.yaml @@ -0,0 +1,12 @@ +{{- if .Values.build.enabled }} +apiVersion: image.openshift.io/v1 +kind: ImageStream +metadata: + labels: + {{- include "backstage.labels" . | nindent 4 }} + name: {{ include "backstage.fullname" . }} + namespace: {{ .Release.Namespace }} +spec: + lookupPolicy: + local: true +{{- end }} diff --git a/charts/backstage/templates/deployment.yaml b/charts/backstage/templates/deployment.yaml index 1b9406ca..7333c91e 100644 --- a/charts/backstage/templates/deployment.yaml +++ b/charts/backstage/templates/deployment.yaml @@ -26,9 +26,11 @@ spec: securityContext: {{- toYaml .Values.securityContext | nindent 12 }} {{- end }} - image: "{{ template "backstage.image" .Values.image }}" + image: "{{ include "backstage.image" . }}" + {{- if not .Values.build.enabled }} imagePullPolicy: {{ .Values.image.pullPolicy }} - command: ['node', 'packages/backend', '--config', '/config/app-config.yaml'] + {{- end }} + command: ['node', 'packages/backend', '--config', '/config/app-config.yaml'] env: - name: POSTGRES_ADMIN_PASSWORD valueFrom: diff --git a/charts/backstage/templates/postgresql/postgresql-ss.yaml b/charts/backstage/templates/postgresql/postgresql-ss.yaml index f62fe215..1eb95ef3 100644 --- a/charts/backstage/templates/postgresql/postgresql-ss.yaml +++ b/charts/backstage/templates/postgresql/postgresql-ss.yaml @@ -37,7 +37,7 @@ spec: {{- end }} terminationMessagePolicy: File imagePullPolicy: {{ .Values.postgres.image.pullPolicy }} - image: "{{ template "backstage.image" .Values.postgres.image }}" + image: "{{ template "parseImage" .Values.postgres.image }}" serviceAccount: {{ include "backstage.serviceAccountName" . }} {{- if .Values.postgres.storage.enabled }} volumes: diff --git a/charts/backstage/values.schema.json b/charts/backstage/values.schema.json index 12aaa69b..1919d14b 100644 --- a/charts/backstage/values.schema.json +++ b/charts/backstage/values.schema.json @@ -6,8 +6,6 @@ "title": "Root Schema", "required": [ "replicaCount", - "image", - "imagePullSecrets", "nameOverride", "fullnameOverride", "service", @@ -20,6 +18,37 @@ "securityContext", "backstage" ], + "oneOf": [ + { + "required": [ + "image" + ], + "properties": { + "build": { + "properties": { + "enabled": { + "enum": [ + false + ] + } + } + } + } + }, + { + "properties": { + "build": { + "properties": { + "enabled": { + "enum": [ + true + ] + } + } + } + } + } + ], "properties": { "replicaCount": { "type": "integer", @@ -82,6 +111,77 @@ } ] }, + "build": { + "type": "object", + "default": {}, + "title": "The build Schema", + "required": [ + "enabled" + ], + "properties": { + "enabled": { + "type": "boolean", + "default": false, + "title": "The enabled Schema", + "examples": [ + true + ] + }, + "ref": { + "type": "string", + "default": "", + "title": "The ref Schema", + "examples": [ + "" + ] + }, + "url": { + "type": "string", + "default": "", + "title": "The url Schema", + "examples": [ + "" + ] + }, + "contextDir": { + "type": "string", + "default": "", + "title": "The contextDir Schema", + "examples": [ + "" + ] + }, + "mode": { + "type": "string", + "default": "", + "title": "The mode Schema", + "examples": [ + "" + ] + }, + "dockerfilePath": { + "type": "string", + "default": "", + "title": "The dockerfilePath Schema", + "examples": [ + "" + ] + }, + "sourceScripts": { + "type": "string", + "default": "", + "title": "The sourceScripts Schema", + "examples": [ + "" + ] + } + }, + "examples": [ + { + "enabled": false + } + ] + }, "imagePullSecrets": { "type": "array", "default": [], @@ -905,6 +1005,15 @@ "version": "latest" }, "imagePullSecrets": [], + "build": { + "enabled": false, + "ref": "master", + "uri": "https://github.com/backstage/demo.git", + "contextDir": "", + "mode": "source", + "dockerfilePath": "", + "sourceScripts": "https://raw.githubusercontent.com/redhat-developer/redhat-backstage-build/main/.s2i/bin/" + }, "nameOverride": "", "fullnameOverride": "", "service": { diff --git a/charts/backstage/values.yaml b/charts/backstage/values.yaml index cea62558..96a7c62e 100644 --- a/charts/backstage/values.yaml +++ b/charts/backstage/values.yaml @@ -6,6 +6,22 @@ image: repository: redhat-developer/redhat-backstage-build version: "latest" +build: + ## If set to true the "image" section is ignored. Instead the image is built in cluster + enabled: false + ## Git reference for the source repository + ref: master + ## URI to the source repository + uri: https://github.com/backstage/demo.git + ## Optional path within the repository. Defaults to repository root. + contextDir: "" + ## Build mode selector, either set to "docker" to build from a local (in repo) Dockerfile or to "source" to build via source-to-image + mode: "source" + ## Custom path to the Dockerfile. Defaults to current context path within the repo. Applicable only if mode == "docker" + dockerfilePath: "" + ## Optional custom path to source-to-image assemble/run/etc. scripts. If set to blank, scripts need to be defined in your source repository + sourceScripts: "https://raw.githubusercontent.com/redhat-developer/redhat-backstage-build/main/.s2i/bin/" + imagePullSecrets: [] nameOverride: "" fullnameOverride: ""