Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/targetallocator-deployment-annotations.yaml
Original file line number Diff line number Diff line change
@@ -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: ""
5 changes: 4 additions & 1 deletion hack/check-operator-ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 24 additions & 5 deletions internal/controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -1947,6 +1955,7 @@ prometheus_cr:
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d",
"test": "test",
},
},
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -2079,6 +2091,7 @@ prometheus_cr:
},
Annotations: map[string]string{
"opentelemetry-targetallocator-config/hash": "f80c054419fe2f9030368557da143e200c70772d1d5f1be50ed55ae960b4b17d",
"test": "test",
},
},
Spec: policyV1.PodDisruptionBudgetSpec{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
22 changes: 21 additions & 1 deletion internal/manifests/targetallocator/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions internal/manifests/targetallocator/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
10 changes: 6 additions & 4 deletions internal/manifests/targetallocator/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
27 changes: 27 additions & 0 deletions internal/manifests/targetallocator/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/targetallocator/networkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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)
np := &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: params.TargetAllocator.Namespace,
Annotations: annotations,
Annotations: podAnnotations,
Labels: labels,
},
Spec: networkingv1.NetworkPolicySpec{
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/targetallocator/poddisruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ data:
filter_strategy: ""
kind: ConfigMap
metadata:
name: ta-targetallocator
name: ta-targetallocator
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ta-targetallocator
annotations:
e2e-test-annotation: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ apiVersion: opentelemetry.io/v1alpha1
kind: TargetAllocator
metadata:
name: ta
annotations:
e2e-test-annotation: "true"
spec:
---
apiVersion: opentelemetry.io/v1beta1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ metadata:
name: stateful-targetallocator
labels:
app.kubernetes.io/name: test
annotations:
e2e-test-annotation: "true"
spec:
selector:
matchLabels:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ metadata:
name: stateful
labels:
app.kubernetes.io/name: test
annotations:
e2e-test-annotation: "true"