Skip to content

Commit

Permalink
fix: preserve coredns config during cluster restart
Browse files Browse the repository at this point in the history
  • Loading branch information
cjc7373 committed Jun 12, 2024
1 parent 85347bd commit 5621d59
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
59 changes: 20 additions & 39 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sort"
"strconv"
"strings"
"text/template"
"time"

"github.com/docker/go-connections/nat"
Expand All @@ -54,6 +55,10 @@ import (
goyaml "gopkg.in/yaml.v2"
)

//go:embed templates/coredns-custom.yaml.tmpl
var customDNSTemplateStr string
var customDNSTemplate = template.Must(template.New("customDNS").Parse(customDNSTemplateStr))

// ClusterRun orchestrates the steps of cluster creation, configuration and starting
func ClusterRun(ctx context.Context, runtime k3drt.Runtime, clusterConfig *config.ClusterConfig) error {
/*
Expand Down Expand Up @@ -1046,58 +1051,34 @@ 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 {
hosts := ""
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)
}
hosts := make([]string, 0, len(net.Members)+1)

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

// 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 += fmt.Sprintf("%s %s\n", member.IP.String(), member.Name)
hosts = append(hosts, fmt.Sprintf("%s %s\n", member.IP.String(), 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))
act := actions.RewriteFileAction{
var custom_dns bytes.Buffer
err = customDNSTemplate.Execute(&custom_dns, hosts)
if err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
act := actions.WriteFileAction{
Runtime: runtime,
Path: "/var/lib/rancher/k3s/server/manifests/coredns.yaml",
Content: []byte(custom_dns.Bytes()),
Dest: "/var/lib/rancher/k3s/server/manifests/coredns-custom.yaml",
Mode: 0744,
RewriteFunc: func(input []byte) ([]byte, error) {
split, err := util.SplitYAML(input)
if err != nil {
return nil, fmt.Errorf("error splitting yaml: %w", err)
}

var outputBuf bytes.Buffer
outputEncoder := util.NewYAMLEncoder(&outputBuf)

for _, d := range split {
var doc map[string]interface{}
if err := yaml.Unmarshal(d, &doc); err != nil {
return nil, err
}
if kind, ok := doc["kind"]; ok {
if strings.ToLower(kind.(string)) == "configmap" {
configmapData, ok := doc["data"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid ConfigMap data type: %T", doc["data"])
}
configmapData["NodeHosts"] = hosts
}
}
if err := outputEncoder.Encode(doc); err != nil {
return nil, err
}
}
_ = outputEncoder.Close()
return outputBuf.Bytes(), nil
},
}

// get the first server in the list and run action on it once it's ready for it
Expand Down
13 changes: 13 additions & 0 deletions pkg/client/templates/coredns-custom.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns-custom
namespace: kube-system
data:
hosts.override: |
hosts {
{{- range . }}
{{ . }}
{{- end }}
fallthrough
}

0 comments on commit 5621d59

Please sign in to comment.