Skip to content

Commit

Permalink
Revert "fix: preserve coredns config during cluster restart (#1453)"
Browse files Browse the repository at this point in the history
This reverts commit 71b5755.
  • Loading branch information
iwilltry42 committed Jul 5, 2024
1 parent 7dc4a5c commit 1067c1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
58 changes: 35 additions & 23 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"sort"
"strconv"
"strings"
"text/template"
"time"

"github.com/docker/go-connections/nat"
Expand All @@ -55,10 +54,6 @@ 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 @@ -1063,18 +1058,11 @@ 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 {
type record struct {
IP string
Hostname string
}

records := make([]record, 0)
hosts := ""

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

// more hosts: network members ("neighbor" containers)
Expand All @@ -1083,21 +1071,45 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust
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 {
records = append(records, record{IP: member.IP.String(), Hostname: member.Name})
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))
var custom_dns bytes.Buffer
err = customDNSTemplate.Execute(&custom_dns, records)
if err != nil {
return fmt.Errorf("failed to render template: %w", err)
}
act := actions.WriteFileAction{
act := actions.RewriteFileAction{
Runtime: runtime,
Content: []byte(custom_dns.Bytes()),
Dest: "/var/lib/rancher/k3s/server/manifests/coredns-custom.yaml",
Path: "/var/lib/rancher/k3s/server/manifests/coredns.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
15 changes: 0 additions & 15 deletions pkg/client/templates/coredns-custom.yaml.tmpl

This file was deleted.

0 comments on commit 1067c1a

Please sign in to comment.