From 3e9baaf67f1f369f8f857a172691bbb6ea64005b Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 20 Feb 2024 15:41:24 +0800 Subject: [PATCH] Adds eureka and test infrastructure to support it This adds Eureka discovery properties, and some test infrastructure so we can easily verify it from a different (test) pod. Signed-off-by: Adrian Cole --- README.md | 7 +++ charts/zipkin/ci/default-values.yaml | 2 - charts/zipkin/ci/eureka-values.yaml | 27 +++++++++ charts/zipkin/ci/servicePort-values.yaml | 8 +++ charts/zipkin/templates/_helpers.tpl | 11 ++++ charts/zipkin/templates/deployment.yaml | 30 ++++++++++ charts/zipkin/templates/service.yaml | 1 + .../templates/tests/test-connection.yaml | 13 ++++- charts/zipkin/values.schema.json | 57 +++++++++++++++++++ charts/zipkin/values.yaml | 16 +++++- 10 files changed, 166 insertions(+), 6 deletions(-) delete mode 100644 charts/zipkin/ci/default-values.yaml create mode 100644 charts/zipkin/ci/eureka-values.yaml create mode 100644 charts/zipkin/ci/servicePort-values.yaml diff --git a/README.md b/README.md index eb90355..081728a 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,12 @@ You can then run `helm search repo zipkin` to see the charts. | Key | Type | Default | Description | |--------------------------------------------|--------|------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | affinity | object | `{}` | | +| args | list | `[]` | arguments to the zipkin start command | | autoscaling.enabled | bool | `false` | | | autoscaling.maxReplicas | int | `100` | | | autoscaling.minReplicas | int | `1` | | | autoscaling.targetCPUUtilizationPercentage | int | `80` | | +| command | list | `["start-zipkin"]` | command used to start zipkin | | fullnameOverride | string | `""` | | | image.pullPolicy | string | `"IfNotPresent"` | | | image.repository | string | `"openzipkin/zipkin-slim"` | | @@ -54,6 +56,11 @@ You can then run `helm search repo zipkin` to see the charts. | serviceAccount.name | string | `""` | If not set and create is true, a name is generated using the fullname template | | serviceAccount.psp | bool | `false` | | | tolerations | list | `[]` | | +| zipkin.discovery.eureka.serviceUrl | string | no default | v2 endpoint of Eureka, e.g. `https://eureka-prod/eureka/v2` | +| zipkin.discovery.eureka.app | string | `"zipkin"` | The application this instance registers to | +| zipkin.discovery.eureka.hostName | string | detects | The instance `hostName` and `vipAddress` | +| zipkin.discovery.eureka.instanceId | string | `"hostName:app:port"` | | +| zipkin.storage.type | string | `"mem"` | | | zipkin.selfTracing.enabled | bool | `false` | | | zipkin.storage.elasticsearch.hosts | string | no default | | | zipkin.storage.elasticsearch.index | string | `"zipkin"` | | diff --git a/charts/zipkin/ci/default-values.yaml b/charts/zipkin/ci/default-values.yaml deleted file mode 100644 index 31bc2f1..0000000 --- a/charts/zipkin/ci/default-values.yaml +++ /dev/null @@ -1,2 +0,0 @@ ---- -zipkin: {} diff --git a/charts/zipkin/ci/eureka-values.yaml b/charts/zipkin/ci/eureka-values.yaml new file mode 100644 index 0000000..0a06f7d --- /dev/null +++ b/charts/zipkin/ci/eureka-values.yaml @@ -0,0 +1,27 @@ +--- +# To avoid a race, sleep until Eureka is running before starting zipkin. +command: ['/bin/sh', '-c'] +args: + - 'while ! wget -q --spider http://localhost:8761/eureka/v2/apps;do sleep 1; done && start-zipkin' + +zipkin: + discovery: + eureka: + serviceUrl: http://localhost:8761/eureka/v2 + hostName: zipkin + +# extra containers are in the same pod, so this means Eureka is accessible to +# zipkin via localhost. +extraContainers: + - name: eureka + image: 'ghcr.io/openzipkin/zipkin-eureka' + ports: + - containerPort: 8761 + +# test-connection runs in a different pod, so it can't verify Eureka unless we +# expose a service port. +extraServicePorts: + - port: 8761 + targetPort: 8761 + protocol: TCP + name: eureka diff --git a/charts/zipkin/ci/servicePort-values.yaml b/charts/zipkin/ci/servicePort-values.yaml new file mode 100644 index 0000000..9d8f2c6 --- /dev/null +++ b/charts/zipkin/ci/servicePort-values.yaml @@ -0,0 +1,8 @@ +--- +# Tests use a different pod and access zipkin via its service port. +# This makes sure we can change this to a value different from what the +# container listens on (9411). +service: + port: 9413 + +zipkin: {} diff --git a/charts/zipkin/templates/_helpers.tpl b/charts/zipkin/templates/_helpers.tpl index f1120cb..394cf90 100644 --- a/charts/zipkin/templates/_helpers.tpl +++ b/charts/zipkin/templates/_helpers.tpl @@ -92,3 +92,14 @@ Add extra containers to the pod spec {{- end }} {{- end }} {{- end }} + +{{/* +Add extra ports to the pod spec +*/}} +{{- define "zipkin.extraServicePorts" -}} +{{- if .Values.extraServicePorts }} +{{- with .Values.extraServicePorts }} +{{- toYaml . | nindent 2 }} +{{- end }} +{{- end }} +{{- end }} diff --git a/charts/zipkin/templates/deployment.yaml b/charts/zipkin/templates/deployment.yaml index 77c7be7..e2bd60e 100644 --- a/charts/zipkin/templates/deployment.yaml +++ b/charts/zipkin/templates/deployment.yaml @@ -47,11 +47,41 @@ spec: securityContext: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + {{- if .Values.command }} + command: + {{- range .Values.command }} + - {{ . }} + {{- end }} + {{- end }} + {{- if .Values.args }} + args: + {{- range .Values.args }} + - {{ . }} + {{- end }} + {{- end }} env: {{- if .Values.zipkin.selfTracing.enabled }} - name: SELF_TRACING_ENABLED value: "true" {{- end }} + {{- if .Values.zipkin.discovery.eureka }} + {{- with .Values.zipkin.discovery.eureka }} + - name: EUREKA_SERVICE_URL + value: {{ .serviceUrl | quote }} + {{- if .app }} + - name: EUREKA_APP_NAME + value: {{ .app | quote }} + {{- end }} + {{- if .hostName }} + - name: EUREKA_APP_NAME + value: {{ .hostName | quote }} + {{- end }} + {{- if .instanceId }} + - name: EUREKA_INSTANCE_ID + value: {{ .instanceId | quote }} + {{- end }} + {{- end }} + {{- end }} - name: STORAGE_TYPE value: "{{ .Values.zipkin.storage.type }}" {{- if eq .Values.zipkin.storage.type "elasticsearch" }} diff --git a/charts/zipkin/templates/service.yaml b/charts/zipkin/templates/service.yaml index 64958ce..e59f1c8 100644 --- a/charts/zipkin/templates/service.yaml +++ b/charts/zipkin/templates/service.yaml @@ -25,5 +25,6 @@ spec: targetPort: 9411 protocol: TCP name: http-query + {{- include "zipkin.extraServicePorts" . | nindent 2 }} selector: {{- include "zipkin.selectorLabels" . | nindent 4 }} diff --git a/charts/zipkin/templates/tests/test-connection.yaml b/charts/zipkin/templates/tests/test-connection.yaml index 5b4b881..58e7167 100644 --- a/charts/zipkin/templates/tests/test-connection.yaml +++ b/charts/zipkin/templates/tests/test-connection.yaml @@ -9,16 +9,23 @@ metadata: "helm.sh/hook": test spec: containers: - - name: get-api-services +{{- if .Values.zipkin.discovery.eureka }} + - name: verify-eureka image: 'ghcr.io/openzipkin/alpine:3.19.1' command: [ '/bin/sh', '-c' ] + # Make sure zipkin registered in Eureka at startup + args: [ 'wget -q --spider http://{{ include "zipkin.fullname" . }}:8761/eureka/v2/apps/ZIPKIN' ] +{{- end }} + - name: get-api + image: "ghcr.io/openzipkin/alpine:3.19.1" + command: [ '/bin/sh', '-c' ] # Get an arbitrary API endpoint using the ClusterIP and service port - args: [ 'wget -qO ---spider --header "b3: cafebabecafebabe-cafebabecafebabe-1" http://{{ include "zipkin.fullname" . }}:{{ .Values.service.port }}/api/v2/services' ] + args: [ 'wget -q --spider --header "b3: cafebabecafebabe-cafebabecafebabe-1" http://{{ include "zipkin.fullname" . }}:{{ .Values.service.port }}/api/v2/services' ] {{- if .Values.zipkin.selfTracing.enabled }} - name: get-trace image: 'ghcr.io/openzipkin/alpine:3.19.1' command: [ '/bin/sh', '-c' ] # If self-tracing, sleep for the trace to process. Then, get it by the constant ID passed above. - args: [ 'sleep 3 && wget -qO ---spider http://{{ include "zipkin.fullname" . }}:{{ .Values.service.port }}/api/v2/trace/cafebabecafebabe' ] + args: [ 'sleep 3 && wget -q --spider http://{{ include "zipkin.fullname" . }}:{{ .Values.service.port }}/api/v2/trace/cafebabecafebabe' ] {{- end }} restartPolicy: Never diff --git a/charts/zipkin/values.schema.json b/charts/zipkin/values.schema.json index e0ccaaf..3fedc53 100644 --- a/charts/zipkin/values.schema.json +++ b/charts/zipkin/values.schema.json @@ -5,6 +5,13 @@ "affinity": { "type": "object" }, + "args": { + "description": "Arguments to the zipkin start command", + "type": "array", + "items": { + "type": "string" + } + }, "autoscaling": { "type": "object", "properties": { @@ -22,6 +29,26 @@ } } }, + "command": { + "description": "Zipkin start command", + "type": "array", + "items": { + "type": "string" + } + }, + "extraServicePorts": { + "type": "array", + "items": { + "type": "object", + "required": ["name"], + "properties": { + "name": {"type": "string"}, + "protocol": {"enum": ["TCP", "UDP", "SCTP"]}, + "port": {"type": "integer"}, + "targetPort": {"type": "integer"} + } + } + }, "extraContainers": { "type": "array", "items": { @@ -173,6 +200,36 @@ "zipkin": { "type": "object", "properties": { + "discovery": { + "type": "object", + "properties": { + "eureka": { + "type": "object", + "properties": { + "serviceUrl": { + "type": "string" + }, + "app": { + "type": "string" + }, + "hostName": { + "type": "string" + }, + "instanceId": { + "type": "string" + } + }, + "required": [ + "serviceUrl" + ] + }, + "type": { + "enum": [ + "eureka" + ] + } + } + }, "storage": { "type": "object", "properties": { diff --git a/charts/zipkin/values.yaml b/charts/zipkin/values.yaml index 4bd22b6..6d1ea37 100644 --- a/charts/zipkin/values.yaml +++ b/charts/zipkin/values.yaml @@ -18,6 +18,11 @@ image: # Overrides the image tag whose default is the chart appVersion. tag: "" +# Zipkin start command +command: ["start-zipkin"] +# Arguments to pass to the zipkin start command +args: [] + imagePullSecrets: [] nameOverride: "" fullnameOverride: "" @@ -101,6 +106,7 @@ affinity: {} priorityClassName: "" zipkin: + discovery: {} # selfTracing generates traces for HTTP paths under /api selfTracing: enabled: false @@ -113,5 +119,13 @@ zipkin: extraEnv: {} # JAVA_OPTS: "-Xms128m -Xmx512m -XX:+ExitOnOutOfMemoryError" -# extra containers to add to the same pod (accessible via localhost) +# The below are typically only used in tests: +# extra containers to add to the same pod. extraContainers: [] + +# extra ports to add besides the default service.port +extraServicePorts: [] +# - port: 9410 +# targetPort: 9410 +# protocol: TCP +# name: scribe