This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (165 loc) · 5.34 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
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
package main
import (
"context"
"flag"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/danopia/kube-pet-node/controller"
"github.com/danopia/kube-pet-node/controllers/pods"
"github.com/danopia/kube-pet-node/controllers/selfprovision"
"github.com/danopia/kube-pet-node/pkg/podman"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
func main() {
var nameFlag = flag.String("hostname", "", "name to use for the Kubernetes node, will get prefixed with 'pet-'")
var kubeconfFlag = flag.String("kubeconfig-path", "node-kubeconfig.yaml", "path to client config with a system:node clusterrolebinding")
var podmanSockFlag = flag.String("podman-socket", "tcp:127.0.0.1:8410", "podman socket location, either 'tcp:' or 'unix:' prefix")
var vpnIfaceFlag = flag.String("vpn-iface", "wg-gke", "network interface which the other cluster nodes and pods are available on")
var cniNetFlag = flag.String("cni-net", "kube-pet-net", "CNI network which provides local pods with networking and addresses")
var maxPodsFlag = flag.Int("max-pods", 25, "number of pods this node should support. 0 effectively disables scheduling")
_ = flag.String("controllers", "firewall,podman", "which features to run")
flag.Parse()
var nodeName string
// read the kubeconfig ourselves to see what our user is called in it
kubeConfig, err := (&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfFlag}).Load()
if err != nil {
panic(err)
}
kubeCtx := kubeConfig.Contexts[kubeConfig.CurrentContext]
if strings.HasPrefix(kubeCtx.AuthInfo, "system:node:") {
nodeName = strings.TrimPrefix(kubeCtx.AuthInfo, "system:node:")
}
if *nameFlag != "" {
nodeName = "pet-" + *nameFlag
}
if nodeName == "" {
log.Fatalln("Hostname is required and also couldn't be detected from the kubeconfig, try passing --hostname=<xyz>")
}
// look up our VPN information
nodeIP, err := fetchNodeAddressFromInterface(*vpnIfaceFlag)
if err != nil {
log.Println("WARN: failed to find Node IP from", *vpnIfaceFlag, ":", err)
} else {
log.Println("Discovered node IP:", nodeIP)
}
// unix:/run/user/1000/podman/podman.sock
// tcp:127.0.0.1:8410
podSockParts := strings.SplitN(*podmanSockFlag, ":", 2)
podman := podman.NewPodmanClient(podSockParts[0], podSockParts[1])
podStorage, err := pods.NewPodSpecStorage()
if err != nil {
panic(err)
}
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfFlag)
if err != nil {
panic(err)
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
// actually set up our lifespan now that we've loaded enough deps
ctx, cancel := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
cancel()
<-sig
log.Fatalln("WARN: Received second signal -- bailing without shutdown!")
}()
// sync our local pod info sources
podManager, err := pods.NewPodManager(podman, podStorage)
if err != nil {
panic(err)
}
// discover CIDRs that pods can use
var podNets []net.IPNet
maxPods := *maxPodsFlag
cniNetworks, err := podman.NetworkInspect(ctx, *cniNetFlag)
if err != nil {
log.Println("WARN: failed to read CNI", *cniNetFlag, ":", err)
}
for _, cniNetwork := range cniNetworks {
for _, plugin := range cniNetwork.Plugins {
if plugin.Ipam != nil {
// log.Printf("%+v", plugin.Ipam)
if plugin.Ipam.Subnet != "" {
_, ipn, err := net.ParseCIDR(plugin.Ipam.Subnet)
if err != nil {
panic(err)
}
podNets = append(podNets, *ipn)
} else {
log.Println("TODO: cni plugin IPAM without a Subnet")
}
}
}
}
if len(podNets) < 1 && maxPods > 0 {
log.Println("WARN: I couldn't discover any pod networks! I'm going to refuse to run any pods.")
maxPods = 0
}
log.Println("Pod networks:", podNets)
// construct the node
petNode, err := controller.NewPetNode(ctx, nodeName, podManager, clientset, maxPods, *vpnIfaceFlag, nodeIP, podNets, *cniNetFlag)
if err != nil {
panic(err)
}
if petNode.PodRunner != nil {
select {
case <-petNode.PodRunner.Ready():
log.Println("Ready...")
<-petNode.PodRunner.Done()
log.Println("Done!")
case <-petNode.PodRunner.Done():
log.Println("Done...")
}
if petNode.PodRunner.Err() != nil {
log.Println(petNode.PodRunner.Err())
// handle error
}
} else {
selfProvisioner := selfprovision.NewController(nodeName, *vpnIfaceFlag, *cniNetFlag, clientset, podman)
if err := selfProvisioner.Perform(ctx); err != nil {
panic(err)
}
}
log.Println("exit")
}
func fetchNodeAddressFromInterface(ifaceName string) (net.IP, error) {
vpnIface, err := net.InterfaceByName(ifaceName)
if err != nil {
return nil, err
}
vpnAddrs, err := vpnIface.Addrs()
if err != nil {
return nil, err
}
// retrieve node address from the VPN info
for _, addr := range vpnAddrs {
if net, ok := addr.(*net.IPNet); ok {
// log.Println(net, net.IP.IsGlobalUnicast())
if net.IP.IsGlobalUnicast() {
ones, bits := net.Mask.Size()
if ones == bits {
// a single addr, treat as node address
return net.IP, nil
} else {
log.Println("Skipping CIDR Addr", addr.String(), "on vpn interface")
}
}
} else {
log.Println("Skipping weird Addr", addr.String(), "on vpn interface")
}
}
return nil, nil
}