Skip to content

Commit

Permalink
and now via api
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Afro committed Dec 27, 2024
1 parent 30f4319 commit ab9ba87
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 22 deletions.
44 changes: 22 additions & 22 deletions ansible/roles/dof_rabbitmq/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@
- prometheus


- name: Get RabbitMQ pods in the namespace
kubernetes.core.k8s_info:
api_version: v1
kind: Pod
- name: Enable Feature Flags ConfigMap
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
label_selectors:
- app.kubernetes.io/component=rabbitmq
register: rabbitmq_pods
template: enable-feature-flags-job-configmap.yaml.j2
tags:
- configmap

- name: Enable RabbitMQ feature flags in all pods
kubernetes.core.k8s_exec:
- name: Delete existing feature flags job if it exists
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
pod: "{{ item.metadata.name }}"
container: rabbitmq
command: "rabbitmqctl list_feature_flags"
with_items: "{{ rabbitmq_pods.resources }}"
register: exec_results
api_version: batch/v1
kind: Job
name: enable-feature-flags-job
state: absent
tags:
- job

- name: Debug the output of k8s_exec results
debug:
msg: |
Pod: {{ item.item.metadata.name }}
Standard Output: {{ item.stdout }}
Standard Error: {{ item.stderr }}
Return Code: {{ item.rc }}
with_items: "{{ exec_results.results }}"
- name: Create the Enable Feature Flags Job
kubernetes.core.k8s:
kubeconfig: ~/.kube/config
namespace: "{{ NAMESPACE }}"
template: enable-feature-flags-job.yaml.j2
tags:
- job
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: enable-feature-flags-script
namespace: {{ NAMESPACE }}
labels:
app: rabbitmq
data:
enable_feature_flags.sh: |
#!/bin/bash
set -euo pipefail

if [ -z "${RABBITMQ_MGMT_USERNAME:-}" ] || [ -z "${RABBITMQ_MGMT_PASSWORD:-}" ]; then
echo "ERROR: RABBITMQ_MGMT_USERNAME and RABBITMQ_MGMT_PASSWORD must be set." >&2
exit 1
fi

RABBITMQ_API="http://rabbitmq-svc:15672"

get_nodes() {
curl -s -u "$RABBITMQ_MGMT_USERNAME:$RABBITMQ_MGMT_PASSWORD" "$RABBITMQ_API/api/nodes" | jq -r '.[].name'
}

get_feature_flags() {
curl -s -u "$RABBITMQ_MGMT_USERNAME:$RABBITMQ_MGMT_PASSWORD" "$RABBITMQ_API/api/feature-flags" | jq -r '.[] | "\(.name) \(.state)"'
}

enable_feature_flag() {
local flag_name=$1
curl -s -u "$RABBITMQ_MGMT_USERNAME:$RABBITMQ_MGMT_PASSWORD" -X PUT "$RABBITMQ_API/api/feature-flags/$flag_name" | jq .
}

main() {
nodes=$(get_nodes)
if [ -z "$nodes" ]; then
echo "ERROR: No nodes found. Exiting." >&2
exit 1
fi
echo "$nodes"

feature_flags=$(get_feature_flags)
if [ -z "$feature_flags" ]; then
echo "ERROR: No feature flags found. Exiting." >&2
exit 1
fi
echo "$feature_flags"

echo "$feature_flags" | while read -r flag state; do
if [ "$state" != "enabled" ]; then
enable_feature_flag "$flag"
else
echo "Feature flag $flag is already enabled."
fi
done
}

main
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: batch/v1
kind: Job
metadata:
name: enable-feature-flags-job
labels:
app: rabbitmq
spec:
template:
metadata:
labels:
app: rabbitmq
spec:
containers:
- name: rabbitmq-enable-feature-flags-job
image: quay.io/schulcloudverbund/infra-tools:latest
envFrom:
- secretRef:
name: rabbitmq-secret
volumeMounts:
- name: script
mountPath: /enable_feature_flags.sh
subPath: enable_feature_flags.sh
command: ['/bin/bash','-c']
args: ['cp /enable_feature_flags.sh /enable_feature_flags.run.sh && chmod +x /enable_feature_flags.run.sh && ./enable_feature_flags.run.sh']
volumes:
- name: script
configMap:
name: enable-feature-flags-script
items:
- key: enable_feature_flags.sh
path: enable_feature_flags.sh
restartPolicy: Never
backoffLimit: 4

0 comments on commit ab9ba87

Please sign in to comment.