1 # Copyright 2021 Canonical Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
15 # For those usages not covered by the Apache License, Version 2.0 please
16 # contact: legal@canonical.com
18 # To get in touch with the maintainers, please contact:
19 # osm-charmers@lists.launchpad.net
22 from charms
.layer
.caas_base
import pod_spec_set
23 from charms
.reactive
import when
, when_not
, hook
24 from charms
.reactive
import endpoint_from_flag
25 from charms
.reactive
.flags
import set_flag
, get_state
, clear_flag
27 from charmhelpers
.core
.hookenv
import (
33 from charms
import layer
34 from charms
.osm
.k8s
import is_pod_up
, get_service_ip
37 @hook("upgrade-charm")
38 @when("leadership.is_leader")
40 clear_flag("mariadb-k8s.configured")
43 @when("config.changed")
44 @when("leadership.is_leader")
46 clear_flag("mariadb-k8s.configured")
49 @when_not("mariadb-k8s.configured")
50 @when("leadership.is_leader")
52 layer
.status
.maintenance("Configuring mariadb-k8s container")
54 spec
= make_pod_spec()
55 log("set pod spec:\n{}".format(spec
))
58 set_flag("mariadb-k8s.configured")
61 @when("mariadb-k8s.configured")
62 def set_mariadb_active():
63 layer
.status
.active("ready")
66 @when_not("leadership.is_leader")
67 def non_leaders_active():
68 layer
.status
.active("ready")
71 @when("mariadb-k8s.configured", "mysql.database.requested")
72 def provide_database():
73 mysql
= endpoint_from_flag("mysql.database.requested")
75 if not is_pod_up("mysql"):
76 log("The pod is not ready.")
79 for request
, application
in mysql
.database_requests().items():
82 log("request -> {0} for app -> {1}".format(request
, application
))
83 user
= get_state("user")
84 password
= get_state("password")
85 database_name
= get_state("database")
86 root_password
= get_state("root_password")
88 log("db params: {0}:{1}@{2}".format(user
, password
, database_name
))
90 service_ip
= get_service_ip("mysql")
92 mysql
.provide_database(
96 database_name
=database_name
,
99 root_password
=root_password
,
101 mysql
.mark_complete()
102 except Exception as e
:
103 log("Exception while providing database: {}".format(e
))
107 """Make pod specification for Kubernetes
110 pod_spec: Pod specification for Kubernetes
112 if config().get("ha-mode"):
113 with
open("reactive/spec_template_ha.yaml") as spec_file
:
114 pod_spec_template
= spec_file
.read()
115 image
= config().get("ha-image")
117 with
open("reactive/spec_template.yaml") as spec_file
:
118 pod_spec_template
= spec_file
.read()
119 image
= config().get("image")
124 user
= cfg
.get("user")
125 password
= cfg
.get("password")
126 database
= cfg
.get("database")
127 root_password
= cfg
.get("root_password")
128 app_name
= application_name()
130 set_flag("user", user
)
131 set_flag("password", password
)
132 set_flag("database", database
)
133 set_flag("root_password", root_password
)
136 "name": md
.get("name"),
137 "docker_image": image
,
138 "application_name": app_name
,
141 return pod_spec_template
% data