-
Notifications
You must be signed in to change notification settings - Fork 0
/
cni.go
57 lines (49 loc) · 1.13 KB
/
cni.go
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
package main
import (
"encoding/json"
"flag"
"log"
"net"
"os"
"strings"
v1 "k8s.io/api/core/v1"
)
var (
cniPath = flag.String("cni-config", "/etc/cni/net.d/10-knet.json", "CNI configuration to write")
cniDNS = flag.String("cni-dns", "8.8.8.8", "CNI nameservers")
)
func writeCNIConfig(node *v1.Node, iface *net.Interface) {
if *cniPath == "" {
return
}
ranges := make([][]map[string]any, 0, len(node.Spec.PodCIDRs))
for _, cidr := range node.Spec.PodCIDRs {
ranges = append(ranges, []map[string]any{
{"subnet": cidr},
})
}
ba, err := json.MarshalIndent(map[string]any{
"cniVersion": "0.3.1",
"name": "knet-wg",
"type": "ptp",
"ipam": map[string]any{
"type": "host-local",
"ranges": ranges,
"routes": []map[string]any{
{"dst": "0.0.0.0/0"},
},
},
"dns": map[string]any{
"nameservers": strings.Split(*cniDNS, ","),
},
"mtu": iface.MTU,
}, "", " ")
if err != nil {
log.Fatal("failed to marshal CNI config: ", err)
}
log.Print("writing CNI config to ", *cniPath)
err = os.WriteFile(*cniPath, ba, 0644)
if err != nil {
log.Fatal("failed to write CNI config: ", err)
}
}