|
| 1 | +/* |
| 2 | +Copyright 2021 Contributors to the EdgeNet project. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "github.com/EdgeNet-project/node/pkg/cluster" |
| 22 | + "github.com/EdgeNet-project/node/pkg/network" |
| 23 | + "github.com/EdgeNet-project/node/pkg/platforms" |
| 24 | + "github.com/thanhpk/randstr" |
| 25 | + "gopkg.in/yaml.v3" |
| 26 | + "log" |
| 27 | + "net" |
| 28 | + "os" |
| 29 | + "path/filepath" |
| 30 | + "strings" |
| 31 | +) |
| 32 | + |
| 33 | +const defaultKubeconfigURL = "https://raw.githubusercontent.com/EdgeNet-project/edgenet/master/configs/public.cfg" |
| 34 | +const edgenetConfigFile = "/opt/edgenet/config.yaml" |
| 35 | +const kubeletEnvFile = "/etc/default/kubelet" |
| 36 | + |
| 37 | +func check(err error) { |
| 38 | + if err != nil { |
| 39 | + log.Panic(err) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type edgenetConfig struct { |
| 44 | + // HostnameRoot is the deterministic component of the hostname (e.g. `aws-eu-west-1a`). |
| 45 | + HostnameRoot string `yaml:"hostnameRoot"` |
| 46 | + // HostnameSuffix is the random component of the hostname (e.g. `3fe8`). |
| 47 | + HostnameSuffix string `yaml:"hostnameSuffix"` |
| 48 | + // KubeconfigURL is the URL of the cluster public kubeconfig file. |
| 49 | + KubeconfigURL string `yaml:"kubeconfigURL"` |
| 50 | + // Platform is the host platform (e.g. `ec2`) |
| 51 | + Platform string `yaml:"platform"` |
| 52 | + // LocalIPv4 is the IPv4 address associated to the main network interface. |
| 53 | + // In presence of NAT, this will be a private IP. |
| 54 | + LocalIPv4 net.IP `yaml:"localIPv4"` |
| 55 | + // PublicIPv4 is the public IPv4 address of the host. |
| 56 | + // This address must be reachable from the Internet. |
| 57 | + PublicIPv4 net.IP `yaml:"publicIPv4"` |
| 58 | +} |
| 59 | + |
| 60 | +// load the EdgeNet configuration from the specified file. |
| 61 | +func (c *edgenetConfig) load(file string) { |
| 62 | + buf, err := os.ReadFile(file) |
| 63 | + if os.IsNotExist(err) { |
| 64 | + return |
| 65 | + } |
| 66 | + check(err) |
| 67 | + check(yaml.Unmarshal(buf, c)) |
| 68 | +} |
| 69 | + |
| 70 | +// save the EdgeNet configuration to the specified filed. |
| 71 | +func (c edgenetConfig) save(file string) { |
| 72 | + buf, err := yaml.Marshal(&c) |
| 73 | + check(err) |
| 74 | + check(os.WriteFile(file, buf, 0644)) |
| 75 | +} |
| 76 | + |
| 77 | +// getHostnameRoot returns the deterministic component of the hostname. |
| 78 | +func getHostnameRoot(platform string) string { |
| 79 | + switch platform { |
| 80 | + case platforms.Azure: |
| 81 | + region := platforms.AzureGetMetadata("compute/location") |
| 82 | + return fmt.Sprintf("az-%s", region) |
| 83 | + case platforms.EC2: |
| 84 | + region := platforms.EC2GetMetadata("placement/availability-zone") |
| 85 | + return fmt.Sprintf("aws-%s", region) |
| 86 | + case platforms.GENI: |
| 87 | + // TODO: From slice name? |
| 88 | + geoIP := network.GeoIP() |
| 89 | + return strings.ToLower(fmt.Sprintf("geni-%s-%s", geoIP.CountryCode, geoIP.RegionCode)) |
| 90 | + case platforms.GCP: |
| 91 | + region := platforms.GCPGetMetadata("zone") |
| 92 | + region = strings.Split(region, "/")[3] |
| 93 | + return fmt.Sprintf("gcp-%s", region) |
| 94 | + case platforms.NUC: |
| 95 | + geoIP := network.GeoIP() |
| 96 | + return strings.ToLower(fmt.Sprintf("nuc-%s-%s", geoIP.CountryCode, geoIP.RegionCode)) |
| 97 | + case platforms.SCW: |
| 98 | + meta := platforms.SCWGetMetadata() |
| 99 | + return fmt.Sprintf("scw-%s", meta.Location.ZoneID) |
| 100 | + default: |
| 101 | + geoIP := network.GeoIP() |
| 102 | + return strings.ToLower(fmt.Sprintf("%s-%s", geoIP.CountryCode, geoIP.RegionCode)) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// getIPv4 returns the local and public IPv4 addresses associated to the host. |
| 107 | +func getIPv4(platform string) (net.IP, net.IP) { |
| 108 | + switch platform { |
| 109 | + case platforms.Azure: |
| 110 | + // The NIC public IP is not available through Azure metadata... |
| 111 | + // https://docs.microsoft.com/en-us/answers/questions/7932/public-ip-not-available-via-metadata.html |
| 112 | + localIP := net.ParseIP(platforms.AzureGetMetadata("network/interfaces/0/ipv4/ipAddress/0/privateIpAddress")) |
| 113 | + publicIP := network.PublicIPv4() |
| 114 | + return localIP, publicIP |
| 115 | + case platforms.EC2: |
| 116 | + localIP := net.ParseIP(platforms.EC2GetMetadata("local-ipv4")) |
| 117 | + publicIP := net.ParseIP(platforms.EC2GetMetadata("public-ipv4")) |
| 118 | + return localIP, publicIP |
| 119 | + case platforms.GCP: |
| 120 | + localIP := net.ParseIP(platforms.GCPGetMetadata("network-interfaces/0/ip")) |
| 121 | + publicIP := net.ParseIP(platforms.GCPGetMetadata("network-interfaces/0/access-configs/0/external-ip")) |
| 122 | + return localIP, publicIP |
| 123 | + case platforms.SCW: |
| 124 | + meta := platforms.SCWGetMetadata() |
| 125 | + localIP := net.ParseIP(meta.PrivateIP) |
| 126 | + publicIP := net.ParseIP(meta.PublicIP.Address) |
| 127 | + return localIP, publicIP |
| 128 | + default: |
| 129 | + localIP := network.LocalIPv4() |
| 130 | + publicIP := network.PublicIPv4() |
| 131 | + return localIP, publicIP |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func main() { |
| 136 | + log.Println("step=ensure-dir") |
| 137 | + check(os.MkdirAll(filepath.Dir(edgenetConfigFile), 0755)) |
| 138 | + |
| 139 | + log.Println("step=load-config") |
| 140 | + config := edgenetConfig{} |
| 141 | + config.load(edgenetConfigFile) |
| 142 | + log.Printf("config=%+v\n", config) |
| 143 | + |
| 144 | + if config.KubeconfigURL == "" { |
| 145 | + config.KubeconfigURL = defaultKubeconfigURL |
| 146 | + } |
| 147 | + |
| 148 | + if config.Platform == "" { |
| 149 | + log.Println("step=detect-platform") |
| 150 | + config.Platform = platforms.Detect() |
| 151 | + } |
| 152 | + |
| 153 | + if config.HostnameRoot == "" { |
| 154 | + log.Println("step=get-hostname-root") |
| 155 | + config.HostnameRoot = getHostnameRoot(config.Platform) |
| 156 | + } |
| 157 | + |
| 158 | + if config.HostnameSuffix == "" { |
| 159 | + log.Println("step=get-hostname-suffix") |
| 160 | + config.HostnameSuffix = randstr.Hex(2) |
| 161 | + } |
| 162 | + |
| 163 | + if config.LocalIPv4 == nil || config.PublicIPv4 == nil { |
| 164 | + log.Println("step=get-ip") |
| 165 | + config.LocalIPv4, config.PublicIPv4 = getIPv4(config.Platform) |
| 166 | + } |
| 167 | + |
| 168 | + log.Println("step=save-config") |
| 169 | + log.Printf("config=%+v\n", config) |
| 170 | + config.save(edgenetConfigFile) |
| 171 | + |
| 172 | + if !config.LocalIPv4.Equal(config.PublicIPv4) { |
| 173 | + log.Println("step=set-public-ip") |
| 174 | + network.AssignPublicIP(config.LocalIPv4, config.PublicIPv4) |
| 175 | + network.RewritePublicIP(config.LocalIPv4, config.PublicIPv4) |
| 176 | + network.SetKubeletNodeIP(kubeletEnvFile, config.PublicIPv4) |
| 177 | + } |
| 178 | + |
| 179 | + log.Println("step=set-hostname") |
| 180 | + hostname := fmt.Sprintf("%s-%s", config.HostnameRoot, config.HostnameSuffix) |
| 181 | + network.SetHostname(hostname) |
| 182 | + |
| 183 | + log.Println("step=join-cluster") |
| 184 | + cluster.Join(defaultKubeconfigURL, config.PublicIPv4, hostname) |
| 185 | +} |
0 commit comments