Skip to content

Commit

Permalink
Allow Agent pods to have Affinity (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliogreff committed Jun 22, 2021
1 parent 1ac42df commit 022a88e
Show file tree
Hide file tree
Showing 8 changed files with 1,288 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/v1alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ type DatadogAgentSpecAgentSpec struct {
// Provide Agent Network Policy configuration
// +optional
NetworkPolicy NetworkPolicySpec `json:"networkPolicy,omitempty"`

// If specified, the pod's scheduling constraints.
// +optional
Affinity *corev1.Affinity `json:"affinity,omitempty"`
}

// RbacConfig contains RBAC configuration.
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion api/v1alpha1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

617 changes: 617 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml

Large diffs are not rendered by default.

599 changes: 599 additions & 0 deletions config/crd/bases/v1beta1/datadoghq.com_datadogagents.yaml

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions controllers/datadogagent/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ func TestReconcileDatadogAgent_Reconcile(t *testing.T) {
s.AddKnownTypes(networkingv1.SchemeGroupVersion, &networkingv1.NetworkPolicy{})

defaultRequeueDuration := 15 * time.Second
affinity := &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
TopologyKey: "baz",
},
},
},
}

type fields struct {
client client.Client
Expand Down Expand Up @@ -2225,6 +2239,41 @@ func TestReconcileDatadogAgent_Reconcile(t *testing.T) {
return nil
},
},
{
name: "DatadogAgent found and defaulted, DaemonSet has Affinity",
fields: fields{
client: fake.NewFakeClient(),
scheme: s,
recorder: recorder,
},
args: args{
request: newRequest(resourcesNamespace, resourcesName),
loadFunc: func(c client.Client) {
dadOptions := &test.NewDatadogAgentOptions{}
dda := test.NewDefaultedDatadogAgent(resourcesNamespace, resourcesName, dadOptions)

dda.Spec.Agent.Affinity = affinity

_ = c.Create(context.TODO(), dda)
createAgentDependencies(c, dda)
},
},
want: reconcile.Result{RequeueAfter: defaultRequeueDuration},
wantErr: false,
wantFunc: func(c client.Client) error {
ds := &appsv1.DaemonSet{}
err := c.Get(context.TODO(), newRequest(resourcesNamespace, dsName).NamespacedName, ds)
if err != nil {
return err
}

if !reflect.DeepEqual(ds.Spec.Template.Spec.Affinity, affinity) {
return fmt.Errorf("pod affinity does not match the one specified. got: %+v", ds.Spec.Template.Spec.Affinity)
}

return nil
},
},
{
name: "DatadogAgent found and defaulted, Cluster Agent network policies are created",
fields: fields{
Expand Down
1 change: 1 addition & 0 deletions controllers/datadogagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func newAgentPodTemplate(logger logr.Logger, dda *datadoghqv1alpha1.DatadogAgent
HostPID: dda.Spec.Agent.HostPID || isComplianceEnabled(&dda.Spec),
DNSPolicy: dda.Spec.Agent.DNSPolicy,
DNSConfig: dda.Spec.Agent.DNSConfig,
Affinity: dda.Spec.Agent.Affinity,
},
}, nil
}
Expand Down
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ spec:
| --------- | ----------- |
| agent.additionalAnnotations | AdditionalAnnotations provide annotations that will be added to the Agent Pods. |
| agent.additionalLabels | AdditionalLabels provide labels that will be added to the Agent Pods. |
| agent.affinity.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution | The scheduler will prefer to schedule pods to nodes that satisfy the affinity expressions specified by this field, but it may choose a node that violates one or more of the expressions. The node that is most preferred is the one with the greatest sum of weights, i.e. for each node that meets all of the scheduling requirements (resource request, requiredDuringScheduling affinity expressions, etc.), compute a sum by iterating through the elements of this field and adding "weight" to the sum if the node matches the corresponding matchExpressions; the node(s) with the highest sum are the most preferred. |
| agent.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms | Required. A list of node selector terms. The terms are ORed. |
| agent.affinity.podAffinity.preferredDuringSchedulingIgnoredDuringExecution | The scheduler will prefer to schedule pods to nodes that satisfy the affinity expressions specified by this field, but it may choose a node that violates one or more of the expressions. The node that is most preferred is the one with the greatest sum of weights, i.e. for each node that meets all of the scheduling requirements (resource request, requiredDuringScheduling affinity expressions, etc.), compute a sum by iterating through the elements of this field and adding "weight" to the sum if the node has pods which matches the corresponding podAffinityTerm; the node(s) with the highest sum are the most preferred. |
| agent.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution | If the affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled onto the node. If the affinity requirements specified by this field cease to be met at some point during pod execution (e.g. due to a pod label update), the system may or may not try to eventually evict the pod from its node. When there are multiple elements, the lists of nodes corresponding to each podAffinityTerm are intersected, i.e. all terms must be satisfied. |
| agent.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution | The scheduler will prefer to schedule pods to nodes that satisfy the anti-affinity expressions specified by this field, but it may choose a node that violates one or more of the expressions. The node that is most preferred is the one with the greatest sum of weights, i.e. for each node that meets all of the scheduling requirements (resource request, requiredDuringScheduling anti-affinity expressions, etc.), compute a sum by iterating through the elements of this field and adding "weight" to the sum if the node has pods which matches the corresponding podAffinityTerm; the node(s) with the highest sum are the most preferred. |
| agent.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution | If the anti-affinity requirements specified by this field are not met at scheduling time, the pod will not be scheduled onto the node. If the anti-affinity requirements specified by this field cease to be met at some point during pod execution (e.g. due to a pod label update), the system may or may not try to eventually evict the pod from its node. When there are multiple elements, the lists of nodes corresponding to each podAffinityTerm are intersected, i.e. all terms must be satisfied. |
| agent.apm.args | Args allows the specification of extra args to `Command` parameter |
| agent.apm.command | Command allows the specification of custom entrypoint for Trace Agent container |
| agent.apm.enabled | Enable this to enable APM and tracing, on port 8126. See also: https://github.com/DataDog/docker-dd-agent#tracing-from-the-host |
Expand Down

0 comments on commit 022a88e

Please sign in to comment.