-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cloud-init for vultr.com * ipv4/v6 support * set static IPs for private interface Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev> Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
- Loading branch information
1 parent
0ff4c7c
commit 19a8ae9
Showing
9 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
internal/app/machined/pkg/runtime/v1alpha1/platform/vultr/vultr.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package vultr | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"github.com/talos-systems/go-procfs/procfs" | ||
"github.com/vultr/metadata" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime" | ||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/errors" | ||
"github.com/talos-systems/talos/pkg/download" | ||
"github.com/talos-systems/talos/pkg/machinery/config" | ||
"github.com/talos-systems/talos/pkg/machinery/config/configloader" | ||
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1" | ||
) | ||
|
||
const ( | ||
// VultrMetadataEndpoint is the local Vultr endpoint fot the instance metadata. | ||
VultrMetadataEndpoint = "http://169.254.169.254/v1.json" | ||
// VultrExternalIPEndpoint is the local Vultr endpoint for the external IP. | ||
VultrExternalIPEndpoint = "http://169.254.169.254/latest/meta-data/public-ipv4" | ||
// VultrHostnameEndpoint is the local Vultr endpoint for the hostname. | ||
VultrHostnameEndpoint = "http://169.254.169.254/latest/meta-data/hostname" | ||
// VultrUserDataEndpoint is the local Vultr endpoint for the config. | ||
VultrUserDataEndpoint = "http://169.254.169.254/latest/user-data" | ||
) | ||
|
||
// Vultr is the concrete type that implements the runtime.Platform interface. | ||
type Vultr struct{} | ||
|
||
// Name implements the runtime.Platform interface. | ||
func (v *Vultr) Name() string { | ||
return "vultr" | ||
} | ||
|
||
// ConfigurationNetwork implements the network configuration interface. | ||
func (v *Vultr) ConfigurationNetwork(metadataConfig []byte, confProvider config.Provider) (config.Provider, error) { | ||
var machineConfig *v1alpha1.Config | ||
|
||
machineConfig, ok := confProvider.(*v1alpha1.Config) | ||
if !ok { | ||
return nil, fmt.Errorf("unable to determine machine config type") | ||
} | ||
|
||
meta := &metadata.MetaData{} | ||
if err := json.Unmarshal(metadataConfig, meta); err != nil { | ||
return nil, err | ||
} | ||
|
||
if machineConfig.MachineConfig == nil { | ||
machineConfig.MachineConfig = &v1alpha1.MachineConfig{} | ||
} | ||
|
||
if machineConfig.MachineConfig.MachineNetwork == nil { | ||
machineConfig.MachineConfig.MachineNetwork = &v1alpha1.NetworkConfig{} | ||
} | ||
|
||
if machineConfig.MachineConfig.MachineNetwork.NetworkInterfaces == nil { | ||
for i, addr := range meta.Interfaces { | ||
iface := &v1alpha1.Device{ | ||
DeviceInterface: fmt.Sprintf("eth%d", i), | ||
} | ||
|
||
if addr.IPv4.Address != "" { | ||
iface.DeviceDHCP = true | ||
} | ||
|
||
if addr.NetworkType == "private" { | ||
iface.DeviceMTU = 1450 | ||
|
||
if addr.IPv4.Address != "" { | ||
mask, _ := net.IPMask(net.ParseIP(addr.IPv4.Netmask).To4()).Size() | ||
|
||
iface.DeviceDHCP = false | ||
iface.DeviceAddresses = append(iface.DeviceAddresses, | ||
fmt.Sprintf("%s/%d", addr.IPv4.Address, mask), | ||
) | ||
} | ||
} | ||
|
||
machineConfig.MachineConfig.MachineNetwork.NetworkInterfaces = append(machineConfig.MachineConfig.MachineNetwork.NetworkInterfaces, iface) | ||
} | ||
} | ||
|
||
return confProvider, nil | ||
} | ||
|
||
// Configuration implements the runtime.Platform interface. | ||
func (v *Vultr) Configuration(ctx context.Context) ([]byte, error) { | ||
log.Printf("fetching Vultr instance config from: %q ", VultrMetadataEndpoint) | ||
|
||
metaConfigDl, err := download.Download(ctx, VultrMetadataEndpoint) | ||
if err != nil { | ||
return nil, errors.ErrNoConfigSource | ||
} | ||
|
||
log.Printf("fetching machine config from: %q", VultrUserDataEndpoint) | ||
|
||
machineConfigDl, err := download.Download(ctx, VultrUserDataEndpoint, | ||
download.WithErrorOnNotFound(errors.ErrNoConfigSource), | ||
download.WithErrorOnEmptyResponse(errors.ErrNoConfigSource)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
confProvider, err := configloader.NewFromBytes(machineConfigDl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
confProvider, err = v.ConfigurationNetwork(metaConfigDl, confProvider) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return confProvider.Bytes() | ||
} | ||
|
||
// Mode implements the runtime.Platform interface. | ||
func (v *Vultr) Mode() runtime.Mode { | ||
return runtime.ModeCloud | ||
} | ||
|
||
// Hostname implements the runtime.Platform interface. | ||
func (v *Vultr) Hostname(ctx context.Context) (hostname []byte, err error) { | ||
log.Printf("fetching hostname from: %q", VultrHostnameEndpoint) | ||
|
||
hostname, err = download.Download(ctx, VultrHostnameEndpoint, | ||
download.WithErrorOnNotFound(errors.ErrNoHostname), | ||
download.WithErrorOnEmptyResponse(errors.ErrNoHostname)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return hostname, nil | ||
} | ||
|
||
// ExternalIPs implements the runtime.Platform interface. | ||
func (v *Vultr) ExternalIPs(ctx context.Context) (addrs []net.IP, err error) { | ||
log.Printf("fetching external IP from: %q", VultrExternalIPEndpoint) | ||
|
||
exIP, err := download.Download(ctx, VultrExternalIPEndpoint, | ||
download.WithErrorOnNotFound(errors.ErrNoExternalIPs), | ||
download.WithErrorOnEmptyResponse(errors.ErrNoExternalIPs)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if addr := net.ParseIP(string(exIP)); addr != nil { | ||
addrs = append(addrs, addr) | ||
} | ||
|
||
return addrs, err | ||
} | ||
|
||
// KernelArgs implements the runtime.Platform interface. | ||
func (v *Vultr) KernelArgs() procfs.Parameters { | ||
return []*procfs.Parameter{} | ||
} |
57 changes: 57 additions & 0 deletions
57
internal/app/machined/pkg/runtime/v1alpha1/platform/vultr/vultr_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package vultr_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/vultr" | ||
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1" | ||
) | ||
|
||
type ConfigSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *ConfigSuite) TestNetworkConfig() { | ||
//nolint:lll | ||
cfg := []byte(`{ | ||
"bgp":{"ipv4":{"my-address":"","my-asn":"","peer-address":"","peer-asn":""},"ipv6":{"my-address":"","my-asn":"","peer-address":"","peer-asn":""}},"hostname":"talos","instance-v2-id":"91b07056-af72-4551-b15b-d57d34071be9","instanceid":"50190000","interfaces":[{"ipv4":{"additional":[],"address":"95.111.222.111","gateway":"95.111.222.1","netmask":"255.255.254.0"},"ipv6":{"additional":[],"address":"2001:19f0:5001:2095:1111:2222:3333:4444","network":"2001:19f0:5001:2095::","prefix":"64"},"mac":"56:00:03:89:53:e0","network-type":"public"},{"ipv4":{"additional":[],"address":"10.7.96.3","gateway":"","netmask":"255.255.240.0"},"ipv6":{"additional":[],"network":"","prefix":""},"mac":"5a:00:03:89:53:e0","network-type":"private","network-v2-id":"dadc2b30-0b55-4fa1-8c29-f67215bd5ac4","networkid":"net6126811851cd7"}],"public-keys":["ssh-ed25519"],"region":{"regioncode":"AMS"},"user-defined":[] | ||
}`) | ||
|
||
p := &vultr.Vultr{} | ||
|
||
defaultMachineConfig := &v1alpha1.Config{} | ||
|
||
machineConfig := &v1alpha1.Config{ | ||
MachineConfig: &v1alpha1.MachineConfig{ | ||
MachineNetwork: &v1alpha1.NetworkConfig{ | ||
NetworkInterfaces: []*v1alpha1.Device{ | ||
{ | ||
DeviceInterface: "eth0", | ||
DeviceDHCP: true, | ||
}, | ||
{ | ||
DeviceInterface: "eth1", | ||
DeviceAddresses: []string{"10.7.96.3/20"}, | ||
DeviceDHCP: false, | ||
DeviceMTU: 1450, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
result, err := p.ConfigurationNetwork(cfg, defaultMachineConfig) | ||
|
||
suite.Require().NoError(err) | ||
suite.Assert().Equal(machineConfig, result) | ||
} | ||
|
||
func TestConfigSuite(t *testing.T) { | ||
suite.Run(t, new(ConfigSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: "Vultr" | ||
description: "Creating a cluster via the CLI (vultr-cli) on Vultr.com." | ||
--- | ||
|
||
Talos is known to work on Vultr.com; however, it is currently undocumented. |