Skip to content

Commit

Permalink
Add exec-host
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Oct 19, 2020
1 parent d956e3e commit 6921de1
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 3 deletions.
28 changes: 28 additions & 0 deletions helmcharts/TestApp/TestApp.Cli-Exec-Host.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Build standard .NET Core application
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS builder
WORKDIR /app

# WARNING: This is completely unoptimised!
COPY . .

# Publish the CLI project to the path /app/output/cli
RUN dotnet publish ./src/TestApp.Cli -c Release -o /app/output/cli

###################

# Runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine

# Copy the background script that keeps the pod alive
WORKDIR /background
COPY ./keep_alive.sh ./keep_alive.sh
# Ensure the file is executable
RUN chmod +x /background/keep_alive.sh

# Set the command that runs when the pod is started
CMD "/background/keep_alive.sh"

WORKDIR /app

# Copy the CLI tool into this container
COPY --from=builder ./app/output/cli .
1 change: 1 addition & 0 deletions helmcharts/TestApp/build_and_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHART="./charts/test-app" \
RELEASE_NAME="my-test-app-release" \
NAMESPACE="local" \
HELM_ARGS="--set test-app-cli.image.tag=$1 \
--set test-app-cli-exec-host.image.tag=$1 \
--set test-app-api.image.tag=$1 \
--set test-app-service.image.tag=$1 \
" \
Expand Down
2 changes: 1 addition & 1 deletion helmcharts/TestApp/charts/test-app/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1
version: 0.1.2

# 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v2
name: test-app-cli-exec-host
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.0

# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "test-app-cli-exec-host.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-exec-host.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-exec-host.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "test-app-cli-exec-host.labels" -}}
helm.sh/chart: {{ include "test-app-cli-exec-host.chart" . }}
{{ include "test-app-cli-exec-host.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-exec-host.selectorLabels" -}}
app.kubernetes.io/name: {{ include "test-app-cli-exec-host.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "test-app-cli-exec-host.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "test-app-cli-exec-host.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "test-app-cli-exec-host.fullname" . }}
labels:
{{- include "test-app-cli-exec-host.labels" . | nindent 4 }}
spec:
replicas: 1
selector:
matchLabels:
{{- include "test-app-cli-exec-host.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "test-app-cli-exec-host.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "test-app-cli-exec-host.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
{{- $env := merge (.Values.env | default dict) (.Values.global.env | default dict) -}}
{{ range $k, $v := $env }}
- name: {{ $k | quote }}
value: {{ $v | quote }}
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
10 changes: 10 additions & 0 deletions helmcharts/TestApp/charts/test-app/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ test-app-cli:
## Should the job be rescheduled on the same node if it fails, or just stopped
restartPolicy: Never

test-app-cli-exec-host:

image:
repository: andrewlock/my-test-cli-exec-host
pullPolicy: IfNotPresent
tag: ""

serviceAccount:
create: false

test-app-api:

replicaCount: 1
Expand Down
2 changes: 2 additions & 0 deletions helmcharts/TestApp/docker_build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
set -eu

# in case we are being run from outside this directory
cd "$(dirname "$0")"
Expand All @@ -9,5 +10,6 @@ if [ -z "$*" ]; then
fi

docker build -f TestApp.Cli.Dockerfile . -t andrewlock/my-test-cli:$1
docker build -f TestApp.Cli-Exec-Host.Dockerfile . -t andrewlock/my-test-cli-exec-host:$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
14 changes: 14 additions & 0 deletions helmcharts/TestApp/keep_alive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# https://superuser.com/a/1299463
die_func() {
echo "Terminating"
exit 1
}

trap die_func TERM

echo "Sleeping..."
# restarts once a day
sleep 86400 &
wait
12 changes: 10 additions & 2 deletions helmcharts/TestApp/src/TestApp.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ class Program
{
static void Main(string[] args)
{
Thread.Sleep(30_000);
Console.WriteLine("Hello World!");
if(args.Length > 0 && args[0] == "say-hello")
{
Console.WriteLine("Hello world!");
}
else
{
Console.WriteLine("Running migrations...");
Thread.Sleep(30_000);
Console.WriteLine("Mmigrations complete!");
}
}
}
}

0 comments on commit 6921de1

Please sign in to comment.