9eae0b19cfe455cd1448513b81fb98846b349320
[osm/devops.git] / installers / charm / mon-k8s / reactive / mon_k8s.py
1 # Copyright 2020 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from charms.reactive import endpoint_from_flag
15 from charms.layer.caas_base import pod_spec_set
16 from charms.reactive import when, when_not, hook
17 from charms.reactive.flags import set_flag, clear_flag
18 from charmhelpers.core.hookenv import log, metadata, config
19 from charms import layer
20
21
22 @hook("upgrade-charm")
23 @when("leadership.is_leader")
24 def upgrade():
25 clear_flag("mon-k8s.configured")
26
27
28 @when("config.changed")
29 @when("leadership.is_leader")
30 def restart():
31 clear_flag("mon-k8s.configured")
32
33
34 @when_not("kafka.ready")
35 @when_not("mon-k8s.configured")
36 def waiting_for_kafka():
37 layer.status.waiting("Waiting for kafka to be ready")
38
39
40 @when_not("mongo.ready")
41 @when_not("mon-k8s.configured")
42 def waiting_for_mongo():
43 layer.status.waiting("Waiting for mongo to be ready")
44
45
46 @when_not("endpoint.prometheus.available")
47 @when_not("mon-k8s.configured")
48 def waiting_for_prometheus():
49 layer.status.waiting("Waiting for prometheus to be ready")
50
51
52 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
53 @when_not("mon-k8s.configured")
54 @when("leadership.is_leader")
55 def configure():
56 layer.status.maintenance("Configuring mon container")
57 try:
58 kafka = endpoint_from_flag("kafka.ready")
59 mongo = endpoint_from_flag("mongo.ready")
60 prometheus = endpoint_from_flag("endpoint.prometheus.available")
61
62 if kafka and mongo and prometheus:
63 kafka_units = kafka.kafkas()
64 kafka_unit = kafka_units[0]
65
66 mongo_uri = mongo.connection_string()
67 log("Mongo URI: {}".format(mongo_uri))
68
69 prometheus_url = prometheus.targets()[0]["targets"][0]
70
71 if mongo_uri and kafka_unit["host"]:
72
73 spec = make_pod_spec(
74 kafka_unit["host"], kafka_unit["port"], mongo_uri, prometheus_url
75 )
76
77 log("set pod spec:\n{}".format(spec))
78 pod_spec_set(spec)
79 set_flag("mon-k8s.configured")
80 except Exception as e:
81 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
82
83
84 @when("mon-k8s.configured")
85 def set_mon_active():
86 layer.status.active("ready")
87
88
89 def make_pod_spec(kafka_host, kafka_port, mongo_uri, prometheus_url):
90 """Make pod specification for Kubernetes
91
92 Args:
93 kafka_host (str): Kafka hostname or IP
94 kafka_port (int): Kafka port
95 mongo_uri (str): Mongo URI
96 Returns:
97 pod_spec: Pod specification for Kubernetes
98 """
99
100 with open("reactive/spec_template.yaml") as spec_file:
101 pod_spec_template = spec_file.read()
102
103 md = metadata()
104 cfg = config()
105
106 data = {
107 "name": md.get("name"),
108 "docker_image": cfg.get("image"),
109 "kafka_host": kafka_host,
110 "kafka_port": kafka_port,
111 "mongo_uri": mongo_uri,
112 "prometheus_url": prometheus_url,
113 }
114 data.update(cfg)
115 return pod_spec_template % data