blob: 87b6b2c2a5459c8729948d7419742999e1f3e556 [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.
beierlmb1a1c462020-10-23 14:54:56 -040014import logging
15import yaml
16
David Garcia82c5ffa2020-03-09 08:38:17 +010017from charmhelpers.core.hookenv import (
18 log,
19 metadata,
20 config,
21)
22from charms import layer
beierlmb1a1c462020-10-23 14:54:56 -040023from charms.layer.caas_base import pod_spec_set
24from charms.reactive import endpoint_from_flag
25from charms.reactive import when, when_not, hook
26from charms.reactive.flags import set_flag, clear_flag
27
David Garcia68faf8d2020-09-01 10:12:16 +020028
29logger = logging.getLogger(__name__)
30
David Garcia82c5ffa2020-03-09 08:38:17 +010031
32@hook("upgrade-charm")
33@when("leadership.is_leader")
34def upgrade():
35 clear_flag("lcm-k8s.configured")
36
37
38@when("config.changed")
39@when("leadership.is_leader")
40def restart():
41 clear_flag("lcm-k8s.configured")
42
43
44@when_not("kafka.ready")
45@when_not("lcm-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020046@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010047def waiting_for_kafka():
48 layer.status.waiting("Waiting for kafka to be ready")
49
50
51@when_not("mongo.ready")
52@when_not("lcm-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020053@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010054def waiting_for_mongo():
55 layer.status.waiting("Waiting for mongo to be ready")
56
57
58@when_not("ro.ready")
59@when_not("lcm-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020060@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010061def waiting_for_ro():
62 layer.status.waiting("Waiting for ro to be ready")
63
64
65@when("kafka.ready", "mongo.ready", "ro.ready")
66@when_not("lcm-k8s.configured")
67@when("leadership.is_leader")
68def configure():
69 layer.status.maintenance("Configuring lcm container")
70 try:
71 kafka = endpoint_from_flag("kafka.ready")
72 mongo = endpoint_from_flag("mongo.ready")
73 osm_ro = endpoint_from_flag("ro.ready")
74
75 if kafka and mongo and osm_ro:
76 kafka_units = kafka.kafkas()
77 kafka_unit = kafka_units[0]
78
79 mongo_uri = mongo.connection_string()
80 log("Mongo URI: {}".format(mongo_uri))
81
82 ros = osm_ro.ros()
83 ro_unit = ros[0]
84
85 if (
86 mongo_uri
87 and kafka_unit["host"]
88 and kafka_unit["port"]
89 and ro_unit["host"]
90 and ro_unit["port"]
91 ):
92 spec = make_pod_spec(
93 ro_unit["host"],
94 ro_unit["port"],
95 kafka_unit["host"],
96 kafka_unit["port"],
97 mongo_uri,
98 )
99
100 log("set pod spec:\n{}".format(spec))
101 pod_spec_set(spec)
102 layer.status.active("creating container")
103 set_flag("lcm-k8s.configured")
104 except Exception as e:
105 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
106
107
David Garcia68faf8d2020-09-01 10:12:16 +0200108@when("kafka.ready", "mongo.ready", "ro.ready")
109@when_not("leadership.is_leader")
110def non_leaders_active():
111 layer.status.active("ready")
112
113
David Garcia82c5ffa2020-03-09 08:38:17 +0100114@when("lcm-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +0200115@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +0100116def set_lcm_active():
117 layer.status.active("ready")
118
119
120def make_pod_spec(ro_host, ro_port, kafka_host, kafka_port, mongo_uri):
121 """Make pod specification for Kubernetes
122
123 Args:
124 ro_host (str): RO hostname or IP
125 ro_port (str): RO Port
126 kafka_host (str): Kafka hostname or IP
127 kafka_port (int): Kafka port
128 mongo_uri (str): Mongo URI
129 Returns:
130 pod_spec: Pod specification for Kubernetes
131 """
132
133 with open("reactive/spec_template.yaml") as spec_file:
134 pod_spec_template = spec_file.read()
135
136 cfg = config()
137 md = metadata()
138
139 data = {
140 "name": md.get("name"),
David Garcia82c5ffa2020-03-09 08:38:17 +0100141 "ro_host": ro_host,
142 "ro_port": ro_port,
143 "kafka_host": kafka_host,
144 "kafka_port": kafka_port,
145 "mongo_uri": mongo_uri,
146 }
147 data.update(cfg)
David Garcia68faf8d2020-09-01 10:12:16 +0200148 logs = pod_spec_template % data
149 spec = yaml.safe_load(pod_spec_template % data)
beierlma4a37f72020-06-26 12:55:01 -0400150 if "vca_apiproxy" in cfg and cfg["vca_apiproxy"] != "":
151 spec["containers"][0]["config"]["OSMLCM_VCA_APIPROXY"] = cfg["vca_apiproxy"]
David Garcia68faf8d2020-09-01 10:12:16 +0200152 return spec