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
import layer
23 from charms
.layer
.caas_base
import pod_spec_set
24 from charms
.reactive
import endpoint_from_flag
25 from charms
.reactive
import when
, when_not
, hook
26 from charms
.reactive
.flags
import set_flag
, clear_flag
27 from charmhelpers
.core
.hookenv
import (
33 from charms
.osm
.k8s
import is_pod_up
, get_service_ip
36 @hook("upgrade-charm")
37 @when("leadership.is_leader")
39 clear_flag("zookeeper-k8s.configured")
42 @when("config.changed")
43 @when("leadership.is_leader")
45 clear_flag("zookeeper-k8s.configured")
48 @when_not("zookeeper-k8s.configured")
49 @when("leadership.is_leader")
51 layer
.status
.maintenance("Configuring zookeeper-k8s container")
53 spec
= make_pod_spec()
54 log("set pod spec:\n{}".format(spec
))
56 set_flag("zookeeper-k8s.configured")
58 except Exception as e
:
59 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
62 @when("zookeeper-k8s.configured")
64 layer
.status
.active("ready")
67 @when_not("leadership.is_leader")
68 def non_leaders_active():
69 layer
.status
.active("ready")
72 @when("zookeeper.joined")
73 @when("zookeeper-k8s.configured")
75 layer
.status
.maintenance("Sending Zookeeper configuration")
76 if not is_pod_up("zookeeper"):
77 log("The pod is not ready.")
80 zookeeper
= endpoint_from_flag("zookeeper.joined")
82 service_ip
= get_service_ip("zookeeper")
84 zookeeper
.send_connection(
85 get_zookeeper_client_port(), get_zookeeper_client_port(), service_ip
,
87 layer
.status
.active("ready")
91 """Make pod specification for Kubernetes
94 pod_spec: Pod specification for Kubernetes
96 with
open("reactive/spec_template.yaml") as spec_file
:
97 pod_spec_template
= spec_file
.read()
101 data
= {"name": md
.get("name"), "docker_image_path": cfg
.get("image")}
103 return pod_spec_template
% data
106 def get_zookeeper_client_port():
107 """Returns Zookeeper port"""
109 return cfg
.get("client-port")