Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[helm nats 1.x] add pod disruption budget #747

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions helm/charts/nats/files/pod-disruption-budget.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: {{ .Values.podDisruptionBudget.name }}
labels:
{{- include "nats.labels" $ | nindent 4 }}
spec:
maxUnavailable: 1
selector:
matchLabels:
{{- include "nats.selectorLabels" $ | nindent 6 }}
1 change: 1 addition & 0 deletions helm/charts/nats/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Set default values.
{{- $_ := set .natsBox.contextsSecret "name" (.natsBox.contextsSecret.name | default (printf "%s-box-contexts" $name)) }}
{{- $_ := set .natsBox.deployment "name" (.natsBox.deployment.name | default (printf "%s-box" $name)) }}
{{- $_ := set .natsBox.serviceAccount "name" (.natsBox.serviceAccount.name | default (printf "%s-box" $name)) }}
{{- $_ := set .podDisruptionBudget "name" (.podDisruptionBudget.name | default $name) }}
{{- $_ := set .service "name" (.service.name | default $name) }}
{{- $_ := set .serviceAccount "name" (.serviceAccount.name | default $name) }}
{{- $_ := set .statefulSet "name" (.statefulSet.name | default $name) }}
Expand Down
6 changes: 6 additions & 0 deletions helm/charts/nats/templates/pod-disruption-budget.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{- include "nats.defaultValues" . }}
{{- with .Values.podDisruptionBudget }}
{{- if .enabled }}
{{- include "nats.loadMergePatch" (merge (dict "file" "pod-disruption-budget.yaml" "ctx" $) .) }}
{{- end }}
{{- end }}
13 changes: 9 additions & 4 deletions helm/charts/nats/test/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
)

type Resources struct {
Expand All @@ -27,10 +28,11 @@ type Resources struct {
NatsBoxContextsSecret Resource[corev1.Secret]
NatsBoxDeployment Resource[appsv1.Deployment]
NatsBoxServiceAccount Resource[corev1.ServiceAccount]
PodDisruptionBudget Resource[policyv1.PodDisruptionBudget]
PodMonitor Resource[monitoringv1.PodMonitor]
Service Resource[corev1.Service]
ServiceAccount Resource[corev1.ServiceAccount]
StatefulSet Resource[appsv1.StatefulSet]
PodMonitor Resource[monitoringv1.PodMonitor]
ExtraConfigMap Resource[corev1.ConfigMap]
ExtraService Resource[corev1.Service]
}
Expand Down Expand Up @@ -109,6 +111,12 @@ func GenerateResources(fullName string) *Resources {
NatsBoxServiceAccount: Resource[corev1.ServiceAccount]{
ID: "ServiceAccount/" + fullName + "-box",
},
PodDisruptionBudget: Resource[policyv1.PodDisruptionBudget]{
ID: "PodDisruptionBudget/" + fullName,
},
PodMonitor: Resource[monitoringv1.PodMonitor]{
ID: "PodMonitor/" + fullName,
},
Service: Resource[corev1.Service]{
ID: "Service/" + fullName,
},
Expand All @@ -118,9 +126,6 @@ func GenerateResources(fullName string) *Resources {
StatefulSet: Resource[appsv1.StatefulSet]{
ID: "StatefulSet/" + fullName,
},
PodMonitor: Resource[monitoringv1.PodMonitor]{
ID: "PodMonitor/" + fullName,
},
ExtraConfigMap: Resource[corev1.ConfigMap]{
ID: "ConfigMap/" + fullName + "-extra",
},
Expand Down
73 changes: 47 additions & 26 deletions helm/charts/nats/test/defaults_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package test

import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
policyv1 "k8s.io/api/policy/v1"
"sync"
"testing"

monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -332,6 +333,50 @@ exec sh -ec "$0"
},
},
},
PodDisruptionBudget: Resource[policyv1.PodDisruptionBudget]{
ID: dr.PodDisruptionBudget.ID,
HasValue: true,
Value: policyv1.PodDisruptionBudget{
TypeMeta: v1.TypeMeta{
Kind: "PodDisruptionBudget",
APIVersion: "policy/v1",
},
ObjectMeta: v1.ObjectMeta{
Name: fullName,
Labels: natsLabels(),
},
Spec: policyv1.PodDisruptionBudgetSpec{
MaxUnavailable: &intstr.IntOrString{IntVal: 1},
Selector: &v1.LabelSelector{
MatchLabels: natsSelectorLabels(),
},
},
},
},
PodMonitor: Resource[monitoringv1.PodMonitor]{
ID: dr.PodMonitor.ID,
HasValue: false,
Value: monitoringv1.PodMonitor{
TypeMeta: v1.TypeMeta{
Kind: "PodMonitor",
APIVersion: "monitoring.coreos.com/v1",
},
ObjectMeta: v1.ObjectMeta{
Name: fullName,
Labels: natsLabels(),
},
Spec: monitoringv1.PodMonitorSpec{
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{
Port: "prom-metrics",
},
},
Selector: v1.LabelSelector{
MatchLabels: natsSelectorLabels(),
},
},
},
},
Service: Resource[corev1.Service]{
ID: dr.Service.ID,
HasValue: true,
Expand Down Expand Up @@ -408,7 +453,7 @@ exec sh -ec "$0"
},
},
{
Name: "SERVER_NAME",
Name: "SERVER_NAME",
Value: "$(POD_NAME)",
},
},
Expand Down Expand Up @@ -532,30 +577,6 @@ exec sh -ec "$0"
},
},
},
PodMonitor: Resource[monitoringv1.PodMonitor]{
ID: dr.PodMonitor.ID,
HasValue: false,
Value: monitoringv1.PodMonitor{
TypeMeta: v1.TypeMeta{
Kind: "PodMonitor",
APIVersion: "monitoring.coreos.com/v1",
},
ObjectMeta: v1.ObjectMeta{
Name: fullName,
Labels: natsLabels(),
},
Spec: monitoringv1.PodMonitorSpec{
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{
{
Port: "prom-metrics",
},
},
Selector: v1.LabelSelector{
MatchLabels: natsSelectorLabels(),
},
},
},
},
ExtraConfigMap: Resource[corev1.ConfigMap]{
ID: dr.ExtraConfigMap.ID,
HasValue: false,
Expand Down
9 changes: 9 additions & 0 deletions helm/charts/nats/test/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ configMap:
annotations:
test: test
patch: [{op: add, path: /metadata/labels/test, value: "test"}]
podDisruptionBudget:
merge:
metadata:
annotations:
test: test
patch: [{op: add, path: /metadata/labels/test, value: "test"}]
serviceAccount:
enabled: true
merge:
Expand Down Expand Up @@ -566,6 +572,9 @@ natsBox:
expected.Service.Value.ObjectMeta.Annotations = annotations()
expected.Service.Value.ObjectMeta.Labels["test"] = "test"

expected.PodDisruptionBudget.Value.ObjectMeta.Annotations = annotations()
expected.PodDisruptionBudget.Value.ObjectMeta.Labels["test"] = "test"

expected.ServiceAccount.HasValue = true
expected.ServiceAccount.Value.ObjectMeta.Annotations = annotations()
expected.ServiceAccount.Value.ObjectMeta.Labels["test"] = "test"
Expand Down
10 changes: 10 additions & 0 deletions helm/charts/nats/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@ configMap:
# defaults to "{{ include "nats.fullname" $ }}-config"
name:

# pod disruption budget
podDisruptionBudget:
enabled: true
# merge or patch the pod disruption budget
# https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#poddisruptionbudget-v1-policy
merge: {}
patch: []
# defaults to "{{ include "nats.fullname" $ }}"
name:

# service account
serviceAccount:
enabled: false
Expand Down