| sousaedu | 89f86a3 | 2021-01-15 16:59:14 +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.reactive import when, when_not, hook |
| 23 | from charms.reactive.flags import set_flag, clear_flag |
| 24 | from charmhelpers.core.hookenv import ( |
| 25 | log, |
| 26 | metadata, |
| 27 | config, |
| 28 | network_get, |
| 29 | relation_id, |
| 30 | ) |
| 31 | from charms import layer |
| 32 | from charmhelpers.core import hookenv |
| 33 | import traceback |
| 34 | |
| 35 | |
| 36 | @hook("upgrade-charm") |
| 37 | @when("leadership.is_leader") |
| 38 | def upgrade(): |
| 39 | clear_flag("prometheus-k8s.configured") |
| 40 | |
| 41 | |
| 42 | @when("config.changed") |
| 43 | @when("leadership.is_leader") |
| 44 | def restart(): |
| 45 | clear_flag("prometheus-k8s.configured") |
| 46 | |
| 47 | |
| 48 | @when_not("prometheus-k8s.configured") |
| 49 | @when("leadership.is_leader") |
| 50 | def configure(): |
| 51 | layer.status.maintenance("Configuring prometheus container") |
| 52 | try: |
| 53 | spec = make_pod_spec() |
| 54 | log("set pod spec:\n{}".format(spec)) |
| 55 | layer.caas_base.pod_spec_set(spec) |
| 56 | set_flag("prometheus-k8s.configured") |
| 57 | layer.status.active("ready") |
| 58 | |
| 59 | except Exception as e: |
| 60 | layer.status.blocked("k8s spec failed to deploy: {}".format(e)) |
| 61 | log(traceback.format_exc(), level=hookenv.ERROR) |
| 62 | |
| 63 | |
| 64 | @when("prometheus-k8s.configured") |
| 65 | def set_prometheus_active(): |
| 66 | layer.status.active("ready") |
| 67 | |
| 68 | |
| 69 | @when_not("leadership.is_leader") |
| 70 | def non_leaders_active(): |
| 71 | layer.status.active("ready") |
| 72 | |
| 73 | |
| 74 | @when("prometheus-k8s.configured", "endpoint.prometheus.available") |
| 75 | def send_config(prometheus): |
| 76 | layer.status.maintenance("Sending prometheus configuration") |
| 77 | cfg = config() |
| 78 | try: |
| 79 | info = network_get("prometheus", relation_id()) |
| 80 | log("network info {0}".format(info)) |
| 81 | host = info.get("ingress-addresses", [""])[0] |
| 82 | |
| 83 | prometheus.configure(hostname=host, port=cfg.get("advertised-port")) |
| 84 | clear_flag("endpoint.prometheus.available") |
| 85 | except Exception as e: |
| 86 | log("Exception sending config: {}".format(e)) |
| 87 | |
| 88 | |
| 89 | def make_pod_spec(): |
| 90 | """Make pod specification for Kubernetes |
| 91 | |
| 92 | Returns: |
| 93 | pod_spec: Pod specification for Kubernetes |
| 94 | """ |
| 95 | with open("reactive/spec_template.yaml") as spec_file: |
| 96 | pod_spec_template = spec_file.read() |
| 97 | |
| 98 | md = metadata() |
| 99 | cfg = config() |
| 100 | |
| 101 | data = { |
| 102 | "name": md.get("name"), |
| 103 | "docker_image": cfg.get("prometheus-image"), |
| 104 | "a_docker_image": cfg.get("alpine-image"), |
| 105 | } |
| 106 | data.update(cfg) |
| 107 | return pod_spec_template % data |