Skip to content

Commit

Permalink
refactor kafka exporter script and add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Oct 23, 2024
1 parent 8ea4c80 commit 0402b27
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 35 deletions.
81 changes: 81 additions & 0 deletions addons/kafka/scripts-ut-spec/kafka_exporter_setup_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "kafka_exporter_setup_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

Describe "Kafka Exporter Setup Script Tests"
# Load the scripts to be tested and dependencies
Include ../scripts/kafka-exporter-setup.sh

init() {
ut_mode="true"
}
BeforeAll "init"

un_setup() {
# Reset environment variables before each test
unset BROKER_POD_FQDN_LIST
unset COMBINE_POD_FQDN_LIST
unset TLS_ENABLED
}

Describe "generate_kafka_servers()"
It "returns an error if both BROKER_POD_FQDN_LIST and COMBINE_POD_FQDN_LIST are unset"
un_setup
When run generate_kafka_servers
The stderr should include "Error: BROKER_POD_FQDN_LIST and COMBINE_POD_FQDN_LIST environment variable is not set"
The status should be failure
End

It "generates server list from COMBINE_POD_FQDN_LIST"
un_setup
COMBINE_POD_FQDN_LIST="kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local,kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local"
When run generate_kafka_servers
The output should equal " --kafka.server=kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local:9094 --kafka.server=kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local:9094"
The status should be success
End

It "generates server list from BROKER_POD_FQDN_LIST if COMBINE_POD_FQDN_LIST is unset"
un_setup
BROKER_POD_FQDN_LIST="kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local,kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local"
When run generate_kafka_servers
The output should equal " --kafka.server=kafka-kafka-0.kafka-kafka-headless.default.svc.cluster.local:9094 --kafka.server=kafka-kafka-1.kafka-kafka-headless.default.svc.cluster.local:9094"
The status should be success
End
End

Describe "get_start_kafka_exporter_cmd()"
It "returns failure if generate_kafka_servers fails"
generate_kafka_servers() {
return 1
}
When run get_start_kafka_exporter_cmd
The stderr should include "failed to generate kafka servers. Exiting."
The status should be failure
End

It "returns the correct command with TLS enabled"
un_setup
COMBINE_POD_FQDN_LIST="combine1.example.com"
TLS_ENABLED="true"
When run get_start_kafka_exporter_cmd
The output should include "kafka_exporter --web.listen-address=:9308 --tls.enabled --kafka.server=combine1.example.com:9094"
The stderr should include "TLS_ENABLED is set to true, start kafka_exporter with tls enabled."
The status should be success
End

It "returns the correct command with TLS disabled"
un_setup
BROKER_POD_FQDN_LIST="broker1.example.com"
TLS_ENABLED=""
When run get_start_kafka_exporter_cmd
The output should include "kafka_exporter --web.listen-address=:9308 --kafka.server=broker1.example.com:9094"
The stderr should include "TLS_ENABLED is not set, start kafka_exporter with tls disabled."
The status should be success
End
End
End
76 changes: 76 additions & 0 deletions addons/kafka/scripts/kafka-exporter-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

# shellcheck disable=SC2034
ut_mode="false"
test || __() {
# when running in non-unit test mode, set the options "set -ex".
set -ex;
}

generate_kafka_servers() {
local servers=""

if [[ -z "$BROKER_POD_FQDN_LIST" ]] && [[ -z "$COMBINE_POD_FQDN_LIST" ]]; then
echo "Error: BROKER_POD_FQDN_LIST and COMBINE_POD_FQDN_LIST environment variable is not set, Please check and try again." >&2
return 1
fi

## try to use COMBINE_POD_FQDN_LIST first
if [[ -n "$COMBINE_POD_FQDN_LIST" ]]; then
IFS=',' read -r -a combine_pod_fqdn_list <<< "$COMBINE_POD_FQDN_LIST"
for pod_fqdn in "${combine_pod_fqdn_list[@]}"; do
servers="${servers} --kafka.server=${pod_fqdn}:9094"
done
echo "$servers"
return 0
fi

# if COMBINE_POD_FQDN_LIST is not set, use BROKER_POD_FQDN_LIST
IFS=',' read -r -a broker_pod_fqdn_list <<< "$BROKER_POD_FQDN_LIST"
for pod_fqdn in "${broker_pod_fqdn_list[@]}"; do
servers="${servers} --kafka.server=${pod_fqdn}:9094"
done
echo "$servers"
return 0
}

get_start_kafka_exporter_cmd() {
local servers
local status
servers=$(generate_kafka_servers)
status=$?
if [[ $status -ne 0 ]]; then
echo "failed to generate kafka servers. Exiting." >&2
return 1
fi

if [[ -n "$TLS_ENABLED" ]]; then
echo "TLS_ENABLED is set to true, start kafka_exporter with tls enabled." >&2
echo "kafka_exporter --web.listen-address=:9308 --tls.enabled ${servers}"
else
echo "TLS_ENABLED is not set, start kafka_exporter with tls disabled." >&2
echo "kafka_exporter --web.listen-address=:9308 ${servers}"
fi
return 0
}

start_kafka_exporter() {
local cmd
cmd=$(get_start_kafka_exporter_cmd)
status=$?
if [[ $status -ne 0 ]]; then
ehco "failed to get start kafka_exporter command. Exiting." >&2
exit 1
fi
$cmd
}

# This is magic for shellspec ut framework.
# Sometime, functions are defined in a single shell script.
# You will want to test it. but you do not want to run the script.
# When included from shellspec, __SOURCED__ variable defined and script
# end here. The script path is assigned to the __SOURCED__ variable.
${__SOURCED__:+false} : || return 0

# main
start_kafka_exporter
33 changes: 0 additions & 33 deletions addons/kafka/scripts/kafka-exporter-setup.tpl

This file was deleted.

File renamed without changes.
15 changes: 15 additions & 0 deletions addons/kafka/templates/cmpd-exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ spec:
serviceKind: kafka-exporter
serviceVersion: {{ .Values.defaultServiceVersion.exporter }}
updateStrategy: BestEffortParallel
vars:
## when server mode is kafka-broker
- name: BROKER_POD_FQDN_LIST
valueFrom:
componentVarRef:
compDef: {{ include "kafka-broker.cmpdRegexpPattern" . }}
optional: true
podFQDNs: Required
## when server mode is kafka-combine
- name: COMBINE_POD_FQDN_LIST
valueFrom:
componentVarRef:
compDef: {{ include "kafka-combine.cmpdRegexpPattern" . }}
optional: true
podFQDNs: Required
scripts:
- name: kafka-scripts-tpl
templateRef: {{ include "kafka.serverScriptsTplName" . }}
Expand Down
4 changes: 2 additions & 2 deletions addons/kafka/templates/scripts-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ metadata:
{{- end }}
data:
kafka-server-setup.sh: |-
{{- .Files.Get "scripts/kafka-server-setup.tpl" | nindent 4 }}
{{- .Files.Get "scripts/kafka-server-setup.sh" | nindent 4 }}
kafka-exporter-setup.sh: |-
{{- .Files.Get "scripts/kafka-exporter-setup.tpl" | nindent 4 }}
{{- .Files.Get "scripts/kafka-exporter-setup.shsh" | nindent 4 }}
kafka-env.sh: |-
{{- .Files.Get "scripts/kafka-env.sh" | nindent 4 }}
---
Expand Down

0 comments on commit 0402b27

Please sign in to comment.