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 log
, metadata
, config
19 from charms
import layer
20 from charms
.osm
.k8s
import get_service_ip
23 @hook("upgrade-charm")
24 @when("leadership.is_leader")
26 clear_flag("ro-k8s.configured")
29 @when("config.changed")
30 @when("leadership.is_leader")
32 clear_flag("ro-k8s.configured")
35 @when_not("mysql.available")
36 @when_not("ro-k8s.configured")
37 def waiting_for_mysql():
38 layer
.status
.waiting("Waiting for mysql to be ready")
41 @when("mysql.available")
42 @when_not("ro-k8s.configured")
43 @when("leadership.is_leader")
45 layer
.status
.maintenance("Configuring ro container")
47 mysql
= endpoint_from_flag("mysql.available")
54 mysql
.root_password(),
57 log("set pod spec:\n{}".format(spec
))
59 layer
.status
.active("creating container")
60 set_flag("ro-k8s.configured")
61 except Exception as e
:
62 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
65 @when("ro-k8s.configured")
67 layer
.status
.active("ready")
70 @when("ro-k8s.configured", "ro.joined")
72 layer
.status
.maintenance("Sending RO configuration")
74 ro
= endpoint_from_flag("ro.joined")
76 service_ip
= get_service_ip("ro")
79 service_ip
, get_ro_port(),
81 clear_flag("ro.joined")
82 except Exception as e
:
83 log("Fail sending RO configuration: {}".format(e
))
87 mysql_host
, mysql_port
, mysql_user
, mysql_password
, mysql_root_password
89 """Make pod specification for Kubernetes
92 mysql_name (str): RO DB name
93 mysql_host (str): RO DB host
94 mysql_port (int): RO DB port
95 mysql_user (str): RO DB user
96 mysql_password (str): RO DB password
98 pod_spec: Pod specification for Kubernetes
101 with
open("reactive/spec_template.yaml") as spec_file
:
102 pod_spec_template
= spec_file
.read()
108 "name": md
.get("name"),
109 "docker_image": cfg
.get("image"),
110 "mysql_host": mysql_host
,
111 "mysql_port": mysql_port
,
112 "mysql_user": mysql_user
,
113 "mysql_password": mysql_password
,
114 "mysql_root_password": mysql_root_password
,
118 return pod_spec_template
% data
122 """Returns RO port"""
124 return cfg
.get("advertised-port")