85ec9c5b0cd311b09108eeaafa8298c785c8fff2
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 (
23 from charms
import layer
26 @hook("upgrade-charm")
27 @when("leadership.is_leader")
29 clear_flag("lcm-k8s.configured")
32 @when("config.changed")
33 @when("leadership.is_leader")
35 clear_flag("lcm-k8s.configured")
38 @when_not("kafka.ready")
39 @when_not("lcm-k8s.configured")
40 def waiting_for_kafka():
41 layer
.status
.waiting("Waiting for kafka to be ready")
44 @when_not("mongo.ready")
45 @when_not("lcm-k8s.configured")
46 def waiting_for_mongo():
47 layer
.status
.waiting("Waiting for mongo to be ready")
51 @when_not("lcm-k8s.configured")
53 layer
.status
.waiting("Waiting for ro to be ready")
56 @when("kafka.ready", "mongo.ready", "ro.ready")
57 @when_not("lcm-k8s.configured")
58 @when("leadership.is_leader")
60 layer
.status
.maintenance("Configuring lcm container")
62 kafka
= endpoint_from_flag("kafka.ready")
63 mongo
= endpoint_from_flag("mongo.ready")
64 osm_ro
= endpoint_from_flag("ro.ready")
66 if kafka
and mongo
and osm_ro
:
67 kafka_units
= kafka
.kafkas()
68 kafka_unit
= kafka_units
[0]
70 mongo_uri
= mongo
.connection_string()
71 log("Mongo URI: {}".format(mongo_uri
))
78 and kafka_unit
["host"]
79 and kafka_unit
["port"]
91 log("set pod spec:\n{}".format(spec
))
93 layer
.status
.active("creating container")
94 set_flag("lcm-k8s.configured")
95 except Exception as e
:
96 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
99 @when("lcm-k8s.configured")
100 def set_lcm_active():
101 layer
.status
.active("ready")
104 def make_pod_spec(ro_host
, ro_port
, kafka_host
, kafka_port
, mongo_uri
):
105 """Make pod specification for Kubernetes
108 ro_host (str): RO hostname or IP
109 ro_port (str): RO Port
110 kafka_host (str): Kafka hostname or IP
111 kafka_port (int): Kafka port
112 mongo_uri (str): Mongo URI
114 pod_spec: Pod specification for Kubernetes
117 with
open("reactive/spec_template.yaml") as spec_file
:
118 pod_spec_template
= spec_file
.read()
124 "name": md
.get("name"),
125 "docker_image": cfg
.get("image"),
128 "kafka_host": kafka_host
,
129 "kafka_port": kafka_port
,
130 "mongo_uri": mongo_uri
,
134 return pod_spec_template
% data