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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package nodeagent
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/clock"
)
type cniRange struct {
Subnet string `json:"subnet"`
}
type cniRoute struct {
Dst string `json:"dst"`
}
type cniIPAM struct {
Type string `json:"type"`
Ranges [][]cniRange `json:"ranges"`
Routes []cniRoute `json:"routes"`
}
type cniBridge struct {
Type string `json:"type"`
Bridge string `json:"bridge"`
IsGateway bool `json:"isGateway"`
IPMasq bool `json:"ipMasq"`
HairpinMode bool `json:"hairpinMode"`
MTU int `json:"mtu"`
IPAM cniIPAM `json:"ipam"`
}
type cniPortmap struct {
Type string `json:"type"`
Capabilities map[string]bool `json:"capabilities"`
}
type cniConflist struct {
CNIVersion string `json:"cniVersion"`
Name string `json:"name"`
Plugins []any `json:"plugins"`
}
func CNI(ctx context.Context, cfg Config, log *slog.Logger) error {
if log == nil {
log = slog.Default()
}
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
env, err := readEnvFile(cfg.APIServerEnvPath)
if err != nil {
return fmt.Errorf("nodeagent: reading %s for NODE_NAME: %w", cfg.APIServerEnvPath, err)
}
nodeName = env["NODE_NAME"]
}
if !validName(nodeName) {
return fmt.Errorf("nodeagent: NODE_NAME %q is not a node name", nodeName)
}
cidr, err := awaitPodCIDR(ctx, cfg, nodeName, log)
if err != nil {
return err
}
if err := validPodCIDR(cidr); err != nil {
return err
}
if err := os.MkdirAll(cfg.CNIConfDir, 0o755); err != nil {
return fmt.Errorf("nodeagent: creating %s: %w", cfg.CNIConfDir, err)
}
if err := os.MkdirAll(cfg.CNIBinDir, 0o755); err != nil {
return fmt.Errorf("nodeagent: creating %s: %w", cfg.CNIBinDir, err)
}
for _, plugin := range []string{"bridge", "host-local", "portmap", "loopback"} {
link := filepath.Join(cfg.CNIBinDir, plugin)
if err := os.Remove(link); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("nodeagent: replacing the CNI plugin link %s: %w", link, err)
}
if err := os.Symlink(cfg.CNIPluginPath(), link); err != nil {
return fmt.Errorf("nodeagent: linking %s to %s: %w", link, cfg.CNIPluginPath(), err)
}
}
content, err := json.MarshalIndent(conflistFor(cidr), "", " ")
if err != nil {
return fmt.Errorf("nodeagent: encoding the CNI conflist: %w", err)
}
content = append(content, '\n')
conf := filepath.Join(cfg.CNIConfDir, CNIConflistName)
if err := writeAtomic(conf, content, 0o644); err != nil {
return err
}
log.Info("wrote the bridge CNI conflist", "path", conf, "podCIDR", cidr, "plugins", cfg.CNIPluginPath())
return nil
}
func awaitPodCIDR(ctx context.Context, cfg Config, nodeName string, log *slog.Logger) (string, error) {
limit := time.Duration(cfg.CNIWaitAttempts) * cfg.CNIWaitInterval()
wctx, cancel := context.WithTimeout(ctx, limit)
defer cancel()
last := "the kubelet kubeconfig " + cfg.KubeletKubeconfig + " is not written yet"
for {
if fileNonEmpty(cfg.KubeletKubeconfig) {
cidr, err := followPodCIDR(wctx, cfg, nodeName)
if cidr != "" {
return cidr, nil
}
if ctx.Err() != nil {
return "", ctx.Err()
}
if err != nil {
last = err.Error()
log.Info("the node watch ended before a podCIDR; it restarts", "node", nodeName, "err", last)
}
}
if err := clock.SleepErr(wctx, cfg.CNIWaitInterval()); err != nil {
if ctx.Err() != nil {
return "", ctx.Err()
}
return "", fmt.Errorf("nodeagent: node %s has no podCIDR yet after %s: %s", nodeName, limit, last)
}
}
}
func followPodCIDR(ctx context.Context, cfg Config, nodeName string) (string, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cmd := exec.CommandContext(ctx, cfg.K3sPath, "kubectl", "--kubeconfig", cfg.KubeletKubeconfig,
"get", "node", nodeName, "--watch", "-o", `jsonpath={.spec.podCIDR}{"\n"}`)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("nodeagent: watching node %s: %w", nodeName, err)
}
lines := bufio.NewScanner(out)
for lines.Scan() {
if cidr := strings.TrimSpace(lines.Text()); cidr != "" {
cancel()
_ = cmd.Wait()
return cidr, nil
}
}
err = cmd.Wait()
if msg := strings.TrimSpace(stderr.String()); msg != "" {
return "", fmt.Errorf("nodeagent: watching node %s: %s", nodeName, msg)
}
if err != nil {
return "", fmt.Errorf("nodeagent: watching node %s: %w", nodeName, err)
}
return "", fmt.Errorf("nodeagent: the watch of node %s closed", nodeName)
}
func validPodCIDR(cidr string) error {
ip, network, err := net.ParseCIDR(cidr)
if err != nil || ip.To4() == nil || network.IP.To4() == nil {
return fmt.Errorf("nodeagent: podCIDR %s is not an IPv4 CIDR", cidr)
}
return nil
}
func conflistFor(cidr string) cniConflist {
return cniConflist{
CNIVersion: "1.0.0",
Name: "socialweb-pods",
Plugins: []any{
cniBridge{
Type: "bridge",
Bridge: PodBridge,
IsGateway: true,
IPMasq: false,
HairpinMode: true,
MTU: PodMTU,
IPAM: cniIPAM{
Type: "host-local",
Ranges: [][]cniRange{{{Subnet: cidr}}},
Routes: []cniRoute{{Dst: "0.0.0.0/0"}},
},
},
cniPortmap{Type: "portmap", Capabilities: map[string]bool{"portMappings": true}},
},
}
}