diff --git a/Dockerfile b/Dockerfile index 8c6c38f..1a0a9b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ ARG TARGETOS ARG TARGETARCH # Install necessary dependencies -RUN apk add --no-cache +RUN apk add --no-cache curl jq COPY init-scripts /init-scripts COPY scripts /tmp/scripts diff --git a/init-scripts/run.sh b/init-scripts/run.sh index 42bd0fc..5efeb67 100755 --- a/init-scripts/run.sh +++ b/init-scripts/run.sh @@ -6,7 +6,26 @@ set -o pipefail # set -o xtrace # Uncomment this line for debugging purposes cp -r /tmp/scripts/* /opt/kafka/init-scripts - -# TODO(): kafka rack id extraction from pod scheduled node using label +if [[ -n "${TOPOLOGY_KEY:-}" && -n "${NODE_NAME:-}" ]]; then + TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) + APISERVER="https://kubernetes.default.svc" + CACERT="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + RESPONSE=$(curl -s --cacert $CACERT --header "Authorization: Bearer $TOKEN" \ + --header "Accept: application/json" \ + $APISERVER/api/v1/nodes/$NODE_NAME) + if [ $? -ne 0 ]; then + echo "Error: Failed to query API server" >&2 + exit 1 + fi + echo $RESPONSE + LABEL_VALUE=$(echo $RESPONSE | jq -r ".metadata.labels.\"$TOPOLOGY_KEY\" // error(\"Label $TOPOLOGY_KEY not found\")") + # if error returned from here, exit + if [ $? -ne 0 ]; then + echo "Error: Label $TOPOLOGY_KEY not found on node $NODE_NAME" >&2 + exit 1 + fi + echo "broker.rack=$LABEL_VALUE" >> /opt/kafka/init-scripts/rack.properties + echo "Set broker.rack=$LABEL_VALUE for node $NODE_NAME" +fi echo "Kafka Initializing Done!!" diff --git a/scripts/setup.sh b/scripts/setup.sh index e06020b..f20ba86 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -209,6 +209,16 @@ update_configuration() { cat "$operator_config" "$server_config" | awk -F'=' '!seen[$1]++' > "$server_config.updated" mv "$server_config.updated" "$final_config_path" fi + # If $process_roles is not controller and /opt/kafka/init-scripts/rack.properties file exists, + # append or replace rack.id in final_config_path + if [[ "$process_roles" != "controller" && -f /opt/kafka/init-scripts/rack.properties ]]; then + if grep -q "^broker.rack=" "$final_config_path"; then + sed -i "s/^broker.rack=.*/$(grep '^broker.rack=' /opt/kafka/init-scripts/rack.properties)/" "$final_config_path" + else + cat /opt/kafka/init-scripts/rack.properties >> "$final_config_path" + fi + fi + info "Updated configuration file by process_roles" }