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
.reactive
import when
, when_not
, hook
23 from charms
.reactive
.flags
import set_flag
, clear_flag
24 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("prometheus-k8s.configured")
42 @when("config.changed")
43 @when("leadership.is_leader")
45 clear_flag("prometheus-k8s.configured")
48 @when_not("prometheus-k8s.configured")
49 @when("leadership.is_leader")
51 layer
.status
.maintenance("Configuring prometheus container")
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")
59 except Exception as e
:
60 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
61 log(traceback
.format_exc(), level
=hookenv
.ERROR
)
64 @when("prometheus-k8s.configured")
65 def set_prometheus_active():
66 layer
.status
.active("ready")
69 @when_not("leadership.is_leader")
70 def non_leaders_active():
71 layer
.status
.active("ready")
74 @when("prometheus-k8s.configured", "endpoint.prometheus.available")
75 def send_config(prometheus
):
76 layer
.status
.maintenance("Sending prometheus configuration")
79 info
= network_get("prometheus", relation_id())
80 log("network info {0}".format(info
))
81 host
= info
.get("ingress-addresses", [""])[0]
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
))
90 """Make pod specification for Kubernetes
93 pod_spec: Pod specification for Kubernetes
95 with
open("reactive/spec_template.yaml") as spec_file
:
96 pod_spec_template
= spec_file
.read()
102 "name": md
.get("name"),
103 "docker_image": cfg
.get("prometheus-image"),
104 "a_docker_image": cfg
.get("alpine-image"),
107 return pod_spec_template
% data