Skip to content

Commit

Permalink
Add topology spread constrains
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Fernandez <mariofer@redhat.com>
  • Loading branch information
marioferh committed Jun 28, 2023
1 parent 2bb7887 commit d731adf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions Documentation/api.md
Expand Up @@ -182,6 +182,7 @@ The `KubeStateMetricsConfig` resource defines settings for the `kube-state-metri
| -------- | ---- | ----------- |
| nodeSelector | map[string]string | Defines the nodes on which the pods are scheduled. |
| tolerations | [][v1.Toleration](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#toleration-v1-core) | Defines tolerations for the pods. |
| topologySpreadConstraints | []v1.TopologySpreadConstraint | Defines a pod's topology spread constraints. |

[Back to TOC](#table-of-contents)

Expand Down
Expand Up @@ -22,6 +22,8 @@ Appears in: link:clustermonitoringconfiguration.adoc[ClusterMonitoringConfigurat

|tolerations|[]v1.Toleration|Defines tolerations for the pods.

|topologySpreadConstraints|[]v1.TopologySpreadConstraint|Defines a pod's topology spread constraints.

|===

link:../index.adoc[Back to TOC]
4 changes: 4 additions & 0 deletions pkg/manifests/manifests.go
Expand Up @@ -757,6 +757,10 @@ func (f *Factory) KubeStateMetricsDeployment() (*appsv1.Deployment, error) {
d.Spec.Template.Spec.Tolerations = f.config.ClusterMonitoringConfiguration.KubeStateMetricsConfig.Tolerations
}

if len(f.config.ClusterMonitoringConfiguration.KubeStateMetricsConfig.TopologySpreadConstraints) > 0 {
d.Spec.Template.Spec.TopologySpreadConstraints = f.config.ClusterMonitoringConfiguration.KubeStateMetricsConfig.TopologySpreadConstraints
}

return d, nil
}

Expand Down
20 changes: 19 additions & 1 deletion pkg/manifests/manifests_test.go
Expand Up @@ -3128,7 +3128,17 @@ nodeExporter:
}

func TestKubeStateMetrics(t *testing.T) {
c, err := NewConfigFromString(``, false)
config :=
(`kubeStateMetrics:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: type
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
foo: bar`)

c, err := NewConfigFromString(config, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -3176,6 +3186,14 @@ func TestKubeStateMetrics(t *testing.T) {
t.Fatalf("incorrect TLS version \n got %s, \nwant %s", kubeRbacProxyMinTLSVersionArg, expectedKubeRbacProxyMinTLSVersionArg)
}

if d.Spec.Template.Spec.TopologySpreadConstraints[0].MaxSkew != 1 {
t.Fatal("kube-state-metrics topology spread contraints MaxSkew not configured correctly")
}

if d.Spec.Template.Spec.TopologySpreadConstraints[0].WhenUnsatisfiable != "DoNotSchedule" {
t.Fatal("kube-state-metrics topology spread contraints WhenUnsatisfiable not configured correctly")
}

d2, err := f.KubeStateMetricsDeployment()
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/manifests/types.go
Expand Up @@ -137,6 +137,8 @@ type KubeStateMetricsConfig struct {
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
// Defines tolerations for the pods.
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
// Defines a pod's topology spread constraints.
TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
}

// The `PrometheusK8sConfig` resource defines settings for the Prometheus
Expand Down

0 comments on commit d731adf

Please sign in to comment.