Prepare installer and pods for Rel TWELVE
[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 @when("leadership.is_leader")
37 def waiting_for_kafka():
38 layer.status.waiting("Waiting for kafka to be ready")
39
40
41 @when_not("mongo.ready")
42 @when_not("mon-k8s.configured")
43 @when("leadership.is_leader")
44 def waiting_for_mongo():
45 layer.status.waiting("Waiting for mongo to be ready")
46
47
48 @when_not("endpoint.prometheus.available")
49 @when_not("mon-k8s.configured")
50 @when("leadership.is_leader")
51 def waiting_for_prometheus():
52 layer.status.waiting("Waiting for prometheus to be ready")
53
54
55 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
56 @when_not("mon-k8s.configured")
57 @when("leadership.is_leader")
58 def configure():
59 layer.status.maintenance("Configuring mon container")
60 try:
61 kafka = endpoint_from_flag("kafka.ready")
62 mongo = endpoint_from_flag("mongo.ready")
63 prometheus = endpoint_from_flag("endpoint.prometheus.available")
64
65 if kafka and mongo and prometheus:
66 kafka_units = kafka.kafkas()
67 kafka_unit = kafka_units[0]
68
69 mongo_uri = mongo.connection_string()
70 log("Mongo URI: {}".format(mongo_uri))
71
72 prometheus_url = prometheus.targets()[0]["targets"][0]
73
74 if mongo_uri and kafka_unit["host"]:
75
76 spec = make_pod_spec(
77 kafka_unit["host"], kafka_unit["port"], mongo_uri, prometheus_url
78 )
79
80 log("set pod spec:\n{}".format(spec))
81 pod_spec_set(spec)
82 set_flag("mon-k8s.configured")
83 except Exception as e:
84 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
85
86
87 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
88 @when_not("leadership.is_leader")
89 def non_leaders_active():
90 layer.status.active("ready")
91
92
93 @when("mon-k8s.configured")
94 def set_mon_active():
95 layer.status.active("ready")
96
97
98 def make_pod_spec(kafka_host, kafka_port, mongo_uri, prometheus_url):
99 """Make pod specification for Kubernetes
100
101 Args:
102 kafka_host (str): Kafka hostname or IP
103 kafka_port (int): Kafka port
104 mongo_uri (str): Mongo URI
105 Returns:
106 pod_spec: Pod specification for Kubernetes
107 """
108
109 with open("reactive/spec_template.yaml") as spec_file:
110 pod_spec_template = spec_file.read()
111
112 md = metadata()
113 cfg = config()
114
115 data = {
116 "name": md.get("name"),
117 "kafka_host": kafka_host,
118 "kafka_port": kafka_port,
119 "mongo_uri": mongo_uri,
120 "prometheus_url": prometheus_url,
121 }
122 data.update(cfg)
123 return pod_spec_template % data