Skip to content

Commit

Permalink
fix pod anti affinity (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-mez committed Sep 2, 2020
1 parent be28a3d commit 4faeb97
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/datadogagent/clusteragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func newClusterAgentPodTemplate(agentdeployment *datadoghqv1alpha1.DatadogAgent,
VolumeMounts: volumeMounts,
},
},
Affinity: getPodAffinity(clusterAgentSpec.Affinity, getClusterAgentName(agentdeployment)),
Affinity: clusterAgentSpec.Affinity,
Tolerations: clusterAgentSpec.Tolerations,
PriorityClassName: agentdeployment.Spec.ClusterAgent.PriorityClassName,
Volumes: volumes,
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/datadogagent/clusteragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var testClusterAgentReplicas int32 = 1

func clusterAgentDefaultPodSpec() corev1.PodSpec {
return corev1.PodSpec{
Affinity: getPodAffinity(nil, "foo-cluster-agent"),
ServiceAccountName: "foo-cluster-agent",
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -396,7 +395,7 @@ func Test_newClusterAgentDeploymentFromInstance_EnvVars(t *testing.T) {
func Test_newClusterAgentDeploymentFromInstance_CustomDeploymentName(t *testing.T) {
customDeploymentName := "custom-cluster-agent-deployment"
deploymentNamePodSpec := clusterAgentDefaultPodSpec()
deploymentNamePodSpec.Affinity = getPodAffinity(nil, customDeploymentName)
deploymentNamePodSpec.Affinity = nil

deploymentNameAgentDeployment := test.NewDefaultedDatadogAgent("bar", "foo",
&test.NewDatadogAgentOptions{
Expand Down
25 changes: 24 additions & 1 deletion pkg/controller/datadogagent/clusterchecksrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func newClusterChecksRunnerPodTemplate(dda *datadoghqv1alpha1.DatadogAgent, labe
},
},
Volumes: getVolumesForClusterChecksRunner(dda),
Affinity: getPodAffinity(clusterChecksRunnerSpec.Affinity, getClusterChecksRunnerName(dda)),
Affinity: getPodAffinity(clusterChecksRunnerSpec.Affinity),
Tolerations: clusterChecksRunnerSpec.Tolerations,
PriorityClassName: clusterChecksRunnerSpec.PriorityClassName,
},
Expand Down Expand Up @@ -479,3 +479,26 @@ func getVolumeMountsForClusterChecksRunner(dda *datadoghqv1alpha1.DatadogAgent)
func getClusterChecksRunnerCustomConfigConfigMapName(dda *datadoghqv1alpha1.DatadogAgent) string {
return fmt.Sprintf("%s-runner-datadog-yaml", dda.Name)
}

// getPodAffinity returns the pod anti affinity of the cluster check runner pods
// the default anti affinity ensures we don't schedule multiple cluster check runners on the same node
func getPodAffinity(affinity *corev1.Affinity) *corev1.Affinity {
if affinity != nil {
return affinity
}

return &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
datadoghqv1alpha1.AgentDeploymentComponentLabelKey: datadoghqv1alpha1.DefaultClusterChecksRunnerResourceSuffix,
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
}
}
80 changes: 73 additions & 7 deletions pkg/controller/datadogagent/clusterchecksrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package datadogagent

import (
"fmt"
"reflect"
"testing"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

datadoghqv1alpha1 "github.com/DataDog/datadog-operator/pkg/apis/datadoghq/v1alpha1"
test "github.com/DataDog/datadog-operator/pkg/apis/datadoghq/v1alpha1/test"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"

"github.com/google/go-cmp/cmp"
assert "github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

var testClusterChecksRunnerReplicas int32 = 2

func clusterChecksRunnerDefaultPodSpec() corev1.PodSpec {
return corev1.PodSpec{
Affinity: getPodAffinity(nil, "foo-cluster-checks-runner"),
Affinity: getPodAffinity(nil),
ServiceAccountName: "foo-cluster-checks-runner",
InitContainers: []corev1.Container{
{
Expand Down Expand Up @@ -399,3 +400,68 @@ func Test_newClusterChecksRunnerDeploymentFromInstance_EnvVars(t *testing.T) {
}
test.Run(t)
}

func Test_getPodAffinity(t *testing.T) {
tests := []struct {
name string
affinity *corev1.Affinity
want *corev1.Affinity
}{
{
name: "no user-defined affinity - apply default",
affinity: nil,
want: &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"agent.datadoghq.com/component": "cluster-checks-runner",
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
},
{
name: "user-defined affinity",
affinity: &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
TopologyKey: "baz",
},
},
},
},
want: &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
TopologyKey: "baz",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getPodAffinity(tt.affinity); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getPodAffinity() = %v, want %v", got, tt.want)
}
})
}
}
21 changes: 0 additions & 21 deletions pkg/controller/datadogagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,27 +1547,6 @@ func getDefaultAPMAgentLivenessProbe() *corev1.Probe {
return livenessProbe
}

func getPodAffinity(affinity *corev1.Affinity, labelValue string) *corev1.Affinity {
if affinity != nil {
return affinity
}

return &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": labelValue,
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
}
}

func updateDaemonSetStatus(ds *appsv1.DaemonSet, dsStatus *datadoghqv1alpha1.DaemonSetStatus, updateTime *metav1.Time) *datadoghqv1alpha1.DaemonSetStatus {
if dsStatus == nil {
dsStatus = &datadoghqv1alpha1.DaemonSetStatus{}
Expand Down

0 comments on commit 4faeb97

Please sign in to comment.