| 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 | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 54 | ingress_whitelist_source_range: Optional[str] |
| 55 | tls_secret_name: Optional[str] |
| 56 | |
| 57 | @validator("port") |
| 58 | def validate_port(cls, v): |
| 59 | if v <= 0: |
| 60 | raise ValueError("value must be greater than 0") |
| 61 | return v |
| 62 | |
| 63 | @validator("max_file_size") |
| 64 | def validate_max_file_size(cls, v): |
| 65 | if v < 0: |
| 66 | raise ValueError("value must be equal or greater than 0") |
| 67 | return v |
| 68 | |
| 69 | @validator("site_url") |
| 70 | def validate_site_url(cls, v): |
| 71 | if v: |
| 72 | parsed = urlparse(v) |
| 73 | if not parsed.scheme.startswith("http"): |
| 74 | raise ValueError("value must start with http") |
| 75 | return v |
| 76 | |
| 77 | @validator("ingress_whitelist_source_range") |
| 78 | def validate_ingress_whitelist_source_range(cls, v): |
| 79 | if v: |
| 80 | ip_network(v) |
| 81 | return v |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 82 | |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 83 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 84 | class NgUiCharm(CharmedOsmBase): |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 85 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 86 | super().__init__(*args, oci_image="image") |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 87 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 88 | self.nbi_client = HttpClient(self, "nbi") |
| 89 | self.framework.observe(self.on["nbi"].relation_changed, self.configure_pod) |
| 90 | self.framework.observe(self.on["nbi"].relation_broken, self.configure_pod) |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 91 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 92 | def _check_missing_dependencies(self, config: ConfigModel): |
| 93 | missing_relations = [] |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 94 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 95 | if self.nbi_client.is_missing_data_in_app(): |
| 96 | missing_relations.append("nbi") |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 97 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 98 | if missing_relations: |
| 99 | raise RelationsMissing(missing_relations) |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 100 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 101 | def _build_files(self, config: ConfigModel): |
| 102 | files_builder = FilesV3Builder() |
| 103 | files_builder.add_file( |
| 104 | "default", |
| 105 | Template(Path("files/default").read_text()).substitute( |
| 106 | port=config.port, |
| 107 | server_name=config.server_name, |
| 108 | max_file_size=config.max_file_size, |
| 109 | nbi_host=self.nbi_client.host, |
| 110 | nbi_port=self.nbi_client.port, |
| 111 | ), |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 112 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 113 | return files_builder.build() |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 114 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 115 | def build_pod_spec(self, image_info): |
| 116 | # Validate config |
| 117 | config = ConfigModel(**dict(self.config)) |
| 118 | # Check relations |
| 119 | self._check_missing_dependencies(config) |
| 120 | # Create Builder for the PodSpec |
| 121 | pod_spec_builder = PodSpecV3Builder() |
| 122 | # Build Container |
| 123 | container_builder = ContainerV3Builder(self.app.name, image_info) |
| 124 | container_builder.add_port(name=self.app.name, port=config.port) |
| 125 | container = container_builder.build() |
| 126 | container_builder.add_tcpsocket_readiness_probe( |
| 127 | config.port, |
| 128 | initial_delay_seconds=45, |
| 129 | timeout_seconds=5, |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 130 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 131 | container_builder.add_tcpsocket_liveness_probe( |
| 132 | config.port, |
| 133 | initial_delay_seconds=45, |
| 134 | timeout_seconds=15, |
| 135 | ) |
| 136 | container_builder.add_volume_config( |
| 137 | "configuration", |
| 138 | "/etc/nginx/sites-available/", |
| 139 | self._build_files(config), |
| 140 | ) |
| 141 | # Add container to pod spec |
| 142 | pod_spec_builder.add_container(container) |
| 143 | # Add ingress resources to pod spec if site url exists |
| 144 | if config.site_url: |
| 145 | parsed = urlparse(config.site_url) |
| 146 | annotations = { |
| 147 | "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format( |
| 148 | str(config.max_file_size) + "m" |
| 149 | if config.max_file_size > 0 |
| 150 | else config.max_file_size |
| 151 | ), |
| 152 | } |
| 153 | ingress_resource_builder = IngressResourceV3Builder( |
| 154 | f"{self.app.name}-ingress", annotations |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 155 | ) |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 156 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 157 | if config.ingress_whitelist_source_range: |
| 158 | annotations[ |
| 159 | "nginx.ingress.kubernetes.io/whitelist-source-range" |
| 160 | ] = config.ingress_whitelist_source_range |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 161 | |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 162 | if config.cluster_issuer: |
| 163 | annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer |
| 164 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 165 | if parsed.scheme == "https": |
| 166 | ingress_resource_builder.add_tls( |
| 167 | [parsed.hostname], config.tls_secret_name |
| 168 | ) |
| 169 | else: |
| 170 | annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false" |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 171 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 172 | ingress_resource_builder.add_rule( |
| 173 | parsed.hostname, self.app.name, config.port |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 174 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 175 | ingress_resource = ingress_resource_builder.build() |
| 176 | pod_spec_builder.add_ingress_resource(ingress_resource) |
| 177 | return pod_spec_builder.build() |
| beierlm | a4a37f7 | 2020-06-26 12:55:01 -0400 | [diff] [blame] | 178 | |
| 179 | |
| 180 | if __name__ == "__main__": |
| David Garcia | ef349d9 | 2020-12-10 21:16:12 +0100 | [diff] [blame] | 181 | main(NgUiCharm) |