|
| 1 | +{{- if .Values.pubsubEmulator.enabled }} |
| 2 | +apiVersion: batch/v1 |
| 3 | +kind: Job |
| 4 | +metadata: |
| 5 | + name: {{ .Values.pubsubEmulator.service.name }}-init |
| 6 | + namespace: {{ .Release.Namespace }} |
| 7 | + labels: |
| 8 | + app.kubernetes.io/component: pubsub-init |
| 9 | + annotations: |
| 10 | + "helm.sh/hook": post-install,post-upgrade |
| 11 | + "helm.sh/hook-weight": "-5" |
| 12 | + "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded |
| 13 | +spec: |
| 14 | + template: |
| 15 | + metadata: |
| 16 | + labels: |
| 17 | + app.kubernetes.io/component: pubsub-init |
| 18 | + spec: |
| 19 | + restartPolicy: Never |
| 20 | + serviceAccountName: {{ .Values.serviceAccount.name }} |
| 21 | + containers: |
| 22 | + - name: init |
| 23 | + image: registry.access.redhat.com/ubi9/python-311 |
| 24 | + imagePullPolicy: IfNotPresent |
| 25 | + env: |
| 26 | + - name: PUBSUB_EMULATOR_HOST |
| 27 | + value: "{{ .Values.pubsubEmulator.service.name }}:{{ .Values.pubsubEmulator.service.port }}" |
| 28 | + - name: PUBSUB_PROJECT_ID |
| 29 | + value: "{{ .Values.pubsubEmulator.projectID }}" |
| 30 | + command: |
| 31 | + - /bin/bash |
| 32 | + - -c |
| 33 | + - | |
| 34 | + set -e |
| 35 | + echo "Installing google-cloud-pubsub..." |
| 36 | + pip install --quiet google-cloud-pubsub==2.34.0 |
| 37 | +
|
| 38 | + echo "Initializing Pub/Sub topics and subscriptions..." |
| 39 | + python3 <<'PYTHON' |
| 40 | + import os |
| 41 | + from google.cloud import pubsub_v1 |
| 42 | + from google.api_core import exceptions |
| 43 | +
|
| 44 | + project_id = os.environ['PUBSUB_PROJECT_ID'] |
| 45 | +
|
| 46 | + # Create publisher and subscriber clients |
| 47 | + publisher = pubsub_v1.PublisherClient() |
| 48 | + subscriber = pubsub_v1.SubscriberClient() |
| 49 | +
|
| 50 | + # Topics to create |
| 51 | + topics = ['sourceevents', 'sourcebroadcast', 'agentevents', 'agentbroadcast'] |
| 52 | +
|
| 53 | + # Create topics |
| 54 | + for topic_name in topics: |
| 55 | + topic_path = publisher.topic_path(project_id, topic_name) |
| 56 | + try: |
| 57 | + publisher.create_topic(request={"name": topic_path}) |
| 58 | + print(f"Created topic: {topic_path}") |
| 59 | + except exceptions.AlreadyExists: |
| 60 | + print(f"Topic already exists: {topic_path}") |
| 61 | + except Exception as e: |
| 62 | + print(f"Error creating topic {topic_path}: {e}") |
| 63 | + raise |
| 64 | +
|
| 65 | + # Subscriptions to create (name:topic:filter) |
| 66 | + subscriptions = [ |
| 67 | + ('agentevents-maestro', 'agentevents', 'attributes.ce-originalsource="maestro"'), |
| 68 | + ('agentbroadcast-maestro', 'agentbroadcast', '') |
| 69 | + ] |
| 70 | +
|
| 71 | + # Create subscriptions |
| 72 | + for sub_name, topic_name, filter_expr in subscriptions: |
| 73 | + subscription_path = subscriber.subscription_path(project_id, sub_name) |
| 74 | + topic_path = publisher.topic_path(project_id, topic_name) |
| 75 | + try: |
| 76 | + if filter_expr: |
| 77 | + subscriber.create_subscription( |
| 78 | + request={"name": subscription_path, "topic": topic_path, "filter": filter_expr} |
| 79 | + ) |
| 80 | + print(f"Created subscription: {subscription_path} with filter: {filter_expr}") |
| 81 | + else: |
| 82 | + subscriber.create_subscription( |
| 83 | + request={"name": subscription_path, "topic": topic_path} |
| 84 | + ) |
| 85 | + print(f"Created subscription: {subscription_path}") |
| 86 | + except exceptions.AlreadyExists: |
| 87 | + print(f"Subscription already exists: {subscription_path}") |
| 88 | + except Exception as e: |
| 89 | + print(f"Error creating subscription {subscription_path}: {e}") |
| 90 | + raise |
| 91 | +
|
| 92 | + print("Pub/Sub initialization complete!") |
| 93 | + PYTHON |
| 94 | +{{- end }} |
0 commit comments