Skip to content

docs(e2e): add GMP sidecar injection example script and test - #2042

Open
bwplotka wants to merge 1 commit into
mainfrom
add_gmp_injection_example
Open

docs(e2e): add GMP sidecar injection example script and test#2042
bwplotka wants to merge 1 commit into
mainfrom
add_gmp_injection_example

Conversation

@bwplotka

@bwplotka bwplotka commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Adds a bash script example for injecting GMP sidecars (prometheus and config-reloader) into an existing Kubernetes Deployment via strategic merge patch, alongside an E2E test verifying the injection.

The naming has been updated to use example-deployment and example-service instead of unreliable.

TAG=agy
CONV=bbab9ab7-4203-4ea3-a69b-2c40577629c2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new end-to-end test and a helper bash script to demonstrate and verify the injection of GMP sidecars into a Kubernetes deployment. The review feedback highlights several improvement opportunities in the bash script, including enabling pipefail for safer error handling, validating extracted cluster variables, using declarative kubectl apply for the ConfigMap, and avoiding the touch command in a distroless environment. Additionally, the Go test should be updated to clean up the created ConfigMap to prevent resource leaks.

Comment thread examples/inject-gmp-sidecar.sh Outdated
Comment thread examples/inject-gmp-sidecar.sh Outdated
Comment thread examples/inject-gmp-sidecar.sh Outdated
Comment thread examples/inject-gmp-sidecar.sh Outdated
Comment thread e2e/examples_test.go Outdated
@bwplotka
bwplotka force-pushed the add_gmp_injection_example branch 2 times, most recently from 0fbd8c4 to e4539f0 Compare August 5, 2026 10:44
@bwplotka
bwplotka marked this pull request as ready for review August 5, 2026 10:47
@bwplotka bwplotka changed the title Add GMP sidecar injection example script and e2e test docs(e2e): add GMP sidecar injection example script and test Aug 5, 2026
@bwplotka
bwplotka force-pushed the add_gmp_injection_example branch 2 times, most recently from bf4d04e to d9c3f6e Compare August 5, 2026 12:39
@dashpole
dashpole self-requested a review August 5, 2026 15:45
Comment thread examples/inject-gmp-sidecar.sh Outdated
PORT=80

# Extract labels.
CLUSTER_NAME=$(kubectl -n gmp-system get configmap/collector -o jsonpath='{.data.config\.yaml}' | grep 'cluster:' | awk '{print $2}' | tr -d '"')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason you changed this parsing from yq to grep/awk/tr? Seems more brittle and less readable to pass it through so many tools.

@bwplotka bwplotka Aug 5, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI decided there's no extra dependency to install, which is some benefit (:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming awk is available and grep has consistent behavior, etc.

We already have yq as a dependency of the project.
https://github.com/GoogleCloudPlatform/prometheus-engine/blob/main/tools/go.mod#L252

Comment thread e2e/examples_test.go
@@ -0,0 +1,124 @@
// Copyright 2024 Google LLC

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026?

Usually addlicense should handle this correctly.

@bernot-dev

Copy link
Copy Markdown
Collaborator
  1. Do we want this as part of our main e2e test suite? I supposed it's fine, but I didn't imagine the script being that "supported" because there still could be some user variation, depending on their setup.
  2. Should we add more comments/documentation somewhere to explain what the script is and what it does?

Comment on lines +35 to +38
# Images need to be updated periodically.
DISTROLESS_IMAGE=gke.gcr.io/gke-distroless/bash:gke_distroless_20260220.00_p0@sha256:828371616edc2c38e36868e2f8c992df37e484df72670f148de59867dfdd2490
PROMETHEUS_IMAGE=gke.gcr.io/prometheus-engine/prometheus:v2.53.5-gmp.4-gke.0@sha256:6f349dc0be36c8a61be183254f1126c9935f5332daa96c481f7e0e1b20fe0513
CONFIG_RELOADER_IMAGE=gke.gcr.io/prometheus-engine/config-reloader:v0.18.0-gke.2@sha256:b41862ee7ee3e9f24112ccdb0e53060085af1a8347054a7dbcff04467d3e1e9c

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should integrate this into our regular scanning/bumping. Or reference images from the manifests? 🤔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I was thinking to do this in separate step, but we should

Adds a bash script example for injecting GMP sidecars (prometheus and config-reloader) into an existing Kubernetes Deployment via strategic merge patch, alongside an E2E test verifying the injection.

The naming uses 'example-deployment' and 'example-service'.

TAG=agy
CONV=bbab9ab7-4203-4ea3-a69b-2c40577629c2
@bwplotka
bwplotka force-pushed the add_gmp_injection_example branch from d9c3f6e to f0cef33 Compare August 5, 2026 17:00
PROJECT_ID=$(kubectl -n gmp-system get configmap/collector -o jsonpath='{.data.config\.yaml}' | yq '.global.external_labels.project_id')


if [[ -z "${CLUSTER_NAME}" || -z "${LOCATION}" || -z "${PROJECT_ID}" ]]; then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If any of these label keys are missing in the ConfigMap, yq outputs the literal string "null" rather than an empty string, which bypasses the -z check. We should check for "null" explicitly as well:

Suggested change
if [[ -z "${CLUSTER_NAME}" || -z "${LOCATION}" || -z "${PROJECT_ID}" ]]; then
if [[ -z "${CLUSTER_NAME}" || "${CLUSTER_NAME}" == "null" || -z "${LOCATION}" || "${LOCATION}" == "null" || -z "${PROJECT_ID}" || "${PROJECT_ID}" == "null" ]]; then

Comment thread e2e/examples_test.go
if err := kubeClient.Create(ctx, deployment); err != nil {
t.Fatalf("error creating example-deployment: %s", err)
}
defer func() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the test times out and ctx expires, these deferred deletions will fail with context deadline exceeded, leaving the deployment and ConfigMap behind in the test cluster. Consider creating a fresh background context with a short timeout here for cleanup operations.

Comment thread e2e/examples_test.go
// 3. Verify sidecars are injected and ConfigMap is created.
if err := wait.PollUntilContextCancel(ctx, 2*time.Second, true, func(ctx context.Context) (bool, error) {
cm := &corev1.ConfigMap{}
if getErr := kubeClient.Get(ctx, client.ObjectKey{Name: "example-deployment", Namespace: "default"}, cm); getErr != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep this consistent with the cleanup logic above and avoid hardcoded strings, consider referencing deployment.Name and deployment.Namespace here and on line 100:

Suggested change
if getErr := kubeClient.Get(ctx, client.ObjectKey{Name: "example-deployment", Namespace: "default"}, cm); getErr != nil {
if getErr := kubeClient.Get(ctx, client.ObjectKey{Name: deployment.Name, Namespace: deployment.Namespace}, cm); getErr != nil {

Comment thread e2e/examples_test.go
return hasProm && hasReloader, nil
}); err != nil {
t.Fatalf("failed to verify injected sidecars or config map: %s", err)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This polling loop confirms that the Deployment spec in the API server was patched with the sidecars, but it doesn't verify that the pods actually start up cleanly at runtime without crashing or failing image pulls. Consider waiting for the deployment pods to reach ready status after this check so the test catches runtime failures or config unmarshalling errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants