blob: 5d56c27d48cdcfea17ccb0a9ed30342c798e2913 [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("pol-k8s.configured")
26
27
28@when("config.changed")
29@when("leadership.is_leader")
30def restart():
31 clear_flag("pol-k8s.configured")
32
33
34@when_not("kafka.ready")
David Garcia68faf8d2020-09-01 10:12:16 +020035@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010036@when_not("pol-k8s.configured")
37def waiting_for_kafka():
38 layer.status.waiting("Waiting for kafka to be ready")
39
40
41@when_not("mongo.ready")
David Garcia68faf8d2020-09-01 10:12:16 +020042@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010043@when_not("pol-k8s.configured")
44def waiting_for_mongo():
45 layer.status.waiting("Waiting for mongo to be ready")
46
47
48@when("kafka.ready", "mongo.ready")
49@when_not("pol-k8s.configured")
50@when("leadership.is_leader")
51def configure():
52 layer.status.maintenance("Configuring pol container")
53 try:
54 kafka = endpoint_from_flag("kafka.ready")
55 mongo = endpoint_from_flag("mongo.ready")
56
57 if kafka and mongo:
58 kafka_units = kafka.kafkas()
59 kafka_unit = kafka_units[0]
60
61 mongo_uri = mongo.connection_string()
62 log("Mongo URI: {}".format(mongo_uri))
63
64 if mongo_uri and kafka_unit["host"]:
65 spec = make_pod_spec(kafka_unit["host"], kafka_unit["port"], mongo_uri)
66
67 log("set pod spec:\n{}".format(spec))
68 pod_spec_set(spec)
69 set_flag("pol-k8s.configured")
70 except Exception as e:
71 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
72
73
David Garcia68faf8d2020-09-01 10:12:16 +020074@when("kafka.ready", "mongo.ready")
75@when_not("leadership.is_leader")
76def non_leaders_active():
77 layer.status.active("ready")
78
79
David Garcia82c5ffa2020-03-09 08:38:17 +010080@when("pol-k8s.configured")
81def set_pol_active():
82 layer.status.active("ready")
83
84
85def make_pod_spec(kafka_host, kafka_port, mongo_uri):
86 """Make pod specification for Kubernetes
87
88 Args:
89 kafka_host (str): Kafka hostname or IP
90 kafka_port (int): Kafka port
91 mongo_host (str): Mongo URI
92 Returns:
93 pod_spec: Pod specification for Kubernetes
94 """
95
96 with open("reactive/spec_template.yaml") as spec_file:
97 pod_spec_template = spec_file.read()
98
99 md = metadata()
100 cfg = config()
101 data = {
102 "name": md.get("name"),
David Garcia82c5ffa2020-03-09 08:38:17 +0100103 "kafka_host": kafka_host,
104 "kafka_port": kafka_port,
105 "mongo_uri": mongo_uri,
106 }
107 data.update(cfg)
108 return pod_spec_template % data