1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package clusterprovider
import (
"context"
"errors"
"fmt"
"io/fs"
"net"
"net/url"
"os"
"strconv"
"strings"
"github.com/johnandersen777/socialweb-computer-kcp/internal/clusterstack"
"github.com/johnandersen777/socialweb-computer-kcp/internal/procgroup"
)
func repointApiserverClients(spec clusterstack.Spec) func(context.Context, procgroup.Spec) (procgroup.Spec, error) {
return func(_ context.Context, ps procgroup.Spec) (procgroup.Spec, error) {
port := strconv.Itoa(spec.K3sApiserverPort)
for _, path := range spec.ApiserverClientKubeconfigs() {
if err := repointKubeconfig(path, port); err != nil {
return ps, fmt.Errorf("clusterprovider: pointing %s at the apiserver port %s of %s: %w", path, port, spec.Cluster, err)
}
}
return ps, nil
}
}
func repointKubeconfig(path, port string) error {
raw, err := os.ReadFile(path)
if errors.Is(err, fs.ErrNotExist) {
return nil
}
if err != nil {
return err
}
out, changed, err := withServerPort(string(raw), port)
if err != nil || !changed {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, []byte(out), 0o600); err != nil {
return err
}
return os.Rename(tmp, path)
}
func withServerPort(raw, port string) (string, bool, error) {
lines := strings.Split(raw, "\n")
for i, line := range lines {
trimmed := strings.TrimSpace(line)
value, ok := strings.CutPrefix(trimmed, "server: ")
if !ok {
continue
}
u, err := url.Parse(value)
if err != nil {
return raw, false, err
}
if u.Port() == port {
return raw, false, nil
}
u.Host = net.JoinHostPort(u.Hostname(), port)
lines[i] = line[:len(line)-len(trimmed)] + "server: " + u.String()
return strings.Join(lines, "\n"), true, nil
}
return raw, false, nil
}