diff --git a/.chloggen/targetallocator-deployment-annotations.yaml b/.chloggen/targetallocator-deployment-annotations.yaml new file mode 100644 index 0000000000..4c476e1501 --- /dev/null +++ b/.chloggen/targetallocator-deployment-annotations.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action) +component: target allocator + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Annotations of the `OpenTelemetryCollector` and `TargetAllocator` resources are now propagated to the target allocator deployment resource. + +# One or more tracking issues related to the change +issues: [4393] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: "" diff --git a/hack/check-operator-ready.go b/hack/check-operator-ready.go index 8de631a37d..a052a12d87 100644 --- a/hack/check-operator-ready.go +++ b/hack/check-operator-ready.go @@ -35,7 +35,10 @@ func main() { var timeout int var kubeconfigPath string - defaultKubeconfigPath := filepath.Join(homedir.HomeDir(), ".kube", "config") + defaultKubeconfigPath := os.Getenv("KUBECONFIG") + if defaultKubeconfigPath == "" { + defaultKubeconfigPath = filepath.Join(homedir.HomeDir(), ".kube", "config") + } pflag.IntVar(&timeout, "timeout", 300, "The timeout for the check.") pflag.StringVar(&kubeconfigPath, "kubeconfig-path", defaultKubeconfigPath, "Absolute path to the KubeconfigPath file") diff --git a/internal/controllers/builder_test.go b/internal/controllers/builder_test.go index a96363cd0b..2f0f52b89f 100644 --- a/internal/controllers/builder_test.go +++ b/internal/controllers/builder_test.go @@ -1839,6 +1839,9 @@ func TestBuildTargetAllocator(t *testing.T) { Name: "test", Namespace: "test", Labels: nil, + Annotations: map[string]string{ + "test": "test", + }, }, Spec: v1alpha1.TargetAllocatorSpec{ FilterStrategy: v1beta1.TargetAllocatorFilterStrategyRelabelConfig, @@ -1885,7 +1888,9 @@ func TestBuildTargetAllocator(t *testing.T) { "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "test": "test", + }, }, Data: map[string]string{ "targetallocator.yaml": `allocation_strategy: consistent-hashing @@ -1929,7 +1934,10 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "test": "test", + }, }, Spec: appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ @@ -1947,6 +1955,7 @@ prometheus_cr: }, Annotations: map[string]string{ "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "test": "test", }, }, Spec: corev1.PodSpec{ @@ -2034,6 +2043,9 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, + Annotations: map[string]string{ + "test": "test", + }, }, }, &corev1.Service{ @@ -2079,6 +2091,7 @@ prometheus_cr: }, Annotations: map[string]string{ "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + "test": "test", }, }, Spec: policyV1.PodDisruptionBudgetSpec{ @@ -2210,7 +2223,9 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d", + }, }, Spec: appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ @@ -2541,7 +2556,9 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-targetallocator-config/hash": "286a5a4e7ec6d2ce652a4ce23e135c10053b4c87fd080242daa5bf21dcd5a337", + }, }, Spec: appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ @@ -2846,7 +2863,9 @@ prometheus_cr: "app.kubernetes.io/part-of": "opentelemetry", "app.kubernetes.io/version": "latest", }, - Annotations: nil, + Annotations: map[string]string{ + "opentelemetry-targetallocator-config/hash": "3e2818ab54d866289de7837779e86e9c95803c43c0c4b58b25123e809ae9b771", + }, }, Spec: appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ diff --git a/internal/manifests/targetallocator/annotations.go b/internal/manifests/targetallocator/annotations.go index f52c390880..3d2dbea7e5 100644 --- a/internal/manifests/targetallocator/annotations.go +++ b/internal/manifests/targetallocator/annotations.go @@ -15,8 +15,28 @@ import ( const configMapHashAnnotationKey = "opentelemetry-targetallocator-config/hash" -// Annotations returns the annotations for the TargetAllocator Pod. +// Annotations returns the annotations for the TargetAllocator resources. func Annotations(instance v1alpha1.TargetAllocator, configMap *v1.ConfigMap, filterAnnotations []string) map[string]string { + annotations := make(map[string]string, len(instance.ObjectMeta.Annotations)) + if instance.ObjectMeta.Annotations != nil { + for k, v := range instance.ObjectMeta.Annotations { + if !manifestutils.IsFilteredSet(k, filterAnnotations) { + annotations[k] = v + } + } + } + if configMap != nil { + cmHash := getConfigMapSHA(configMap) + if cmHash != "" { + annotations[configMapHashAnnotationKey] = getConfigMapSHA(configMap) + } + } + + return annotations +} + +// PodAnnotations returns the annotations for the TargetAllocator Pod. +func PodAnnotations(instance v1alpha1.TargetAllocator, configMap *v1.ConfigMap, filterAnnotations []string) map[string]string { // Make a copy of PodAnnotations to be safe annotations := make(map[string]string, len(instance.Spec.PodAnnotations)) for key, value := range instance.Spec.PodAnnotations { diff --git a/internal/manifests/targetallocator/annotations_test.go b/internal/manifests/targetallocator/annotations_test.go index be1e8c04dd..5ec58faf61 100644 --- a/internal/manifests/targetallocator/annotations_test.go +++ b/internal/manifests/targetallocator/annotations_test.go @@ -20,8 +20,8 @@ func TestPodAnnotations(t *testing.T) { instance.Spec.PodAnnotations = map[string]string{ "key": "value", } - annotations := Annotations(instance, nil, []string{".*\\.bar\\.io"}) - assert.Subset(t, annotations, instance.Spec.PodAnnotations) + podAnnotations := PodAnnotations(instance, nil, []string{".*\\.bar\\.io"}) + assert.Subset(t, podAnnotations, instance.Spec.PodAnnotations) } func TestConfigMapHash(t *testing.T) { @@ -39,14 +39,14 @@ func TestConfigMapHash(t *testing.T) { expectedConfig := expectedConfigMap.Data[targetAllocatorFilename] require.NotEmpty(t, expectedConfig) expectedHash := sha256.Sum256([]byte(expectedConfig)) - annotations := Annotations(targetAllocator, expectedConfigMap, []string{".*\\.bar\\.io"}) - require.Contains(t, annotations, configMapHashAnnotationKey) - cmHash := annotations[configMapHashAnnotationKey] + podAnnotations := PodAnnotations(targetAllocator, expectedConfigMap, []string{".*\\.bar\\.io"}) + require.Contains(t, podAnnotations, configMapHashAnnotationKey) + cmHash := podAnnotations[configMapHashAnnotationKey] assert.Equal(t, fmt.Sprintf("%x", expectedHash), cmHash) } func TestInvalidConfigNoHash(t *testing.T) { instance := targetAllocatorInstance() - annotations := Annotations(instance, nil, []string{".*\\.bar\\.io"}) - require.NotContains(t, annotations, configMapHashAnnotationKey) + podAnnotations := PodAnnotations(instance, nil, []string{".*\\.bar\\.io"}) + require.NotContains(t, podAnnotations, configMapHashAnnotationKey) } diff --git a/internal/manifests/targetallocator/deployment.go b/internal/manifests/targetallocator/deployment.go index da008a126b..a59fbdf5e4 100644 --- a/internal/manifests/targetallocator/deployment.go +++ b/internal/manifests/targetallocator/deployment.go @@ -23,12 +23,14 @@ func Deployment(params Params) (*appsv1.Deployment, error) { configMap = nil } annotations := Annotations(params.TargetAllocator, configMap, params.Config.AnnotationsFilter) + podAnnotations := PodAnnotations(params.TargetAllocator, configMap, params.Config.AnnotationsFilter) return &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: params.TargetAllocator.Namespace, - Labels: labels, + Name: name, + Namespace: params.TargetAllocator.Namespace, + Labels: labels, + Annotations: annotations, }, Spec: appsv1.DeploymentSpec{ Replicas: params.TargetAllocator.Spec.Replicas, @@ -38,7 +40,7 @@ func Deployment(params Params) (*appsv1.Deployment, error) { Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, - Annotations: annotations, + Annotations: podAnnotations, }, Spec: corev1.PodSpec{ ServiceAccountName: ServiceAccountName(params.TargetAllocator), diff --git a/internal/manifests/targetallocator/deployment_test.go b/internal/manifests/targetallocator/deployment_test.go index 985b7b9990..d5357b306d 100644 --- a/internal/manifests/targetallocator/deployment_test.go +++ b/internal/manifests/targetallocator/deployment_test.go @@ -145,10 +145,37 @@ func TestDeploymentNewDefault(t *testing.T) { assert.Contains(t, d.Spec.Template.Annotations, configMapHashAnnotationKey) assert.Len(t, d.Spec.Template.Annotations, 1) + // should only have the ConfigMap hash annotation + assert.Contains(t, d.ObjectMeta.Annotations, configMapHashAnnotationKey) + assert.Len(t, d.ObjectMeta.Annotations, 1) + // the pod selector should match the pod spec's labels assert.Subset(t, d.Spec.Template.Labels, d.Spec.Selector.MatchLabels) } +func TestDeploymentAnnotations(t *testing.T) { + // prepare + testAnnotationValues := map[string]string{"annotation-key": "annotation-value"} + otelcol := collectorInstance() + targetAllocator := targetAllocatorInstance() + targetAllocator.ObjectMeta.Annotations = testAnnotationValues + cfg := config.New() + + params := Params{ + Collector: otelcol, + TargetAllocator: targetAllocator, + Config: cfg, + Log: logger, + } + + // test + ds, err := Deployment(params) + assert.NoError(t, err) + // verify + assert.Equal(t, "my-instance-targetallocator", ds.Name) + assert.Subset(t, ds.ObjectMeta.Annotations, testAnnotationValues) +} + func TestDeploymentPodAnnotations(t *testing.T) { // prepare testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"} diff --git a/internal/manifests/targetallocator/networkpolicy.go b/internal/manifests/targetallocator/networkpolicy.go index 04841a6108..d48cffc9be 100644 --- a/internal/manifests/targetallocator/networkpolicy.go +++ b/internal/manifests/targetallocator/networkpolicy.go @@ -29,7 +29,7 @@ func NetworkPolicy(params Params) (*networkingv1.NetworkPolicy, error) { name := naming.TargetAllocatorNetworkPolicy(params.TargetAllocator.Name) labels := manifestutils.Labels(params.TargetAllocator.ObjectMeta, name, params.TargetAllocator.Status.Image, ComponentOpenTelemetryTargetAllocator, params.Config.LabelsFilter) - annotations := Annotations(params.TargetAllocator, nil, params.Config.AnnotationsFilter) + podAnnotations := PodAnnotations(params.TargetAllocator, nil, params.Config.AnnotationsFilter) tcp := corev1.ProtocolTCP apiServerPort := intstr.FromInt32(defaultAPIServerPort) @@ -37,7 +37,7 @@ func NetworkPolicy(params Params) (*networkingv1.NetworkPolicy, error) { ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: params.TargetAllocator.Namespace, - Annotations: annotations, + Annotations: podAnnotations, Labels: labels, }, Spec: networkingv1.NetworkPolicySpec{ diff --git a/internal/manifests/targetallocator/poddisruptionbudget.go b/internal/manifests/targetallocator/poddisruptionbudget.go index d9ba2447e5..8f16373b8d 100644 --- a/internal/manifests/targetallocator/poddisruptionbudget.go +++ b/internal/manifests/targetallocator/poddisruptionbudget.go @@ -49,13 +49,13 @@ func PodDisruptionBudget(params Params) (*policyV1.PodDisruptionBudget, error) { params.Log.Info("failed to construct target allocator config map for annotations") configMap = nil } - annotations := Annotations(params.TargetAllocator, configMap, params.Config.AnnotationsFilter) + podAnnotations := PodAnnotations(params.TargetAllocator, configMap, params.Config.AnnotationsFilter) objectMeta := metav1.ObjectMeta{ Name: name, Namespace: params.TargetAllocator.Namespace, Labels: labels, - Annotations: annotations, + Annotations: podAnnotations, } return &policyV1.PodDisruptionBudget{ diff --git a/tests/e2e-targetallocator-cr/targetallocator-label/00-assert.yaml b/tests/e2e-targetallocator-cr/targetallocator-label/00-assert.yaml index 0aa109cdc3..1d7b39a58f 100644 --- a/tests/e2e-targetallocator-cr/targetallocator-label/00-assert.yaml +++ b/tests/e2e-targetallocator-cr/targetallocator-label/00-assert.yaml @@ -42,4 +42,11 @@ data: filter_strategy: "" kind: ConfigMap metadata: - name: ta-targetallocator \ No newline at end of file + name: ta-targetallocator +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: ta-targetallocator + annotations: + e2e-test-annotation: "true" \ No newline at end of file diff --git a/tests/e2e-targetallocator-cr/targetallocator-label/00-install.yaml b/tests/e2e-targetallocator-cr/targetallocator-label/00-install.yaml index b905f8d88e..2ff5366e55 100644 --- a/tests/e2e-targetallocator-cr/targetallocator-label/00-install.yaml +++ b/tests/e2e-targetallocator-cr/targetallocator-label/00-install.yaml @@ -3,6 +3,8 @@ apiVersion: opentelemetry.io/v1alpha1 kind: TargetAllocator metadata: name: ta + annotations: + e2e-test-annotation: "true" spec: --- apiVersion: opentelemetry.io/v1beta1 diff --git a/tests/e2e-targetallocator/targetallocator-features/02-assert.yaml b/tests/e2e-targetallocator/targetallocator-features/02-assert.yaml index 29b0bd3bb2..15123f73b9 100644 --- a/tests/e2e-targetallocator/targetallocator-features/02-assert.yaml +++ b/tests/e2e-targetallocator/targetallocator-features/02-assert.yaml @@ -4,6 +4,8 @@ metadata: name: stateful-targetallocator labels: app.kubernetes.io/name: test + annotations: + e2e-test-annotation: "true" spec: selector: matchLabels: diff --git a/tests/e2e-targetallocator/targetallocator-features/02-install.yaml b/tests/e2e-targetallocator/targetallocator-features/02-install.yaml index 3ee4c7a576..099cd84eff 100644 --- a/tests/e2e-targetallocator/targetallocator-features/02-install.yaml +++ b/tests/e2e-targetallocator/targetallocator-features/02-install.yaml @@ -4,4 +4,6 @@ metadata: name: stateful labels: app.kubernetes.io/name: test + annotations: + e2e-test-annotation: "true"