-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
238 lines (211 loc) · 7.78 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright 2021 Contributors to the EdgeNet project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"github.com/EdgeNet-project/node/pkg/cluster"
"github.com/EdgeNet-project/node/pkg/network"
"github.com/EdgeNet-project/node/pkg/platforms"
"github.com/EdgeNet-project/node/pkg/utils"
"github.com/thanhpk/randstr"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gopkg.in/yaml.v3"
"log"
"math/rand"
"net"
"os"
"path/filepath"
"strings"
"time"
)
const defaultVPNNetworkV4 = "10.183.0.0/20"
const defaultVPNNetworkV6 = "fdb4:ae86:ec99:4004::/64"
const edgenetConfigFile = "/opt/edgenet/config.yaml"
const kubeletEnvFileDebian = "/etc/default/kubelet"
const kubeletEnvFileRedHat = "/etc/sysconfig/kubelet"
const vpnLinkName = "edgenetmesh0"
func check(err error) {
if err != nil {
log.Panic(err)
}
}
type edgenetConfig struct {
// HostnameRoot is the deterministic component of the hostname (e.g. `aws-eu-west-1a`).
HostnameRoot string `yaml:"hostnameRoot"`
// HostnameSuffix is the random component of the hostname (e.g. `3fe8`).
HostnameSuffix string `yaml:"hostnameSuffix"`
// KubeconfigURL is the URL of the cluster public kubeconfig file.
KubeconfigURL string `yaml:"kubeconfigURL"`
// Platform is the host platform (e.g. `ec2`)
Platform string `yaml:"platform"`
// LocalIPv4 is the IPv4 address associated to the main network interface.
// In presence of NAT, this will be a private IP.
LocalIPv4 net.IP `yaml:"localIPv4"`
// PublicIPv4 is the public IPv4 address of the host.
// This address must be reachable from the Internet.
PublicIPv4 net.IP `yaml:"publicIPv4"`
// VPNIPv4 is the private IPv4 address of the VPN mesh interface.
// This address must be unique among the node in the cluster.
VPNIPv4 *utils.IPWithMask `yaml:"vpnIPv4"`
// VPNIPv4 is the private IPv6 address of the VPN mesh interface.
// This address must be unique among the node in the cluster.
VPNIPv6 *utils.IPWithMask `yaml:"vpnIPv6"`
// VPNPrivateKey is the WireGuard private key of the VPN mesh interface.
VPNPrivateKey string `yaml:"vpnPrivateKey"`
// VPNListenPort is the WireGuard port of the VPN mesh interface.
VPNListenPort int `yaml:"vpnListenPort"`
}
// load the EdgeNet configuration from the specified file.
func (c *edgenetConfig) load(file string) {
buf, err := os.ReadFile(file)
if os.IsNotExist(err) {
return
}
check(err)
check(yaml.Unmarshal(buf, c))
}
// save the EdgeNet configuration to the specified filed.
func (c edgenetConfig) save(file string) {
buf, err := yaml.Marshal(&c)
check(err)
check(os.WriteFile(file, buf, 0644))
}
// getHostnameRoot returns the deterministic component of the hostname.
func getHostnameRoot(platform string) string {
switch platform {
case platforms.Azure:
region := platforms.AzureGetMetadata("compute/location")
return fmt.Sprintf("az-%s", region)
case platforms.EC2:
region := platforms.EC2GetMetadata("placement/availability-zone")
return fmt.Sprintf("aws-%s", region)
case platforms.GENI:
// TODO: From slice name?
geoIP := network.GeoIP()
return strings.ToLower(fmt.Sprintf("geni-%s-%s", geoIP.CountryCode, geoIP.Region))
case platforms.GCP:
region := platforms.GCPGetMetadata("instance/zone")
region = strings.Split(region, "/")[3]
return fmt.Sprintf("gcp-%s", region)
case platforms.NUC:
geoIP := network.GeoIP()
return strings.ToLower(fmt.Sprintf("nuc-%s-%s", geoIP.CountryCode, geoIP.Region))
case platforms.SCW:
meta := platforms.SCWGetMetadata()
return fmt.Sprintf("scw-%s", meta.Location.ZoneID)
default:
geoIP := network.GeoIP()
return strings.ToLower(fmt.Sprintf("%s-%s", geoIP.CountryCode, geoIP.Region))
}
}
// getIPv4 returns the local and public IPv4 addresses associated to the host.
func getIPv4(platform string) (net.IP, net.IP) {
switch platform {
case platforms.Azure:
localIP := net.ParseIP(platforms.AzureGetMetadata("network/interface/0/ipv4/ipAddress/0/privateIpAddress"))
publicIP := network.PublicIPv4()
return localIP, publicIP
case platforms.EC2:
localIP := net.ParseIP(platforms.EC2GetMetadata("local-ipv4"))
publicIP := network.PublicIPv4()
return localIP, publicIP
case platforms.GCP:
localIP := net.ParseIP(platforms.GCPGetMetadata("instance/network-interfaces/0/ip"))
publicIP := network.PublicIPv4()
return localIP, publicIP
case platforms.SCW:
meta := platforms.SCWGetMetadata()
localIP := net.ParseIP(meta.PrivateIP)
publicIP := network.PublicIPv4()
return localIP, publicIP
default:
localIP := network.LocalIPv4()
publicIP := network.PublicIPv4()
return localIP, publicIP
}
}
func main() {
log.Println("step=ensure-dir")
check(os.MkdirAll(filepath.Dir(edgenetConfigFile), 0755))
log.Println("step=load-config")
config := edgenetConfig{}
config.load(edgenetConfigFile)
log.Printf("config=%+v\n", config)
if config.Platform == "" {
log.Println("step=detect-platform")
config.Platform = platforms.Detect()
}
if config.HostnameRoot == "" {
log.Println("step=get-hostname-root")
config.HostnameRoot = getHostnameRoot(config.Platform)
}
if config.HostnameSuffix == "" {
log.Println("step=get-hostname-suffix")
config.HostnameSuffix = randstr.Hex(2)
}
if config.LocalIPv4 == nil || config.PublicIPv4 == nil {
log.Println("step=get-ip")
config.LocalIPv4, config.PublicIPv4 = getIPv4(config.Platform)
}
// https://github.com/EdgeNet-project/edgenet/issues/156
if config.VPNIPv4 == nil || config.VPNIPv6 == nil {
log.Println("step=get-vpn-ip")
_, vpnNetworkV4, err := net.ParseCIDR(defaultVPNNetworkV4)
check(err)
_, vpnNetworkV6, err := net.ParseCIDR(defaultVPNNetworkV6)
check(err)
config.VPNIPv4, config.VPNIPv6 = cluster.FindVPNIPs(config.KubeconfigURL, *vpnNetworkV4, *vpnNetworkV6)
}
if config.VPNPrivateKey == "" {
log.Println("step=get-vpn-private-key")
key, err := wgtypes.GeneratePrivateKey()
check(err)
config.VPNPrivateKey = key.String()
}
if config.VPNListenPort == 0 {
log.Println("step=get-vpn-listen-port")
rand.Seed(time.Now().UnixNano())
config.VPNListenPort = rand.Intn(32768) + 32768
}
log.Println("step=save-config")
log.Printf("config=%+v\n", config)
config.save(edgenetConfigFile)
log.Println("step=set-hostname")
hostname := fmt.Sprintf("%s-%s.edge-net.io", config.HostnameRoot, config.HostnameSuffix)
network.SetHostname(hostname)
// https://github.com/EdgeNet-project/edgenet/issues/156
if config.VPNIPv4 != nil && config.VPNIPv6 != nil {
log.Println("step=configure-vpn")
network.InitializeVPN(vpnLinkName, config.VPNPrivateKey, config.VPNListenPort)
network.AssignVPNIP(vpnLinkName, *config.VPNIPv4, *config.VPNIPv6)
privateKey, err := wgtypes.ParseKey(config.VPNPrivateKey)
check(err)
cluster.CreateVPNPeer(config.KubeconfigURL, hostname, config.PublicIPv4, config.VPNIPv4.IP, config.VPNIPv6.IP, config.VPNListenPort, privateKey.PublicKey().String())
// Pre-establish the tunnels before the VPNPeer controller gets started.
peers := cluster.ListVPNPeer(config.KubeconfigURL)
for _, peer := range peers {
network.AddPeer(vpnLinkName, peer)
}
}
var nodeIP net.IP
if config.LocalIPv4.Equal(config.PublicIPv4) {
nodeIP = config.PublicIPv4
} else {
nodeIP = config.VPNIPv4.IP
}
log.Println("step=set-node-ip")
network.SetKubeletNodeIP(kubeletEnvFileDebian, nodeIP)
network.SetKubeletNodeIP(kubeletEnvFileRedHat, nodeIP)
log.Println("step=join-cluster")
cluster.Join(config.KubeconfigURL, hostname, nodeIP)
}