Skip to content

Commit 61716bd

Browse files
authored
[oauth2-proxy/PDB] Add Kubernetes 1.21+ support & fix broken selector labels (#66)
* feat: update PDB to support Kubernetes 1.21+ Starting Kubernetes 1.21, the PDB feature went stable * fix: rework and fix broken PDB selector labels It appears to have broken a long time ago and is related to 0ea4dc4 and cbd5275 * chore: fixup invert order for backward compat without changes by the end users * chore: bump chart version * refactor: rework Ingress to support custom kubeVersion + refact * chore: fixup
1 parent 7089e0e commit 61716bd

File tree

7 files changed

+84
-28
lines changed

7 files changed

+84
-28
lines changed

helm/oauth2-proxy/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: oauth2-proxy
2-
version: 5.0.2
2+
version: 5.0.3
33
apiVersion: v2
44
appVersion: 7.2.0
55
home: https://oauth2-proxy.github.io/oauth2-proxy/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{/*
2+
Returns the appropriate apiVersion for podDisruptionBudget object.
3+
*/}}
4+
{{- define "capabilities.podDisruptionBudget.apiVersion" -}}
5+
{{- if semverCompare ">=1.21-0" ( .Values.kubeVersion | default .Capabilities.KubeVersion.Version ) -}}
6+
{{- print "policy/v1" -}}
7+
{{- else -}}
8+
{{- print "policy/v1beta1" -}}
9+
{{- end -}}
10+
{{- end -}}
11+
12+
{{/*
13+
Return the appropriate apiVersion for ingress object.
14+
*/}}
15+
{{- define "capabilities.ingress.apiVersion" -}}
16+
{{- if semverCompare "<1.14-0" ( .Values.kubeVersion | default .Capabilities.KubeVersion.Version ) -}}
17+
{{- print "extensions/v1beta1" -}}
18+
{{- else if semverCompare "<1.19-0" ( .Values.kubeVersion | default .Capabilities.KubeVersion.Version ) -}}
19+
{{- print "networking.k8s.io/v1beta1" -}}
20+
{{- else -}}
21+
{{- print "networking.k8s.io/v1" -}}
22+
{{- end -}}
23+
{{- end -}}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{{/*
2+
Returns `true` if the API `ingressClassName` field is supported and `false` otherwise
3+
*/}}
4+
{{- define "ingress.supportsIngressClassName" -}}
5+
{{- if ( semverCompare "<1.18-0" ( .Values.kubeVersion | default .Capabilities.KubeVersion.Version ) ) -}}
6+
{{- print "false" -}}
7+
{{- else -}}
8+
{{- print "true" -}}
9+
{{- end -}}
10+
{{- end -}}
11+
12+
{{/*
13+
Returns `true` if the API `pathType` field is supported and `false` otherwise
14+
*/}}
15+
{{- define "ingress.supportsPathType" -}}
16+
{{- if ( semverCompare "<1.18-0" ( .Values.kubeVersion | default .Capabilities.KubeVersion.Version ) ) -}}
17+
{{- print "false" -}}
18+
{{- else -}}
19+
{{- print "true" -}}
20+
{{- end -}}
21+
{{- end -}}
22+
23+
{{/*
24+
Returns the appropriate ingress `backend` fields depending on the Kubernetes API version.
25+
e.g.: `{{ include "common.ingress.backend" (dict "serviceName" "backendName" "servicePort" "backendPort" "context" $) }}`
26+
Where the dict must contain the following entries:
27+
- `serviceName` {String} - Name of an existing service backend
28+
- `servicePort` {String|Number} - Port name or port number of the service.
29+
- `context` {Dict} - (Parent) Context for the template evaluation required for the API version detection.
30+
*/}}
31+
{{- define "ingress.backend" -}}
32+
{{- $apiVersion := ( include "capabilities.ingress.apiVersion" .context ) -}}
33+
{{- if or ( eq $apiVersion "extensions/v1beta1" ) ( eq $apiVersion "networking.k8s.io/v1beta1" ) -}}
34+
serviceName: {{ .serviceName }}
35+
servicePort: {{ .servicePort }}
36+
{{- else -}}
37+
service:
38+
name: {{ .serviceName }}
39+
port:
40+
{{- if typeIs "string" .servicePort }}
41+
name: {{ .servicePort }}
42+
{{- else if or ( typeIs "int" .servicePort ) ( typeIs "float64" .servicePort ) }}
43+
number: {{ .servicePort }}
44+
{{- end }}
45+
{{- end -}}
46+
{{- end -}}

helm/oauth2-proxy/templates/deprecation.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{{- if .Values.service.port }}
33
{{ fail "`service.port` does no longer exist. It has been renamed to `service.portNumber`" }}
44
{{- end }}
5-
{{- if .Capabilities.APIVersions.Has "networking.k8s.io/v1" -}}
5+
{{- if eq ( include "capabilities.ingress.apiVersion" . ) "networking.k8s.io/v1" -}}
66
{{- range .Values.ingress.extraPaths }}
77
{{- if or (.backend.serviceName) (.backend.servicePort) }}
88
{{ fail "Please update the format of your `ingress.extraPaths` to the new ingress apiVersion `networking.k8s.io/v1` format" }}

helm/oauth2-proxy/templates/ingress.yaml

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@
44
{{- $ingressPath := .Values.ingress.path -}}
55
{{- $ingressPathType := .Values.ingress.pathType -}}
66
{{- $extraPaths := .Values.ingress.extraPaths -}}
7-
{{- $apiV1 := false -}}
8-
{{- if and (.Capabilities.APIVersions.Has "networking.k8s.io/v1") (semverCompare ">= v1.19.0-0" .Capabilities.KubeVersion.Version) -}}
9-
apiVersion: networking.k8s.io/v1
10-
{{- $apiV1 = true -}}
11-
{{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1beta1" -}}
12-
apiVersion: networking.k8s.io/v1beta1
13-
{{- else -}}
14-
apiVersion: extensions/v1beta1
15-
{{- end }}
7+
apiVersion: {{ include "capabilities.ingress.apiVersion" . }}
168
kind: Ingress
179
metadata:
1810
labels:
@@ -24,8 +16,8 @@ metadata:
2416
{{ toYaml . | indent 4 }}
2517
{{- end }}
2618
spec:
27-
{{- if .Values.ingress.className }}
28-
ingressClassName: {{ .Values.ingress.className }}
19+
{{- if and .Values.ingress.className ( eq "true" ( include "ingress.supportsIngressClassName" . ) ) }}
20+
ingressClassName: {{ .Values.ingress.className | quote }}
2921
{{- end }}
3022
rules:
3123
{{- range $host := .Values.ingress.hosts }}
@@ -35,20 +27,11 @@ spec:
3527
{{- if $extraPaths }}
3628
{{ toYaml $extraPaths | indent 10 }}
3729
{{- end }}
38-
{{- if $apiV1 }}
3930
- path: {{ $ingressPath }}
31+
{{- if eq "true" ( include "ingress.supportsPathType" $ ) }}
4032
pathType: {{ $ingressPathType }}
41-
backend:
42-
service:
43-
name: {{ $serviceName }}
44-
port:
45-
number: {{ $servicePort }}
46-
{{- else }}
47-
- path: {{ $ingressPath }}
48-
backend:
49-
serviceName: {{ $serviceName }}
50-
servicePort: {{ $servicePort }}
51-
{{- end }}
33+
{{- end }}
34+
backend: {{- include "ingress.backend" ( dict "serviceName" $serviceName "servicePort" $servicePort "context" $ ) | nindent 14 }}
5235
{{- end -}}
5336
{{- if .Values.ingress.tls }}
5437
tls:
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{- if and .Values.podDisruptionBudget.enabled (gt (.Values.replicaCount | int) 1) }}
2-
apiVersion: policy/v1beta1
2+
apiVersion: {{ include "capabilities.podDisruptionBudget.apiVersion" . }}
33
kind: PodDisruptionBudget
44
metadata:
55
labels:
@@ -9,7 +9,6 @@ metadata:
99
spec:
1010
selector:
1111
matchLabels:
12-
app: {{ template "oauth2-proxy.name" . }}
13-
release: {{ .Release.Name }}
12+
{{- include "oauth2-proxy.selectorLabels" . | indent 6 }}
1413
minAvailable: {{ .Values.podDisruptionBudget.minAvailable }}
1514
{{- end }}

helm/oauth2-proxy/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Force the target Kubernetes version (it uses Helm `.Capabilities` if not set).
2+
# This is especially useful for `helm template` as capabilities are always empty
3+
# due to the fact that it doesn't query an actual cluster
4+
kubeVersion:
5+
16
# Oauth client configuration specifics
27
config:
38
# Add config annotations

0 commit comments

Comments
 (0)