Skip to content

Instantly share code, notes, and snippets.

@wbuchwalter
Created September 1, 2017 14:19
Show Gist options
  • Save wbuchwalter/075eabdbb16e971bebf2c125df595a8e to your computer and use it in GitHub Desktop.
Save wbuchwalter/075eabdbb16e971bebf2c125df595a8e to your computer and use it in GitHub Desktop.
import pykube
import os
import json
def main():
kubeconfig = '/root/.kube/config'
if os.path.isfile(kubeconfig):
# for using locally
api = pykube.HTTPClient(
pykube.KubeConfig.from_file(kubeconfig))
else:
# for using on kube
api = pykube.HTTPClient(
pykube.KubeConfig.from_service_account())
watch(api)
def watch(api):
def name_filter(x): return x.object.labels['name'] == 'qdrouterd'
watch = pykube.Pod.objects(api, namespace="default").filter(field_selector={'status.phase': 'Running'}).watch()
# watch is a generator:
for watch_event in watch:
if 'name' in watch_event.object.labels and name_filter(watch_event):
print(watch_event)
pod_ip = watch_event.object._original_obj['status']['podIP']
print(pod_ip)
create_listener(pod_ip)
# qdmanage -b admin:123456@<pod-ip> create type=listener port=20102 host=0.0.0.0 saslMechanisms=ANONYMOUS
# qdmanage -b admin:[email protected] create type=connector role=inter-router host=0.0.0.0 port=20003 saslMechanisms=ANONYMOUS
def create_listener(ip):
bashCommand = "qdmanage -b admin:123456@{}:5672 create type=listener port=20102 host=0.0.0.0 saslMechanisms=ANONYMOUS".format(ip)
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print(output)
print(error)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment