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
27 logger
= logging
.getLogger(__name__
)
30 @hook("upgrade-charm")
31 @when("leadership.is_leader")
33 clear_flag("lcm-k8s.configured")
36 @when("config.changed")
37 @when("leadership.is_leader")
39 clear_flag("lcm-k8s.configured")
42 @when_not("kafka.ready")
43 @when_not("lcm-k8s.configured")
44 @when("leadership.is_leader")
45 def waiting_for_kafka():
46 layer
.status
.waiting("Waiting for kafka to be ready")
49 @when_not("mongo.ready")
50 @when_not("lcm-k8s.configured")
51 @when("leadership.is_leader")
52 def waiting_for_mongo():
53 layer
.status
.waiting("Waiting for mongo to be ready")
57 @when_not("lcm-k8s.configured")
58 @when("leadership.is_leader")
60 layer
.status
.waiting("Waiting for ro to be ready")
63 @when("kafka.ready", "mongo.ready", "ro.ready")
64 @when_not("lcm-k8s.configured")
65 @when("leadership.is_leader")
67 layer
.status
.maintenance("Configuring lcm container")
69 kafka
= endpoint_from_flag("kafka.ready")
70 mongo
= endpoint_from_flag("mongo.ready")
71 osm_ro
= endpoint_from_flag("ro.ready")
73 if kafka
and mongo
and osm_ro
:
74 kafka_units
= kafka
.kafkas()
75 kafka_unit
= kafka_units
[0]
77 mongo_uri
= mongo
.connection_string()
78 log("Mongo URI: {}".format(mongo_uri
))
85 and kafka_unit
["host"]
86 and kafka_unit
["port"]
98 log("set pod spec:\n{}".format(spec
))
100 layer
.status
.active("creating container")
101 set_flag("lcm-k8s.configured")
102 except Exception as e
:
103 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
106 @when("kafka.ready", "mongo.ready", "ro.ready")
107 @when_not("leadership.is_leader")
108 def non_leaders_active():
109 layer
.status
.active("ready")
112 @when("lcm-k8s.configured")
113 @when("leadership.is_leader")
114 def set_lcm_active():
115 layer
.status
.active("ready")
118 def make_pod_spec(ro_host
, ro_port
, kafka_host
, kafka_port
, mongo_uri
):
119 """Make pod specification for Kubernetes
122 ro_host (str): RO hostname or IP
123 ro_port (str): RO Port
124 kafka_host (str): Kafka hostname or IP
125 kafka_port (int): Kafka port
126 mongo_uri (str): Mongo URI
128 pod_spec: Pod specification for Kubernetes
131 with
open("reactive/spec_template.yaml") as spec_file
:
132 pod_spec_template
= spec_file
.read()
138 "name": md
.get("name"),
139 "docker_image": cfg
.get("image"),
142 "kafka_host": kafka_host
,
143 "kafka_port": kafka_port
,
144 "mongo_uri": mongo_uri
,
147 logs
= pod_spec_template
% data
148 spec
= yaml
.safe_load(pod_spec_template
% data
)
149 if "vca_apiproxy" in cfg
and cfg
["vca_apiproxy"] != "":
150 spec
["containers"][0]["config"]["OSMLCM_VCA_APIPROXY"] = cfg
["vca_apiproxy"]