Skip to content

Commit 75e9285

Browse files
authored
Assigns default nameserver in sandbox service. (#276)
* Closes #148. * Storing the default nameserver in the bundle config means that DNS won't work if the container stops and then restarts later when the subnet address has changed.
1 parent 48db623 commit 75e9285

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

Sources/ContainerClient/Utility.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,12 @@ public struct Utility {
169169
networkStatuses.append(networkStatus)
170170
}
171171

172-
let nameservers: [String]
173-
if management.dnsNameservers.isEmpty {
174-
let subnet = try CIDRAddress(networkStatuses[0].address)
175-
let nameserver = IPv4Address(fromValue: subnet.lower.value + 1).description
176-
nameservers = [nameserver]
177-
} else {
178-
nameservers = management.dnsNameservers
179-
}
180-
181172
if management.dnsDisabled {
182173
config.dns = nil
183174
} else {
184175
let domain = management.dnsDomain ?? ClientDefaults.getOptional(key: .defaultDNSDomain)
185176
config.dns = .init(
186-
nameservers: nameservers,
177+
nameservers: management.dnsNameservers,
187178
domain: domain,
188179
searchDomains: management.dnsSearchDomains,
189180
options: management.dnsOptions

Sources/Services/ContainerSandboxService/SandboxService.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,26 @@ public actor SandboxService {
8282
bootlog: bundle.bootlog.path,
8383
logger: self.log
8484
)
85-
let config = try bundle.configuration
85+
var config = try bundle.configuration
8686
let container = LinuxContainer(
8787
config.id,
8888
rootfs: try bundle.containerRootfs.asMount,
8989
vmm: vmm,
9090
logger: self.log
9191
)
92+
93+
// dynamically configure the DNS nameserver from a network if no explicit configuration
94+
if let dns = config.dns, dns.nameservers.isEmpty {
95+
if let nameserver = try await self.getDefaultNameserver(networks: config.networks) {
96+
config.dns = ContainerConfiguration.DNSConfiguration(
97+
nameservers: [nameserver],
98+
domain: dns.domain,
99+
searchDomains: dns.searchDomains,
100+
options: dns.options
101+
)
102+
}
103+
}
104+
92105
try await self.configureContainer(container: container, config: config)
93106

94107
let fqdn: String
@@ -641,6 +654,19 @@ public actor SandboxService {
641654
configureInitialProcess(container: container, process: config.initProcess)
642655
}
643656

657+
private func getDefaultNameserver(networks: [String]) async throws -> String? {
658+
for network in networks {
659+
let client = NetworkClient(id: network)
660+
let state = try await client.state()
661+
guard case .running(_, let status) = state else {
662+
continue
663+
}
664+
return status.gateway
665+
}
666+
667+
return nil
668+
}
669+
644670
private func configureInitialProcess(container: LinuxContainer, process: ProcessConfiguration) {
645671
container.arguments = [process.executable] + process.arguments
646672
container.environment = modifyingEnvironment(process)

0 commit comments

Comments
 (0)