Skip to content

Commit

Permalink
Update the target allocator on any manifest change (#1027)
Browse files Browse the repository at this point in the history
* Update the target allocator on any manifest change

* Change test to check for prometheusCR change validation

* Fix test

* Updated test to be more flexible for the future
  • Loading branch information
jaronoff97 authored Aug 10, 2022
1 parent 4eb4054 commit 07b9f22
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
4 changes: 4 additions & 0 deletions pkg/collector/parser/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func singlePortFromConfigEndpoint(logger logr.Logger, name string, config map[in
case name == "kubeletstats":
return nil

// ignore prometheus receiver as it has no listening endpoint
case name == "prometheus":
return nil

default:
endpoint = getAddressFromConfig(logger, name, endpointKey, config)
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/collector/reconcile/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ func expectedDeployments(ctx context.Context, params Params, expected []appsv1.D
updated.Labels = map[string]string{}
}

if desired.Labels["app.kubernetes.io/component"] == "opentelemetry-targetallocator" {
updated.Spec.Template.Spec.Containers[0].Image = desired.Spec.Template.Spec.Containers[0].Image
} else {
updated.Spec = desired.Spec
}
updated.Spec = desired.Spec
updated.ObjectMeta.OwnerReferences = desired.ObjectMeta.OwnerReferences

for k, v := range desired.ObjectMeta.Annotations {
Expand Down
46 changes: 23 additions & 23 deletions pkg/collector/reconcile/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ func TestExpectedDeployments(t *testing.T) {
assert.Len(t, expected, 0)
})

t.Run("should update target allocator deployment when the prometheusCR is updated", func(t *testing.T) {
ctx := context.Background()
createObjectIfNotExists(t, "test-targetallocator", &expectedTADeploy)
orgUID := expectedTADeploy.OwnerReferences[0].UID

updatedParam, err := newParams(expectedTADeploy.Spec.Template.Spec.Containers[0].Image, "")
assert.NoError(t, err)
updatedParam.Instance.Spec.TargetAllocator.PrometheusCR.Enabled = true
updatedDeploy := targetallocator.Deployment(updatedParam.Config, logger, updatedParam.Instance)

err = expectedDeployments(ctx, param, []v1.Deployment{updatedDeploy})
assert.NoError(t, err)

actual := v1.Deployment{}
exists, err := populateObjectIfExists(t, &actual, types.NamespacedName{Namespace: "default", Name: "test-targetallocator"})

assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, orgUID, actual.OwnerReferences[0].UID)
assert.ElementsMatch(t, actual.Spec.Template.Spec.Containers[0].Args, []string{"--enable-prometheus-cr-watcher"})
assert.Equal(t, int32(1), *actual.Spec.Replicas)
})

t.Run("should not update target allocator deployment replicas when collector max replicas is set", func(t *testing.T) {
replicas, maxReplicas := int32(2), int32(10)
param := Params{
Expand Down Expand Up @@ -168,29 +191,6 @@ func TestExpectedDeployments(t *testing.T) {
assert.Equal(t, int32(2), *actual.Spec.Replicas)
})

t.Run("should not update target allocator deployment when the container image is not updated", func(t *testing.T) {
ctx := context.Background()
createObjectIfNotExists(t, "test-targetallocator", &expectedTADeploy)
orgUID := expectedTADeploy.OwnerReferences[0].UID

updatedParam, err := newParams("test/test-img", "")
assert.NoError(t, err)
updatedDeploy := targetallocator.Deployment(updatedParam.Config, logger, param.Instance)
*updatedDeploy.Spec.Replicas = int32(3)

err = expectedDeployments(ctx, param, []v1.Deployment{updatedDeploy})
assert.NoError(t, err)

actual := v1.Deployment{}
exists, err := populateObjectIfExists(t, &actual, types.NamespacedName{Namespace: "default", Name: "test-targetallocator"})

assert.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, orgUID, actual.OwnerReferences[0].UID)
assert.Equal(t, expectedTADeploy.Spec.Template.Spec.Containers[0].Image, actual.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, int32(1), *actual.Spec.Replicas)
})

t.Run("should update target allocator deployment when the container image is updated", func(t *testing.T) {
ctx := context.Background()
createObjectIfNotExists(t, "test-targetallocator", &expectedTADeploy)
Expand Down

0 comments on commit 07b9f22

Please sign in to comment.