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
.layer
.caas_base
import pod_spec_set
15 from charms
.reactive
import endpoint_from_flag
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
20 from charms
.osm
.k8s
import get_service_ip
23 @hook("upgrade-charm")
24 @when("leadership.is_leader")
26 clear_flag("ro-k8s.configured")
29 @when("config.changed")
30 @when("leadership.is_leader")
32 clear_flag("ro-k8s.configured")
34 @when_not("kafka.ready")
35 @when_not("ro-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("ro-k8s.configured")
43 @when("leadership.is_leader")
44 def waiting_for_mongo():
45 layer
.status
.waiting("Waiting for mongo to be ready")
48 @when("kafka.ready", "mongo.ready")
49 @when_not("ro-k8s.configured")
50 @when("leadership.is_leader")
52 layer
.status
.maintenance("Configuring ro container")
54 kafka
= endpoint_from_flag("kafka.ready")
55 mongo
= endpoint_from_flag("mongo.ready")
58 kafka_units
= kafka
.kafkas()
59 kafka_unit
= kafka_units
[0]
61 mongo_uri
= mongo
.connection_string()
62 log("Mongo URI: {}".format(mongo_uri
))
66 and kafka_unit
["host"]
67 and kafka_unit
["port"]
77 log("set pod spec:\n{}".format(spec
))
79 layer
.status
.active("creating container")
80 set_flag("ro-k8s.configured")
81 except Exception as e
:
82 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
85 @when("kafka.ready", "mongo.ready")
86 @when_not("leadership.is_leader")
87 def non_leaders_active():
88 layer
.status
.active("ready")
91 @when("ro-k8s.configured")
93 layer
.status
.active("ready")
96 @when("ro-k8s.configured", "ro.joined")
98 layer
.status
.maintenance("Sending RO configuration")
100 ro
= endpoint_from_flag("ro.joined")
102 service_ip
= get_service_ip("ro")
105 service_ip
, get_ro_port(),
107 clear_flag("ro.joined")
108 except Exception as e
:
109 log("Fail sending RO configuration: {}".format(e
))
112 def make_pod_spec(kafka_host
, kafka_port
, mongo_uri
):
113 """Make pod specification for Kubernetes
116 kafka_host (str): Kafka hostname or IP
117 kafka_port (int): Kafka port
118 mongo_uri (str): Mongo URI
120 pod_spec: Pod specification for Kubernetes
123 with
open("reactive/spec_template.yaml") as spec_file
:
124 pod_spec_template
= spec_file
.read()
130 "name": md
.get("name"),
131 "kafka_host": kafka_host
,
132 "kafka_port": kafka_port
,
133 "mongo_uri": mongo_uri
,
137 return pod_spec_template
% data
141 """Returns RO port"""
143 return cfg
.get("advertised-port")