blob: 8ad6f98c8dd346c81313d6a1d3ee922784777190 [file] [log] [blame]
David Garcia82c5ffa2020-03-09 08:38:17 +01001# Copyright 2020 Canonical Ltd.
2#
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
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
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.
14from charms.layer.caas_base import pod_spec_set
15from charms.reactive import endpoint_from_flag
16from charms.reactive import when, when_not, hook
17from charms.reactive.flags import set_flag, clear_flag
18from charmhelpers.core.hookenv import log, metadata, config
19from charms import layer
20from charms.osm.k8s import get_service_ip
21
22
23@hook("upgrade-charm")
24@when("leadership.is_leader")
25def upgrade():
26 clear_flag("ro-k8s.configured")
27
28
29@when("config.changed")
30@when("leadership.is_leader")
31def restart():
32 clear_flag("ro-k8s.configured")
33
34
35@when_not("mysql.available")
36@when_not("ro-k8s.configured")
David Garcia68faf8d2020-09-01 10:12:16 +020037@when("leadership.is_leader")
David Garcia82c5ffa2020-03-09 08:38:17 +010038def waiting_for_mysql():
39 layer.status.waiting("Waiting for mysql to be ready")
40
41
42@when("mysql.available")
43@when_not("ro-k8s.configured")
44@when("leadership.is_leader")
45def configure():
46 layer.status.maintenance("Configuring ro container")
47 try:
48 mysql = endpoint_from_flag("mysql.available")
49
David Garcia68faf8d2020-09-01 10:12:16 +020050 if mysql.host() is not None:
51 spec = make_pod_spec(
52 mysql.host(),
53 mysql.port(),
54 mysql.user(),
55 mysql.password(),
56 mysql.root_password(),
57 )
David Garcia82c5ffa2020-03-09 08:38:17 +010058
David Garcia68faf8d2020-09-01 10:12:16 +020059 log("set pod spec:\n{}".format(spec))
60 pod_spec_set(spec)
61 layer.status.active("creating container")
62 set_flag("ro-k8s.configured")
David Garcia82c5ffa2020-03-09 08:38:17 +010063 except Exception as e:
64 layer.status.blocked("k8s spec failed to deploy: {}".format(e))
65
66
David Garcia68faf8d2020-09-01 10:12:16 +020067@when("mysql.available")
68@when_not("leadership.is_leader")
69def non_leaders_active():
70 layer.status.active("ready")
71
72
David Garcia82c5ffa2020-03-09 08:38:17 +010073@when("ro-k8s.configured")
74def set_ro_active():
75 layer.status.active("ready")
76
77
78@when("ro-k8s.configured", "ro.joined")
79def send_config():
80 layer.status.maintenance("Sending RO configuration")
81 try:
82 ro = endpoint_from_flag("ro.joined")
83 if ro:
84 service_ip = get_service_ip("ro")
85 if service_ip:
86 ro.send_connection(
87 service_ip, get_ro_port(),
88 )
89 clear_flag("ro.joined")
90 except Exception as e:
91 log("Fail sending RO configuration: {}".format(e))
92
93
94def make_pod_spec(
95 mysql_host, mysql_port, mysql_user, mysql_password, mysql_root_password
96):
97 """Make pod specification for Kubernetes
98
99 Args:
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
105 Returns:
106 pod_spec: Pod specification for Kubernetes
107 """
108
109 with open("reactive/spec_template.yaml") as spec_file:
110 pod_spec_template = spec_file.read()
111
112 md = metadata()
113 cfg = config()
114
115 data = {
116 "name": md.get("name"),
117 "docker_image": cfg.get("image"),
118 "mysql_host": mysql_host,
119 "mysql_port": mysql_port,
120 "mysql_user": mysql_user,
121 "mysql_password": mysql_password,
122 "mysql_root_password": mysql_root_password,
123 }
124 data.update(cfg)
125
126 return pod_spec_template % data
127
128
129def get_ro_port():
130 """Returns RO port"""
131 cfg = config()
132 return cfg.get("advertised-port")