Skip to content

Commit

Permalink
feat: [TKC-2622] entrypoint telemetry (#5893)
Browse files Browse the repository at this point in the history
* feat: testkube kind Docker image

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: tune entrypoint

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: removed

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: remove files

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add logs

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: cloud image

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* Update build/kind/oss/Dockerfile.kind

Co-authored-by: Dawid Rusnak <dawid@drcode.pl>

* fix: remove org and env id

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: move folder

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: remove example

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: add docker container env

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: docker telemetry

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: change params

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: add entrypoint telemetry

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* feat: add entrypoint telemetry

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix error message

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

* fix: change parameter

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>

---------

Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>
Co-authored-by: Dawid Rusnak <dawid@drcode.pl>
  • Loading branch information
vsukhin and rangoo94 authored Oct 3, 2024
1 parent 4aba7f8 commit db5f27b
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 2 deletions.
9 changes: 8 additions & 1 deletion build/kind/Dockerfile.kind
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FROM docker:20.10.24-dind

# Step 2: Install necessary dependencies (curl, bash, tini)
RUN apk add --no-cache bash curl tini
RUN apk add --no-cache bash curl tini jq

# Step 3: Install Kind (Kubernetes in Docker)
RUN curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64 && \
Expand All @@ -26,5 +26,12 @@ RUN chmod +x /usr/local/bin/entrypoint.sh
COPY ./images /images
COPY k6.yaml /examples/k6.yaml

ARG segmentio_key
ENV SEGMENTIO_KEY=$segmentio_key
ARG ga_id
ENV GA_ID=$ga_id
ARG ga_secret
ENV GA_SECRET=$ga_secret

# Step 8: Set Docker entry point for DIND (Docker-in-Docker)
ENTRYPOINT ["tini", "--", "/usr/local/bin/entrypoint.sh"]
169 changes: 168 additions & 1 deletion build/kind/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,176 @@ log() {
echo "[INFO] $1"
}

_detect_arch() {
case $(uname -m) in
amd64|x86_64) echo "x86_64"
;;
arm64|aarch64) echo "arm64"
;;
i386) echo "i386"
;;
*) echo "Unsupported processor architecture";
;;
esac
}

_detect_os(){
case $(uname) in
Linux) echo "Linux"
;;
Darwin) echo "Darwin"
;;
Windows) echo "Windows"
;;
esac
}

_detect_version() {
local tag

tag="$(
curl -s "https://api.github.com/repos/kubeshop/testkube/releases/latest" \
2>/dev/null \
| jq -r '.tag_name' \
)"

echo "${tag/#v/}" # remove leading v if present

}

_calculate_machine_id() {
local hash

# Calculate hash using md5sum
hash=$(echo -n "$(hostname)" | md5sum | awk '{print $1}')

echo "$hash"
}

version="$(_detect_version)"
arch="$(_detect_arch)"
os="$(_detect_os)"
machine_id="$(_calculate_machine_id)"

# Function to send event message to Segment.io
send_event_to_segment() {
local event="$1"
local error_code="$2"
local error_type="$3"
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

# Prepare the JSON payload
local payload=$(cat <<EOF
{
"userId": "$machine_id",
"event": "$event",
"properties": {
"name": "testkube-api-server",
"version": "$version",
"arch": "$arch",
"os": "$os",
"eventCategory": "api",
"containerEnv": "docker",
"contextType": "agent",
"machineId": "$machine_id",
"clusterType": "kind",
"errorType": "$error_type",
"errorCode": "$error_code",
"agentKey": "$AGENT_KEY"
},
"context": {
"app": {
"name": "testkube-api-server",
"version": "$version",
"build": "cloud"
}
},
"timestamp": "$timestamp"
}
EOF
)

# Send the message to Segment via HTTP API
curl -X POST -H "Content-Type: application/json" -u "$SEGMENTIO_KEY:" -d "$payload" https://api.segment.io/v1/track

# Check if the curl command was successful
if [ $? -eq 0 ]; then
log "Message successfully sent to Segment."
else
log "Failed to send message to Segment."
fi
}

# Function to send event message to GA
send_event_to_ga() {
local event="$1"
local error_code="$2"
local error_type="$3"

# Prepare the JSON payload
local payload=$(cat <<EOF
{
"client_id": "$machine_id",
"user_id": "$machine_id",
"events": [{
"name": "$event",
"params": {
"event_count": 1,
"event_category": "api",
"app_version": "$version",
"app_name": "testkube-api-server",
"machine_id": "$machine_id",
"operating_system": "$os",
"architecture": "$arch",
"context": {
"container_env": "docker",
"type": "agent"
},
"cluster_type": "kind",
"error_type": "$error_type",
"error_code": "$error_code",
"agent_key": "$AGENT_KEY"
}
}]
}
EOF
)

# Send the message to GA via HTTP API
# "
curl -X POST -H "Content-Type: application/json" -d "$payload" "https://www.google-analytics.com/mp/collect?measurement_id=$GA_ID&api_secret=$GA_SECRET"

# Check if the curl command was successful
if [ $? -eq 0 ]; then
log "Message successfully sent to GA."
else
log "Failed to send message to GA."
fi

}

send_telenetry() {
local event="$1"
local error_code="$2"
local error_type="$3"

send_event_to_segment "$event" "$error_code" "$error_type"
send_event_to_ga "$event" "$error_code" "$error_type"
}

send_telenetry "docker_installation_started"

# Check if agent key is provided
if [ -z "$AGENT_KEY" ]; then
log "Please provide AGENT_KEY env var"
send_telenetry "docker_installation_failed" "parameter_not_found" "agent key is empty"
exit 1
fi

# Check if cloud url is provided
if [ -z "$CLOUD_URL" ]; then
log "Please provide CLOUD_URL env var"
send_telenetry "docker_installation_failed" "parameter_not_found" "cloud url is empty"
exit 1
fi

Expand All @@ -40,6 +201,7 @@ log "Creating Kubernetes cluster using Kind (Kubernetes v1.31.0)..."
kind create cluster --name testkube-cluster --image kindest/node:v1.31.0 --wait 5m
if [ $? -ne 0 ]; then
log "Failed to create Kind cluster."
send_telenetry "docker_installation_failed" "kind_error" "Kind cluster was not created"
exit 1
fi

Expand All @@ -48,6 +210,7 @@ log "Verifying cluster is up..."
kubectl cluster-info
if [ $? -ne 0 ]; then
log "Failed to verify cluster."
send_telenetry "docker_installation_failed" "kind_error" "Kind cluster is nor accessible"
exit 1
fi

Expand All @@ -61,6 +224,7 @@ log "Installing Testkube via Helm..."
helm install testkube testkube/testkube --namespace testkube --create-namespace --set testkube-api.cloud.key=$AGENT_KEY --set testkube-api.minio.enabled=false --set mongodb.enabled=false --set testkube-dashboard.enabled=false --set testkube-api.cloud.url=$CLOUD_URL --set testkube-api.containerEnv=docker
if [ $? -ne 0 ]; then
log "Testkube installation failed."
send_telenetry "docker_installation_failed" "helm_error" "Testkube installation failed"
exit 1
fi

Expand All @@ -75,7 +239,8 @@ do

# Check if there are any pods in the Testkube namespace
if [ -z "$pod_status" ]; then
log "No pods found in Testkube namespace."
log "No pods found in testkube namespace."
send_telenetry "docker_installation_failed" "tetkube_error" "No pods found in testkube namespace"
exit 1
fi

Expand Down Expand Up @@ -124,6 +289,7 @@ done

if [ $counter -eq 15 ]; then
log "Testkube validation failed."
send_telenetry "docker_installation_failed" "tetkube_error" "Testkube pods are not up and running"
exit 1
fi
log "Testkube is up and running."
Expand All @@ -134,6 +300,7 @@ kubectl apply -f /examples/k6.yaml -n testkube

log "Testkube installation successful!"
log "You can now use Testkube in your Kind Kubernetes cluster."
send_telenetry "docker_installation_succeed"

# Step 10: Bring docker service back to foreground
fg %1

0 comments on commit db5f27b

Please sign in to comment.