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 def waiting_for_kafka():
50 layer
.status
.waiting("Waiting for kafka to be ready")
53 @when_not("mongo.ready")
54 @when_not("nbi-k8s.configured")
55 def waiting_for_mongo():
56 layer
.status
.waiting("Waiting for mongo to be ready")
59 @when_not("endpoint.prometheus.available")
60 @when_not("nbi-k8s.configured")
61 def waiting_for_prometheus():
62 layer
.status
.waiting("Waiting for prometheus to be ready")
65 @when_not("keystone.ready")
66 @when_not("nbi-k8s.configured")
67 def waiting_for_keystone():
68 auth_backend
= config().get("auth-backend")
69 if auth_backend
== "keystone":
70 layer
.status
.waiting("Waiting for Keystone to be ready")
72 set_flag("keystone.ready")
75 @when("kafka.ready", "mongo.ready", "endpoint.prometheus.available", "keystone.ready")
76 @when_not("nbi-k8s.configured")
77 @when("leadership.is_leader")
79 layer
.status
.maintenance("Configuring nbi container")
81 kafka
= endpoint_from_flag("kafka.ready")
82 mongo
= endpoint_from_flag("mongo.ready")
83 prometheus
= endpoint_from_flag("endpoint.prometheus.available")
85 if kafka
and mongo
and prometheus
:
86 kafka_units
= kafka
.kafkas()
87 kafka_unit
= kafka_units
[0]
89 mongo_uri
= mongo
.connection_string()
90 log("Mongo URI: {}".format(mongo_uri
))
92 prometheus_uri
= prometheus
.targets()[0]["targets"][0]
96 and kafka_unit
["host"]
97 and kafka_unit
["port"]
109 auth_backend
= config().get("auth-backend")
111 if auth_backend
== "keystone":
112 keystone
= endpoint_from_flag("keystone.ready")
114 keystone_units
= keystone
.keystones()
115 keystone_unit
= keystone_units
[0]
117 keystone_unit
["host"]
118 and keystone_unit
["port"]
119 and keystone_unit
["user_domain_name"]
120 and keystone_unit
["project_domain_name"]
121 and keystone_unit
["username"]
122 and keystone_unit
["password"]
123 and keystone_unit
["service"]
126 "OSMNBI_AUTHENTICATION_BACKEND": "keystone",
127 "OSMNBI_AUTHENTICATION_AUTH_URL": keystone_unit
["host"],
128 "OSMNBI_AUTHENTICATION_AUTH_PORT": keystone_unit
[
131 "OSMNBI_AUTHENTICATION_USER_DOMAIN_NAME": keystone_unit
[
134 "OSMNBI_AUTHENTICATION_PROJECT_DOMAIN_NAME": keystone_unit
[
135 "project_domain_name"
137 "OSMNBI_AUTHENTICATION_SERVICE_USERNAME": keystone_unit
[
140 "OSMNBI_AUTHENTICATION_SERVICE_PASSWORD": keystone_unit
[
143 "OSMNBI_AUTHENTICATION_SERVICE_PROJECT": keystone_unit
[
147 spec
["containers"][0]["config"].update(auth_keystone
)
148 elif auth_backend
== "internal":
149 spec
["containers"][0]["config"][
150 "OSMNBI_AUTHENTICATION_BACKEND"
153 layer
.status
.blocked(
154 "Unknown authentication method: {}".format(auth_backend
)
157 log("set pod spec:\n{}".format(spec
))
159 set_flag("nbi-k8s.configured")
160 except Exception as e
:
161 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
162 log("Error: {}".format(traceback
.format_exc()))
165 @when("nbi-k8s.configured")
166 def set_nbi_active():
167 layer
.status
.active("ready")
170 @when("nbi-k8s.configured", "nbi.joined")
172 layer
.status
.maintenance("Sending NBI configuration")
174 nbi
= endpoint_from_flag("nbi.joined")
176 service_ip
= get_service_ip("nbi")
178 nbi
.send_connection(service_ip
, get_nbi_port())
179 clear_flag("nbi.joined")
180 except Exception as e
:
181 log("Fail sending NBI configuration: {}".format(e
))
184 def make_pod_spec(kafka_host
, kafka_port
, mongo_uri
, prometheus_uri
):
185 """Make pod specification for Kubernetes
188 kafka_host (str): Kafka hostname or IP
189 kafka_port (int): Kafka port
190 mongo_uri (str): Mongo URI
191 prometheus_uri (str): Prometheus URI
193 pod_spec: Pod specification for Kubernetes
196 with
open("reactive/spec_template.yaml") as spec_file
:
197 pod_spec_template
= spec_file
.read()
201 prometheus_host
, prometheus_port
= parse_hostport(prometheus_uri
)
203 "name": md
.get("name"),
204 "docker_image": cfg
.get("image"),
205 "mongo_uri": mongo_uri
,
206 "kafka_host": "{}".format(kafka_host
),
207 "kafka_port": "{}".format(kafka_port
),
208 "prometheus_host": "{}".format(prometheus_host
),
209 "prometheus_port": "{}".format(prometheus_port
),
213 return pod_spec_template
% data
216 def parse_hostport(uri
):
218 uri
= uri
.split("//")[1]
219 result
= urllib
.parse
.urlsplit("//" + uri
)
220 return result
.hostname
, result
.port
224 """Returns NBI port"""
226 return cfg
.get("advertised-port")