2 # Copyright 2020 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
24 from pydantic
import BaseModel
, constr
, PositiveInt
25 from typing
import Any
, Dict
, List
27 logger
= logging
.getLogger(__name__
)
30 class ConfigData(BaseModel
):
31 """Configuration data model."""
33 openstack_default_granularity
: PositiveInt
34 global_request_timeout
: PositiveInt
35 log_level
: constr(regex
=r
"^(INFO|DEBUG)$")
36 collector_interval
: PositiveInt
37 evaluator_interval
: PositiveInt
38 database_commonkey
: constr(min_length
=1)
39 vca_host
: constr(min_length
=1)
40 vca_user
: constr(min_length
=1)
41 vca_password
: constr(min_length
=1)
45 class RelationData(BaseModel
):
46 """Relation data model."""
48 message_host
: constr(min_length
=1)
49 message_port
: PositiveInt
50 database_uri
: constr(regex
=r
"^(mongodb://)")
51 prometheus_host
: constr(min_length
=1)
52 prometheus_port
: PositiveInt
55 def _make_pod_ports(port
: int) -> List
[Dict
[str, Any
]]:
56 """Generate pod ports details.
59 port (int): port to expose.
62 List[Dict[str, Any]]: pod port details.
64 return [{"name": "mon", "containerPort": port
, "protocol": "TCP"}]
67 def _make_pod_envconfig(
68 config
: Dict
[str, Any
], relation_state
: Dict
[str, Any
]
70 """Generate pod environment configuration.
73 config (Dict[str, Any]): configuration information.
74 relation_state (Dict[str, Any]): relation state information.
77 Dict[str, Any]: pod environment configuration.
80 # General configuration
81 "ALLOW_ANONYMOUS_LOGIN": "yes",
82 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
["openstack_default_granularity"],
83 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config
["global_request_timeout"],
84 "OSMMON_GLOBAL_LOGLEVEL": config
["log_level"],
85 "OSMMON_COLLECTOR_INTERVAL": config
["collector_interval"],
86 "OSMMON_EVALUATOR_INTERVAL": config
["evaluator_interval"],
88 "OSMMON_MESSAGE_DRIVER": "kafka",
89 "OSMMON_MESSAGE_HOST": relation_state
["message_host"],
90 "OSMMON_MESSAGE_PORT": relation_state
["message_port"],
91 # Database configuration
92 "OSMMON_DATABASE_DRIVER": "mongo",
93 "OSMMON_DATABASE_URI": relation_state
["database_uri"],
94 "OSMMON_DATABASE_COMMONKEY": config
["database_commonkey"],
95 # Prometheus configuration
96 "OSMMON_PROMETHEUS_URL": f
"http://{relation_state['prometheus_host']}:{relation_state['prometheus_port']}",
98 "OSMMON_VCA_HOST": config
["vca_host"],
99 "OSMMON_VCA_USER": config
["vca_user"],
100 "OSMMON_VCA_SECRET": config
["vca_password"],
101 "OSMMON_VCA_CACERT": config
["vca_cacert"],
107 def _make_startup_probe() -> Dict
[str, Any
]:
108 """Generate startup probe.
111 Dict[str, Any]: startup probe.
114 "exec": {"command": ["/usr/bin/pgrep python3"]},
115 "initialDelaySeconds": 60,
120 def _make_readiness_probe(port
: int) -> Dict
[str, Any
]:
121 """Generate readiness probe.
124 port (int): [description]
127 Dict[str, Any]: readiness probe.
135 "successThreshold": 1,
136 "failureThreshold": 3,
140 def _make_liveness_probe(port
: int) -> Dict
[str, Any
]:
141 """Generate liveness probe.
144 port (int): [description]
147 Dict[str, Any]: liveness probe.
153 "initialDelaySeconds": 45,
156 "successThreshold": 1,
157 "failureThreshold": 3,
162 image_info
: Dict
[str, str],
163 config
: Dict
[str, Any
],
164 relation_state
: Dict
[str, Any
],
165 app_name
: str = "mon",
168 """Generate the pod spec information.
171 image_info (Dict[str, str]): Object provided by
172 OCIImageResource("image").fetch().
173 config (Dict[str, Any]): Configuration information.
174 relation_state (Dict[str, Any]): Relation state information.
175 app_name (str, optional): Application name. Defaults to "mon".
176 port (int, optional): Port for the container. Defaults to 8000.
179 Dict[str, Any]: Pod spec dictionary for the charm.
184 ConfigData(**(config
))
185 RelationData(**(relation_state
))
187 ports
= _make_pod_ports(port
)
188 env_config
= _make_pod_envconfig(config
, relation_state
)
195 "imageDetails": image_info
,
196 "imagePullPolicy": "Always",
198 "envConfig": env_config
,
201 "kubernetesResources": {
202 "ingressResources": [],