-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (128 loc) · 3.72 KB
/
main.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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
var (
ifName = flag.String("ifname", "kwg", "Wireguard interface name")
keyPath = flag.String("key-path", "wg.key", "path to the private key")
cfgPath = flag.String("config", "/etc/wireguard/kwg.cfg", "WireGuard configuration to write")
kubeconfig = flag.String("kubeconfig", os.Getenv("KUBECONFIG"), "path to the kube-config")
master = flag.String("master", "", "Master if not using the default master")
nodeName = flag.String("node-name", func() string {
s, _ := os.Hostname()
return s
}(), "node name")
k *kubernetes.Clientset
ctx, cancel = context.WithCancel(context.Background())
stopCh = make(chan struct{}, 1)
)
func main() {
flag.Parse()
log.Print("knet-wg starting")
err := connect()
if err != nil {
log.Fatal(err)
}
var key wgtypes.Key
{ // ensure we have a key
keyData, err := os.ReadFile(*keyPath)
if err == nil {
key, err = wgtypes.ParseKey(string(keyData))
if err != nil {
log.Fatal(err)
}
} else if os.IsNotExist(err) {
key, err = wgtypes.GeneratePrivateKey()
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(*keyPath, []byte(key.String()), 0600)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
// ------------------------------------------------------------------------
node, err := k.CoreV1().Nodes().Get(ctx, *nodeName, metav1.GetOptions{})
if err != nil {
log.Fatal("failed to get node ", *nodeName, ": ", err)
}
{ // ensure the node has published its key
if annotation(node, annPubkey) != key.PublicKey().String() {
log.Print("setting our pubkey annotation to node")
node.Annotations[annPubkey] = key.PublicKey().String()
_, err = k.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
if err != nil {
log.Fatal(err)
}
}
}
// ------------------------------------------------------------------------
iface, _ := net.InterfaceByName(*ifName)
{ // create the kwg interface
// err is not usable here, only an internal value is set in OpError.Err
if iface == nil {
log.Print("creating interface ", *ifName)
_ = run("ip", "link", "add", *ifName, "type", "wireguard")
iface, _ = net.InterfaceByName(*ifName)
}
err = run("ip", "link", "set", *ifName, "up")
if err != nil {
log.Fatal("failed to set link up: ", err)
}
syncNodeIPs(node.Spec.PodCIDRs, iface)
}
// ------------------------------------------------------------------------
writeCNIConfig(node, iface)
setupNFT(node.Spec.PodCIDRs)
// ------------------------------------------------------------------------
factory := informers.NewSharedInformerFactory(k, time.Second*30)
factory.Start(stopCh)
coreFactory := factory.Core().V1()
nodesInformer := coreFactory.Nodes().Informer()
nodesInformer.AddEventHandler(&nodeEventHandler{
hs: map[string]uint64{},
nodes: map[string]nodeInfo{},
})
nodesInformer.Run(stopCh)
}
func connect() (err error) {
cfg, err := clientcmd.BuildConfigFromFlags(*master, *kubeconfig)
if err != nil {
err = fmt.Errorf("failed to build Kubernetes config: %w", err)
return
}
c, err := kubernetes.NewForConfig(cfg)
if err != nil {
err = fmt.Errorf("failed to build Kubernetes client: %w", err)
return
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-c
log.Print("interrupted, cancelling operations...")
cancel()
close(stopCh)
<-c
log.Print("second interrupt, exiting now.")
os.Exit(1)
}()
k = c
return
}