| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 2 | # Copyright 2021 Canonical Ltd. |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 3 | # |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 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 |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 7 | # |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 9 | # |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 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 |
| 14 | # under the License. |
| 15 | # |
| 16 | # For those usages not covered by the Apache License, Version 2.0 please |
| 17 | # contact: legal@canonical.com |
| 18 | # |
| 19 | # To get in touch with the maintainers, please contact: |
| 20 | # osm-charmers@lists.launchpad.net |
| 21 | ## |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 22 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 23 | # pylint: disable=E0213 |
| 24 | |
| 25 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 26 | from ipaddress import ip_network |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 27 | import logging |
| 28 | from pathlib import Path |
| 29 | from string import Template |
| 30 | from typing import NoReturn, Optional |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 31 | from urllib.parse import urlparse |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 32 | |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 33 | from ops.main import main |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 34 | from opslib.osm.charm import CharmedOsmBase, RelationsMissing |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 35 | from opslib.osm.interfaces.http import HttpClient |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 36 | from opslib.osm.pod import ( |
| 37 | ContainerV3Builder, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 38 | FilesV3Builder, |
| 39 | IngressResourceV3Builder, |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 40 | PodSpecV3Builder, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 41 | ) |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 42 | from opslib.osm.validator import ModelValidator, validator |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 43 | |
| 44 | |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 45 | logger = logging.getLogger(__name__) |
| 46 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 47 | |
| 48 | class ConfigModel(ModelValidator): |
| 49 | port: int |
| 50 | server_name: str |
| 51 | max_file_size: int |
| 52 | site_url: Optional[str] |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 53 | cluster_issuer: Optional[str] |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 54 | ingress_class: Optional[str] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 55 | ingress_whitelist_source_range: Optional[str] |
| 56 | tls_secret_name: Optional[str] |
| sousaedu | 0dc25b3 | 2021-08-30 16:33:33 +0100 | [diff] [blame] | 57 | image_pull_policy: str |
| sousaedu | 540d937 | 2021-09-29 01:53:30 +0100 | [diff] [blame] | 58 | security_context: bool |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 59 | |
| 60 | @validator("port") |
| 61 | def validate_port(cls, v): |
| 62 | if v <= 0: |
| 63 | raise ValueError("value must be greater than 0") |
| 64 | return v |
| 65 | |
| 66 | @validator("max_file_size") |
| 67 | def validate_max_file_size(cls, v): |
| 68 | if v < 0: |
| 69 | raise ValueError("value must be equal or greater than 0") |
| 70 | return v |
| 71 | |
| 72 | @validator("site_url") |
| 73 | def validate_site_url(cls, v): |
| 74 | if v: |
| 75 | parsed = urlparse(v) |
| 76 | if not parsed.scheme.startswith("http"): |
| 77 | raise ValueError("value must start with http") |
| 78 | return v |
| 79 | |
| 80 | @validator("ingress_whitelist_source_range") |
| 81 | def validate_ingress_whitelist_source_range(cls, v): |
| 82 | if v: |
| 83 | ip_network(v) |
| 84 | return v |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 85 | |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 86 | @validator("image_pull_policy") |
| 87 | def validate_image_pull_policy(cls, v): |
| 88 | values = { |
| 89 | "always": "Always", |
| 90 | "ifnotpresent": "IfNotPresent", |
| 91 | "never": "Never", |
| 92 | } |
| 93 | v = v.lower() |
| 94 | if v not in values.keys(): |
| 95 | raise ValueError("value must be always, ifnotpresent or never") |
| 96 | return values[v] |
| 97 | |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 98 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 99 | class NgUiCharm(CharmedOsmBase): |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 100 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 101 | super().__init__(*args, oci_image="image") |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 102 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 103 | self.nbi_client = HttpClient(self, "nbi") |
| 104 | self.framework.observe(self.on["nbi"].relation_changed, self.configure_pod) |
| 105 | self.framework.observe(self.on["nbi"].relation_broken, self.configure_pod) |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 106 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 107 | def _check_missing_dependencies(self, config: ConfigModel): |
| 108 | missing_relations = [] |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 109 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 110 | if self.nbi_client.is_missing_data_in_app(): |
| 111 | missing_relations.append("nbi") |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 112 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 113 | if missing_relations: |
| 114 | raise RelationsMissing(missing_relations) |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 115 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 116 | def _build_files(self, config: ConfigModel): |
| 117 | files_builder = FilesV3Builder() |
| 118 | files_builder.add_file( |
| 119 | "default", |
| David Garcia | d680be4 | 2021-08-17 11:03:55 +0200 | [diff] [blame] | 120 | Template(Path("templates/default.template").read_text()).substitute( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 121 | port=config.port, |
| 122 | server_name=config.server_name, |
| 123 | max_file_size=config.max_file_size, |
| 124 | nbi_host=self.nbi_client.host, |
| 125 | nbi_port=self.nbi_client.port, |
| 126 | ), |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 127 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 128 | return files_builder.build() |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 129 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 130 | def build_pod_spec(self, image_info): |
| 131 | # Validate config |
| 132 | config = ConfigModel(**dict(self.config)) |
| 133 | # Check relations |
| 134 | self._check_missing_dependencies(config) |
| 135 | # Create Builder for the PodSpec |
| sousaedu | 540d937 | 2021-09-29 01:53:30 +0100 | [diff] [blame] | 136 | pod_spec_builder = PodSpecV3Builder( |
| 137 | enable_security_context=config.security_context |
| 138 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 139 | # Build Container |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 140 | container_builder = ContainerV3Builder( |
| sousaedu | 540d937 | 2021-09-29 01:53:30 +0100 | [diff] [blame] | 141 | self.app.name, |
| 142 | image_info, |
| 143 | config.image_pull_policy, |
| 144 | run_as_non_root=config.security_context, |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 145 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 146 | container_builder.add_port(name=self.app.name, port=config.port) |
| 147 | container = container_builder.build() |
| 148 | container_builder.add_tcpsocket_readiness_probe( |
| 149 | config.port, |
| 150 | initial_delay_seconds=45, |
| 151 | timeout_seconds=5, |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 152 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 153 | container_builder.add_tcpsocket_liveness_probe( |
| 154 | config.port, |
| 155 | initial_delay_seconds=45, |
| 156 | timeout_seconds=15, |
| 157 | ) |
| 158 | container_builder.add_volume_config( |
| 159 | "configuration", |
| 160 | "/etc/nginx/sites-available/", |
| 161 | self._build_files(config), |
| 162 | ) |
| 163 | # Add container to pod spec |
| 164 | pod_spec_builder.add_container(container) |
| 165 | # Add ingress resources to pod spec if site url exists |
| 166 | if config.site_url: |
| 167 | parsed = urlparse(config.site_url) |
| 168 | annotations = { |
| 169 | "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format( |
| 170 | str(config.max_file_size) + "m" |
| 171 | if config.max_file_size > 0 |
| 172 | else config.max_file_size |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 173 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 174 | } |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 175 | if config.ingress_class: |
| 176 | annotations["kubernetes.io/ingress.class"] = config.ingress_class |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 177 | ingress_resource_builder = IngressResourceV3Builder( |
| 178 | f"{self.app.name}-ingress", annotations |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 179 | ) |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 180 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 181 | if config.ingress_whitelist_source_range: |
| 182 | annotations[ |
| 183 | "nginx.ingress.kubernetes.io/whitelist-source-range" |
| 184 | ] = config.ingress_whitelist_source_range |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 185 | |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 186 | if config.cluster_issuer: |
| 187 | annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer |
| 188 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 189 | if parsed.scheme == "https": |
| 190 | ingress_resource_builder.add_tls( |
| 191 | [parsed.hostname], config.tls_secret_name |
| 192 | ) |
| 193 | else: |
| 194 | annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false" |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 195 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 196 | ingress_resource_builder.add_rule( |
| 197 | parsed.hostname, self.app.name, config.port |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 198 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 199 | ingress_resource = ingress_resource_builder.build() |
| 200 | pod_spec_builder.add_ingress_resource(ingress_resource) |
| 201 | return pod_spec_builder.build() |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 202 | |
| 203 | |
| 204 | if __name__ == "__main__": |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 205 | main(NgUiCharm) |