Skip to content
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

feat: add traefik hosts to dns resolve #3

Merged
merged 10 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion container/.gitignore

This file was deleted.

11 changes: 4 additions & 7 deletions container/Containerfile
Original file line number Diff line number Diff line change
@@ -13,20 +13,17 @@ RUN mkdir -p /tmp/dnsmasq \
WORKDIR /tmp/dnsmasq
# Build dnsmasq
RUN make
# Apply configuration patch
COPY conf.patch /tmp/dnsmasq/conf.patch
RUN patch < conf.patch

FROM base as dist
# Copy built dnsmasq and configuration
COPY --from=build /tmp/dnsmasq/src/dnsmasq /usr/local/sbin/dnsmasq
COPY --from=build /tmp/dnsmasq/dnsmasq.conf.example /etc/dnsmasq.conf
# Since we're defining a config directory, we need to create it
RUN mkdir -p /etc/dnsmasq.d
COPY dnsmasq.conf /etc/dnsmasq.conf
# Creating directory for custom configuration and additional hosts
RUN mkdir -p /etc/dnsmasq.d /etc/dnsmasq_hosts.d
# Set custom entrypoint and command
COPY entrypoint.sh /entrypoint
EXPOSE 53/udp
EXPOSE 67/udp
VOLUME [ "/etc/dnsmasq.d" ]
VOLUME [ "/etc/dnsmasq.d", "/etc/dnsmasq_hosts.d" ]
ENTRYPOINT [ "/entrypoint" ]
CMD [ "dnsmasq" ]
82 changes: 0 additions & 82 deletions container/conf.patch

This file was deleted.

20 changes: 20 additions & 0 deletions container/dnsmasq.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# REF: https://dnsmasq.org/docs/dnsmasq-man.html

# container related configuration
keep-in-foreground
log-facility=/dev/stdout
cache-size=4000

# DNS
all-servers
domain-needed
bogus-priv
no-resolv
no-hosts

# DHCP
dhcp-authoritative

# Conf Directories
hostsdir=/etc/dnsmasq_hosts.d
Tbaile marked this conversation as resolved.
Show resolved Hide resolved
conf-dir=/etc/dnsmasq.d/,*.conf
12 changes: 0 additions & 12 deletions imageroot/actions/create-module/10setup

This file was deleted.

1 change: 1 addition & 0 deletions imageroot/actions/create-module/10setup
38 changes: 38 additions & 0 deletions imageroot/bin/reload_hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import subprocess

import agent
import network

"""
Generate a host file from traefik that will be used to resolve CNAMEs
Then save all CNAMES in a config file for DNSMasq
"""

fqdn = subprocess.run(['hostname', '-f'], capture_output=True, text=True, check=True).stdout.strip()

config = json.load(open("config.json"))
if 'interface' in config and config['interface'] != '':
# retrieve the first network interface that matches the configuration, then get the first IP address provided
interface = next(interface for interface in network.list_interfaces() if interface["name"] == config["interface"])
ip = interface["addresses"][0]["address"]
# save a host file for dnsmasq, so that it can resolve the node fqdn
with open(f'dnsmasq_hosts.d/00_node_hosts', 'w') as file:
file.write('# This file is automatically generated by NethServer, manual changes will be lost.\n')
file.write(f'{ip} {fqdn}\n')

# save a new configuration file for dnsmasq, so that it can resolve the CNAMEs to the node fqdn
redis_client = agent.redis_connect(use_replica=True)
with open("dnsmasq.d/01cnames.conf", "w") as file:
file.write("# This file is automatically generated by NethServer, manual changes will be lost.\n")
file.write('cname=')
for host in redis_client.smembers(f'{agent.resolve_agent_id("traefik@node")}/hosts'):
file.write(f'{host},')
file.write(f'{fqdn}\n')
2 changes: 2 additions & 0 deletions imageroot/dnsmasq.service
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ Restart=on-failure
TimeoutStopSec=70
ExecStartPre=/bin/rm \
-f %t/%n.ctr-id
ExecStartPre=runagent -m %N reload_hosts
ExecStart=/usr/bin/podman run \
--cidfile=%t/%n.ctr-id \
--cgroups=no-conmon \
@@ -23,6 +24,7 @@ ExecStart=/usr/bin/podman run \
--network=host \
--cap-add=NET_ADMIN,NET_RAW \
--volume=/var/lib/nethserver/%N/state/dnsmasq.d:/etc/dnsmasq.d:Z \
--volume=/var/lib/nethserver/%N/state/dnsmasq_hosts.d:/etc/dnsmasq_hosts.d:Z \
${DNSMASQ_SERVER_IMAGE}
ExecStop=/usr/bin/podman stop \
--ignore -t 10 \
1 change: 1 addition & 0 deletions imageroot/events/fqdn-changed/10handler
20 changes: 20 additions & 0 deletions imageroot/events/hosts-changed/10handler
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import os
import sys

import agent

# parse data and init variables
data = json.load(sys.stdin)
# skip if the event comes from another node
if os.environ['NODE_ID'] != str(data['node_id']):
sys.exit(0)

agent.run_helper('systemctl', 'restart', os.getenv('MODULE_ID'))
19 changes: 19 additions & 0 deletions imageroot/update-module.d/10setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env sh

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

set -e
exec 1>&2

# This file will be executed during module creation and module update, be aware of that.
Tbaile marked this conversation as resolved.
Show resolved Hide resolved

# Create the dnsmasq.d and hosts_dir directory
mkdir -p dnsmasq.d dnsmasq_hosts.d
# Install systemd service
install -m 644 ../dnsmasq.service "/etc/systemd/system/${MODULE_ID}.service"
systemctl daemon-reload
# restart service only if it's already running
systemctl try-restart "${MODULE_ID}"
14 changes: 0 additions & 14 deletions imageroot/update-module.d/90systemd

This file was deleted.