-
Notifications
You must be signed in to change notification settings - Fork 6
Add otel4k8s script #146
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
Merged
Merged
Add otel4k8s script #146
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| #!/bin/bash | ||
| # This script assumes that you are using a k8s environment and have deployed an application simulation already. | ||
| # | ||
| # It has been tested with the following setup instructions for the cluster: | ||
| # minikube start --cpus 6 --memory 10g --network-plugin=cni --cni=false && cilium install && cilium status --wait | ||
| # | ||
| # With the simulation deployed into the k8s namespace "my-simulation", run it as follows: | ||
| # | ||
| # ./otel4k8s.sh "my-simulation" | ||
| # | ||
| # This will deploy cert manager, otel operater, a LGTM-stack in a single container and the instrumentation instructions | ||
| # for your applications. The last step will restart all pods that belong to the simulation. | ||
| # | ||
| # For a more detailed configuration run | ||
| # | ||
| # ./otel4k8s.sh --help | ||
| # | ||
| # NOTE: Currently only java will be auto instrumented, other languages will be added soon | ||
|
|
||
| CERT_MANAGER_VERSION=1.16.3 | ||
| O11Y_NAMESPACE=o11y | ||
| APP_NAMESPACE="" | ||
|
|
||
| WITH_CERT_MANAGER=1 | ||
| WITH_OTEL_OPERATOR=1 | ||
| WITH_LGTM_CONTAINER=1 | ||
|
|
||
| KUBECTL="kubectl" | ||
|
|
||
| # Function to display help | ||
| show_help() { | ||
| echo "Usage: otel4k8s.sh [OPTION]" | ||
| echo "Deploy cert manager, OpenTelemetry operator, a LGTM-stack in a single container and automatic instrumentation for a running app simulation" | ||
| echo | ||
| echo "Options:" | ||
| echo " --patch Increase the patch number by 1." | ||
| echo " (no option) Display the current version." | ||
| } | ||
|
|
||
| if [[ $# -eq 1 && ${1} != --* ]]; then | ||
| APP_NAMESPACE=${1} | ||
| else | ||
|
|
||
| for ARG in "$@"; do | ||
| case "$ARG" in | ||
| --help) | ||
| show_help | ||
| exit 0 | ||
| ;; | ||
| --app-namespace=*) | ||
| APP_NAMESPACE="${ARG#*=}" | ||
| shift | ||
| ;; | ||
| --o11y-namespace=*) | ||
| O11Y_NAMESPACE="${ARG#*=}" | ||
| shift | ||
| ;; | ||
| --no-cert-manager) | ||
| WITH_CERT_MANAGER=0 | ||
| shift | ||
| ;; | ||
| --no-lgtm) | ||
| WITH_LGTM_CONTAINER=0 | ||
| shift | ||
| ;; | ||
| --no-otel-operator) | ||
| WITH_OTEL_OPERATOR=0 | ||
| shift | ||
| ;; | ||
| --dry-run) | ||
| KUBECTL="echo kubectl" | ||
| shift | ||
| ;; | ||
| *) | ||
| echo "Unknown option: $ARG" | ||
| show_help | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| fi | ||
|
|
||
| # helper function that will rerun commands until they are successful | ||
| retry_until_success() { | ||
| local delay=5 # Seconds to wait between retries | ||
| local max_attempts=0 # 0 means unlimited retries | ||
| local attempt=0 | ||
|
|
||
| while true; do | ||
| "$@" && return 0 # Run the command; if it succeeds, return success | ||
|
|
||
| attempt=$((attempt + 1)) | ||
| echo "Attempt $attempt failed. Retrying in $delay seconds..." | ||
|
|
||
| if [[ $max_attempts -gt 0 && $attempt -ge $max_attempts ]]; then | ||
| echo "Maximum retry attempts reached. Giving up." | ||
| return 1 | ||
| fi | ||
|
|
||
| sleep "$delay" | ||
| done | ||
| } | ||
|
|
||
| # Install Cert Manager | ||
| if [[ ${WITH_CERT_MANAGER} -eq 1 ]]; then | ||
| retry_until_success ${KUBECTL} apply -f "https://github.com/cert-manager/cert-manager/releases/download/v${CERT_MANAGER_VERSION}/cert-manager.yaml" | ||
| fi | ||
|
|
||
| # Install the OTel operator | ||
| if [[ ${WITH_OTEL_OPERATOR} -eq 1 ]]; then | ||
| retry_until_success ${KUBECTL} apply -f https://github.com/open-telemetry/opentelemetry-operator/releases/latest/download/opentelemetry-operator.yaml | ||
| fi | ||
|
|
||
| # Install LGTM in one container | ||
| if [[ ${WITH_LGTM_CONTAINER} -eq 1 ]]; then | ||
| ${KUBECTL} create namespace ${O11Y_NAMESPACE} | ||
| retry_until_success ${KUBECTL} apply -f https://raw.githubusercontent.com/grafana/docker-otel-lgtm/refs/heads/main/k8s/lgtm.yaml --namespace "${O11Y_NAMESPACE}" | ||
| fi | ||
|
|
||
| add_otel_instrumentation() { | ||
| ${KUBECTL} apply --namespace "${APP_NAMESPACE}" -f - <<EOF | ||
| apiVersion: opentelemetry.io/v1alpha1 | ||
| kind: Instrumentation | ||
| metadata: | ||
| name: my-instrumentation | ||
| spec: | ||
| exporter: | ||
| endpoint: http://lgtm.${O11Y_NAMESPACE}.svc.cluster.local:4317 | ||
| sampler: | ||
| type: always_on | ||
| EOF | ||
| } | ||
|
|
||
| retry_until_success add_otel_instrumentation | ||
|
|
||
| # TODO: This is currently only injecting the java automatic instrumentation, we need a way to identify the language and inject the right one | ||
| # see https://github.com/cisco-open/app-simulator/issues/147 | ||
| for deployment in $(kubectl get deployments -n "${APP_NAMESPACE}" -o jsonpath='{.items[*].metadata.name}'); do | ||
| ${KUBECTL} patch deployment ${deployment} -n "${APP_NAMESPACE}" -p '{"spec": {"template":{"metadata":{"annotations":{"instrumentation.opentelemetry.io/inject-java":"true"}}}} }' | ||
| done | ||
noMoreCLI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ${KUBECTL} rollout restart deployment -n "${APP_NAMESPACE}" | ||
|
|
||
| ${KUBECTL} port-forward service/lgtm 3000:3000 4317:4317 4318:4318 --namespace ${O11Y_NAMESPACE} & | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.