diff --git a/pkg/clusteragent/autoscaling/workload/controller_test.go b/pkg/clusteragent/autoscaling/workload/controller_test.go index e55ee8ef884bac..be6233bf738252 100644 --- a/pkg/clusteragent/autoscaling/workload/controller_test.go +++ b/pkg/clusteragent/autoscaling/workload/controller_test.go @@ -232,98 +232,120 @@ func TestLeaderCreateDeleteRemote(t *testing.T) { assert.Len(t, f.store.GetAll(), 0) } -func TestDatadogPodAutoscalerTargetingClusterAgent(t *testing.T) { - testTime := time.Now() - f := newFixture(t, testTime) - - t.Setenv("DD_POD_NAME", "datadog-agent-cluster-agent-7dbf798595-tp9lg") - currentNs := common.GetMyNamespace() - id := fmt.Sprintf("%s/dpa-dca", currentNs) - - dpaSpec := datadoghq.DatadogPodAutoscalerSpec{ - TargetRef: autoscalingv2.CrossVersionObjectReference{ - Kind: "Deployment", - Name: "datadog-agent-cluster-agent", - APIVersion: "apps/v1", +func TestDatadogPodAutoscalerTargetingClusterAgentErrors(t *testing.T) { + tests := []struct { + name string + targetRef autoscalingv2.CrossVersionObjectReference + }{ + { + "target set to cluster agent deployment", + autoscalingv2.CrossVersionObjectReference{ + Kind: "Deployment", + Name: "datadog-agent-cluster-agent", + APIVersion: "apps/v1", + }, + }, + { + "target set to cluster agent replicaset", + autoscalingv2.CrossVersionObjectReference{ + Kind: "ReplicaSet", + Name: "datadog-agent-cluster-agent-7dbf798595", + APIVersion: "apps/v1", + }, }, - // Local owner means .Spec source of truth is K8S - Owner: datadoghq.DatadogPodAutoscalerLocalOwner, } - dpa, dpaTyped := newFakePodAutoscaler(currentNs, "dpa-dca", 1, dpaSpec, datadoghq.DatadogPodAutoscalerStatus{}) - f.InformerObjects = append(f.InformerObjects, dpa) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + testTime := time.Now() + f := newFixture(t, testTime) - expectedDPAError := &datadoghq.DatadogPodAutoscaler{ - TypeMeta: metav1.TypeMeta{ - Kind: "DatadogPodAutoscaler", - APIVersion: "datadoghq.com/v1alpha1", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "dpa-dca", - Namespace: currentNs, - Generation: 1, - UID: dpa.GetUID(), - }, - Spec: datadoghq.DatadogPodAutoscalerSpec{ - TargetRef: autoscalingv2.CrossVersionObjectReference{ - Kind: "", - Name: "", - APIVersion: "", - }, - Owner: "", - }, - Status: datadoghq.DatadogPodAutoscalerStatus{ - Conditions: []datadoghq.DatadogPodAutoscalerCondition{ - { - Type: datadoghq.DatadogPodAutoscalerErrorCondition, - Status: corev1.ConditionTrue, - LastTransitionTime: metav1.NewTime(testTime), - Reason: "Autoscaling target cannot be set to the cluster agent", - }, - { - Type: datadoghq.DatadogPodAutoscalerActiveCondition, - Status: corev1.ConditionTrue, - LastTransitionTime: metav1.NewTime(testTime), - }, - { - Type: datadoghq.DatadogPodAutoscalerHorizontalAbleToRecommendCondition, - Status: corev1.ConditionUnknown, - LastTransitionTime: metav1.NewTime(testTime), - }, - { - Type: datadoghq.DatadogPodAutoscalerVerticalAbleToRecommendCondition, - Status: corev1.ConditionUnknown, - LastTransitionTime: metav1.NewTime(testTime), + t.Setenv("DD_POD_NAME", "datadog-agent-cluster-agent-7dbf798595-tp9lg") + currentNs := common.GetMyNamespace() + id := fmt.Sprintf("%s/dpa-dca", currentNs) + + dpaSpec := datadoghq.DatadogPodAutoscalerSpec{ + TargetRef: tt.targetRef, + // Local owner means .Spec source of truth is K8S + Owner: datadoghq.DatadogPodAutoscalerLocalOwner, + } + + dpa, dpaTyped := newFakePodAutoscaler(currentNs, "dpa-dca", 1, dpaSpec, datadoghq.DatadogPodAutoscalerStatus{}) + f.InformerObjects = append(f.InformerObjects, dpa) + + expectedDPAError := &datadoghq.DatadogPodAutoscaler{ + TypeMeta: metav1.TypeMeta{ + Kind: "DatadogPodAutoscaler", + APIVersion: "datadoghq.com/v1alpha1", }, - { - Type: datadoghq.DatadogPodAutoscalerHorizontalScalingLimitedCondition, - Status: corev1.ConditionFalse, - LastTransitionTime: metav1.NewTime(testTime), + ObjectMeta: metav1.ObjectMeta{ + Name: "dpa-dca", + Namespace: currentNs, + Generation: 1, + UID: dpa.GetUID(), }, - { - Type: datadoghq.DatadogPodAutoscalerHorizontalAbleToScaleCondition, - Status: corev1.ConditionUnknown, - LastTransitionTime: metav1.NewTime(testTime), + Spec: datadoghq.DatadogPodAutoscalerSpec{ + TargetRef: autoscalingv2.CrossVersionObjectReference{ + Kind: "", + Name: "", + APIVersion: "", + }, + Owner: "", }, - { - Type: datadoghq.DatadogPodAutoscalerVerticalAbleToApply, - Status: corev1.ConditionUnknown, - LastTransitionTime: metav1.NewTime(testTime), + Status: datadoghq.DatadogPodAutoscalerStatus{ + Conditions: []datadoghq.DatadogPodAutoscalerCondition{ + { + Type: datadoghq.DatadogPodAutoscalerErrorCondition, + Status: corev1.ConditionTrue, + LastTransitionTime: metav1.NewTime(testTime), + Reason: "Autoscaling target cannot be set to the cluster agent", + }, + { + Type: datadoghq.DatadogPodAutoscalerActiveCondition, + Status: corev1.ConditionTrue, + LastTransitionTime: metav1.NewTime(testTime), + }, + { + Type: datadoghq.DatadogPodAutoscalerHorizontalAbleToRecommendCondition, + Status: corev1.ConditionUnknown, + LastTransitionTime: metav1.NewTime(testTime), + }, + { + Type: datadoghq.DatadogPodAutoscalerVerticalAbleToRecommendCondition, + Status: corev1.ConditionUnknown, + LastTransitionTime: metav1.NewTime(testTime), + }, + { + Type: datadoghq.DatadogPodAutoscalerHorizontalScalingLimitedCondition, + Status: corev1.ConditionFalse, + LastTransitionTime: metav1.NewTime(testTime), + }, + { + Type: datadoghq.DatadogPodAutoscalerHorizontalAbleToScaleCondition, + Status: corev1.ConditionUnknown, + LastTransitionTime: metav1.NewTime(testTime), + }, + { + Type: datadoghq.DatadogPodAutoscalerVerticalAbleToApply, + Status: corev1.ConditionUnknown, + LastTransitionTime: metav1.NewTime(testTime), + }, + }, }, - }, - }, + } + expectedUnstructuredError, err := autoscaling.ToUnstructured(expectedDPAError) + assert.NoError(t, err) + f.RunControllerSync(true, id) + + f.Objects = append(f.Objects, dpaTyped) + f.Actions = nil + + f.ExpectUpdateStatusAction(expectedUnstructuredError) + f.RunControllerSync(true, id) + assert.Len(t, f.store.GetAll(), 1) + pai, found := f.store.Get(id) + assert.Truef(t, found, "Expected to find DatadogPodAutoscaler in store") + assert.Equal(t, errors.New("Autoscaling target cannot be set to the cluster agent"), pai.Error()) + }) } - expectedUnstructuredError, err := autoscaling.ToUnstructured(expectedDPAError) - assert.NoError(t, err) - f.RunControllerSync(true, id) - - f.Objects = append(f.Objects, dpaTyped) - f.Actions = nil - - f.ExpectUpdateStatusAction(expectedUnstructuredError) - f.RunControllerSync(true, id) - assert.Len(t, f.store.GetAll(), 1) - pai, found := f.store.Get(id) - assert.Truef(t, found, "Expected to find DatadogPodAutoscaler in store") - assert.Equal(t, errors.New("Autoscaling target cannot be set to the cluster agent"), pai.Error()) }