1 # Copyright 2021 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
15 # For those usages not covered by the Apache License, Version 2.0 please
16 # contact: legal@canonical.com
18 # To get in touch with the maintainers, please contact:
19 # osm-charmers@lists.launchpad.net
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 (
31 from charms
import layer
32 from charmhelpers
.core
import hookenv
36 @hook("upgrade-charm")
37 @when("leadership.is_leader")
39 clear_flag("grafana-k8s.configured")
42 @when("config.changed")
43 @when("leadership.is_leader")
45 clear_flag("grafana-k8s.configured")
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")
54 @when("endpoint.prometheus.available")
55 @when_not("grafana-k8s.configured")
56 @when("leadership.is_leader")
58 layer
.status
.maintenance("Configuring grafana container")
60 prometheus
= endpoint_from_flag("endpoint.prometheus.available")
61 prometheus_url
= prometheus
.targets()[0]["targets"][0]
64 spec
= make_pod_spec(prometheus_url
)
65 log("set pod spec:\n{}".format(spec
))
67 set_flag("grafana-k8s.configured")
68 layer
.status
.active("ready")
70 except Exception as e
:
71 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
72 log(traceback
.format_exc(), level
=hookenv
.ERROR
)
75 @when("grafana-k8s.configured")
76 def set_grafana_active():
77 layer
.status
.active("ready")
80 @when("endpoint.prometheus.available")
81 @when_not("leadership.is_leader")
82 def non_leaders_active():
83 layer
.status
.active("ready")
86 def make_pod_spec(prometheus_url
):
87 """Make pod specification for Kubernetes
90 pod_spec: Pod specification for Kubernetes
92 with
open("reactive/spec_template.yaml") as spec_file
:
93 pod_spec_template
= spec_file
.read()
99 "name": md
.get("name"),
100 "docker_image": cfg
.get("image"),
101 "prometheus_url": prometheus_url
,
104 return pod_spec_template
% data