Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat: support multiple services (#10)
Browse files Browse the repository at this point in the history
- Support multiple services
- Warning about migration of a single service port to multiple service
ports
- Change `appIngressroute` to `appingressroutes`
- Update linter, add unit test
- Support `helm status` get access address

---------

Co-authored-by: Phil Chen <[email protected]>
  • Loading branch information
YkeLit and fahchen authored Mar 11, 2024
1 parent e0aa0a0 commit e6fb782
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .github/linters/ct.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ charts:
- .
namespace: default
release-label: app.kubernetes.io/namespace
additional-commands:
- helm unittest --strict .
4 changes: 2 additions & 2 deletions .github/workflows/lint-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand All @@ -27,7 +27,7 @@ jobs:
- name: install helm unittest plugin
run: |
helm env
helm plugin install https://github.com/quintush/helm-unittest.git --version 0.2.8
helm plugin install https://github.com/helm-unittest/helm-unittest.git
- name: Run chart-testing (lint)
run: ct lint --config .github/linters/ct.yaml
Expand Down
2 changes: 1 addition & 1 deletion Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: A Helm chart template for byzanteam application
# chart 类型
type: application
# chart 版本
version: 1.3.1
version: 1.4.0
# 项目源码的URL列表
sources:
- https://github.com/Byzanteam/application-chart-template/
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# application-chart-template

![Version: 1.1.0](https://img.shields.io/badge/Version-1.1.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.0.0](https://img.shields.io/badge/AppVersion-1.0.0-informational?style=flat-square)
![Version: 1.4.0](https://img.shields.io/badge/Version-1.4.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.0.0](https://img.shields.io/badge/AppVersion-1.0.0-informational?style=flat-square)

A Helm chart template for byzanteam application

Expand All @@ -18,7 +18,7 @@ A Helm chart template for byzanteam application

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| appIngressroute | list | `[]` | |
| appIngressroutes | list | `[]` | |
| applicationHosts | list | `[]` | |
| applicationTLS | object | `{}` | |
| corsSettings | object | `{}` | |
Expand All @@ -36,8 +36,9 @@ A Helm chart template for byzanteam application
| nameOverride | string | `""` | |
| replicaCount | int | `1` | |
| restartPolicy | string | `"Always"` | |
| service.name | string | `"http"` | |
| service.port | int | `80` | |
| service.ports[].name | string | `"http"` | |
| service.ports[].port | int | `80` | |
| service.ports[].protocol | string | `TCP` | |
| service.type | string | `"ClusterIP"` | |
| volumeMounts | list | `[]` | |
| volumes | list | `[]` | |
Expand Down Expand Up @@ -73,11 +74,13 @@ applicationHosts:
```
### 4. 设置反向代理对应 path 规则
```yaml
appIngressroute:
appIngressroutes:
- name: example-name
path: /example-path
servicePortName: 0
```
### 5. 设置外部服务代理规则
```yaml
externalIngressroute:
- name: "external-name"
Expand Down
21 changes: 21 additions & 0 deletions templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ include "application-chart-template.fullname" . }} has been Installed!
{{- if .Values.appIngressroute }}
Use the link below to access the app:
{{- range $host := .Values.applicationHosts }}
{{- range $ingress := $.Values.appIngressroute }}
{{ empty $.Values.applicationTLS | ternary "http" "https" }}://{{ $host }}{{ $ingress.path }}
{{- end }}
{{- end }}
Warnig: Migrate the `.Values.appIngressroute` variable to `.Values.appIngressroutes`
{{- end }}
{{- if .Values.appIngressroutes }}
Use the link below to access the app:
{{- range $host := .Values.applicationHosts }}
{{- range $ingress := $.Values.appIngressroute }}
{{ empty $.Values.applicationTLS | ternary "http" "https" }}://{{ $host }}{{ $ingress.path }}
{{- end }}
{{- end }}
{{- end }}
{{- if .Values.service.port }}
Warnig: Migrate the `.Values.service.port` (object) variable to `.Values.service.ports` (list)
{{- end }}
13 changes: 0 additions & 13 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ Return the imagePullSecret
{{- end }}
{{- end }}

{{/*
Host for access rule
*/}}
{{- define "application-chart-template.applicationHosts" -}}
{{- $hosts := .Values.applicationHosts }}
{{- $orOperator := " || " }}
{{- $ruleHosts := list }}
{{- range $_, $host := $hosts }}
{{- $ruleHosts = append $ruleHosts (printf "Host(`%s`)" $host) }}
{{- end }}
{{- printf "(%s)" (join $orOperator $ruleHosts) }}
{{- end }}

{/*
Build secret keys
*/}
Expand Down
58 changes: 58 additions & 0 deletions templates/_ingressroute.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{/*
Host for access rule
*/}}
{{- define "application-chart-template.applicationHosts" -}}
{{- $hosts := .Values.applicationHosts }}
{{- $orOperator := " || " }}
{{- $ruleHosts := list }}
{{- range $_, $host := $hosts }}
{{- $ruleHosts = append $ruleHosts (printf "Host(`%s`)" $host) }}
{{- end }}
{{- printf "(%s)" (join $orOperator $ruleHosts) }}
{{- end }}

{{/*
Middlewares for web ingress-routes
*/}}
{{- define "application-chart-template.webMiddlewares" -}}
{{- $webMiddlewares := list "compress" "strip-prefix" }}
{{- if $.Values.applicationTLS }}
{{- $webMiddlewares = append $webMiddlewares "redirect-to-https" }}
{{- end }}
{{- if $.Values.corsSettings }}
{{- $webMiddlewares = append $webMiddlewares "cors" }}
{{- end }}
{{- range $webMiddlewares }}
- name: {{ include "application-chart-template.fullname" $ }}-{{ . }}
{{- end }}
{{- end }}

{{/*
Middlewares for websecure ingress-routes
*/}}
{{- define "application-chart-template.websecureMiddlewares" -}}
{{- $websecureMiddlewares := list "compress" "strip-prefix" }}
{{- if $.Values.corsSettings }}
{{- $websecureMiddlewares = append $websecureMiddlewares "cors" }}
{{- end }}
{{- range $websecureMiddlewares }}
- name: {{ include "application-chart-template.fullname" $ }}-{{ . }}
{{- end }}
{{- end }}

{{/*
Renders the ingress service port value
*/}}
{{- define "application-chart-template.ingressServicePort" -}}
{{- $portName := "" -}}
{{- range $port := .ports -}}
{{- if eq $.servicePortName $port.name -}}
{{- $portName = $.servicePortName -}}
{{- end -}}
{{- end -}}
{{- if $portName -}}
{{- printf "%s" $portName -}}
{{- else -}}
{{- fail "The same service port name could not be found!" -}}
{{- end -}}
{{- end -}}
10 changes: 10 additions & 0 deletions templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ spec:
{{ .Values.command | toYaml | nindent 12 }}
{{- end }}
ports:
# TODO: 1. Update multiple service ports. Input multiple service ports using list.
# 2. Delete this after the version 2.0.0
{{- if .Values.service.port }}
- name: {{ .Values.service.name }}
containerPort: {{ .Values.service.port }}
protocol: TCP
{{- else }}
{{- range .Values.service.ports }}
- name: {{ .name }}
containerPort: {{ .port }}
protocol: {{ .protocol }}
{{- end }}
{{- end }}
volumeMounts:
{{- .Values.volumeMounts | toYaml | nindent 12 }}
restartPolicy: {{ .Values.restartPolicy }}
Expand Down
13 changes: 13 additions & 0 deletions templates/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ metadata:
{{- include "application-chart-template.labels" . | nindent 4 }}
namespace: {{ .Release.Namespace }}
spec:
# TODO: 1. Update multiple service ports. Input multiple service ports using list.
# 2. Delete this after the version 2.0.0
{{- if .Values.service.port }}
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.name }}
protocol: TCP
name: {{ .Values.service.name }}
{{- else }}
type: {{ .Values.service.type }}
ports:
{{- range .Values.service.ports }}
- port: {{ .port }}
targetPort: {{ .name }}
protocol: {{ .protocol }}
name: {{ .name }}
{{- end }}
{{- end }}
selector:
{{- include "application-chart-template.selectorLabels" . | nindent 4 }}
121 changes: 100 additions & 21 deletions templates/traefik/ingress-route.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
# TODO: 1. Change `appIngressroute` to `appIngressroutes`
# 2. Delete this after the version 2.0.0
{{- range .Values.appIngressroute }}
---
{{/* Middlewares for web ingress-routes */}}
{{- $webMiddlewares := list "compress" "strip-prefix" }}
{{- if $.Values.applicationTLS | empty | not }}
{{- $webMiddlewares = append $webMiddlewares "redirect-to-https" }}
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: {{ include "application-chart-template.fullname" $ }}-{{ .name }}-web
namespace: {{ $.Values.namespace }}
spec:
entryPoints:
- web
routes:
# TODO: 1. Update multiple service ports. Input multiple service ports using list.
# 2. Delete this after the version 2.0.0
{{- if $.Values.service.port }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ $.Values.service.port }}
middlewares:
{{- include "application-chart-template.webMiddlewares" $ | nindent 8}}
{{- else }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ include "application-chart-template.ingressServicePort" (dict "ports" $.Values.service.ports "servicePortName" .servicePortName) }}
middlewares:
{{- include "application-chart-template.webMiddlewares" $ | nindent 8}}
{{- end }}

---
{{- if $.Values.applicationTLS }}

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: {{ include "application-chart-template.fullname" $ }}-{{ .name }}-websecure
namespace: {{ $.Values.namespace }}
spec:
entryPoints:
- websecure
routes:
# TODO: 1. Update multiple service ports. Input multiple service ports using list.
# 2. Delete this after the version 2.0.0
{{- if $.Values.service.port }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ $.Values.service.port }}
middlewares:
{{- include "application-chart-template.websecureMiddlewares" $ | nindent 8}}
{{- else }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ include "application-chart-template.ingressServicePort" (dict "ports" $.Values.service.ports "servicePortName" .servicePortName) }}
middlewares:
{{- include "application-chart-template.websecureMiddlewares" $ | nindent 8}}
{{- end }}
tls:
{{- if $.Values.applicationTLS.certResolver }}
certResolver: {{ $.Values.applicationTLS.certResolver }}
{{- else if $.Values.applicationTLS.TLSSecret }}
secretName: {{ include "application-chart-template.fullname" $ }}-tls-secret
{{- end }}
{{- end }}
{{- if $.Values.corsSettings | empty | not }}
{{- $webMiddlewares = append $webMiddlewares "cors" }}
{{- end }}

{{- range .Values.appIngressroutes }}
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
Expand All @@ -18,23 +82,28 @@ spec:
entryPoints:
- web
routes:
# TODO: 1. Update multiple service ports. Input multiple service ports using list.
# 2. Delete this after the version 2.0.0
{{- if $.Values.service.port }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ $.Values.service.port }}
middlewares:
{{- range $webMiddlewares }}
- name: {{ include "application-chart-template.fullname" $ }}-{{ . }}
{{- end }}
---
{{- if $.Values.applicationTLS | empty | not }}
{{- include "application-chart-template.webMiddlewares" $ | nindent 8}}
{{- else }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ include "application-chart-template.ingressServicePort" (dict "ports" $.Values.service.ports "servicePortName" .servicePortName) }}
middlewares:
{{- include "application-chart-template.webMiddlewares" $ | nindent 8}}
{{- end }}

{{/* Middlewares for websecure ingress-routes */}}
{{- $websecureMiddlewares := list "compress" "strip-prefix" }}
{{- if $.Values.corsSettings | empty | not }}
{{- $websecureMiddlewares = append $websecureMiddlewares "cors" }}
{{- end }}
---
{{- if $.Values.applicationTLS }}

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
Expand All @@ -45,19 +114,29 @@ spec:
entryPoints:
- websecure
routes:
# TODO: 1. Update multiple service ports. Input multiple service ports using list.
# 2. Delete this after the version 2.0.0
{{- if $.Values.service.port }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ $.Values.service.port }}
middlewares:
{{- range $websecureMiddlewares }}
- name: {{ include "application-chart-template.fullname" $ }}-{{ . }}
{{- end }}
{{- include "application-chart-template.websecureMiddlewares" $ | nindent 8}}
{{- else }}
- match: {{ include "application-chart-template.applicationHosts" $ }} && PathPrefix(`{{ .path }}`)
kind: Rule
services:
- name: {{ include "application-chart-template.fullname" $ }}
port: {{ include "application-chart-template.ingressServicePort" (dict "ports" $.Values.service.ports "servicePortName" .servicePortName) }}
middlewares:
{{- include "application-chart-template.websecureMiddlewares" $ | nindent 8}}
{{- end }}
tls:
{{- if $.Values.applicationTLS.certResolver | empty | not }}
{{- if $.Values.applicationTLS.certResolver }}
certResolver: {{ $.Values.applicationTLS.certResolver }}
{{- else if $.Values.applicationTLS.TLSSecret | empty | not }}
{{- else if $.Values.applicationTLS.TLSSecret }}
secretName: {{ include "application-chart-template.fullname" $ }}-tls-secret
{{- end }}
{{- end }}
Expand Down
Loading

0 comments on commit e6fb782

Please sign in to comment.