Skip to content

Commit a0308bb

Browse files
authored
Add support for TLS & tests (#146)
* Add support for TLS & tests Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com> * TLS support: Helm charts & custom examples in dev Makefile Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com> --------- Signed-off-by: Jirka Kremser <jiri.kremser@gmail.com>
1 parent 13af8bf commit a0308bb

File tree

18 files changed

+719
-81
lines changed

18 files changed

+719
-81
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ helmchart/otel-add-on/*.tgz
1515

1616
# Other
1717
/vendor/
18+
19+
# Tests
20+
/certs
21+
values_tmp

Makefile

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
###############################
44
SHELL = /bin/bash
55
GH_REPO_ORG = kedify
6-
VERSION ?= main
6+
VERSION ?= main
77
GIT_COMMIT ?= $(shell git rev-list -1 HEAD)
8-
LATEST_TAG ?= $(shell git fetch --force --tags &> /dev/null ; git describe --tags $(git rev-list --tags --max-count=1))
8+
LATEST_TAG ?= $(shell git fetch --force --tags &> /dev/null ; git describe --tags $(git rev-list --tags --max-count=1))
99
GO_LDFLAGS="-X github.com/${GH_REPO_ORG}/otel-add-on/build.version=${VERSION} -X github.com/${GH_REPO_ORG}/otel-add-on/build.gitCommit=${GIT_COMMIT}"
1010
BUILD_PLATFORMS ?= linux/amd64,linux/arm64
1111

@@ -18,43 +18,61 @@ endif
1818
CGO ?=0
1919
TARGET_OS ?=linux
2020

21+
define SERVER_DOMAINS
22+
basicConstraints=CA:FALSE
23+
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
24+
extendedKeyUsage = serverAuth, clientAuth
25+
subjectAltName = @alt_names
26+
[alt_names]
27+
DNS.1 = localhost
28+
DNS.2 = *.keda.svc.cluster.local
29+
DNS.3 = *.keda.svc
30+
DNS.4 = *.keda
31+
IP.1 = 127.0.0.1
32+
endef
33+
export SERVER_DOMAINS
34+
2135
GO_BUILD_VARS= GO111MODULE=on CGO_ENABLED=$(CGO) GOOS=$(TARGET_OS) GOARCH=$(ARCH)
2236

2337
###############################
2438
# TARGETS
2539
###############################
2640
all: help
2741

42+
##@ Build
43+
2844
.PHONY: build
29-
build: ## Builds the binary.
45+
build: ## Builds the binary.
3046
@$(call say,Build the binary)
3147
${GO_BUILD_VARS} go build -ldflags $(GO_LDFLAGS) -o bin/otel-add-on .
3248

33-
.PHONY: run
34-
run: ## Runs the scaler locally.
35-
go run ./main.go
36-
3749
.PHONY: build-image
38-
build-image: build ## Builds the container image for current arch.
50+
build-image: build ## Builds the container image for current arch.
3951
@$(call say,Build container image $(CONTAINER_IMAGE))
4052
docker build . -t ${CONTAINER_IMAGE} --build-arg VERSION=${VERSION} --build-arg GIT_COMMIT=${GIT_COMMIT}
4153

4254
.PHONY: build-image-multiarch
43-
build-image-multiarch: ## Builds the container image for arm64 and amd64.
55+
build-image-multiarch: ## Builds the container image for arm64 and amd64.
4456
@$(call say,Build container image $(CONTAINER_IMAGE))
4557
docker buildx build --output=type=registry --platform=${BUILD_PLATFORMS} . -t ${CONTAINER_IMAGE} --build-arg VERSION=${VERSION} --build-arg GIT_COMMIT=${GIT_COMMIT}
4658

4759
.PHONY: build-image-goreleaser
4860
build-image-goreleaser: ## Builds the multi-arch container image using goreleaser.
4961
goreleaser release --skip=validate,publish,sbom --clean --snapshot
5062

63+
##@ General
64+
65+
.PHONY: run
66+
run: ## Runs the scaler locally.
67+
go run ./main.go
68+
5169
.PHONY: test
52-
test: ## Runs golang unit tests.
70+
test: test-certs ## Runs golang unit tests.
5371
@$(call say,Running golang unit tests)
5472
go test -race -v ./...
5573

5674
.PHONY: e2e-test
57-
e2e-test: ## Runs end to end tests. This will spawn a k3d cluster.
75+
e2e-test: ## Runs end to end tests. This will spawn a k3d cluster.
5876
@$(call say,Running end to end tests)
5977
cd e2e-tests && go test -count=1 -race -v ./...
6078

@@ -75,12 +93,32 @@ codegen: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
7593
./hack/update-codegen.sh
7694

7795
.PHONY: deploy-helm
78-
deploy-helm: ## Deploys helm chart with otel-collector and otel scaler.
96+
deploy-helm: ## Deploys helm chart with otel-collector and otel scaler.
7997
@$(call say,Deploy helm chart to current k8s context)
8098
cd helmchart/otel-add-on && \
8199
helm dependency build && \
82100
helm upgrade -i kedify-otel .
83101

102+
.PHONY: rootca-test-certs
103+
rootca-test-certs:
104+
@$(call say,CA cert)
105+
rm -rf certs
106+
mkdir -p certs
107+
openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout certs/rootCA.key -out certs/rootCA.crt -subj "/C=US/CN=Keda-OTel-Scaler-Root-CA"
108+
rm -rf certs/rootCA.srl
109+
110+
.PHONY: rootca-test-certs
111+
test-certs: rootca-test-certs ## Generates certs for local unit and e2e tests
112+
@$(call say,Server cert)
113+
echo "$$SERVER_DOMAINS" > certs/domains.ext
114+
openssl req -new -nodes -newkey rsa:2048 -keyout certs/server.key -out certs/server.csr -subj "/C=US/ST=KedaState/L=KedaCity/O=Test-Certificates/CN=keda-otel-scaler.keda.svc"
115+
openssl x509 -req -sha256 -days 1024 -in certs/server.csr -CA certs/rootCA.crt -CAkey certs/rootCA.key -CAcreateserial -extfile certs/domains.ext -out certs/server.crt
116+
117+
@$(call say,Client cert)
118+
openssl req -new -nodes -newkey rsa:2048 -keyout certs/client.key -out certs/client.csr -subj "/C=US/ST=KedaState/L=KedaCity/O=Test-Certificates/CN=client"
119+
openssl x509 -req -sha256 -days 1024 -in certs/client.csr -CA certs/rootCA.crt -CAkey certs/rootCA.key -CAcreateserial -out certs/client.crt
120+
rm -rf certs/*.{csr,srl,ext}
121+
84122
.PHONY: logs
85123
logs:
86124
@$(call say,logs)
@@ -95,8 +133,8 @@ gomodifytags: ## Download gomodifytags locally if necessary.
95133
GOBIN=$(shell pwd)/bin go install github.com/fatih/gomodifytags@v1.17.0
96134

97135
.PHONY: help
98-
help: ## Show this help.
99-
@egrep -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}'
136+
help: ## Display this help.
137+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-24s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
100138

101139

102140
###############################

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ helm upgrade -i keda-otel-scaler -nkeda oci://ghcr.io/kedify/charts/otel-add-on
9898
k apply -f examples/so.yaml
9999
```
100100

101+
### Advanced setups
102+
103+
Check some prepared examples in the [`./examples`](./examples) directory and also checked the `dev.Makefile` if you want to
104+
set up mTLS between a collector and this scaler.
105+
106+
```bash
107+
λ make -f dev.Makefile
108+
Usage:
109+
make <target>
110+
Demos
111+
demo-podinfo setup ./examples/metric-pull
112+
demo-podinfo-tls setup ./examples/metric-pull with TLS
113+
demo-otel-upstream setup ./examples/metric-push
114+
demo-operator setup ./examples/otel-operator
115+
demo-operator-tls setup ./examples/otel-operator with TLS
116+
117+
λ make -f dev.Makefile demo-podinfo-tls
118+
...
119+
```
120+
101121
## Troubleshooting
102122

103123
To figure out the actual value of a metric query, there is a simple REST api that can be used:

dev.Makefile

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,110 @@ include Makefile
33
LOCAL_ENDPOINT ?= host.k3d.internal
44
SO_NAME ?= otel-example
55

6+
## helpers
7+
check_defined = \
8+
$(strip $(foreach 1,$1, \
9+
$(call __check_defined,$1,$(strip $(value 2)))))
10+
__check_defined = \
11+
$(if $(value $1),, \
12+
$(error Undefined $1$(if $2, ($2))))
13+
check_k3d = \
14+
@if [ -z $$(kubectl config current-context | grep "^k3d-") ]; then \
15+
echo "Create k3d cluster first!" ;\
16+
exit 1 ;\
17+
fi
18+
19+
##@ Dev
620
.PHONY: dev-k3d
7-
dev-k3d: build-image ## Builds the container image for current arch, imports it to running k3d and restarts the scaler.
21+
dev-k3d: ## Builds the container image for current arch, imports it to running k3d and restarts the scaler.
822
@$(call say,Doing the dev cycle)
9-
k3d image import ghcr.io/kedify/otel-add-on:latest
23+
@$(call check_k3d)
24+
k3d image import -c $(shell kubectl config current-context | sed -e "s/^k3d-//") ghcr.io/kedify/otel-add-on:latest
1025
helm upgrade --reuse-values \
11-
kedify-otel helmchart/otel-add-on \
12-
--set image.tag=latest \
26+
keda-otel-scaler helmchart/otel-add-on \
27+
-nkeda \
28+
--set image.tag=latest \
1329
--set image.pullPolicy=IfNotPresent \
14-
--set settings.logs.logLvl=debug \
15-
kubectl rollout restart deploy/otel-add-on-scaler
30+
--set settings.logs.logLvl=debug
31+
kubectl -nkeda rollout restart deploy/keda-otel-scaler
1632

1733
.PHONY: dev-local
1834
dev-local: ## Prepare the SO and otel collector for local debug
1935
@$(call say,Prepare the conditions for local debug)
36+
@$(call check_k3d)
2037
helm upgrade --reuse-values \
21-
kedify-otel helmchart/otel-add-on \
22-
--set replicaCount=1 \
23-
--set opentelemetry-collector.config.exporters.otlp.endpoint=$(LOCAL_ENDPOINT):4317
38+
keda-otel-scaler helmchart/otel-add-on \
39+
-nkeda \
40+
--set replicaCount=1 \
41+
--set opentelemetry-collector.config.exporters.otlp.endpoint=$(LOCAL_ENDPOINT):4317
2442
kubectl patch so $(SO_NAME) --type=json -p '[{"op":"replace","path":"/spec/triggers/0/metadata/scalerAddress","value":"$(LOCAL_ENDPOINT):4318"}]'
25-
@$(call say,Continue by running the scaler locally from your favorite IDE outsice of K8s)
43+
@$(call say,Continue by running the scaler locally from your favorite IDE outside of K8s)
2644
@echo "Make sure $(LOCAL_ENDPOINT):4317 and $(LOCAL_ENDPOINT):4318 are listening.."
2745

2846
.PHONY: undo-dev-local
2947
undo-dev-local: ## Revers the SO and otel collector for local debug
3048
@$(call say,Revert the conditions for local debug)
49+
@$(call check_k3d)
3150
helm upgrade --reuse-values \
32-
kedify-otel helmchart/otel-add-on \
33-
--set replicaCount=1 \
34-
--set opentelemetry-collector.config.exporters.otlp.endpoint=keda-otel-scaler:4317
35-
kubectl patch so $(SO_NAME) --type=json -p '[{"op":"replace","path":"/spec/triggers/0/metadata/scalerAddress","value":"keda-otel-scaler:4318"}]'
36-
kubectl scale deploy/otel-add-on-scaler --replicas=1
51+
keda-otel-scaler helmchart/otel-add-on \
52+
-nkeda \
53+
--set replicaCount=1 \
54+
--set opentelemetry-collector.config.exporters.otlp.endpoint=keda-otel-scaler.keda.svc:4317
55+
kubectl patch so $(SO_NAME) --type=json -p '[{"op":"replace","path":"/spec/triggers/0/metadata/scalerAddress","value":"keda-otel-scaler.keda.svc:4318"}]'
56+
kubectl scale -nkeda deploy/keda-otel-scaler --replicas=1
57+
58+
.PHONY: k8s-certs
59+
k8s-certs: test-certs ## Creates k8s secrets from the generated certificates
60+
@$(call say,Preparing certs)
61+
@$(call check_k3d)
62+
kubectl -nkeda delete secret --ignore-not-found server-tls client-tls root-ca
63+
kubectl -nkeda create secret tls server-tls --cert=certs/server.crt --key=certs/server.key
64+
kubectl -nkeda create secret tls client-tls --cert=certs/client.crt --key=certs/client.key
65+
kubectl -nkeda create secret generic root-ca --from-file=rootCA.crt=certs/rootCA.crt
66+
67+
##@ Demos
68+
.PHONY: demo-podinfo
69+
demo-podinfo: ## setup ./examples/metric-pull
70+
./examples/metric-pull/setup.sh
71+
$(MAKE) -f dev.Makefile dev-k3d
72+
73+
.PHONY: demo-podinfo-tls
74+
demo-podinfo-tls: ## setup ./examples/metric-pull with TLS
75+
SETUP_ONLY=true ./examples/metric-pull/setup.sh
76+
$(MAKE) -f dev.Makefile k8s-certs
77+
helm upgrade -i -nkeda keda-otel-scaler ./helmchart/otel-add-on -f ./examples/metric-pull/scaler-with-collector-pull-tls-values.yaml
78+
$(MAKE) -f dev.Makefile dev-k3d
79+
kubectl apply -f ./examples/metric-pull/podinfo-so.yaml
80+
@$(call say,Done)
81+
@echo "Continue with: (hey -n 7000 -z 180s http://localhost:8181/delay/2 &> /dev/null)&"
82+
83+
.PHONY: demo-otel-upstream
84+
demo-otel-upstream: ## setup ./examples/metric-push
85+
./examples/metric-push/setup.sh
86+
$(MAKE) -f dev.Makefile dev-k3d
87+
88+
.PHONY: demo-operator
89+
demo-operator: ## setup ./examples/otel-operator
90+
@:$(call check_defined, PR_BRANCH GH_PAT)
91+
$(call check_k3d)
92+
./examples/otel-operator/setup.sh
93+
$(MAKE) -f dev.Makefile dev-k3d
94+
95+
.PHONY: demo-operator-tls
96+
demo-operator-tls: ## setup ./examples/otel-operator with TLS
97+
SETUP_ONLY=true ./examples/otel-operator/setup.sh
98+
$(MAKE) -f dev.Makefile k8s-certs
99+
# helm cant merge correctly value files when the later overrides an item in an array (w/ unique name)
100+
rm -rf values_tmp && ./hack/mergeValues.sh \
101+
examples/otel-operator/scaler-with-operator-with-collector-values.yaml \
102+
examples/otel-operator/tls-overlay-values.yaml > values_tmp
103+
@$(call say,Merged values:)
104+
yq values_tmp
105+
helm upgrade -i \
106+
keda-otel-scaler helmchart/otel-add-on \
107+
-nkeda \
108+
-f ./values_tmp
109+
rm -rf values_tmp
110+
$(MAKE) -f dev.Makefile dev-k3d
111+
@$(call say,Creating SO)
112+
kubectl apply -f <(cat ./examples/otel-operator/so.yaml | envsubst)

0 commit comments

Comments
 (0)