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 log
, metadata
, config
, goal_state
27 from charms
import layer
30 @hook("upgrade-charm")
31 @when("leadership.is_leader")
33 clear_flag("mongodb-k8s.configured")
36 @when("config.changed")
37 @when("leadership.is_leader")
39 clear_flag("mongodb-k8s.configured")
42 @when_not("mongodb-k8s.configured")
43 @when("leadership.is_leader")
45 layer
.status
.maintenance("Configuring MongoDB container")
47 spec
= make_pod_spec()
48 log("set pod spec:\n{}".format(spec
))
50 set_flag("mongodb-k8s.configured")
51 except Exception as e
:
52 layer
.status
.blocked("k8s spec failed to deploy: {}".format(e
))
55 @when("mongodb-k8s.configured")
56 def set_mongodb_active():
57 layer
.status
.active("ready")
60 @when_not("leadership.is_leader")
61 def non_leaders_active():
62 layer
.status
.active("ready")
65 @when("mongodb-k8s.configured", "mongo.joined")
67 layer
.status
.maintenance("Sending mongo configuration")
69 mongo
= endpoint_from_flag("mongo.joined")
72 mongo_uri
= "mongodb://"
73 for i
, unit
in enumerate(goal_state()["units"]):
74 pod_base_name
= unit
.split("/")[0]
75 service_name
= cfg
.get("service-name")
76 pod_name
= "{}-{}".format(pod_base_name
, i
)
79 mongo_uri
+= "{}.{}:{}".format(
80 pod_name
, service_name
, get_mongodb_port()
82 if cfg
.get("enable-sidecar"):
83 mongo_uri
+= "/?replicaSet={}".format(get_mongodb_replset())
84 log("Mongo URI: {}".format(mongo_uri
))
85 mongo
.send_connection_string(mongo_uri
)
86 clear_flag("mongo.joined")
87 except Exception as e
:
88 log("Exception sending config: {}".format(e
))
89 clear_flag("mongo.joined")
93 """Make pod specification for Kubernetes
96 pod_spec: Pod specification for Kubernetes
101 if cfg
.get("enable-sidecar"):
102 with
open("reactive/spec_template_ha.yaml") as spec_file
:
103 pod_spec_template
= spec_file
.read()
105 with
open("reactive/spec_template.yaml") as spec_file
:
106 pod_spec_template
= spec_file
.read()
109 "name": md
.get("name"),
110 "docker_image": cfg
.get("mongodb-image"),
111 "sc_docker_image": cfg
.get("sidecar-image"),
112 "pod_labels": "juju-app={}".format(cfg
.get("advertised-hostname")),
116 return pod_spec_template
% data
119 def get_mongodb_port():
120 """Returns MongoDB port"""
122 return cfg
.get("advertised-port")
125 def get_mongodb_replset():
126 """Returns MongoDB port"""
128 return cfg
.get("replica-set")