2 # Copyright 2021 Canonical Ltd.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 # pylint: disable=E0213
25 from ipaddress
import ip_network
27 from pathlib
import Path
28 from typing
import NoReturn
, Optional
29 from urllib
.parse
import urlparse
31 from ops
.main
import main
32 from opslib
.osm
.charm
import CharmedOsmBase
, RelationsMissing
33 from opslib
.osm
.interfaces
.grafana
import GrafanaDashboardTarget
34 from opslib
.osm
.interfaces
.mongo
import MongoClient
35 from opslib
.osm
.interfaces
.prometheus
import PrometheusScrapeTarget
36 from opslib
.osm
.pod
import (
38 IngressResourceV3Builder
,
41 from opslib
.osm
.validator
import ModelValidator
, validator
44 logger
= logging
.getLogger(__name__
)
49 class ConfigModel(ModelValidator
):
50 site_url
: Optional
[str]
51 cluster_issuer
: Optional
[str]
52 ingress_class
: Optional
[str]
53 ingress_whitelist_source_range
: Optional
[str]
54 tls_secret_name
: Optional
[str]
55 mongodb_uri
: Optional
[str]
56 image_pull_policy
: str
58 @validator("site_url")
59 def validate_site_url(cls
, v
):
62 if not parsed
.scheme
.startswith("http"):
63 raise ValueError("value must start with http")
66 @validator("ingress_whitelist_source_range")
67 def validate_ingress_whitelist_source_range(cls
, v
):
72 @validator("mongodb_uri")
73 def validate_mongodb_uri(cls
, v
):
74 if v
and not v
.startswith("mongodb://"):
75 raise ValueError("mongodb_uri is not properly formed")
78 @validator("image_pull_policy")
79 def validate_image_pull_policy(cls
, v
):
82 "ifnotpresent": "IfNotPresent",
86 if v
not in values
.keys():
87 raise ValueError("value must be always, ifnotpresent or never")
91 class MongodbExporterCharm(CharmedOsmBase
):
92 def __init__(self
, *args
) -> NoReturn
:
93 super().__init
__(*args
, oci_image
="image")
95 # Provision Kafka relation to exchange information
96 self
.mongodb_client
= MongoClient(self
, "mongodb")
97 self
.framework
.observe(self
.on
["mongodb"].relation_changed
, self
.configure_pod
)
98 self
.framework
.observe(self
.on
["mongodb"].relation_broken
, self
.configure_pod
)
100 # Register relation to provide a Scraping Target
101 self
.scrape_target
= PrometheusScrapeTarget(self
, "prometheus-scrape")
102 self
.framework
.observe(
103 self
.on
["prometheus-scrape"].relation_joined
, self
._publish
_scrape
_info
106 # Register relation to provide a Dasboard Target
107 self
.dashboard_target
= GrafanaDashboardTarget(self
, "grafana-dashboard")
108 self
.framework
.observe(
109 self
.on
["grafana-dashboard"].relation_joined
, self
._publish
_dashboard
_info
112 def _publish_scrape_info(self
, event
) -> NoReturn
:
113 """Publishes scraping information for Prometheus.
116 event (EventBase): Prometheus relation event.
118 if self
.unit
.is_leader():
120 urlparse(self
.model
.config
["site_url"]).hostname
121 if self
.model
.config
["site_url"]
122 else self
.model
.app
.name
125 if self
.model
.config
.get("site_url", "").startswith("https://"):
127 elif self
.model
.config
.get("site_url", "").startswith("http://"):
130 self
.scrape_target
.publish_info(
133 metrics_path
="/metrics",
134 scrape_interval
="30s",
135 scrape_timeout
="15s",
138 def _publish_dashboard_info(self
, event
) -> NoReturn
:
139 """Publish dashboards for Grafana.
142 event (EventBase): Grafana relation event.
144 if self
.unit
.is_leader():
145 self
.dashboard_target
.publish_info(
147 dashboard
=Path("templates/mongodb_exporter_dashboard.json").read_text(),
150 def _check_missing_dependencies(self
, config
: ConfigModel
):
151 """Check if there is any relation missing.
154 config (ConfigModel): object with configuration information.
157 RelationsMissing: if kafka is missing.
159 missing_relations
= []
161 if not config
.mongodb_uri
and self
.mongodb_client
.is_missing_data_in_unit():
162 missing_relations
.append("mongodb")
164 if missing_relations
:
165 raise RelationsMissing(missing_relations
)
167 def build_pod_spec(self
, image_info
):
168 """Build the PodSpec to be used.
171 image_info (str): container image information.
174 Dict: PodSpec information.
177 config
= ConfigModel(**dict(self
.config
))
179 if config
.mongodb_uri
and not self
.mongodb_client
.is_missing_data_in_unit():
180 raise Exception("Mongodb data cannot be provided via config and relation")
183 self
._check
_missing
_dependencies
(config
)
185 # Create Builder for the PodSpec
186 pod_spec_builder
= PodSpecV3Builder()
189 container_builder
= ContainerV3Builder(
190 self
.app
.name
, image_info
, config
.image_pull_policy
192 container_builder
.add_port(name
=self
.app
.name
, port
=PORT
)
193 container_builder
.add_http_readiness_probe(
196 initial_delay_seconds
=10,
202 container_builder
.add_http_liveness_probe(
205 initial_delay_seconds
=60,
207 failure_threshold
=10,
212 if config
.mongodb_uri
213 else self
.mongodb_client
.connection_string
215 parsed
= urlparse(unparsed
)
216 mongodb_uri
= f
"mongodb://{parsed.netloc.split(',')[0]}{parsed.path}"
218 mongodb_uri
+= f
"?{parsed.query}"
220 container_builder
.add_envs(
222 "MONGODB_URI": mongodb_uri
,
225 container
= container_builder
.build()
227 # Add container to PodSpec
228 pod_spec_builder
.add_container(container
)
230 # Add ingress resources to PodSpec if site url exists
232 parsed
= urlparse(config
.site_url
)
234 if config
.ingress_class
:
235 annotations
["kubernetes.io/ingress.class"] = config
.ingress_class
236 ingress_resource_builder
= IngressResourceV3Builder(
237 f
"{self.app.name}-ingress", annotations
240 if config
.ingress_whitelist_source_range
:
242 "nginx.ingress.kubernetes.io/whitelist-source-range"
243 ] = config
.ingress_whitelist_source_range
245 if config
.cluster_issuer
:
246 annotations
["cert-manager.io/cluster-issuer"] = config
.cluster_issuer
248 if parsed
.scheme
== "https":
249 ingress_resource_builder
.add_tls(
250 [parsed
.hostname
], config
.tls_secret_name
253 annotations
["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
255 ingress_resource_builder
.add_rule(parsed
.hostname
, self
.app
.name
, PORT
)
256 ingress_resource
= ingress_resource_builder
.build()
257 pod_spec_builder
.add_ingress_resource(ingress_resource
)
259 logger
.debug(pod_spec_builder
.build())
261 return pod_spec_builder
.build()
264 if __name__
== "__main__":
265 main(MongodbExporterCharm
)