Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(e2e): add e2e test for modifying existing deployments; fix: add env var DASH0_OTEL_COLLECTOR_BASE_URL to containers #5

Merged
merged 5 commits into from
May 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion internal/controller/dash0_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (r *Dash0Reconciler) modifyExistingResources(ctx context.Context, dash0Cust
}, &deployment); err != nil {
return fmt.Errorf("error when fetching deployment %s/%s: %w", deployment.GetNamespace(), deployment.GetName(), err)
}
hasBeenModified := k8sresources.ModifyPodSpec(&deployment.Spec.Template.Spec, logger)
hasBeenModified := k8sresources.ModifyPodSpec(&deployment.Spec.Template.Spec, deployment.GetNamespace(), logger)
if hasBeenModified {
return r.Client.Update(ctx, &deployment)
} else {
Expand Down
10 changes: 6 additions & 4 deletions internal/controller/dash0_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ var _ = Describe("Dash0 Controller", func() {
InitContainers: 1,
Dash0InitContainerIdx: 0,
Containers: []ContainerExpectations{{
VolumeMounts: 1,
Dash0VolumeMountIdx: 0,
EnvVars: 1,
NodeOptionsEnvVarIdx: 0,
VolumeMounts: 1,
Dash0VolumeMountIdx: 0,
EnvVars: 2,
NodeOptionsEnvVarIdx: 0,
Dash0CollectorBaseUrlEnvVarIdx: 1,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
}},
})
VerifySuccessEvent(ctx, clientset, namespace, deploymentName, "controller")
Expand Down
40 changes: 33 additions & 7 deletions internal/k8sresources/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const (
dash0InstrumentationDirectory = "/opt/dash0/instrumentation"
// envVarLdPreloadName = "LD_PRELOAD"
// envVarLdPreloadValue = "/opt/dash0/preload/inject.so"
envVarNodeOptionsName = "NODE_OPTIONS"
envVarNodeOptionsValue = "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js"
envVarNodeOptionsName = "NODE_OPTIONS"
envVarNodeOptionsValue = "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js"
envVarDash0CollectorBaseUrlName = "DASH0_OTEL_COLLECTOR_BASE_URL"
envVarDash0CollectorBaseUrlNameValueTemplate = "http://dash0-opentelemetry-collector-daemonset.%s.svc.cluster.local:4318"
)

var (
Expand All @@ -36,13 +38,13 @@ var (
initContainerReadOnlyRootFilesystem = true
)

func ModifyPodSpec(podSpec *corev1.PodSpec, logger logr.Logger) bool {
func ModifyPodSpec(podSpec *corev1.PodSpec, namespace string, logger logr.Logger) bool {
originalSpec := podSpec.DeepCopy()
addInstrumentationVolume(podSpec)
addInitContainer(podSpec)
for idx := range podSpec.Containers {
container := &podSpec.Containers[idx]
instrumentContainer(container, logger)
instrumentContainer(container, namespace, logger)
}
return !reflect.DeepEqual(originalSpec, podSpec)
}
Expand Down Expand Up @@ -129,10 +131,10 @@ func createInitContainer(podSpec *corev1.PodSpec) *corev1.Container {
}
}

func instrumentContainer(container *corev1.Container, logger logr.Logger) {
func instrumentContainer(container *corev1.Container, namespace string, logger logr.Logger) {
logger = logger.WithValues("container", container.Name)
addMount(container)
addEnvironmentVariables(container, logger)
addEnvironmentVariables(container, namespace, logger)
}

func addMount(container *corev1.Container) {
Expand All @@ -154,9 +156,14 @@ func addMount(container *corev1.Container) {
}
}

func addEnvironmentVariables(container *corev1.Container, logger logr.Logger) {
func addEnvironmentVariables(container *corev1.Container, namespace string, logger logr.Logger) {
// For now, we directly modify NODE_OPTIONS. Consider migrating to an LD_PRELOAD hook at some point.
addOrPrependToEnvironmentVariable(container, envVarNodeOptionsName, envVarNodeOptionsValue, logger)

addOrReplaceEnvironmentVariable(
container,
envVarDash0CollectorBaseUrlName,
fmt.Sprintf(envVarDash0CollectorBaseUrlNameValueTemplate, namespace))
}

func addOrPrependToEnvironmentVariable(container *corev1.Container, name string, value string, logger logr.Logger) {
Expand Down Expand Up @@ -186,3 +193,22 @@ func addOrPrependToEnvironmentVariable(container *corev1.Container, name string,
container.Env[idx].Value = fmt.Sprintf("%s %s", value, previousValue)
}
}

func addOrReplaceEnvironmentVariable(container *corev1.Container, name string, value string) {
if container.Env == nil {
container.Env = make([]corev1.EnvVar, 0)
}
idx := slices.IndexFunc(container.Env, func(c corev1.EnvVar) bool {
return c.Name == name
})

if idx < 0 {
container.Env = append(container.Env, corev1.EnvVar{
Name: name,
Value: value,
})
} else {
container.Env[idx].ValueFrom = nil
container.Env[idx].Value = value
}
}
62 changes: 36 additions & 26 deletions internal/k8sresources/modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
// intentional. However, this test should be used for more fine-grained test cases, while dash0_webhook_test.go should
// be used to verify external effects (recording events etc.) that cannot be covered in this test.

var _ = Describe("Dash0 Webhook", func() {
var _ = Describe("Dash0 Resource Modification", func() {

ctx := context.Background()

Context("when mutating new deployments", func() {
It("should inject Dash into a new basic deployment", func() {
deployment := BasicDeployment(TestNamespaceName, DeploymentName)
result := ModifyPodSpec(&deployment.Spec.Template.Spec, log.FromContext(ctx))
result := ModifyPodSpec(&deployment.Spec.Template.Spec, TestNamespaceName, log.FromContext(ctx))

Expect(result).To(BeTrue())
VerifyModifiedDeployment(deployment, DeploymentExpectations{
Expand All @@ -34,17 +34,19 @@ var _ = Describe("Dash0 Webhook", func() {
InitContainers: 1,
Dash0InitContainerIdx: 0,
Containers: []ContainerExpectations{{
VolumeMounts: 1,
Dash0VolumeMountIdx: 0,
EnvVars: 1,
NodeOptionsEnvVarIdx: 0,
VolumeMounts: 1,
Dash0VolumeMountIdx: 0,
EnvVars: 2,
NodeOptionsEnvVarIdx: 0,
Dash0CollectorBaseUrlEnvVarIdx: 1,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
}},
})
})

It("should inject Dash into a new deployment that has multiple Containers, and already has Volumes and init Containers", func() {
deployment := DeploymentWithMoreBellsAndWhistles(TestNamespaceName, DeploymentName)
result := ModifyPodSpec(&deployment.Spec.Template.Spec, log.FromContext(ctx))
result := ModifyPodSpec(&deployment.Spec.Template.Spec, TestNamespaceName, log.FromContext(ctx))

Expect(result).To(BeTrue())
VerifyModifiedDeployment(deployment, DeploymentExpectations{
Expand All @@ -54,24 +56,28 @@ var _ = Describe("Dash0 Webhook", func() {
Dash0InitContainerIdx: 2,
Containers: []ContainerExpectations{
{
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 2,
NodeOptionsEnvVarIdx: 1,
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
Dash0CollectorBaseUrlEnvVarIdx: 2,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
{
VolumeMounts: 3,
Dash0VolumeMountIdx: 2,
EnvVars: 3,
NodeOptionsEnvVarIdx: 2,
VolumeMounts: 3,
Dash0VolumeMountIdx: 2,
EnvVars: 4,
NodeOptionsEnvVarIdx: 2,
Dash0CollectorBaseUrlEnvVarIdx: 3,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
},
})
})

It("should update existing Dash artifacts in a new deployment", func() {
deployment := DeploymentWithExistingDash0Artifacts(TestNamespaceName, DeploymentName)
result := ModifyPodSpec(&deployment.Spec.Template.Spec, log.FromContext(ctx))
result := ModifyPodSpec(&deployment.Spec.Template.Spec, TestNamespaceName, log.FromContext(ctx))

Expect(result).To(BeTrue())
VerifyModifiedDeployment(deployment, DeploymentExpectations{
Expand All @@ -81,18 +87,22 @@ var _ = Describe("Dash0 Webhook", func() {
Dash0InitContainerIdx: 1,
Containers: []ContainerExpectations{
{
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 2,
NodeOptionsEnvVarIdx: 1,
NodeOptionsUsesValueFrom: true,
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
NodeOptionsUsesValueFrom: true,
Dash0CollectorBaseUrlEnvVarIdx: 2,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
{
VolumeMounts: 3,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
NodeOptionsValue: "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js --require something-else --experimental-modules",
VolumeMounts: 3,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
NodeOptionsValue: "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js --require something-else --experimental-modules",
Dash0CollectorBaseUrlEnvVarIdx: 0,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions internal/webhook/dash0_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *Handler) SetupWebhookWithManager(mgr ctrl.Manager) error {
return nil
}

func (h *Handler) Handle(ctx context.Context, request admission.Request) admission.Response {
func (h *Handler) Handle(_ context.Context, request admission.Request) admission.Response {
logger := log.WithValues("gvk", request.Kind, "namespace", request.Namespace, "name", request.Name)
logger.Info("incoming admission request")

Expand All @@ -59,7 +59,7 @@ func (h *Handler) Handle(ctx context.Context, request admission.Request) admissi
return admission.Errored(http.StatusInternalServerError, fmt.Errorf("error while parsing the resource: %w", err))
}

hasBeenModified := k8sresources.ModifyPodSpec(&deployment.Spec.Template.Spec, logger)
hasBeenModified := k8sresources.ModifyPodSpec(&deployment.Spec.Template.Spec, request.Namespace, logger)
if !hasBeenModified {
return admission.Allowed("no changes")
}
Expand Down
54 changes: 32 additions & 22 deletions internal/webhook/dash0_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ var _ = Describe("Dash0 Webhook", func() {
InitContainers: 1,
Dash0InitContainerIdx: 0,
Containers: []ContainerExpectations{{
VolumeMounts: 1,
Dash0VolumeMountIdx: 0,
EnvVars: 1,
NodeOptionsEnvVarIdx: 0,
VolumeMounts: 1,
Dash0VolumeMountIdx: 0,
EnvVars: 2,
NodeOptionsEnvVarIdx: 0,
Dash0CollectorBaseUrlEnvVarIdx: 1,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
}},
})
})
Expand All @@ -51,16 +53,20 @@ var _ = Describe("Dash0 Webhook", func() {
Dash0InitContainerIdx: 2,
Containers: []ContainerExpectations{
{
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 2,
NodeOptionsEnvVarIdx: 1,
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
Dash0CollectorBaseUrlEnvVarIdx: 2,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
{
VolumeMounts: 3,
Dash0VolumeMountIdx: 2,
EnvVars: 3,
NodeOptionsEnvVarIdx: 2,
VolumeMounts: 3,
Dash0VolumeMountIdx: 2,
EnvVars: 4,
NodeOptionsEnvVarIdx: 2,
Dash0CollectorBaseUrlEnvVarIdx: 3,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
},
})
Expand All @@ -79,18 +85,22 @@ var _ = Describe("Dash0 Webhook", func() {
Dash0InitContainerIdx: 1,
Containers: []ContainerExpectations{
{
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 2,
NodeOptionsEnvVarIdx: 1,
NodeOptionsUsesValueFrom: true,
VolumeMounts: 2,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
NodeOptionsUsesValueFrom: true,
Dash0CollectorBaseUrlEnvVarIdx: 2,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
{
VolumeMounts: 3,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
NodeOptionsValue: "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js --require something-else --experimental-modules",
VolumeMounts: 3,
Dash0VolumeMountIdx: 1,
EnvVars: 3,
NodeOptionsEnvVarIdx: 1,
NodeOptionsValue: "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js --require something-else --experimental-modules",
Dash0CollectorBaseUrlEnvVarIdx: 0,
Dash0CollectorBaseUrlEnvVarExpectedValue: "http://dash0-opentelemetry-collector-daemonset.test-namespace.svc.cluster.local:4318",
},
},
})
Expand Down
7 changes: 7 additions & 0 deletions test-resources/bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Test Scripts
============

The scripts in this folder are meant for running semi-manual tests.
They deploy the operator and applications under monitoring - this part is automated via scripts.
Whether the instrumentation was successful or not needs to be verified manually.
This can also be tested in a fully automated way by the tests in `test/e2e`.
16 changes: 16 additions & 0 deletions test-resources/bin/ensure-namespace-exists.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

set -euo pipefail

target_namespace=${1:-default}

if [[ "${target_namespace}" == default ]]; then
exit 0
fi

if ! kubectl get ns ${target_namespace} &> /dev/null; then
kubectl create ns ${target_namespace}
fi
13 changes: 10 additions & 3 deletions test-resources/bin/test-cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#!/usr/bin/env bash

# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

set -euo pipefail


cd "$(dirname ${BASH_SOURCE})"/../..

kubectl delete -k config/samples || true
target_namespace=${1:-default}

kubectl delete -n ${target_namespace} -k config/samples || true
make uninstall || true
make undeploy || true
test-resources/node.js/express/undeploy.sh
test-resources/collector/undeploy.sh
test-resources/node.js/express/undeploy.sh ${target_namespace}
test-resources/collector/undeploy.sh ${target_namespace}
Loading