This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrequires.py
63 lines (52 loc) · 2.66 KB
/
requires.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
from charmhelpers.core import unitdata
from charms.reactive import Endpoint
from charms.reactive import when_any, when_not
from charms.reactive import set_state, remove_state
db = unitdata.kv()
class CNIPluginClient(Endpoint):
def manage_flags(self):
config = self.get_config()
# Announce changes to kubeconfig-hash
kubeconfig_hash = config.get("kubeconfig-hash")
kubeconfig_hash_key = self.expand_name("{endpoint_name}.kubeconfig-hash")
if kubeconfig_hash:
set_state(self.expand_name("{endpoint_name}.kubeconfig.available"))
if kubeconfig_hash != db.get(kubeconfig_hash_key):
set_state(self.expand_name("{endpoint_name}.kubeconfig.changed"))
db.set(kubeconfig_hash_key, kubeconfig_hash)
# Announce changes to service-cidr
service_cidr = config.get("service-cidr")
service_cidr_key = self.expand_name("{endpoint_name}.service-cidr")
if service_cidr:
set_state(self.expand_name("{endpoint_name}.service_cidr.available"))
if service_cidr != db.get(service_cidr_key):
set_state(self.expand_name("{endpoint_name}.service_cidr.changed"))
db.set(service_cidr_key, service_cidr)
# Announce changes to image-registry
image_registry = config.get("image-registry")
image_registry_key = self.expand_name("{endpoint_name}.image-registry")
if image_registry:
set_state(self.expand_name("{endpoint_name}.image_registry.available"))
if image_registry != db.get(image_registry_key):
set_state(self.expand_name("{endpoint_name}.image_registry.changed"))
db.set(image_registry_key, image_registry)
@when_any("endpoint.{endpoint_name}.joined", "endpoint.{endpoint_name}.changed")
def changed(self):
"""Indicate the relation is connected, and if the relation data is
set it is also available."""
set_state(self.expand_name("{endpoint_name}.connected"))
remove_state(self.expand_name("endpoint.{endpoint_name}.changed"))
@when_not("endpoint.{endpoint_name}.joined")
def broken(self):
"""Indicate the relation is no longer available and not connected."""
remove_state(self.expand_name("{endpoint_name}.connected"))
def get_config(self):
"""Get the kubernetes configuration information."""
return self.all_joined_units.received_raw
def set_config(self, cidr, cni_conf_file):
"""Sets the CNI configuration information."""
for relation in self.relations:
relation.to_publish_raw.update(
{"cidr": cidr, "cni-conf-file": cni_conf_file}
)