blob: 6e4588c4f81ffd00fb75305a7a7eb6c6ffbcc26f [file] [log] [blame]
David Garcia36c87722021-08-30 18:01:22 +02001#!/usr/bin/env python3
2# Copyright 2021 Canonical Ltd.
3#
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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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
14# under the License.
15#
16# For those usages not covered by the Apache License, Version 2.0 please
17# contact: legal@canonical.com
18#
19# To get in touch with the maintainers, please contact:
20# osm-charmers@lists.launchpad.net
21##
22
23# pylint: disable=E0213
24
25import logging
26from typing import NoReturn
27
28
29from ops.framework import EventBase
30from ops.main import main
31from opslib.osm.charm import CharmedOsmBase
32from opslib.osm.interfaces.zookeeper import ZookeeperCluster, ZookeeperServer
33from opslib.osm.pod import ContainerV3Builder, PodSpecV3Builder
34from opslib.osm.validator import ModelValidator, validator
35
36logger = logging.getLogger(__name__)
37
38CLIENT_PORT = 2181
39SERVER_PORT = 2888
40LEADER_ELECTION_PORT = 3888
41
42
43class ConfigModel(ModelValidator):
44 log_level: str
45 image_pull_policy: str
46 min_session_timeout: int
47 max_session_timeout: int
48 purge_interval: int
49 snap_retain_count: int
50 max_client_cnxns: int
51 heap: int
52 sync_limit: int
53 init_limit: int
54 tick_time: int
55
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")
60 return v
61
62 @validator("image_pull_policy")
63 def validate_image_pull_policy(cls, v):
64 values = {
65 "always": "Always",
66 "ifnotpresent": "IfNotPresent",
67 "never": "Never",
68 }
69 v = v.lower()
70 if v not in values.keys():
71 raise ValueError("value must be always, ifnotpresent or never")
72 return values[v]
73
74
75class ZookeeperCharm(CharmedOsmBase):
76 """Zookeeper Charm."""
77
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)
87
88 @property
89 def num_units(self):
90 return self.zookeeper_cluster.num_units
91
92 @property
93 def zookeeper_uri(self):
94 return self.zookeeper_cluster.zookeeper_uri
95
96 def _setup_cluster(self, event: EventBase):
97 """Publishes Zookeeper information and reconfigures the pod.
98
99 Args:
100 event (EventBase): Zookeeper Cluster relation event.
101 """
102 self._publish_zookeeper_info(event)
103 self.configure_pod()
104
105 def _publish_info(self, event: EventBase):
106 """Publishes Zookeeper information.
107
108 Args:
109 event (EventBase): Zookeeper relation event.
110 """
111 if self.unit.is_leader():
112 zk_uri = self.zookeeper_uri
113 if zk_uri:
114 self.zookeeper_server.publish_info(zk_uri)
115 else:
116 event.defer()
117
118 def build_pod_spec(self, image_info):
119 # Validate config
120 config = ConfigModel(**dict(self.config))
121
122 # Create Builder for the PodSpec
123 pod_spec_builder = PodSpecV3Builder()
124
125 # Build Container
126 container_builder = ContainerV3Builder(
127 self.app.name, image_info, config.image_pull_policy
128 )
129
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(
134 CLIENT_PORT,
135 initial_delay_seconds=10,
136 timeout_seconds=5,
137 failure_threshold=6,
138 success_threshold=1,
139 )
140 container_builder.add_tcpsocket_liveness_probe(
141 CLIENT_PORT, initial_delay_seconds=20
142 )
143 container_builder.add_command(
144 [
145 "sh",
146 "-c",
147 " ".join(
148 [
149 "start-zookeeper",
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}",
167 ]
168 ),
169 ]
170 )
171
172 container = container_builder.build()
173
174 # Add container to pod spec
175 pod_spec_builder.add_container(container)
176
177 return pod_spec_builder.build()
178
179
180if __name__ == "__main__":
181 main(ZookeeperCharm)