blob: cd4d568d9133853ad3d38e76b2e9f40b7ba2e1b6 [file] [log] [blame]
David Garcia82c5ffa2020-03-09 08:38:17 +01001# 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.
14from charms.reactive import endpoint_from_flag
15from charms.layer.caas_base import pod_spec_set
16from charms.reactive import when, when_not, hook
17from charms.reactive.flags import set_flag, clear_flag
18from charmhelpers.core.hookenv import log, metadata, config
19from charms import layer
20
21
22@hook("upgrade-charm")
23@when("leadership.is_leader")
24def upgrade():
25 clear_flag("mon-k8s.configured")
26
27
28@when("config.changed")
29@when("leadership.is_leader")
30def restart():
31 clear_flag("mon-k8s.configured")
32
33
34@when_not("kafka.ready")
35@when_not("mon-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020036@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010037def 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")
David Garcia68faf8d2020-09-01 10:12:16 +020043@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010044def 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")
David Garcia68faf8d2020-09-01 10:12:16 +020050@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010051def 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")
58def 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
David Garcia68faf8d2020-09-01 10:12:16 +020087@when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
88@when_not("leadership.is_leader")
89def non_leaders_active():
90 layer.status.active("ready")
91
92
David Garcia82c5ffa2020-03-09 08:38:17 +010093@when("mon-k8s.configured")
94def set_mon_active():
95 layer.status.active("ready")
96
97
98def 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"),
David Garcia82c5ffa2020-03-09 08:38:17 +0100117 "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