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

feat: add global affinity option #3462

Merged
merged 14 commits into from
Dec 21, 2023
1 change: 1 addition & 0 deletions deploy/helm/sumologic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ The following table lists the configurable parameters of the Sumo Logic chart an
| `sumologic.podAnnotations` | Additional annotations for the pods. | `{}` |
| `sumologic.nodeSelector` | Default node selector for Kubernetes objects created by this chart. This can be overriden for specific components. | `{}` |
| `sumologic.tolerations` | Default tolerations for Kubernetes objects created by this chart. This can be overriden for specific components. | `[]` |
| `sumologic.affinity` | Default affinity for Kubernetes objects created by this chart. This can be overriden for specific components. | `[]` |
| `sumologic.scc.create` | Create OpenShift's Security Context Constraint | `false` |
| `sumologic.serviceAccount.annotations` | Add custom annotations to sumologic serviceAccounts | `{}` |
| `sumologic.setup.job.pullSecrets` | Optional list of secrets that will be used for pulling images for Sumo Logic's setup job. | `Nil` |
Expand Down
21 changes: 21 additions & 0 deletions deploy/helm/sumologic/templates/_helpers/_common.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ Example:
{{- end -}}
{{- end -}}

{{/*
Returns default affinity for all objects.

Example:

{{ include "kubernetes.defaultAffinity" . }}
*/}}
{{- define "kubernetes.defaultAffinity" -}}
{{- if .Values.sumologic.affinity -}}
{{- toYaml .Values.sumologic.affinity -}}
{{- end -}}
{{- end -}}

{{/*
Environment variables used to configure the HTTP proxy for programs using
Go's net/http. See: https://pkg.go.dev/net/http#RoundTripper
Expand Down Expand Up @@ -585,3 +598,11 @@ Example usage:
{{- template "kubernetes.defaultTolerations" . -}}
{{- end -}}
{{- end -}}

{{- define "pvcCleaner.job.affinity" -}}
{{- if .Values.pvcCleaner.job.affinity -}}
{{- toYaml .Values.pvcCleaner.job.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultAffinity" . -}}
{{- end -}}
{{- end -}}
8 changes: 8 additions & 0 deletions deploy/helm/sumologic/templates/_helpers/_logs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,11 @@ Example Usage:
{{- template "kubernetes.defaultTolerations" . -}}
{{- end -}}
{{- end -}}

{{- define "metadata.logs.statefulset.affinity" -}}
{{- if .Values.metadata.logs.statefulset.affinity -}}
{{- toYaml .Values.metadata.logs.statefulset.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultAffinity" . -}}
{{- end -}}
{{- end -}}
24 changes: 24 additions & 0 deletions deploy/helm/sumologic/templates/_helpers/_metrics.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ Example Usage:
{{- end -}}
{{- end -}}

{{- define "metrics.remoteWriteProxy.affinity" -}}
{{- if .Values.sumologic.metrics.remoteWriteProxy.affinity -}}
{{- toYaml .Values.sumologic.metrics.remoteWriteProxy.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultTolerations" . -}}
{{- end -}}
aboguszewski-sumo marked this conversation as resolved.
Show resolved Hide resolved
{{- end -}}

{{- define "metrics.collector.otelcol.nodeSelector" -}}
{{- if .Values.sumologic.metrics.collector.otelcol.nodeSelector -}}
{{- toYaml .Values.sumologic.metrics.collector.otelcol.nodeSelector -}}
Expand All @@ -91,6 +99,14 @@ Example Usage:
{{- end -}}
{{- end -}}

{{- define "metrics.collector.otelcol.affinity" -}}
{{- if .Values.sumologic.metrics.collector.otelcol.affinity -}}
{{- toYaml .Values.sumologic.metrics.collector.otelcol.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultAffinity" . -}}
{{- end -}}
{{- end -}}

{{- define "metadata.metrics.statefulset.nodeSelector" -}}
{{- if .Values.metadata.metrics.statefulset.nodeSelector -}}
{{- toYaml .Values.metadata.metrics.statefulset.nodeSelector -}}
Expand All @@ -107,6 +123,14 @@ Example Usage:
{{- end -}}
{{- end -}}

{{- define "metadata.metrics.statefulset.affinity" -}}
{{- if .Values.metadata.metrics.statefulset.affinity -}}
{{- toYaml .Values.metadata.metrics.statefulset.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultAffinity" . -}}
{{- end -}}
{{- end -}}

{{- define "sumologic.labels.app.metrics" -}}
{{ template "sumologic.labels.app.otelcol" . }}-metrics
{{- end -}}
Expand Down
8 changes: 8 additions & 0 deletions deploy/helm/sumologic/templates/_helpers/_setup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,11 @@ Example Usage:
{{- template "kubernetes.defaultTolerations" . -}}
{{- end -}}
{{- end -}}

{{- define "setup.job.affinity" -}}
{{- if .Values.sumologic.setup.job.affinity -}}
{{- toYaml .Values.sumologic.setup.job.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultAffinity" . -}}
{{- end -}}
{{- end -}}
8 changes: 8 additions & 0 deletions deploy/helm/sumologic/templates/_helpers/_traces.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ Example Usage:
{{- end -}}
{{- end -}}

{{- define "otelcolInstrumentation.statefulset.affinity" -}}
{{- if .Values.otelcolInstrumentation.statefulset.affinity -}}
{{- toYaml .Values.otelcolInstrumentation.statefulset.affinity -}}
{{- else -}}
{{- template "kubernetes.defaultAffinity" . -}}
{{- end -}}
{{- end -}}

{{/*
Check if autoscaling for traces gateway is enabled.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ spec:
tolerations:
{{ include "otelcolInstrumentation.statefulset.tolerations" . | indent 8 }}
{{- end }}
{{- if or $otelcolInstrumentation.statefulset.affinity $otelcolInstrumentation.statefulset.podAntiAffinity }}
{{- if or (not (empty (include "otelcolInstrumentation.statefulset.tolerations" .))) $otelcolInstrumentation.statefulset.podAntiAffinity }}
affinity:
aboguszewski-sumo marked this conversation as resolved.
Show resolved Hide resolved
{{- if $otelcolInstrumentation.statefulset.affinity }}
{{ toYaml $otelcolInstrumentation.statefulset.affinity | indent 8 }}
{{- if not (empty (include "otelcolInstrumentation.statefulset.affinity" .)) }}
{{ include "otelcolInstrumentation.statefulset.affinity" . | indent 8 }}
{{- end }}
{{- if eq $otelcolInstrumentation.statefulset.podAntiAffinity "hard"}}
{{ include "pod-anti-affinity-hard" . | indent 8 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{- $instance := "" -}}
{{- $defaultNodeSelector := (include "kubernetes.defaultNodeSelector" .) }}
{{- $defaultTolerations := (include "kubernetes.defaultTolerations" .) }}
{{- $defaultAffinity := (include "kubernetes.defaultAffinity" .) }}
{{- $daemonsets := dict "" $.Values.otellogs.daemonset -}}
{{- $daemonsets = deepCopy $daemonsets | merge $.Values.otellogs.additionalDaemonSets -}}
{{- range $name, $value := $daemonsets }}
Expand All @@ -22,6 +23,12 @@
{{- else if $defaultTolerations }}
{{- $tolerations = $defaultTolerations }}
{{- end }}
{{- $affinity := "" }}
{{- if .affinity }}
{{- $affinity = (toYaml .affinity) }}
{{- else if $defaultAffinity }}
{{- $affinity = $defaultAffinity }}
{{- end }}
---
apiVersion: apps/v1
kind: DaemonSet
Expand Down Expand Up @@ -67,9 +74,9 @@ spec:
nodeSelector:
{{ $nodeSelector | indent 8 }}
{{- end }}
{{- if or .affinity }}
{{- if $affinity }}
affinity:
{{ toYaml .affinity | indent 8 }}
{{ $affinity | indent 8 }}
{{- end }}
{{- if $tolerations }}
tolerations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ spec:
{{- end }}
{{- if .Values.metadata.logs.statefulset.affinity }}
affinity:
{{- if .Values.metadata.logs.statefulset.affinity }}
{{ toYaml .Values.metadata.logs.statefulset.affinity | indent 8 }}
{{- if not (empty (include "metadata.logs.statefulset.affinity" .)) }}
{{ include "metadata.logs.statefulset.affinity" . | indent 8 }}
{{- end }}
{{- if eq .Values.metadata.logs.statefulset.podAntiAffinity "hard"}}
{{ include "pod-anti-affinity-hard" . | indent 8 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ spec:
tolerations:
{{ include "metrics.collector.otelcol.tolerations" . | indent 6 }}
{{- end }}
{{- if .Values.sumologic.metrics.collector.otelcol.affinity }}
{{- if not (empty (include "metrics.collector.otelcol.affinity" .)) }}
affinity:
{{ toYaml .Values.sumologic.metrics.collector.otelcol.affinity | indent 6 }}
{{ include "metrics.collector.otelcol.affinity" . | indent 6 }}
{{- end }}
{{- if not (empty (include "metrics.collector.otelcol.nodeSelector" .)) }}
nodeSelector:
Expand All @@ -72,9 +72,9 @@ spec:
tolerations:
{{ include "metrics.collector.otelcol.tolerations" . | indent 6 }}
{{- end }}
{{- if .Values.sumologic.metrics.collector.otelcol.affinity }}
{{- if not (empty (include "metrics.collector.otelcol.affinity" .)) }}
affinity:
{{ toYaml .Values.sumologic.metrics.collector.otelcol.affinity | indent 4 }}
{{ include "metrics.collector.otelcol.affinity" . | indent 6 }}
{{- end }}
{{- if .Values.sumologic.metrics.collector.otelcol.priorityClassName }}
priorityClassName: {{ .Values.sumologic.metrics.collector.otelcol.priorityClassName | quote }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ spec:
tolerations:
{{ include "metadata.metrics.statefulset.tolerations" . | indent 8 }}
{{- end }}
{{- if or .Values.metadata.metrics.statefulset.affinity .Values.metadata.metrics.statefulset.podAntiAffinity }}
{{- if or (not (empty (include "metadata.metrics.statefulset.affinity" .))) .Values.metadata.metrics.statefulset.podAntiAffinity }}
affinity:
{{- if .Values.metadata.metrics.statefulset.affinity }}
{{ toYaml .Values.metadata.metrics.statefulset.affinity | indent 8 }}
{{- if not (empty (include "metadata.metrics.statefulset.affinity" .)) }}
{{ include "metadata.metrics.statefulset.affinity" . | indent 8 }}
{{- end }}
{{- if eq .Values.metadata.metrics.statefulset.podAntiAffinity "hard"}}
{{ include "pod-anti-affinity-hard" . | indent 8 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ spec:
nodeSelector:
{{ include "metrics.remoteWriteProxy.nodeSelector" . | nindent 8 }}
{{- end }}
{{- if or .Values.sumologic.metrics.remoteWriteProxy.affinity }}
{{- if not (empty (include "metrics.remoteWriteProxy.affinity" .)) }}
affinity:
{{ toYaml .Values.sumologic.metrics.remoteWriteProxy.affinity | indent 8 }}
{{ include "metrics.remoteWriteProxy.affinity" . | nindent 8 }}
{{- end }}
{{- if not (empty (include "metrics.remoteWriteProxy.nodeSelector" .)) }}
{{- if not (empty (include "metrics.remoteWriteProxy.tolerations" .)) }}
tolerations:
{{ include "metrics.remoteWriteProxy.tolerations" . | nindent 8 }}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ spec:
tolerations:
{{ include "pvcCleaner.job.tolerations" . | indent 12 }}
{{- end }}
{{- with .Values.pvcCleaner.job.affinity }}
{{- if not (empty (include "pvcCleaner.job.affinity" .)) }}
affinity:
{{ toYaml . | indent 12 }}
{{ include "pvcCleaner.job.affinity" . | indent 12 }}
{{- end }}
{{- with .Values.pvcCleaner.job.securityContext }}
securityContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ spec:
tolerations:
{{ include "pvcCleaner.job.tolerations" . | indent 12 }}
{{- end }}
{{- with .Values.pvcCleaner.job.affinity }}
{{- if not (empty (include "pvcCleaner.job.affinity" .)) }}
affinity:
{{ toYaml . | indent 12 }}
{{ include "pvcCleaner.job.affinity" . | indent 12 }}
{{- end }}
{{- with .Values.pvcCleaner.job.securityContext }}
securityContext:
Expand Down
4 changes: 2 additions & 2 deletions deploy/helm/sumologic/templates/setup/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ spec:
tolerations:
{{ include "setup.job.tolerations" . | indent 8 }}
{{- end }}
{{- if .Values.sumologic.setup.job.affinity }}
{{- if not (empty (include "setup.job.affinity" .)) }}
affinity:
{{ toYaml .Values.sumologic.setup.job.affinity | indent 8 }}
{{ include "setup.job.affinity" . | indent 8 }}
{{- end }}
volumes:
- name: setup
Expand Down
3 changes: 3 additions & 0 deletions deploy/helm/sumologic/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ sumologic:
## Global configuration of tolerations
tolerations: []

## Global configuration of affinity
affinity: {}

## Add custom annotations to sumologic serviceAccounts
serviceAccount:
annotations: {}
Expand Down
58 changes: 58 additions & 0 deletions tests/helm/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helm
import (
"fmt"
"path"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -230,6 +231,53 @@ func TestTolerations(t *testing.T) {
}
}

func TestAffinities(t *testing.T) {
t.Parallel()
valuesFilePath := path.Join(testDataDirectory, "node-selector.yaml")
renderedYamlString := RenderTemplate(
t,
&helm.Options{
ValuesFiles: []string{valuesFilePath},
SetStrValues: map[string]string{
"sumologic.accessId": "accessId",
"sumologic.accessKey": "accessKey",
},
Logger: logger.Discard, // the log output is noisy and doesn't help much
},
chartDirectory,
releaseName,
[]string{},
true,
"--namespace",
defaultNamespace,
)

// split the rendered Yaml into individual documents and unmarshal them into K8s objects
// we could use the yaml decoder directly, but we'd have to implement our own unmarshaling logic then
renderedObjects := UnmarshalMultipleFromYaml[unstructured.Unstructured](t, renderedYamlString)

for _, renderedObject := range renderedObjects {
aff, err := GetAffinity(renderedObject)
require.NoError(t, err)
// Objects that are not subject to global settings:
excluded := map[string]bool{
"col-test-sumologic-otelcol-events": true,
}
// Check only node affinity, to avoid checking anti affinities
if _, ok := excluded[renderedObject.GetName()]; !ok && aff != nil {
assert.NotNil(t, aff.NodeAffinity, "%s", renderedObject.GetName())
assert.True(
t,
reflect.DeepEqual(*aff.NodeAffinity, *affinity.NodeAffinity),
"%s should have node affinity set to %s, found %s instead",
renderedObject.GetName(),
affinity,
aff,
)
}
}
}

func TestNameAndLabelLength(t *testing.T) {
// object kinds whose names are limited to 63 characters instead of K8s default of 253
// not all of these are strictly required, but it's a good practice to limit them regardless
Expand Down Expand Up @@ -384,6 +432,16 @@ func GetNodeSelector(object unstructured.Unstructured) (map[string]string, error

return nil, nil
}
func GetAffinity(object unstructured.Unstructured) (*corev1.Affinity, error) {
podSpec, err := GetPodSpec(object)
if err != nil {
return nil, err
} else if podSpec != nil {
return podSpec.Affinity, nil
}

return nil, nil
}

func GetTolerations(object unstructured.Unstructured) ([]corev1.Toleration, error) {
podSpec, err := GetPodSpec(object)
Expand Down
30 changes: 30 additions & 0 deletions tests/helm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,33 @@ var toleration = corev1.Toleration{
// operator: Exists
// effect: "NoSchedule"
}

var affinity = corev1.Affinity{
// affinity:
// nodeAffinity:
// requiredDuringSchedulingIgnoredDuringExecution:
// nodeSelectorTerms:
// - matchExpressions:
// - key: kubernetes.io/os
// operator: NotIn
// values:
// - linux
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "kubernetes.io/os",
Operator: corev1.NodeSelectorOpNotIn,
Values: []string{"linux"},
},
},
MatchFields: nil,
},
},
},
},
PodAffinity: nil,
PodAntiAffinity: nil,
}