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.
17 from charmhelpers
.core
.hookenv
import (
22 from charms
import layer
23 from charms
.layer
.caas_base
import pod_spec_set
24 from charms
.reactive
import endpoint_from_flag
25 from charms
.reactive
import when
, when_not
, hook
26 from charms
.reactive
.flags
import set_flag
, clear_flag
29 logger
= logging
.getLogger(__name__
)
32 @hook("upgrade-charm")
33 @when("leadership.is_leader")
35 clear_flag("lcm-k8s.configured")
38 @when("config.changed")
39 @when("leadership.is_leader")
41 clear_flag("lcm-k8s.configured")
44 @when_not("kafka.ready")
45 @when_not("lcm-k8s.configured")
46 @when("leadership.is_leader")
47 def waiting_for_kafka():
48 layer
.status
.waiting("Waiting for kafka to be ready")
51 @when_not("mongo.ready")
52 @when_not("lcm-k8s.configured")
53 @when("leadership.is_leader")
54 def waiting_for_mongo():
55 layer
.status
.waiting("Waiting for mongo to be ready")
59 @when_not("lcm-k8s.configured")
60 @when("leadership.is_leader")
62 layer
.status
.waiting("Waiting for ro to be ready")
65 @when("kafka.ready", "mongo.ready", "ro.ready")
66 @when_not("lcm-k8s.configured")
67 @when("leadership.is_leader")
69 layer
.status
.maintenance("Configuring lcm container")
71 kafka
= endpoint_from_flag("kafka.ready")
72 mongo
= endpoint_from_flag("mongo.ready")
73 osm_ro
= endpoint_from_flag("ro.ready")
75 if kafka
and mongo
and osm_ro
:
76 kafka_units
= kafka
.kafkas()
77 kafka_unit
= kafka_units
[0]
79 mongo_uri
= mongo
.connection_string()
80 log("Mongo URI: {}".format(mongo_uri
))
87 and kafka_unit
["host"]
88 and kafka_unit
["port"]
100 log("set pod spec:\n{}".format(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
))
108 @when("kafka.ready", "mongo.ready", "ro.ready")
109 @when_not("leadership.is_leader")
110 def non_leaders_active():
111 layer
.status
.active("ready")
114 @when("lcm-k8s.configured")
115 @when("leadership.is_leader")
116 def set_lcm_active():
117 layer
.status
.active("ready")
120 def make_pod_spec(ro_host
, ro_port
, kafka_host
, kafka_port
, mongo_uri
):
121 """Make pod specification for Kubernetes
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
130 pod_spec: Pod specification for Kubernetes
133 with
open("reactive/spec_template.yaml") as spec_file
:
134 pod_spec_template
= spec_file
.read()
140 "name": md
.get("name"),
143 "kafka_host": kafka_host
,
144 "kafka_port": kafka_port
,
145 "mongo_uri": mongo_uri
,
148 logs
= pod_spec_template
% data
149 spec
= yaml
.safe_load(pod_spec_template
% data
)
150 if "vca_apiproxy" in cfg
and cfg
["vca_apiproxy"] != "":
151 spec
["containers"][0]["config"]["OSMLCM_VCA_APIPROXY"] = cfg
["vca_apiproxy"]