| sousaedu | e4c6aeb | 2021-01-15 18:41:15 +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.layer.caas_base import pod_spec_set |
| 23 | from charms.reactive import endpoint_from_flag |
| 24 | from charms.reactive import when, when_not, hook |
| 25 | from charms.reactive.flags import set_flag, clear_flag |
| 26 | from charmhelpers.core.hookenv import ( |
| 27 | log, |
| 28 | metadata, |
| 29 | config, |
| 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("grafana-k8s.configured") |
| 40 | |
| 41 | |
| 42 | @when("config.changed") |
| 43 | @when("leadership.is_leader") |
| 44 | def restart(): |
| 45 | clear_flag("grafana-k8s.configured") |
| 46 | |
| 47 | |
| 48 | @when_not("endpoint.prometheus.available") |
| 49 | @when("leadership.is_leader") |
| 50 | def waiting_for_prometheus_interface(): |
| 51 | layer.status.waiting("Waiting for prometheus interface") |
| 52 | |
| 53 | |
| 54 | @when("endpoint.prometheus.available") |
| 55 | @when_not("grafana-k8s.configured") |
| 56 | @when("leadership.is_leader") |
| 57 | def configure(): |
| 58 | layer.status.maintenance("Configuring grafana container") |
| 59 | try: |
| 60 | prometheus = endpoint_from_flag("endpoint.prometheus.available") |
| 61 | prometheus_url = prometheus.targets()[0]["targets"][0] |
| 62 | |
| 63 | if prometheus_url: |
| 64 | spec = make_pod_spec(prometheus_url) |
| 65 | log("set pod spec:\n{}".format(spec)) |
| 66 | pod_spec_set(spec) |
| 67 | set_flag("grafana-k8s.configured") |
| 68 | layer.status.active("ready") |
| 69 | |
| 70 | except Exception as e: |
| 71 | layer.status.blocked("k8s spec failed to deploy: {}".format(e)) |
| 72 | log(traceback.format_exc(), level=hookenv.ERROR) |
| 73 | |
| 74 | |
| 75 | @when("grafana-k8s.configured") |
| 76 | def set_grafana_active(): |
| 77 | layer.status.active("ready") |
| 78 | |
| 79 | |
| 80 | @when("endpoint.prometheus.available") |
| 81 | @when_not("leadership.is_leader") |
| 82 | def non_leaders_active(): |
| 83 | layer.status.active("ready") |
| 84 | |
| 85 | |
| 86 | def make_pod_spec(prometheus_url): |
| 87 | """Make pod specification for Kubernetes |
| 88 | |
| 89 | Returns: |
| 90 | pod_spec: Pod specification for Kubernetes |
| 91 | """ |
| 92 | with open("reactive/spec_template.yaml") as spec_file: |
| 93 | pod_spec_template = spec_file.read() |
| 94 | |
| 95 | md = metadata() |
| 96 | cfg = config() |
| 97 | |
| 98 | data = { |
| 99 | "name": md.get("name"), |
| 100 | "docker_image": cfg.get("image"), |
| 101 | "prometheus_url": prometheus_url, |
| 102 | } |
| 103 | data.update(cfg) |
| 104 | return pod_spec_template % data |