1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
22 @hook("upgrade-charm")
23 @when("leadership.is_leader")
25 clear_flag("mon-k8s.configured")
28 @when("config.changed")
29 @when("leadership.is_leader")
31 clear_flag("mon-k8s.configured")
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")
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")
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")
55 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
56 @when_not("mon-k8s.configured")
57 @when("leadership.is_leader")
59 layer
.status
.maintenance("Configuring mon container")
61 kafka
= endpoint_from_flag("kafka.ready")
62 mongo
= endpoint_from_flag("mongo.ready")
63 prometheus
= endpoint_from_flag("endpoint.prometheus.available")
65 if kafka
and mongo
and prometheus
:
66 kafka_units
= kafka
.kafkas()
67 kafka_unit
= kafka_units
[0]
69 mongo_uri
= mongo
.connection_string()
70 log("Mongo URI: {}".format(mongo_uri
))
72 prometheus_url
= prometheus
.targets()[0]["targets"][0]
74 if mongo_uri
and kafka_unit
["host"]:
77 kafka_unit
["host"], kafka_unit
["port"], mongo_uri
, prometheus_url
80 log("set pod spec:\n{}".format(spec
))
82 set_flag("mon-k8s.configured")
83 except Exception as e
:
84 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
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")
93 @when("mon-k8s.configured")
95 layer
.status
.active("ready")
98 def make_pod_spec(kafka_host
, kafka_port
, mongo_uri
, prometheus_url
):
99 """Make pod specification for Kubernetes
102 kafka_host (str): Kafka hostname or IP
103 kafka_port (int): Kafka port
104 mongo_uri (str): Mongo URI
106 pod_spec: Pod specification for Kubernetes
109 with
open("reactive/spec_template.yaml") as spec_file
:
110 pod_spec_template
= spec_file
.read()
116 "name": md
.get("name"),
117 "docker_image": cfg
.get("image"),
118 "kafka_host": kafka_host
,
119 "kafka_port": kafka_port
,
120 "mongo_uri": mongo_uri
,
121 "prometheus_url": prometheus_url
,
124 return pod_spec_template
% data