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

Enhanced the affinity for opensearch-cluster to have a default podAntiAffinity #927

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions charts/opensearch-cluster/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,62 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Default pod antiAffinity to nodePool component if no affinity rules defined
This rule helps the nodePool replicas to schedule on different nodes
*/}}
{{- define "nodePools.defaultAffinity" -}}
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: opster.io/opensearch-nodepool
operator: In
values:
- {{ .nodePool.component }}
topologyKey: kubernetes.io/hostname
{{- end }}

{{/*
Takes the pod affinity rules from each nodePool and appends the default podAntiAffinity
*/}}
{{- define "nodePools.affinity" -}}
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: opster.io/opensearch-nodepool
operator: In
values:
- {{ .nodePool.component }}
topologyKey: kubernetes.io/hostname

{{- $nodePool := .nodePool.affinity -}}

{{- /* checks if preferredDuringSchedulingIgnoredDuringExecution exists under podAntiAffinity and appending */ -}}
{{- if not (empty $nodePool.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution) }}
{{ $nodePool.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution | toYaml | nindent 4 | trim }}
{{- end }}

{{- /* checks if requiredDuringSchedulingIgnoredDuringExecution exists under podAntiAffinity and appending */ -}}
{{- if $nodePool.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
requiredDuringSchedulingIgnoredDuringExecution: {{ $nodePool.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution | toYaml | nindent 4 }}
{{- end }}

{{- /* checks if podAffinity exists in affinity and appending */ -}}
{{- if $nodePool.podAffinity }}
podAffinity: {{ $nodePool.podAffinity | toYaml | nindent 4 }}
{{- end }}

{{- /* checks if nodeAffinity exists in affinity and appending */ -}}
{{- if $nodePool.nodeAffinity }}
nodeAffinity: {{ $nodePool.nodeAffinity | toYaml | nindent 4 }}
{{- end }}
{{- end }}
10 changes: 8 additions & 2 deletions charts/opensearch-cluster/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ spec:
{{- with .Values.cluster.initHelper }}
initHelper: {{ . | toYaml | nindent 4 }}
{{- end }}
{{- with .Values.cluster.nodePools }}
nodePools: {{ . | toYaml | nindent 4 }}
nodePools:
{{- range .Values.cluster.nodePools }}
- {{ omit . "affinity" | toYaml | nindent 4 | trim }}
{{- if .affinity }}
{{ include "nodePools.affinity" (dict "nodePool" .) | nindent 4 | trim }}
{{- else }}
{{ include "nodePools.defaultAffinity" (dict "nodePool" .) | nindent 4 | trim }}
{{- end }}
{{- end }}
{{- with .Values.cluster.security }}
security:
Expand Down
3 changes: 3 additions & 0 deletions charts/opensearch-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ cluster:
limits:
memory: "2Gi"
cpu: "500m"
# -- Optional, default podAntiAffinity is added to schedule the nodePool replicas on different nodes
# additional affinity rules defined here will be appended with the default podAntiAffinity
affinity: {}

# Opensearch security configuration
security:
Expand Down