| sousaedu | 3f6fa06 | 2021-01-19 11:32:39 +0000 | [diff] [blame] | 1 | # Copyright 2021 Canonical Ltd. |
| 2 | # |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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 |
| 13 | # under the License. |
| 14 | # |
| 15 | # For those usages not covered by the Apache License, Version 2.0 please |
| 16 | # contact: legal@canonical.com |
| 17 | # |
| 18 | # To get in touch with the maintainers, please contact: |
| 19 | # osm-charmers@lists.launchpad.net |
| 20 | ## |
| 21 | |
| 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 ( |
| 28 | log, |
| 29 | metadata, |
| 30 | config, |
| 31 | ) |
| 32 | |
| 33 | from charms.osm.k8s import is_pod_up, get_service_ip |
| 34 | |
| 35 | |
| 36 | @hook("upgrade-charm") |
| 37 | @when("leadership.is_leader") |
| 38 | def upgrade(): |
| 39 | clear_flag("zookeeper-k8s.configured") |
| 40 | |
| 41 | |
| 42 | @when("config.changed") |
| 43 | @when("leadership.is_leader") |
| 44 | def config_changed(): |
| 45 | clear_flag("zookeeper-k8s.configured") |
| 46 | |
| 47 | |
| 48 | @when_not("zookeeper-k8s.configured") |
| 49 | @when("leadership.is_leader") |
| 50 | def configure(): |
| 51 | layer.status.maintenance("Configuring zookeeper-k8s container") |
| 52 | try: |
| 53 | spec = make_pod_spec() |
| 54 | log("set pod spec:\n{}".format(spec)) |
| 55 | pod_spec_set(spec) |
| 56 | set_flag("zookeeper-k8s.configured") |
| 57 | |
| 58 | except Exception as e: |
| 59 | layer.status.blocked("k8s spec failed to deploy: {}".format(e)) |
| 60 | |
| 61 | |
| 62 | @when("zookeeper-k8s.configured") |
| 63 | def non_leader(): |
| 64 | layer.status.active("ready") |
| 65 | |
| 66 | |
| 67 | @when_not("leadership.is_leader") |
| 68 | def non_leaders_active(): |
| 69 | layer.status.active("ready") |
| 70 | |
| 71 | |
| 72 | @when("zookeeper.joined") |
| 73 | @when("zookeeper-k8s.configured") |
| 74 | def send_config(): |
| 75 | layer.status.maintenance("Sending Zookeeper configuration") |
| 76 | if not is_pod_up("zookeeper"): |
| 77 | log("The pod is not ready.") |
| 78 | return |
| 79 | |
| 80 | zookeeper = endpoint_from_flag("zookeeper.joined") |
| 81 | if zookeeper: |
| 82 | service_ip = get_service_ip("zookeeper") |
| 83 | if service_ip: |
| 84 | zookeeper.send_connection( |
| 85 | get_zookeeper_client_port(), get_zookeeper_client_port(), service_ip, |
| 86 | ) |
| 87 | layer.status.active("ready") |
| 88 | |
| 89 | |
| 90 | def make_pod_spec(): |
| 91 | """Make pod specification for Kubernetes |
| 92 | |
| 93 | Returns: |
| 94 | pod_spec: Pod specification for Kubernetes |
| 95 | """ |
| 96 | with open("reactive/spec_template.yaml") as spec_file: |
| 97 | pod_spec_template = spec_file.read() |
| 98 | |
| 99 | md = metadata() |
| 100 | cfg = config() |
| 101 | data = {"name": md.get("name"), "docker_image_path": cfg.get("image")} |
| 102 | data.update(cfg) |
| 103 | return pod_spec_template % data |
| 104 | |
| 105 | |
| 106 | def get_zookeeper_client_port(): |
| 107 | """Returns Zookeeper port""" |
| 108 | cfg = config() |
| 109 | return cfg.get("client-port") |