blob: 5388466e8a47ec783ca9e4fbd661dd2e4e0f1883 [file] [log] [blame]
beierlma4a37f72020-06-26 12:55:01 -04001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
beierlma4a37f72020-06-26 12:55:01 -04003#
David Garciaef349d92020-12-10 21:16:12 +01004# 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
beierlma4a37f72020-06-26 12:55:01 -04007#
David Garciaef349d92020-12-10 21:16:12 +01008# http://www.apache.org/licenses/LICENSE-2.0
beierlma4a37f72020-06-26 12:55:01 -04009#
David Garciaef349d92020-12-10 21:16:12 +010010# 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##
beierlma4a37f72020-06-26 12:55:01 -040022
David Garcia49379ce2021-02-24 13:48:22 +010023# pylint: disable=E0213
24
25
David Garcia49379ce2021-02-24 13:48:22 +010026from ipaddress import ip_network
David Garciac753dc52021-03-17 15:28:47 +010027import logging
28from pathlib import Path
29from string import Template
30from typing import NoReturn, Optional
David Garcia49379ce2021-02-24 13:48:22 +010031from urllib.parse import urlparse
beierlma4a37f72020-06-26 12:55:01 -040032
beierlma4a37f72020-06-26 12:55:01 -040033from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010034from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garciac753dc52021-03-17 15:28:47 +010035from opslib.osm.interfaces.http import HttpClient
David Garcia49379ce2021-02-24 13:48:22 +010036from opslib.osm.pod import (
37 ContainerV3Builder,
David Garcia49379ce2021-02-24 13:48:22 +010038 FilesV3Builder,
39 IngressResourceV3Builder,
David Garciac753dc52021-03-17 15:28:47 +010040 PodSpecV3Builder,
David Garcia49379ce2021-02-24 13:48:22 +010041)
David Garciac753dc52021-03-17 15:28:47 +010042from opslib.osm.validator import ModelValidator, validator
David Garcia49379ce2021-02-24 13:48:22 +010043
44
beierlma4a37f72020-06-26 12:55:01 -040045logger = logging.getLogger(__name__)
46
David Garcia49379ce2021-02-24 13:48:22 +010047
48class ConfigModel(ModelValidator):
49 port: int
50 server_name: str
51 max_file_size: int
52 site_url: Optional[str]
sousaedu3cc03162021-04-29 16:53:12 +020053 cluster_issuer: Optional[str]
David Garciac35943e2021-06-28 16:50:42 +020054 ingress_class: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010055 ingress_whitelist_source_range: Optional[str]
56 tls_secret_name: Optional[str]
57
58 @validator("port")
59 def validate_port(cls, v):
60 if v <= 0:
61 raise ValueError("value must be greater than 0")
62 return v
63
64 @validator("max_file_size")
65 def validate_max_file_size(cls, v):
66 if v < 0:
67 raise ValueError("value must be equal or greater than 0")
68 return v
69
70 @validator("site_url")
71 def validate_site_url(cls, v):
72 if v:
73 parsed = urlparse(v)
74 if not parsed.scheme.startswith("http"):
75 raise ValueError("value must start with http")
76 return v
77
78 @validator("ingress_whitelist_source_range")
79 def validate_ingress_whitelist_source_range(cls, v):
80 if v:
81 ip_network(v)
82 return v
beierlma4a37f72020-06-26 12:55:01 -040083
David Garciaef349d92020-12-10 21:16:12 +010084
David Garcia49379ce2021-02-24 13:48:22 +010085class NgUiCharm(CharmedOsmBase):
David Garciaef349d92020-12-10 21:16:12 +010086 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +010087 super().__init__(*args, oci_image="image")
David Garciaef349d92020-12-10 21:16:12 +010088
David Garcia49379ce2021-02-24 13:48:22 +010089 self.nbi_client = HttpClient(self, "nbi")
90 self.framework.observe(self.on["nbi"].relation_changed, self.configure_pod)
91 self.framework.observe(self.on["nbi"].relation_broken, self.configure_pod)
David Garciaef349d92020-12-10 21:16:12 +010092
David Garcia49379ce2021-02-24 13:48:22 +010093 def _check_missing_dependencies(self, config: ConfigModel):
94 missing_relations = []
beierlma4a37f72020-06-26 12:55:01 -040095
David Garcia49379ce2021-02-24 13:48:22 +010096 if self.nbi_client.is_missing_data_in_app():
97 missing_relations.append("nbi")
David Garciaef349d92020-12-10 21:16:12 +010098
David Garcia49379ce2021-02-24 13:48:22 +010099 if missing_relations:
100 raise RelationsMissing(missing_relations)
David Garciaef349d92020-12-10 21:16:12 +0100101
David Garcia49379ce2021-02-24 13:48:22 +0100102 def _build_files(self, config: ConfigModel):
103 files_builder = FilesV3Builder()
104 files_builder.add_file(
105 "default",
106 Template(Path("files/default").read_text()).substitute(
107 port=config.port,
108 server_name=config.server_name,
109 max_file_size=config.max_file_size,
110 nbi_host=self.nbi_client.host,
111 nbi_port=self.nbi_client.port,
112 ),
beierlma4a37f72020-06-26 12:55:01 -0400113 )
David Garcia49379ce2021-02-24 13:48:22 +0100114 return files_builder.build()
beierlma4a37f72020-06-26 12:55:01 -0400115
David Garcia49379ce2021-02-24 13:48:22 +0100116 def build_pod_spec(self, image_info):
117 # Validate config
118 config = ConfigModel(**dict(self.config))
119 # Check relations
120 self._check_missing_dependencies(config)
121 # Create Builder for the PodSpec
122 pod_spec_builder = PodSpecV3Builder()
123 # Build Container
124 container_builder = ContainerV3Builder(self.app.name, image_info)
125 container_builder.add_port(name=self.app.name, port=config.port)
126 container = container_builder.build()
127 container_builder.add_tcpsocket_readiness_probe(
128 config.port,
129 initial_delay_seconds=45,
130 timeout_seconds=5,
David Garciaef349d92020-12-10 21:16:12 +0100131 )
David Garcia49379ce2021-02-24 13:48:22 +0100132 container_builder.add_tcpsocket_liveness_probe(
133 config.port,
134 initial_delay_seconds=45,
135 timeout_seconds=15,
136 )
137 container_builder.add_volume_config(
138 "configuration",
139 "/etc/nginx/sites-available/",
140 self._build_files(config),
141 )
142 # Add container to pod spec
143 pod_spec_builder.add_container(container)
144 # Add ingress resources to pod spec if site url exists
145 if config.site_url:
146 parsed = urlparse(config.site_url)
147 annotations = {
148 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
149 str(config.max_file_size) + "m"
150 if config.max_file_size > 0
151 else config.max_file_size
David Garciac35943e2021-06-28 16:50:42 +0200152 )
David Garcia49379ce2021-02-24 13:48:22 +0100153 }
David Garciac35943e2021-06-28 16:50:42 +0200154 if config.ingress_class:
155 annotations["kubernetes.io/ingress.class"] = config.ingress_class
David Garcia49379ce2021-02-24 13:48:22 +0100156 ingress_resource_builder = IngressResourceV3Builder(
157 f"{self.app.name}-ingress", annotations
David Garciaef349d92020-12-10 21:16:12 +0100158 )
David Garciaef349d92020-12-10 21:16:12 +0100159
David Garcia49379ce2021-02-24 13:48:22 +0100160 if config.ingress_whitelist_source_range:
161 annotations[
162 "nginx.ingress.kubernetes.io/whitelist-source-range"
163 ] = config.ingress_whitelist_source_range
David Garciaef349d92020-12-10 21:16:12 +0100164
sousaedu3cc03162021-04-29 16:53:12 +0200165 if config.cluster_issuer:
166 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
167
David Garcia49379ce2021-02-24 13:48:22 +0100168 if parsed.scheme == "https":
169 ingress_resource_builder.add_tls(
170 [parsed.hostname], config.tls_secret_name
171 )
172 else:
173 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
David Garciaef349d92020-12-10 21:16:12 +0100174
David Garcia49379ce2021-02-24 13:48:22 +0100175 ingress_resource_builder.add_rule(
176 parsed.hostname, self.app.name, config.port
David Garciaef349d92020-12-10 21:16:12 +0100177 )
David Garcia49379ce2021-02-24 13:48:22 +0100178 ingress_resource = ingress_resource_builder.build()
179 pod_spec_builder.add_ingress_resource(ingress_resource)
180 return pod_spec_builder.build()
beierlma4a37f72020-06-26 12:55:01 -0400181
182
183if __name__ == "__main__":
David Garciaef349d92020-12-10 21:16:12 +0100184 main(NgUiCharm)