From 68bbac8aefccc363214641249c0c9e095bd9fbc4 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Sun, 4 Oct 2020 19:34:12 +0100 Subject: [PATCH] Add a TestApp.Cli project to emulate running migrations. Add Helm chart, update build --- helmcharts/TestApp/TestApp.Cli.Dockerfile | 18 ++++++ helmcharts/TestApp/TestApp.sln | 15 +++++ helmcharts/TestApp/charts/test-app/Chart.yaml | 2 +- .../test-app-api/templates/deployment.yaml | 7 +++ .../test-app/charts/test-app-cli/Chart.yaml | 23 +++++++ .../test-app-cli/templates/_helpers.tpl | 62 +++++++++++++++++++ .../charts/test-app-cli/templates/job.yaml | 27 ++++++++ .../templates/deployment.yaml | 7 +++ .../TestApp/charts/test-app/values.yaml | 11 ++++ helmcharts/TestApp/docker_build.sh | 1 + helmcharts/TestApp/src/Program.cs | 12 ++++ helmcharts/TestApp/src/TestApp.Cli/Program.cs | 14 +++++ .../src/TestApp.Cli/TestApp.Cli.csproj | 8 +++ helmcharts/TestApp/src/src.csproj | 8 +++ 14 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 helmcharts/TestApp/TestApp.Cli.Dockerfile create mode 100644 helmcharts/TestApp/charts/test-app/charts/test-app-cli/Chart.yaml create mode 100644 helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/_helpers.tpl create mode 100644 helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/job.yaml create mode 100644 helmcharts/TestApp/src/Program.cs create mode 100644 helmcharts/TestApp/src/TestApp.Cli/Program.cs create mode 100644 helmcharts/TestApp/src/TestApp.Cli/TestApp.Cli.csproj create mode 100644 helmcharts/TestApp/src/src.csproj diff --git a/helmcharts/TestApp/TestApp.Cli.Dockerfile b/helmcharts/TestApp/TestApp.Cli.Dockerfile new file mode 100644 index 0000000..62339af --- /dev/null +++ b/helmcharts/TestApp/TestApp.Cli.Dockerfile @@ -0,0 +1,18 @@ +FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine3.12 AS build +WORKDIR /sln + +# Copy project file and restore +COPY "./src/TestApp.Cli/TestApp.Cli.csproj" "./src/TestApp.Cli/" +RUN dotnet restore ./src/TestApp.Cli/TestApp.Cli.csproj + +# Copy the actual source code +COPY "./src/TestApp.Cli" "./src/TestApp.Cli" + +# Build and publish the app +RUN dotnet publish "./src/TestApp.Cli/TestApp.Cli.csproj" -c Release -o /app/publish + +#FINAL image +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine3.12 +WORKDIR /app +COPY --from=build /app/publish . +ENTRYPOINT ["dotnet", "TestApp.Cli.dll"] \ No newline at end of file diff --git a/helmcharts/TestApp/TestApp.sln b/helmcharts/TestApp/TestApp.sln index fcff3b2..5d9d40b 100644 --- a/helmcharts/TestApp/TestApp.sln +++ b/helmcharts/TestApp/TestApp.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp.Api", "src\TestApp. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp.Service", "src\TestApp.Service\TestApp.Service.csproj", "{E9F0BF62-6816-4AA8-9967-FA7A51D88E21}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp.Cli", "src\TestApp.Cli\TestApp.Cli.csproj", "{DC52C136-F30E-452C-9B98-92B3A89C8161}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -46,9 +48,22 @@ Global {E9F0BF62-6816-4AA8-9967-FA7A51D88E21}.Release|x64.Build.0 = Release|Any CPU {E9F0BF62-6816-4AA8-9967-FA7A51D88E21}.Release|x86.ActiveCfg = Release|Any CPU {E9F0BF62-6816-4AA8-9967-FA7A51D88E21}.Release|x86.Build.0 = Release|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Debug|x64.ActiveCfg = Debug|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Debug|x64.Build.0 = Debug|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Debug|x86.ActiveCfg = Debug|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Debug|x86.Build.0 = Debug|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Release|Any CPU.Build.0 = Release|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Release|x64.ActiveCfg = Release|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Release|x64.Build.0 = Release|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Release|x86.ActiveCfg = Release|Any CPU + {DC52C136-F30E-452C-9B98-92B3A89C8161}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {3648E36A-D429-448F-B0F8-5FA5106217A0} = {805C705B-1AAF-4911-A058-E5B0271F0CA4} {E9F0BF62-6816-4AA8-9967-FA7A51D88E21} = {805C705B-1AAF-4911-A058-E5B0271F0CA4} + {DC52C136-F30E-452C-9B98-92B3A89C8161} = {805C705B-1AAF-4911-A058-E5B0271F0CA4} EndGlobalSection EndGlobal diff --git a/helmcharts/TestApp/charts/test-app/Chart.yaml b/helmcharts/TestApp/charts/test-app/Chart.yaml index c9d206c..61e09db 100644 --- a/helmcharts/TestApp/charts/test-app/Chart.yaml +++ b/helmcharts/TestApp/charts/test-app/Chart.yaml @@ -15,7 +15,7 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.1.0 +version: 0.1.1 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to diff --git a/helmcharts/TestApp/charts/test-app/charts/test-app-api/templates/deployment.yaml b/helmcharts/TestApp/charts/test-app/charts/test-app-api/templates/deployment.yaml index be33787..eb0f835 100644 --- a/helmcharts/TestApp/charts/test-app/charts/test-app-api/templates/deployment.yaml +++ b/helmcharts/TestApp/charts/test-app/charts/test-app-api/templates/deployment.yaml @@ -27,6 +27,13 @@ spec: serviceAccountName: {{ include "test-app-api.serviceAccountName" . }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} + initContainers: + - name: "{{ .Chart.Name }}-init" + image: "groundnuty/k8s-wait-for:1.3" + imagePullPolicy: {{ .Values.image.pullPolicy }} + args: + - "job" + - "{{ .Release.Name }}-test-app-cli-{{ .Release.Revision}}" containers: - name: {{ .Chart.Name }} securityContext: diff --git a/helmcharts/TestApp/charts/test-app/charts/test-app-cli/Chart.yaml b/helmcharts/TestApp/charts/test-app/charts/test-app-cli/Chart.yaml new file mode 100644 index 0000000..27e0e36 --- /dev/null +++ b/helmcharts/TestApp/charts/test-app/charts/test-app-cli/Chart.yaml @@ -0,0 +1,23 @@ +apiVersion: v2 +name: test-app-cli +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.1 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +appVersion: 1.16.0 diff --git a/helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/_helpers.tpl b/helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/_helpers.tpl new file mode 100644 index 0000000..fe3eff2 --- /dev/null +++ b/helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "test-app-cli.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "test-app-cli.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "test-app-cli.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "test-app-cli.labels" -}} +helm.sh/chart: {{ include "test-app-cli.chart" . }} +{{ include "test-app-cli.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "test-app-cli.selectorLabels" -}} +app.kubernetes.io/name: {{ include "test-app-cli.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "test-app-cli.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "test-app-cli.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/job.yaml b/helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/job.yaml new file mode 100644 index 0000000..85be3a5 --- /dev/null +++ b/helmcharts/TestApp/charts/test-app/charts/test-app-cli/templates/job.yaml @@ -0,0 +1,27 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: {{ include "test-app-cli.fullname" . }}-{{ .Release.Revision }} + labels: + {{- include "test-app-cli.labels" . | nindent 4 }} +spec: + backoffLimit: 1 + template: + metadata: + labels: + {{- include "test-app-cli.selectorLabels" . | nindent 8 }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + command: ["dotnet"] + args: ["TestApp.Cli.dll", "migrate-database"] + env: + + {{- $env := merge (.Values.env | default dict) (.Values.global.env | default dict) -}} + {{ range $k, $v := $env }} + - name: {{ $k | quote }} + value: {{ $v | quote }} + {{- end }} + restartPolicy: {{ .Values.job.restartPolicy }} \ No newline at end of file diff --git a/helmcharts/TestApp/charts/test-app/charts/test-app-service/templates/deployment.yaml b/helmcharts/TestApp/charts/test-app/charts/test-app-service/templates/deployment.yaml index bd1c915..b60715e 100644 --- a/helmcharts/TestApp/charts/test-app/charts/test-app-service/templates/deployment.yaml +++ b/helmcharts/TestApp/charts/test-app/charts/test-app-service/templates/deployment.yaml @@ -27,6 +27,13 @@ spec: serviceAccountName: {{ include "test-app-service.serviceAccountName" . }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} + initContainers: + - name: "{{ .Chart.Name }}-init" + image: "groundnuty/k8s-wait-for:1.3" + imagePullPolicy: {{ .Values.image.pullPolicy }} + args: + - "job" + - "{{ .Release.Name }}-test-app-cli-{{ .Release.Revision}}" containers: - name: {{ .Chart.Name }} securityContext: diff --git a/helmcharts/TestApp/charts/test-app/values.yaml b/helmcharts/TestApp/charts/test-app/values.yaml index aec459f..8b6a72d 100644 --- a/helmcharts/TestApp/charts/test-app/values.yaml +++ b/helmcharts/TestApp/charts/test-app/values.yaml @@ -4,6 +4,17 @@ global: "ASPNETCORE_ENVIRONMENT": "Staging" "ASPNETCORE_FORWARDEDHEADERS_ENABLED": "true" +test-app-cli: + + image: + repository: andrewlock/my-test-cli + pullPolicy: IfNotPresent + tag: "" + + job: + ## Should the job be rescheduled on the same node if it fails, or just stopped + restartPolicy: Never + test-app-api: replicaCount: 1 diff --git a/helmcharts/TestApp/docker_build.sh b/helmcharts/TestApp/docker_build.sh index 1758586..69fa71c 100755 --- a/helmcharts/TestApp/docker_build.sh +++ b/helmcharts/TestApp/docker_build.sh @@ -8,5 +8,6 @@ if [ -z "$*" ]; then exit 1; fi +docker build -f TestApp.Cli.Dockerfile . -t andrewlock/my-test-cli:$1 docker build -f TestApp.Api.Dockerfile . -t andrewlock/my-test-api:$1 docker build -f TestApp.Service.Dockerfile . -t andrewlock/my-test-service:$1 \ No newline at end of file diff --git a/helmcharts/TestApp/src/Program.cs b/helmcharts/TestApp/src/Program.cs new file mode 100644 index 0000000..3d3e95e --- /dev/null +++ b/helmcharts/TestApp/src/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace src +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/helmcharts/TestApp/src/TestApp.Cli/Program.cs b/helmcharts/TestApp/src/TestApp.Cli/Program.cs new file mode 100644 index 0000000..ea9c79e --- /dev/null +++ b/helmcharts/TestApp/src/TestApp.Cli/Program.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading; + +namespace TestApp.Cli +{ + class Program + { + static void Main(string[] args) + { + Thread.Sleep(30_000); + Console.WriteLine("Hello World!"); + } + } +} diff --git a/helmcharts/TestApp/src/TestApp.Cli/TestApp.Cli.csproj b/helmcharts/TestApp/src/TestApp.Cli/TestApp.Cli.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/helmcharts/TestApp/src/TestApp.Cli/TestApp.Cli.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/helmcharts/TestApp/src/src.csproj b/helmcharts/TestApp/src/src.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/helmcharts/TestApp/src/src.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + +