Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cjc7373 committed Jun 12, 2024
1 parent 5621d59 commit 1be3f59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
21 changes: 14 additions & 7 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,26 +1051,33 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust
// -> inject hostAliases and network members into CoreDNS configmap
if len(servers) > 0 {
postStartErrgrp.Go(func() error {
net, err := runtime.GetNetwork(postStartErrgrpCtx, &cluster.Network)
if err != nil {
return fmt.Errorf("failed to get cluster network %s to inject host records into CoreDNS: %w", cluster.Network.Name, err)
type record struct {
IP string
Hostname string
}
hosts := make([]string, 0, len(net.Members)+1)

records := make([]record, 0)

// hosts: hostAliases (including host.k3d.internal)
for _, hostAlias := range clusterStartOpts.HostAliases {
hosts = append(hosts, fmt.Sprintf("%s %s\n", hostAlias.IP, strings.Join(hostAlias.Hostnames, " ")))
for _, hostname := range hostAlias.Hostnames {
records = append(records, record{IP: hostAlias.IP, Hostname: hostname})
}
}

// more hosts: network members ("neighbor" containers)
net, err := runtime.GetNetwork(postStartErrgrpCtx, &cluster.Network)
if err != nil {
return fmt.Errorf("failed to get cluster network %s to inject host records into CoreDNS: %w", cluster.Network.Name, err)
}
for _, member := range net.Members {
hosts = append(hosts, fmt.Sprintf("%s %s\n", member.IP.String(), member.Name))
records = append(records, record{IP: member.IP.String(), Hostname: member.Name})
}

// inject CoreDNS configmap
l.Log().Infof("Injecting records for hostAliases (incl. host.k3d.internal) and for %d network members into CoreDNS configmap...", len(net.Members))
var custom_dns bytes.Buffer
err = customDNSTemplate.Execute(&custom_dns, hosts)
err = customDNSTemplate.Execute(&custom_dns, records)
if err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/client/templates/coredns-custom.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ metadata:
namespace: kube-system
data:
hosts.override: |
hosts {
{{- range . }}
{{ . }}
{{- end }}
fallthrough
}
file /etc/coredns/custom/additional-dns.db

# a SOA record is required
additional-dns.db: |
@ 3600 IN SOA a.root-servers.net. nstld.verisign-grs.com. 2024061200 1800 900 604800 86400
{{- range . }}
{{ .Hostname }} IN A {{ .IP }}
{{- end }}

0 comments on commit 1be3f59

Please sign in to comment.