Skip to content

Commit

Permalink
Makefile refactor (adds verbosity)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed Jan 28, 2025
1 parent f0d9d74 commit 5409c89
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
MAKEFILE_DIR:=$(dir $(realpath $(firstword ${MAKEFILE_LIST})))
REPO_ROOT:=$(realpath ${MAKEFILE_DIR}../)

# Initcontainer language under test
INITCONTAINER_LANGUAGE:=${INITCONTAINER_LANGUAGE}

# Path to the local copy of the repo newrelic/k8s-agents-operator.
# Only required if building and testing the operator from source.
LOCAL_OPERATOR_REPO_PATH:=${REPO_ROOT}/../k8s-agents-operator
LOCAL_OPERATOR_REPO_PATH?=${REPO_ROOT}/../k8s-agents-operator

.PHONY: default
default: all
Expand All @@ -19,86 +16,110 @@ all: minikube operator test
# Start minikube
.PHONY: minikube
minikube:
minikube start --driver=docker
helm repo update
@echo "===== Starting minikube cluster. ====="
@minikube start --driver=docker

# Install official k8s-agents-operator repo
.PHONY: operator
operator:
helm uninstall k8s-agents-operator --ignore-not-found
helm repo add k8s-agents-operator https://newrelic.github.io/k8s-agents-operator
helm upgrade --install k8s-agents-operator k8s-agents-operator/k8s-agents-operator \
@echo "===== Removing any existing operator deployments. ====="
@helm uninstall k8s-agents-operator --ignore-not-found
@sleep 1
@echo "===== Adding and updating operator helm chart. ====="
@helm repo add --force-update k8s-agents-operator https://newrelic.github.io/k8s-agents-operator
@echo "===== Deploying operator to minikube cluster. ====="
@helm upgrade --install k8s-agents-operator k8s-agents-operator/k8s-agents-operator \
--set=licenseKey=${NEW_RELIC_LICENSE_KEY} \
--set=controllerManager.manager.image.version=latest
sleep 30
kubectl wait --for=condition=Ready pods $$(kubectl get pods | grep k8s-agents-operator | cut -d" " -f1)
--set=controllerManager.manager.image.version=edge
@sleep 5
@kubectl wait --for=condition=Ready pods $$(kubectl get pods | grep k8s-agents-operator | cut -d" " -f1)
@echo "===== Giving operator time to generate certificates. ====="
@sleep 30

# Build and install local copy of k8s-agents-operator
.PHONY: operator-local
operator-local:
eval $$(minikube docker-env --shell=bash) && \
@echo "===== Building operator container image. ====="
@eval $$(minikube docker-env --shell=bash) && \
docker build ${LOCAL_OPERATOR_REPO_PATH}/ -t e2e/k8s-agents-operator:e2e
helm uninstall k8s-agents-operator --ignore-not-found
sleep 1
helm upgrade --install k8s-agents-operator ${LOCAL_OPERATOR_REPO_PATH}/charts/k8s-agents-operator/ \
@echo "===== Removing any existing operator deployments. ====="
@helm uninstall k8s-agents-operator --ignore-not-found
@sleep 1
@echo "===== Adding and updating operator helm chart. ====="
@helm repo add --force-update k8s-agents-operator https://newrelic.github.io/k8s-agents-operator
@echo "===== Deploying operator to minikube cluster. ====="
@helm upgrade --install k8s-agents-operator ${LOCAL_OPERATOR_REPO_PATH}/charts/k8s-agents-operator/ \
--set=licenseKey=${NEW_RELIC_LICENSE_KEY} \
--set=newRelicHost=${NEW_RELIC_HOST} \
--set=controllerManager.manager.image.pullPolicy=Never \
--set=controllerManager.manager.image.repository=e2e/k8s-agents-operator \
--set=controllerManager.manager.image.version=e2e
sleep 30
kubectl wait --for=condition=Ready pods $$(kubectl get pods | grep k8s-agents-operator | cut -d" " -f1)
@sleep 5
@kubectl wait --for=condition=Ready pods $$(kubectl get pods | grep k8s-agents-operator | cut -d" " -f1)
@echo "===== Giving operator time to generate certificates. ====="
@sleep 30

# Set license key in default namespace
.PHONY: license_key_secret
license_key_secret:
kubectl delete secret newrelic-key-secret --ignore-not-found
kubectl create secret generic newrelic-key-secret \
@echo "===== Setting license key secret. ====="
@kubectl delete secret newrelic-key-secret --ignore-not-found
@kubectl create secret generic newrelic-key-secret \
--from-literal="new_relic_license_key=${NEW_RELIC_LICENSE_KEY}"

# Build local initcontainer image
.PHONY: build-initcontainer
build-initcontainer: check-language-arg
eval $$(minikube docker-env --shell=bash) && \
@echo "===== Building initcontainer image. ====="
@eval $$(minikube docker-env --shell=bash) && \
docker build -t e2e/newrelic-${INITCONTAINER_LANGUAGE}-init:e2e ${REPO_ROOT}/src/${INITCONTAINER_LANGUAGE}/

# Build local test app image
.PHONY: build-testapp
build-testapp: check-language-arg
eval $$(minikube docker-env --shell=bash) && \
@echo "===== Building test app container image. ====="
@eval $$(minikube docker-env --shell=bash) && \
docker build -t e2e/test-app-${INITCONTAINER_LANGUAGE}:e2e ${REPO_ROOT}/tests/${INITCONTAINER_LANGUAGE}/

# Deploy and open test app in browser
.PHONY: test
test: build-initcontainer build-testapp check-language-arg
# helm uninstall test-deployment --ignore-not-found
# sleep 5
# helm install test-deployment ${REPO_ROOT}/tests/${INITCONTAINER_LANGUAGE}/chart/
# sleep 5
# kubectl delete pods $$(kubectl get pods -n default | grep test-app- | cut -d" " -f1)
# sleep 5
# helm install --force test-deployment ${REPO_ROOT}/tests/${INITCONTAINER_LANGUAGE}/chart/
# sleep 5

kubectl wait --for=condition=Ready -n default --all pods
@echo "===== Removing any existing test app deployments. ====="
@helm uninstall test-deployment --ignore-not-found
@sleep 1
@echo "===== Deploying test app to minikube cluster. ====="
@helm install test-deployment ${REPO_ROOT}/tests/${INITCONTAINER_LANGUAGE}/chart/
@sleep 5
@kubectl wait --for=condition=Ready -n default --all pods
@echo "===== Recreating test app pod to ensure instrumentation resource is used. ====="
@kubectl delete pods -l "app=test-app-${INITCONTAINER_LANGUAGE}"
@sleep 5
@kubectl wait --for=condition=Ready -n default --all pods
# If yq is installed, check for the initcontainer to be visible on the pod spec
command -v yq && kubectl get pods --output yaml | yq '.items[].spec.initContainers[].name' | grep -q "newrelic-instrumentation-" || $$(echo "Operator failed to attach initcontainer." >&2; exit 1)
minikube service test-app-${INITCONTAINER_LANGUAGE}-service -n default
@which -s yq && \
echo "===== Ensuring initcontainer was successfully attached by operator. ====="; \
kubectl get pods --output yaml | yq '.items[].spec.initContainers[].name' | grep -q "newrelic-instrumentation-" \
&& echo "===== Initcontainer successfully attached. =====" \
|| { echo -e "===== Operator failed to attach initcontainer. =====" >&2; exit 1; }
@echo "===== Opening tunnel to test app and opening web browser. ====="
@minikube service test-app-${INITCONTAINER_LANGUAGE}-service -n default

# View test app container logs
.PHONY: get-pods
get-pods:
kubectl get pods -n default
@kubectl get pods -n default

# View test app container logs
.PHONY: logs-testapp
logs-testapp:
kubectl logs $$(kubectl get pods -n default | grep test-app- | cut -d" " -f1)
@echo "===== Test app logs: ====="
@kubectl logs $$(kubectl get pods -n default | grep test-app- | cut -d" " -f1)

# View test app container logs
.PHONY: logs-operator
logs-operator:
kubectl logs $$(kubectl get pods | grep k8s-agents-operator | cut -d" " -f1)
@echo "===== Operator logs: ====="
@kubectl logs $$(kubectl get pods | grep k8s-agents-operator | cut -d" " -f1)

.PHONY: check-language-arg
check-language-arg:
Expand Down

0 comments on commit 5409c89

Please sign in to comment.