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
55 security_context
: bool
57 @validator("log_level")
58 def validate_log_level(cls
, v
):
59 if v
not in {"INFO", "DEBUG"}:
60 raise ValueError("value must be INFO or DEBUG")
63 @validator("image_pull_policy")
64 def validate_image_pull_policy(cls
, v
):
67 "ifnotpresent": "IfNotPresent",
71 if v
not in values
.keys():
72 raise ValueError("value must be always, ifnotpresent or never")
76 class ZookeeperCharm(CharmedOsmBase
):
77 """Zookeeper Charm."""
79 def __init__(self
, *args
) -> NoReturn
:
80 """Zookeeper Charm constructor."""
81 super().__init
__(*args
, oci_image
="image")
82 # Initialize Zookeeper cluster relation
83 self
.zookeeper_cluster
= ZookeeperCluster(self
, "cluster", CLIENT_PORT
)
84 self
.framework
.observe(self
.on
["cluster"].relation_changed
, self
._setup
_cluster
)
85 # Initialize Zookeeper relation
86 self
.zookeeper_server
= ZookeeperServer(self
, "zookeeper")
87 self
.framework
.observe(self
.on
["zookeeper"].relation_joined
, self
._publish
_info
)
91 return self
.zookeeper_cluster
.num_units
94 def zookeeper_uri(self
):
95 return self
.zookeeper_cluster
.zookeeper_uri
97 def _setup_cluster(self
, event
: EventBase
):
98 """Publishes Zookeeper information and reconfigures the pod.
101 event (EventBase): Zookeeper Cluster relation event.
103 self
._publish
_info
(event
)
106 def _publish_info(self
, event
: EventBase
):
107 """Publishes Zookeeper information.
110 event (EventBase): Zookeeper relation event.
112 if self
.unit
.is_leader():
113 zk_uri
= self
.zookeeper_uri
115 self
.zookeeper_server
.publish_info(zk_uri
)
119 def build_pod_spec(self
, image_info
):
121 config
= ConfigModel(**dict(self
.config
))
123 # Create Builder for the PodSpec
124 pod_spec_builder
= PodSpecV3Builder(
125 enable_security_context
=config
.security_context
129 container_builder
= ContainerV3Builder(
132 config
.image_pull_policy
,
133 run_as_non_root
=config
.security_context
,
136 container_builder
.add_port(name
="client", port
=CLIENT_PORT
)
137 container_builder
.add_port(name
="server", port
=SERVER_PORT
)
138 container_builder
.add_port(name
="leader-election", port
=LEADER_ELECTION_PORT
)
139 container_builder
.add_tcpsocket_readiness_probe(
141 initial_delay_seconds
=10,
146 container_builder
.add_tcpsocket_liveness_probe(
147 CLIENT_PORT
, initial_delay_seconds
=20
149 container_builder
.add_command(
156 f
"--servers={self.num_units}",
157 "--data_dir=/var/lib/zookeeper/data",
158 "--data_log_dir=/var/lib/zookeeper/data/log",
159 "--conf_dir=/opt/zookeeper/conf",
160 f
"--client_port={CLIENT_PORT}",
161 f
"--election_port={LEADER_ELECTION_PORT}",
162 f
"--server_port={SERVER_PORT}",
163 f
"--tick_time={config.tick_time}",
164 f
"--init_limit={config.init_limit}",
165 f
"--sync_limit={config.sync_limit}",
166 f
"--heap={config.heap}M",
167 f
"--max_client_cnxns={config.max_client_cnxns}",
168 f
"--snap_retain_count={config.snap_retain_count}",
169 f
"--purge_interval={config.purge_interval}",
170 f
"--max_session_timeout={config.max_session_timeout}",
171 f
"--min_session_timeout={config.min_session_timeout}",
172 f
"--log_level={config.log_level}",
178 container
= container_builder
.build()
180 # Add container to pod spec
181 pod_spec_builder
.add_container(container
)
183 return pod_spec_builder
.build()
186 if __name__
== "__main__":