2 # Copyright 2021 Canonical Ltd.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 # pylint: disable=E0213
26 from typing
import NoReturn
29 from ops
.framework
import EventBase
30 from ops
.main
import main
31 from opslib
.osm
.charm
import CharmedOsmBase
32 from opslib
.osm
.interfaces
.zookeeper
import ZookeeperCluster
, ZookeeperServer
33 from opslib
.osm
.pod
import ContainerV3Builder
, PodSpecV3Builder
34 from opslib
.osm
.validator
import ModelValidator
, validator
36 logger
= logging
.getLogger(__name__
)
40 LEADER_ELECTION_PORT
= 3888
43 class ConfigModel(ModelValidator
):
45 image_pull_policy
: str
46 min_session_timeout
: int
47 max_session_timeout
: int
49 snap_retain_count
: int
56 @validator("log_level")
57 def validate_log_level(cls
, v
):
58 if v
not in {"INFO", "DEBUG"}:
59 raise ValueError("value must be INFO or DEBUG")
62 @validator("image_pull_policy")
63 def validate_image_pull_policy(cls
, v
):
66 "ifnotpresent": "IfNotPresent",
70 if v
not in values
.keys():
71 raise ValueError("value must be always, ifnotpresent or never")
75 class ZookeeperCharm(CharmedOsmBase
):
76 """Zookeeper Charm."""
78 def __init__(self
, *args
) -> NoReturn
:
79 """Zookeeper Charm constructor."""
80 super().__init
__(*args
, oci_image
="image")
81 # Initialize Zookeeper cluster relation
82 self
.zookeeper_cluster
= ZookeeperCluster(self
, "cluster", CLIENT_PORT
)
83 self
.framework
.observe(self
.on
["cluster"].relation_changed
, self
._setup
_cluster
)
84 # Initialize Zookeeper relation
85 self
.zookeeper_server
= ZookeeperServer(self
, "zookeeper")
86 self
.framework
.observe(self
.on
["zookeeper"].relation_joined
, self
._publish
_info
)
90 return self
.zookeeper_cluster
.num_units
93 def zookeeper_uri(self
):
94 return self
.zookeeper_cluster
.zookeeper_uri
96 def _setup_cluster(self
, event
: EventBase
):
97 """Publishes Zookeeper information and reconfigures the pod.
100 event (EventBase): Zookeeper Cluster relation event.
102 self
._publish
_zookeeper
_info
(event
)
105 def _publish_info(self
, event
: EventBase
):
106 """Publishes Zookeeper information.
109 event (EventBase): Zookeeper relation event.
111 if self
.unit
.is_leader():
112 zk_uri
= self
.zookeeper_uri
114 self
.zookeeper_server
.publish_info(zk_uri
)
118 def build_pod_spec(self
, image_info
):
120 config
= ConfigModel(**dict(self
.config
))
122 # Create Builder for the PodSpec
123 pod_spec_builder
= PodSpecV3Builder()
126 container_builder
= ContainerV3Builder(
127 self
.app
.name
, image_info
, config
.image_pull_policy
130 container_builder
.add_port(name
="client", port
=CLIENT_PORT
)
131 container_builder
.add_port(name
="server", port
=SERVER_PORT
)
132 container_builder
.add_port(name
="leader-election", port
=LEADER_ELECTION_PORT
)
133 container_builder
.add_tcpsocket_readiness_probe(
135 initial_delay_seconds
=10,
140 container_builder
.add_tcpsocket_liveness_probe(
141 CLIENT_PORT
, initial_delay_seconds
=20
143 container_builder
.add_command(
150 f
"--servers={self.num_units}",
151 "--data_dir=/var/lib/zookeeper/data",
152 "--data_log_dir=/var/lib/zookeeper/data/log",
153 "--conf_dir=/opt/zookeeper/conf",
154 f
"--client_port={CLIENT_PORT}",
155 f
"--election_port={LEADER_ELECTION_PORT}",
156 f
"--server_port={SERVER_PORT}",
157 f
"--tick_time={config.tick_time}",
158 f
"--init_limit={config.init_limit}",
159 f
"--sync_limit={config.sync_limit}",
160 f
"--heap={config.heap}M",
161 f
"--max_client_cnxns={config.max_client_cnxns}",
162 f
"--snap_retain_count={config.snap_retain_count}",
163 f
"--purge_interval={config.purge_interval}",
164 f
"--max_session_timeout={config.max_session_timeout}",
165 f
"--min_session_timeout={config.min_session_timeout}",
166 f
"--log_level={config.log_level}",
172 container
= container_builder
.build()
174 # Add container to pod spec
175 pod_spec_builder
.add_container(container
)
177 return pod_spec_builder
.build()
180 if __name__
== "__main__":