Skip to content

Commit

Permalink
Helm chart refactoring & automation (#31)
Browse files Browse the repository at this point in the history
* remove test data

* Create helm chart using the suggested structure from helm3

* Fix minor naming consistency in Dockerfile

* Move skaffold to use helm chart

* improve skaffold configuration

* Update chart name to use the naming convenction

* update sample path

* Update contribution guideline

* Add helm chart validation rules

* Add chart home since is a required field

* Add linting action for helm charts

* Add fixes to chart definition

* fix timeout duration

* Update kind cluster

* test CI with minikube

* Add MetalLB to test load balancer feature

* Publish chart when merged on master

* test publishing chart with fake tag

* move charts dir

* finalize charts publishing CI

* reformat skaffold
  • Loading branch information
mavimo authored Jun 20, 2020
1 parent 2e6dc79 commit 20b498f
Show file tree
Hide file tree
Showing 35 changed files with 617 additions and 773 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/dockerimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ name: Docker Image CI

on:
push:
branches: [ master ]
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build and push Docker images
uses: docker/build-push-action@v1.1.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: caddy/ingress
tag_with_ref: true
tag_with_sha: true
- uses: actions/checkout@v2
- name: Build and push Docker images
uses: docker/build-push-action@v1.1.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: caddy/ingress
tag_with_ref: true
tag_with_sha: true
24 changes: 24 additions & 0 deletions .github/workflows/helmchart-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Release Charts

on:
push:
branches: [master]

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1

- name: Configure Git
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
- name: Run chart-releaser
uses: helm/chart-releaser-action@master
with:
charts_dir: charts
env:
CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
54 changes: 54 additions & 0 deletions .github/workflows/helmchart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Lint and Test Charts

on: pull_request

jobs:
lint-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Fetch history
run: git fetch --prune --unshallow

- name: Run chart-testing (lint)
id: lint
uses: helm/chart-testing-action@v1.0.0-rc.1
with:
image: quay.io/helmpack/chart-testing:v3.0.0-rc.1
command: lint

- name: Create kind cluster
uses: helm/kind-action@v1.0.0-rc.1
with:
version: "v0.8.1"
# Only build a kind cluster if there are chart changes to test.
if: steps.lint.outputs.changed == 'true'

- name: Install MetalLB to allow LoadBalancer services
run: |
kubectl create ns metallb-system
kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ConfigMap
metadata:
name: config
namespace: metallb-system
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 172.17.255.1-172.17.255.200
EOF
if: steps.lint.outputs.changed == 'true'

- name: Run chart-testing (install)
uses: helm/chart-testing-action@v1.0.0-rc.1
with:
image: quay.io/helmpack/chart-testing:v3.0.0-rc.1
command: install
56 changes: 31 additions & 25 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
## Requirements

We will explain how to contribute to this project using a linux machine, in order to be able ot easly contribute you need:
- A running kubernetes cluster (if you don't have one see *Setup a local cluster* section)
- [helm 3](https://helm.sh/) installed on your machine
- [skaffold](https://skaffold.dev/) installed on your machine

- A machine with a public IP in order to use let's encrypt (you can provision ad-hoc machine on any clud provider you use)
- A domain that redirect to server IP
- [kind](https://github.com/kubernetes-sigs/kind) (to create a development cluster)
- [skaffold](https://skaffold.dev/) (to improve development experience)
- [Docker HUB](https://hub.docker.com) account (to store your docker images)
### Setup a local cluster

## Setup a development cluster
- You need a machine with [docker](https://docker.io) up & running
- You need to install [kind](https://kind.sigs.k8s.io/) on your machine

We create a three node cluster (master plus two worker), we start to setup the configuration:
Than we can create a two nodes cluster (one master and one worker):

```bash
cat <<EOF >> cluster.yml
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
```
than we create the cluster
```bash
kind create cluster --config=cluster.yml
```
and activate the `kubectl` config via:
```
kind export kubeconfig
```

## Configure your docker credentials

Authenticate your docker intance:
```
docker login
```

## Setup development env

Replace the docker image you are going to use in `kubernetes/generated/deployment.yaml` and `skaffold.yaml` replacing `MYACCOUNT` with your Docker Hub account in `docker.io/MYACCOUNT/caddy-ingress-controller`
Replace also the domain name to use in `hack/test/example-ingress.yaml` from `kubernetes.localhost` to your domain (ensure also that the subdomain `example1` and `example2` are resolved to the server public IP)

Replace also the domain name to use in `hack/test/example-ingress.yaml` from `MYDOMAIN.TDL` to your domain (ensore also that the subdomain `example1` and `example2` are resolved to the server public IP)
Create a namespace to host the caddy ingress controller:
```
kubectl create ns caddy-system
```

Than we can start skaffold using:
```
skaffold dev --port-forward
```

this will automatically:

- build your docker image every time you change some code
- update kubernetes config every time you change some file
- expose the caddy ingress controller (port 80 and 443) on publc server
- update the helm release every time you change the helm chart
- expose the caddy ingress controller (port 8080 and 8443)

You can test that all work as expected with:
```
curl -H 'Host: example1.kubernetes.localhost http://127.0.0.1:80/hello1
curl -H 'Host: example1.kubernetes.localhost http://127.0.0.1:80/hello2
curl -H 'Host: example2.kubernetes.localhost http://127.0.0.1:80/hello1
curl -H 'Host: example2.kubernetes.localhost http://127.0.0.1:80/hello2
```

## Notes

- You can change local port forwarded by skaffold by changing the port values in the `skaffold.yaml` file on section `portForward` `localPort`. Remind that you can forward only port greather than 1024 if you execute it as non root user
- You can delete your local cluster with the command `kind delete cluster`
- To use TLS your domain should be publically resolved to your cluster IP in order to allow Let's Encript to validate the domain
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ COPY ./internal ./internal

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/ingress-controller ./cmd/caddy

FROM alpine:latest as certs
FROM alpine:latest AS certs
RUN apk --update add ca-certificates

FROM scratch
COPY --from=builder /app/bin/ingress-controller .
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
EXPOSE 80 443
ENTRYPOINT ["/ingress-controller"]
ENTRYPOINT ["/ingress-controller"]
23 changes: 23 additions & 0 deletions charts/caddy-ingress-controller/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
15 changes: 15 additions & 0 deletions charts/caddy-ingress-controller/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v2
name: caddy-ingress-controller
home: https://github.com/caddyserver/ingress
description: A helm chart for the Caddy Kubernetes ingress controller
type: application
version: 0.0.1-rc1
appVersion: v0.1.0
keywords:
- ingress-controller
- caddyserver
sources:
- https://github.com/caddyserver/ingress
maintainers:
- name: mavimo
url: https://github.com/mavimo
63 changes: 63 additions & 0 deletions charts/caddy-ingress-controller/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "caddy-ingress-controller.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 "caddy-ingress-controller.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 "caddy-ingress-controller.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "caddy-ingress-controller.labels" -}}
helm.sh/chart: {{ include "caddy-ingress-controller.chart" . }}
{{ include "caddy-ingress-controller.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "caddy-ingress-controller.selectorLabels" -}}
app.kubernetes.io/name: {{ include "caddy-ingress-controller.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "caddy-ingress-controller.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "caddy-ingress-controller.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{{- if .Values.caddyingresscontroller.rbac.create }}
{{- if .Values.ingressController.rbac.create }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: {{ .Values.name }}-role
name: {{ include "caddy-ingress-controller.name" . }}-role
namespace: {{ .Release.Namespace }}
rules:
- apiGroups:
Expand All @@ -25,4 +25,4 @@ rules:
- list
- get
- watch
{{- end }}
{{- end }}
15 changes: 15 additions & 0 deletions charts/caddy-ingress-controller/templates/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{- if .Values.ingressController.rbac.create }}
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ include "caddy-ingress-controller.name" . }}-role-binding
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ include "caddy-ingress-controller.name" . }}-role
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: {{ include "caddy-ingress-controller.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
{{- end }}
Loading

0 comments on commit 20b498f

Please sign in to comment.