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
24 from charms
.osm
.k8s
import get_service_ip
30 @hook("upgrade-charm")
31 @when("leadership.is_leader")
33 clear_flag("nbi-k8s.configured")
36 @when("config.changed")
37 @when("leadership.is_leader")
39 clear_flag("nbi-k8s.configured")
42 @when("config.changed.auth-backend")
43 def clear_keystone_ready():
44 clear_flag("keystone.ready")
47 @when_not("kafka.ready")
48 @when_not("nbi-k8s.configured")
49 @when("leadership.is_leader")
50 def waiting_for_kafka():
51 layer
.status
.waiting("Waiting for kafka to be ready")
54 @when_not("mongo.ready")
55 @when_not("nbi-k8s.configured")
56 @when("leadership.is_leader")
57 def waiting_for_mongo():
58 layer
.status
.waiting("Waiting for mongo to be ready")
61 @when_not("endpoint.prometheus.available")
62 @when_not("nbi-k8s.configured")
63 @when("leadership.is_leader")
64 def waiting_for_prometheus():
65 layer
.status
.waiting("Waiting for prometheus to be ready")
68 @when_not("keystone.ready")
69 @when("leadership.is_leader")
70 @when_not("nbi-k8s.configured")
71 def waiting_for_keystone():
72 auth_backend
= config().get("auth-backend")
73 if auth_backend
== "keystone":
74 layer
.status
.waiting("Waiting for Keystone to be ready")
76 set_flag("keystone.ready")
79 @when_not("keystone.ready")
80 @when("leadership.is_leader")
81 @when_not("nbi-k8s.configured")
82 def waiting_for_keystone():
83 auth_backend
= config().get("auth-backend")
84 if auth_backend
== "keystone":
85 layer
.status
.waiting("Waiting for Keystone to be ready")
87 set_flag("keystone.ready")
90 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available", "keystone.ready")
91 @when_not("nbi-k8s.configured")
92 @when("leadership.is_leader")
94 layer
.status
.maintenance("Configuring nbi container")
96 kafka
= endpoint_from_flag("kafka.ready")
97 mongo
= endpoint_from_flag("mongo.ready")
98 prometheus
= endpoint_from_flag("endpoint.prometheus.available")
100 if kafka
and mongo
and prometheus
:
101 kafka_units
= kafka
.kafkas()
102 kafka_unit
= kafka_units
[0]
104 mongo_uri
= mongo
.connection_string()
105 log("Mongo URI: {}".format(mongo_uri
))
107 prometheus_uri
= prometheus
.targets()[0]["targets"][0]
111 and kafka_unit
["host"]
112 and kafka_unit
["port"]
124 auth_backend
= config().get("auth-backend")
126 if auth_backend
== "keystone":
127 keystone
= endpoint_from_flag("keystone.ready")
129 keystone_units
= keystone
.keystones()
130 keystone_unit
= keystone_units
[0]
132 keystone_unit
["host"]
133 and keystone_unit
["port"]
134 and keystone_unit
["user_domain_name"]
135 and keystone_unit
["project_domain_name"]
136 and keystone_unit
["username"]
137 and keystone_unit
["password"]
138 and keystone_unit
["service"]
141 "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
142 "OSMNBI_AUTHENTICATION_AUTH_URL": keystone_unit
["host"],
143 "OSMNBI_AUTHENTICATION_AUTH_PORT": keystone_unit
[
146 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": keystone_unit
[
149 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": keystone_unit
[
150 "project_domain_name"
152 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": keystone_unit
[
155 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": keystone_unit
[
158 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": keystone_unit
[
162 spec
["containers"][0]["config"].update(auth_keystone
)
163 elif auth_backend
== "internal":
164 spec
["containers"][0]["config"][
165 "OSMNBI_AUTHENTICATION_BACKEND"
168 layer
.status
.blocked(
169 "Unknown authentication method: {}".format(auth_backend
)
172 log("set pod spec:\n{}".format(spec
))
174 set_flag("nbi-k8s.configured")
175 except Exception as e
:
176 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
177 log("Error: {}".format(traceback
.format_exc()))
180 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available")
181 @when_not("leadership.is_leader")
182 def non_leaders_active():
183 layer
.status
.active("ready")
186 @when("nbi-k8s.configured")
187 def set_nbi_active():
188 layer
.status
.active("ready")
191 @when("nbi-k8s.configured", "nbi.joined")
193 layer
.status
.maintenance("Sending NBI configuration")
195 nbi
= endpoint_from_flag("nbi.joined")
197 service_ip
= get_service_ip("nbi")
199 nbi
.send_connection(service_ip
, get_nbi_port())
200 clear_flag("nbi.joined")
201 except Exception as e
:
202 log("Fail sending NBI configuration: {}".format(e
))
205 def make_pod_spec(kafka_host
, kafka_port
, mongo_uri
, prometheus_uri
):
206 """Make pod specification for Kubernetes
209 kafka_host (str): Kafka hostname or IP
210 kafka_port (int): Kafka port
211 mongo_uri (str): Mongo URI
212 prometheus_uri (str): Prometheus URI
214 pod_spec: Pod specification for Kubernetes
217 with
open("reactive/spec_template.yaml") as spec_file
:
218 pod_spec_template
= spec_file
.read()
222 prometheus_host
, prometheus_port
= parse_hostport(prometheus_uri
)
224 "name": md
.get("name"),
225 "docker_image": cfg
.get("image"),
226 "mongo_uri": mongo_uri
,
227 "kafka_host": "{}".format(kafka_host
),
228 "kafka_port": "{}".format(kafka_port
),
229 "prometheus_host": "{}".format(prometheus_host
),
230 "prometheus_port": "{}".format(prometheus_port
),
234 return pod_spec_template
% data
237 def parse_hostport(uri
):
239 uri
= uri
.split("//")[1]
240 result
= urllib
.parse
.urlsplit("//" + uri
)
241 return result
.hostname
, result
.port
245 """Returns NBI port"""
247 return cfg
.get("advertised-port")