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 @when("leadership.is_leader")
38 def waiting_for_mysql():
39 layer
.status
.waiting("Waiting for mysql to be ready")
42 @when("mysql.available")
43 @when_not("ro-k8s.configured")
44 @when("leadership.is_leader")
46 layer
.status
.maintenance("Configuring ro container")
48 mysql
= endpoint_from_flag("mysql.available")
50 if mysql
.host() is not None:
56 mysql
.root_password(),
59 log("set pod spec:\n{}".format(spec
))
61 layer
.status
.active("creating container")
62 set_flag("ro-k8s.configured")
63 except Exception as e
:
64 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
67 @when("mysql.available")
68 @when_not("leadership.is_leader")
69 def non_leaders_active():
70 layer
.status
.active("ready")
73 @when("ro-k8s.configured")
75 layer
.status
.active("ready")
78 @when("ro-k8s.configured", "ro.joined")
80 layer
.status
.maintenance("Sending RO configuration")
82 ro
= endpoint_from_flag("ro.joined")
84 service_ip
= get_service_ip("ro")
87 service_ip
, get_ro_port(),
89 clear_flag("ro.joined")
90 except Exception as e
:
91 log("Fail sending RO configuration: {}".format(e
))
95 mysql_host
, mysql_port
, mysql_user
, mysql_password
, mysql_root_password
97 """Make pod specification for Kubernetes
100 mysql_name (str): RO DB name
101 mysql_host (str): RO DB host
102 mysql_port (int): RO DB port
103 mysql_user (str): RO DB user
104 mysql_password (str): RO DB password
106 pod_spec: Pod specification for Kubernetes
109 with
open("reactive/spec_template.yaml") as spec_file
:
110 pod_spec_template
= spec_file
.read()
116 "name": md
.get("name"),
117 "mysql_host": mysql_host
,
118 "mysql_port": mysql_port
,
119 "mysql_user": mysql_user
,
120 "mysql_password": mysql_password
,
121 "mysql_root_password": mysql_root_password
,
125 return pod_spec_template
% data
129 """Returns RO port"""
131 return cfg
.get("advertised-port")