diff --git a/packages/system/monitoring/templates/_scheduling.tpl b/packages/system/monitoring/templates/_scheduling.tpl new file mode 100644 index 000000000..e7358c5c6 --- /dev/null +++ b/packages/system/monitoring/templates/_scheduling.tpl @@ -0,0 +1,26 @@ +{{- define "monitoring.topologySpreadConstraints" -}} +{{- $root := index . 0 -}} +{{- $selector := dict "matchLabels" (dict "app.kubernetes.io/name" (index . 1) "app.kubernetes.io/instance" (index . 2)) -}} +{{- $global := list -}} +{{- with ($root.Values._cluster | default dict).scheduling -}} +{{- with get . "globalAppTopologySpreadConstraints" -}} +{{- $parsed := fromYaml . -}} +{{- $global = dig "topologySpreadConstraints" (list) $parsed -}} +{{- if or (not (kindIs "slice" $global)) (not $global) -}} +{{- fail (trim (printf "monitoring: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list. %s" (get $parsed "Error"))) -}} +{{- end -}} +{{- end -}} +{{- end -}} +{{- $constraints := list -}} +{{- range $global -}} +{{- $constraints = append $constraints (ternary . (merge (deepCopy .) (dict "labelSelector" $selector)) (hasKey . "labelSelector")) -}} +{{- end -}} +{{- if not $constraints -}} +{{- $default := dict "maxSkew" 1 "topologyKey" "kubernetes.io/hostname" "whenUnsatisfiable" (index . 3) "labelSelector" $selector -}} +{{- if eq (index . 3) "DoNotSchedule" -}} +{{- $_ := set $default "nodeTaintsPolicy" "Ignore" -}} +{{- end -}} +{{- $constraints = list $default -}} +{{- end -}} +{{- toYaml (dict "topologySpreadConstraints" $constraints) -}} +{{- end -}} diff --git a/packages/system/monitoring/templates/alerta/alerta.yaml b/packages/system/monitoring/templates/alerta/alerta.yaml index 51dae0411..3199c9b69 100644 --- a/packages/system/monitoring/templates/alerta/alerta.yaml +++ b/packages/system/monitoring/templates/alerta/alerta.yaml @@ -441,6 +441,7 @@ spec: apps.cozystack.io/application.name: {{ $.Release.Name }} replicaCount: {{ include "monitoring.alertmanager.replicas" $ }} configSecret: alertmanager + {{- include "monitoring.topologySpreadConstraints" (list $ "vmalertmanager" "alertmanager" "DoNotSchedule") | nindent 2 }} {{- if and $emailEnabled $email.authUsername }} secrets: - {{ $emailPasswordSecret | quote }} diff --git a/packages/system/monitoring/templates/vlogs/vlogs.yaml b/packages/system/monitoring/templates/vlogs/vlogs.yaml index 0e3d6add1..ed70f3215 100644 --- a/packages/system/monitoring/templates/vlogs/vlogs.yaml +++ b/packages/system/monitoring/templates/vlogs/vlogs.yaml @@ -22,13 +22,16 @@ spec: apps.cozystack.io/application.name: {{ $.Release.Name }} vlinsert: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vlinsert" .name "ScheduleAnyway") | nindent 4 }} resources: {} vlselect: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vlselect" .name "ScheduleAnyway") | nindent 4 }} resources: {} vlstorage: retentionPeriod: {{ .retentionPeriod | quote }} replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vlstorage" .name "DoNotSchedule") | nindent 4 }} resources: {} storage: volumeClaimTemplate: diff --git a/packages/system/monitoring/templates/vm/vmcluster.yaml b/packages/system/monitoring/templates/vm/vmcluster.yaml index e2962a12a..a89d410fc 100644 --- a/packages/system/monitoring/templates/vm/vmcluster.yaml +++ b/packages/system/monitoring/templates/vm/vmcluster.yaml @@ -18,9 +18,11 @@ spec: # it's usually more than default 30 maxLabelsPerTimeseries: "60" replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vminsert" .name "ScheduleAnyway") | nindent 4 }} resources: {} vmselect: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vmselect" .name "DoNotSchedule") | nindent 4 }} resources: {} extraArgs: search.maxUniqueTimeseries: "600000" @@ -46,6 +48,7 @@ spec: storage: 2Gi vmstorage: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vmstorage" .name "DoNotSchedule") | nindent 4 }} resources: {} storage: volumeClaimTemplate: diff --git a/packages/system/monitoring/templates/vtraces/vtraces.yaml b/packages/system/monitoring/templates/vtraces/vtraces.yaml index c7f3d386f..5c03a9dda 100644 --- a/packages/system/monitoring/templates/vtraces/vtraces.yaml +++ b/packages/system/monitoring/templates/vtraces/vtraces.yaml @@ -71,9 +71,11 @@ spec: apps.cozystack.io/application.name: {{ $.Release.Name }} insert: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vtinsert" .name "ScheduleAnyway") | nindent 4 }} resources: {} select: replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vtselect" .name "ScheduleAnyway") | nindent 4 }} resources: {} storage: retentionPeriod: {{ .retentionPeriod | quote }} @@ -84,6 +86,7 @@ spec: retention.maxDiskUsagePercent: "80" {{- end }} replicaCount: 2 + {{- include "monitoring.topologySpreadConstraints" (list $ "vtstorage" .name "DoNotSchedule") | nindent 4 }} resources: {} storage: volumeClaimTemplate: diff --git a/packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml b/packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml new file mode 100644 index 000000000..6558f6764 --- /dev/null +++ b/packages/system/monitoring/tests/topology_spread_invalid_policy_test.yaml @@ -0,0 +1,59 @@ +suite: monitoring refuses a malformed platform spread policy +templates: + - templates/vm/vmcluster.yaml + +release: + name: monitoring-system + namespace: tenant-root + +tests: + - it: fails on a platform policy that does not parse + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + asserts: + - failedTemplate: + errorPattern: "globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list\\. error converting YAML to JSON" + + - it: fails on a platform policy without a topologySpreadConstraints list + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + asserts: + - failedTemplate: + errorPattern: "globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list" + + - it: fails on a platform policy with an empty topologySpreadConstraints list + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: [] + asserts: + - failedTemplate: + errorMessage: "monitoring: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list." + + - it: fails on a platform policy whose topologySpreadConstraints is not a list + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + asserts: + - failedTemplate: + errorMessage: "monitoring: _cluster.scheduling.globalAppTopologySpreadConstraints must be YAML with a non-empty topologySpreadConstraints list." diff --git a/packages/system/monitoring/tests/topology_spread_test.yaml b/packages/system/monitoring/tests/topology_spread_test.yaml new file mode 100644 index 000000000..14a20da21 --- /dev/null +++ b/packages/system/monitoring/tests/topology_spread_test.yaml @@ -0,0 +1,453 @@ +suite: monitoring replicas spread over nodes +templates: + - templates/vm/vmcluster.yaml + - templates/vlogs/vlogs.yaml + - templates/vtraces/vtraces.yaml + - templates/alerta/alerta.yaml + +release: + name: monitoring-system + namespace: tenant-root + +set: + _cluster: + root-host: example.org + _namespace: + host: tenant-root.example.org + ingress: tenant-root + tracingStorages: + - name: generic + mode: cluster + retentionPeriod: "14d" + storage: 10Gi + +tests: + - it: requires separate nodes for vmstorage and vmselect and prefers them for vminsert + template: templates/vm/vmcluster.yaml + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vmselect.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vmselect + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vminsert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vminsert + app.kubernetes.io/instance: shortterm + + - it: keys each VMCluster's constraints to its own name + template: templates/vm/vmcluster.yaml + documentSelector: + path: metadata.name + value: longterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints[0].labelSelector.matchLabels + value: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: longterm + - equal: + path: spec.vmselect.topologySpreadConstraints[0].labelSelector.matchLabels + value: + app.kubernetes.io/name: vmselect + app.kubernetes.io/instance: longterm + - equal: + path: spec.vminsert.topologySpreadConstraints[0].labelSelector.matchLabels + value: + app.kubernetes.io/name: vminsert + app.kubernetes.io/instance: longterm + + - it: requires separate nodes for vlstorage and prefers them for vlselect and vlinsert + template: templates/vlogs/vlogs.yaml + documentSelector: + path: metadata.name + value: generic + asserts: + - equal: + path: spec.vlstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vlstorage + app.kubernetes.io/instance: generic + - equal: + path: spec.vlselect.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vlselect + app.kubernetes.io/instance: generic + - equal: + path: spec.vlinsert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vlinsert + app.kubernetes.io/instance: generic + + - it: requires separate nodes for vtstorage and prefers them for vtselect and vtinsert + template: templates/vtraces/vtraces.yaml + documentSelector: + path: kind + value: VTCluster + asserts: + - equal: + path: spec.storage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vtstorage + app.kubernetes.io/instance: generic + - equal: + path: spec.select.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vtselect + app.kubernetes.io/instance: generic + - equal: + path: spec.insert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vtinsert + app.kubernetes.io/instance: generic + + - it: requires separate nodes for the Alertmanager replicas + template: templates/alerta/alerta.yaml + documentSelector: + path: kind + value: VMAlertmanager + asserts: + - equal: + path: spec.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + nodeTaintsPolicy: Ignore + labelSelector: + matchLabels: + app.kubernetes.io/name: vmalertmanager + app.kubernetes.io/instance: alertmanager + + - it: keeps the default when the platform policy is empty + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: "" + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints[0].whenUnsatisfiable + value: DoNotSchedule + - equal: + path: spec.vminsert.topologySpreadConstraints[0].whenUnsatisfiable + value: ScheduleAnyway + + - it: renders the platform policy in place of the default for a VMCluster + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vmselect.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmselect + app.kubernetes.io/instance: shortterm + - equal: + path: spec.vminsert.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vminsert + app.kubernetes.io/instance: shortterm + + - it: renders the platform policy in place of the default for the logs store + template: templates/vlogs/vlogs.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: metadata.name + value: generic + asserts: + - equal: + path: spec.vlstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vlstorage + app.kubernetes.io/instance: generic + + - it: renders the platform policy in place of the default for the traces store + template: templates/vtraces/vtraces.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: kind + value: VTCluster + asserts: + - equal: + path: spec.storage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vtstorage + app.kubernetes.io/instance: generic + + - it: renders the platform policy in place of the default for the Alertmanager + template: templates/alerta/alerta.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: kind + value: VMAlertmanager + asserts: + - equal: + path: spec.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmalertmanager + app.kubernetes.io/instance: alertmanager + + - it: gives every constraint of the platform policy the component's selector + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: DoNotSchedule + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: DoNotSchedule + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + + - it: keeps a selector the platform policy sets itself + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + example.org/spread: all + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + example.org/spread: all + + - it: gives the component's selector to a constraint after one that sets its own + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + example.org/spread: all + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchLabels: + example.org/spread: all + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + + - it: gives the component's selector to a constraint before one that sets its own + template: templates/vm/vmcluster.yaml + set: + _cluster: + scheduling: + globalAppTopologySpreadConstraints: | + topologySpreadConstraints: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchExpressions: + - key: example.org/spread + operator: Exists + documentSelector: + path: metadata.name + value: shortterm + asserts: + - equal: + path: spec.vmstorage.topologySpreadConstraints + value: + - maxSkew: 1 + topologyKey: kubernetes.io/hostname + whenUnsatisfiable: DoNotSchedule + labelSelector: + matchLabels: + app.kubernetes.io/name: vmstorage + app.kubernetes.io/instance: shortterm + - maxSkew: 1 + topologyKey: topology.kubernetes.io/zone + whenUnsatisfiable: ScheduleAnyway + labelSelector: + matchExpressions: + - key: example.org/spread + operator: Exists