-
Notifications
You must be signed in to change notification settings - Fork 12
feat(dns): dns record requirer #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7100997
0921ba1
0a3d43b
4111799
af2c1d1
e25ea6e
af6e08b
708c909
7d4ad81
d8b93bb
f30e538
b7b36ab
d3ffd80
7393fcf
271553d
aa29473
c11e96f
21c4401
31fda99
2896dd8
94947ed
805b0e1
2b4e661
3fa48e5
2de8d02
db226f4
7204454
f625a99
36bf2e0
26b7545
f041cc2
623307c
bd7b0e1
f6b0ec0
8fb27ff
8e361ee
de4473e
1382134
bc9c466
94c229e
522e601
93bafa9
0eaec4f
1bff25c
79832ed
c91a4bf
b0ce0fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| https://github.com/canonical/haproxy-operator/deployments/charmhub-stable-promote | ||
| https://github.com/canonical/haproxy-operator/deployments/charmhub-stable-promote |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,8 @@ | |||||||||||||||||
| from ops.charm import ActionEvent | ||||||||||||||||||
| from ops.model import Port, SecretNotFoundError | ||||||||||||||||||
|
|
||||||||||||||||||
| from charms.dns_integrator.v0.dns_record import DNSRecordRequires | ||||||||||||||||||
| from dns_record import DNS_RECORD_RELATION, DNSRecordService | ||||||||||||||||||
| from haproxy import HAPROXY_CONFIG, HAPROXY_SERVICE, HAProxyService, file_exists, read_file | ||||||||||||||||||
| from http_interface import ( | ||||||||||||||||||
| HTTPBackendAvailableEvent, | ||||||||||||||||||
|
|
@@ -59,7 +61,12 @@ | |||||||||||||||||
| from state.charm_state import CharmState, ProxyMode | ||||||||||||||||||
| from state.ddos_protection import DDosProtection | ||||||||||||||||||
| from state.exception import CharmStateValidationBaseError | ||||||||||||||||||
| from state.ha import HACLUSTER_INTEGRATION, HAPROXY_PEER_INTEGRATION, HAInformation | ||||||||||||||||||
| from state.ha import ( | ||||||||||||||||||
| HACLUSTER_INTEGRATION, | ||||||||||||||||||
| HAPROXY_PEER_INTEGRATION, | ||||||||||||||||||
| HAInformation, | ||||||||||||||||||
| HAInformationValidationError, | ||||||||||||||||||
| ) | ||||||||||||||||||
| from state.haproxy_route import ( | ||||||||||||||||||
| HAPROXY_ROUTE_RELATION, | ||||||||||||||||||
| HAProxyRouteBackend, | ||||||||||||||||||
|
|
@@ -172,6 +179,8 @@ def __init__(self, *args: typing.Any): | |||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| self._tls = TLSRelationService(self.model, self.certificates, self.recv_ca_certs) | ||||||||||||||||||
| self.dns_record_requirer = DNSRecordRequires(self) | ||||||||||||||||||
| self._dns_record_service = DNSRecordService(self.model, self.dns_record_requirer) | ||||||||||||||||||
| self.website_requirer = HTTPProvider(self, WEBSITE_RELATION) | ||||||||||||||||||
|
|
||||||||||||||||||
| self._grafana_agent = COSAgentProvider( | ||||||||||||||||||
|
|
@@ -211,6 +220,18 @@ def __init__(self, *args: typing.Any): | |||||||||||||||||
| self._ingress_per_unit_provider.on.data_removed, self._on_ingress_data_removed | ||||||||||||||||||
| ) | ||||||||||||||||||
| self.framework.observe(self.hacluster.on.ha_ready, self._on_config_changed) | ||||||||||||||||||
| self.framework.observe( | ||||||||||||||||||
| self.on[HACLUSTER_INTEGRATION].relation_changed, self._on_config_changed | ||||||||||||||||||
| ) | ||||||||||||||||||
| self.framework.observe( | ||||||||||||||||||
| self.on[HACLUSTER_INTEGRATION].relation_broken, self._on_config_changed | ||||||||||||||||||
| ) | ||||||||||||||||||
| self.framework.observe( | ||||||||||||||||||
| self.on[DNS_RECORD_RELATION].relation_created, self._on_config_changed | ||||||||||||||||||
| ) | ||||||||||||||||||
| self.framework.observe( | ||||||||||||||||||
| self.on[DNS_RECORD_RELATION].relation_joined, self._on_config_changed | ||||||||||||||||||
| ) | ||||||||||||||||||
| self.framework.observe( | ||||||||||||||||||
| self.recv_ca_certs.on.certificate_set_updated, self._on_ca_certificates_updated | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
@@ -346,6 +367,8 @@ def _reconcile(self) -> None: | |||||||||||||||||
|
|
||||||||||||||||||
| self.unit.set_ports(80) | ||||||||||||||||||
| self.haproxy_service.reconcile_default(charm_state) | ||||||||||||||||||
| if self.unit.is_leader(): | ||||||||||||||||||
| self._update_dns_records() | ||||||||||||||||||
| self.unit.status = ops.ActiveStatus(status_message) | ||||||||||||||||||
|
|
||||||||||||||||||
| def _configure_ingress( | ||||||||||||||||||
|
|
@@ -583,6 +606,41 @@ def _on_ca_certificates_removed(self, _: CertificatesRemovedEvent) -> None: | |||||||||||||||||
| """Handle the CA certificates removed event.""" | ||||||||||||||||||
| self._reconcile() | ||||||||||||||||||
|
|
||||||||||||||||||
| def _update_dns_records(self) -> None: | ||||||||||||||||||
| """Publish A records for all managed hostnames to the dns-record relation. | ||||||||||||||||||
|
|
||||||||||||||||||
| Uses the VIP when an HA relation is active, otherwise uses the ingress | ||||||||||||||||||
| binding address of this unit. | ||||||||||||||||||
|
|
||||||||||||||||||
| Called at the end of _reconcile(), so config/TLS validation has already passed. | ||||||||||||||||||
| """ | ||||||||||||||||||
| if not self.unit.is_leader(): | ||||||||||||||||||
| return | ||||||||||||||||||
|
|
||||||||||||||||||
|
yanksyoon marked this conversation as resolved.
|
||||||||||||||||||
| hostnames = [req.common_name for req in self._get_certificate_requests()] | ||||||||||||||||||
| if not hostnames: | ||||||||||||||||||
| return | ||||||||||||||||||
|
|
||||||||||||||||||
| try: | ||||||||||||||||||
| ha_information = HAInformation.from_charm(self) | ||||||||||||||||||
| except HAInformationValidationError: | ||||||||||||||||||
| ha_information = None | ||||||||||||||||||
|
yanksyoon marked this conversation as resolved.
|
||||||||||||||||||
|
|
||||||||||||||||||
| if ha_information and ha_information.ha_integration_ready and ha_information.vip: | ||||||||||||||||||
| ip = str(ha_information.vip) | ||||||||||||||||||
| else: | ||||||||||||||||||
| network_binding = self.model.get_binding(DNS_RECORD_RELATION) | ||||||||||||||||||
| if network_binding is None: | ||||||||||||||||||
| logger.warning("Cannot resolve network binding for DNS records.") | ||||||||||||||||||
| return | ||||||||||||||||||
| ingress_addresses = network_binding.network.ingress_addresses | ||||||||||||||||||
| if not ingress_addresses: | ||||||||||||||||||
| logger.warning("No ingress addresses found; skipping DNS record update.") | ||||||||||||||||||
| return | ||||||||||||||||||
| ip = str(ingress_addresses[0]) | ||||||||||||||||||
|
Comment on lines
+636
to
+640
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Very minuscule nitpick.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept the existing empty-list guard instead of using |
||||||||||||||||||
|
|
||||||||||||||||||
| self._dns_record_service.update_dns_records(hostnames, ip) | ||||||||||||||||||
|
yanksyoon marked this conversation as resolved.
|
||||||||||||||||||
|
|
||||||||||||||||||
| @validate_config_and_tls(defer=False) | ||||||||||||||||||
| def _on_ingress_per_unit_data_provided(self, _: IngressDataReadyEvent) -> None: | ||||||||||||||||||
| """Handle the data-provided event for ingress-per-unit.""" | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.