blob: 8ea80705ec19bef8c4eccdbc81d1e0aab2f34061 [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.layer.caas_base import pod_spec_set
15from charms.reactive import endpoint_from_flag
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
20from charms.osm.k8s import get_service_ip
21
22
23@hook("upgrade-charm")
24@when("leadership.is_leader")
25def upgrade():
26 clear_flag("ro-k8s.configured")
27
28
29@when("config.changed")
30@when("leadership.is_leader")
31def restart():
32 clear_flag("ro-k8s.configured")
33
tiernoedc193b2020-11-25 13:25:08 +000034@when_not("kafka.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010035@when_not("ro-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020036@when("leadership.is_leader")
tiernoedc193b2020-11-25 13:25:08 +000037def waiting_for_kafka():
38 layer.status.waiting("Waiting for kafka to be ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010039
40
tiernoedc193b2020-11-25 13:25:08 +000041@when_not("mongo.ready")
42@when_not("ro-k8s.configured")
43@when("leadership.is_leader")
44def waiting_for_mongo():
45 layer.status.waiting("Waiting for mongo to be ready")
46
47
48@when("kafka.ready", "mongo.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010049@when_not("ro-k8s.configured")
50@when("leadership.is_leader")
51def configure():
52 layer.status.maintenance("Configuring ro container")
53 try:
tiernoedc193b2020-11-25 13:25:08 +000054 kafka = endpoint_from_flag("kafka.ready")
55 mongo = endpoint_from_flag("mongo.ready")
David Garcia82c5ffa2020-03-09 08:38:17 +010056
tiernoedc193b2020-11-25 13:25:08 +000057 if kafka and mongo:
58 kafka_units = kafka.kafkas()
59 kafka_unit = kafka_units[0]
David Garcia82c5ffa2020-03-09 08:38:17 +010060
tiernoedc193b2020-11-25 13:25:08 +000061 mongo_uri = mongo.connection_string()
62 log("Mongo URI: {}".format(mongo_uri))
63
64 if (
65 mongo_uri
66 and kafka_unit["host"]
67 and kafka_unit["port"]
68 and ro_unit["host"]
69 and ro_unit["port"]
70 ):
71 spec = make_pod_spec(
72 kafka_unit["host"],
73 kafka_unit["port"],
74 mongo_uri,
75 )
76
77 log("set pod spec:\n{}".format(spec))
78 pod_spec_set(spec)
79 layer.status.active("creating container")
80 set_flag("ro-k8s.configured")
David Garcia82c5ffa2020-03-09 08:38:17 +010081 except Exception as e:
82 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
83
84
tiernoedc193b2020-11-25 13:25:08 +000085@when("kafka.ready", "mongo.ready")
David Garcia68faf8d2020-09-01 10:12:16 +020086@when_not("leadership.is_leader")
87def non_leaders_active():
88 layer.status.active("ready")
89
90
David Garcia82c5ffa2020-03-09 08:38:17 +010091@when("ro-k8s.configured")
92def set_ro_active():
93 layer.status.active("ready")
94
95
96@when("ro-k8s.configured", "ro.joined")
97def send_config():
98 layer.status.maintenance("Sending RO configuration")
99 try:
100 ro = endpoint_from_flag("ro.joined")
101 if ro:
102 service_ip = get_service_ip("ro")
103 if service_ip:
104 ro.send_connection(
105 service_ip, get_ro_port(),
106 )
107 clear_flag("ro.joined")
108 except Exception as e:
109 log("Fail sending RO configuration: {}".format(e))
110
111
tiernoedc193b2020-11-25 13:25:08 +0000112def make_pod_spec(kafka_host, kafka_port, mongo_uri):
David Garcia82c5ffa2020-03-09 08:38:17 +0100113 """Make pod specification for Kubernetes
114
115 Args:
tiernoedc193b2020-11-25 13:25:08 +0000116 kafka_host (str): Kafka hostname or IP
117 kafka_port (int): Kafka port
118 mongo_uri (str): Mongo URI
David Garcia82c5ffa2020-03-09 08:38:17 +0100119 Returns:
120 pod_spec: Pod specification for Kubernetes
121 """
122
123 with open("reactive/spec_template.yaml") as spec_file:
124 pod_spec_template = spec_file.read()
125
David Garcia82c5ffa2020-03-09 08:38:17 +0100126 cfg = config()
tiernoedc193b2020-11-25 13:25:08 +0000127 md = metadata()
David Garcia82c5ffa2020-03-09 08:38:17 +0100128
129 data = {
130 "name": md.get("name"),
tiernoedc193b2020-11-25 13:25:08 +0000131 "kafka_host": kafka_host,
132 "kafka_port": kafka_port,
133 "mongo_uri": mongo_uri,
David Garcia82c5ffa2020-03-09 08:38:17 +0100134 }
135 data.update(cfg)
136
137 return pod_spec_template % data
138
139
140def get_ro_port():
141 """Returns RO port"""
142 cfg = config()
143 return cfg.get("advertised-port")