Skip to content

Commit

Permalink
fix: scaleway metadata
Browse files Browse the repository at this point in the history
Support legacy-style region values.
Disable DHCPv4 for external interface when public IPv4 is disabled.

Signed-off-by: Serge Logvinov <[email protected]>
Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
sergelogvinov authored and smira committed Oct 7, 2024
1 parent 7acadc0 commit 77a4a4a
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"log"
"net/netip"
"strconv"
"strings"

"github.com/cosi-project/runtime/pkg/state"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/siderolabs/go-procfs/procfs"

"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (s *Scaleway) ParseMetadata(metadata *instance.Metadata) (*runtime.Platform

var publicIPs []string

if metadata.PublicIP.Address != "" {
if metadata.PublicIP.Address != "" && metadata.PublicIP.Family == "inet" {
publicIPs = append(publicIPs, metadata.PublicIP.Address)
}

Expand All @@ -74,21 +74,23 @@ func (s *Scaleway) ParseMetadata(metadata *instance.Metadata) (*runtime.Platform
Protocol: nethelpers.ProtocolStatic,
Type: nethelpers.TypeUnicast,
Family: nethelpers.FamilyInet4,
Priority: network.DefaultRouteMetric,
Priority: 4 * network.DefaultRouteMetric,
}

route.Normalize()
networkConfig.Routes = []network.RouteSpecSpec{route}

networkConfig.Operators = append(networkConfig.Operators, network.OperatorSpecSpec{
Operator: network.OperatorDHCP4,
LinkName: "eth0",
RequireUp: true,
DHCP4: network.DHCP4OperatorSpec{
RouteMetric: network.DefaultRouteMetric,
},
ConfigLayer: network.ConfigPlatform,
})
if len(metadata.PublicIpsV4) > 0 {
networkConfig.Operators = append(networkConfig.Operators, network.OperatorSpecSpec{
Operator: network.OperatorDHCP4,
LinkName: "eth0",
RequireUp: true,
DHCP4: network.DHCP4OperatorSpec{
RouteMetric: network.DefaultRouteMetric,
},
ConfigLayer: network.ConfigPlatform,
})
}

if metadata.IPv6.Address != "" || len(metadata.PublicIpsV6) > 0 {
address := metadata.IPv6.Address
Expand Down Expand Up @@ -146,25 +148,30 @@ func (s *Scaleway) ParseMetadata(metadata *instance.Metadata) (*runtime.Platform
networkConfig.Routes = append(networkConfig.Routes, route)
}

zoneParts := strings.Split(metadata.Location.ZoneID, "-")
if len(zoneParts) > 2 {
zoneParts = zoneParts[:2]
}

for _, ipStr := range publicIPs {
if ip, err := netip.ParseAddr(ipStr); err == nil {
networkConfig.ExternalIPs = append(networkConfig.ExternalIPs, ip)
}
}

zone, err := scw.ParseZone(metadata.Location.ZoneID)
if err != nil {
return nil, err
}

region, err := zone.Region()
if err != nil {
return nil, err
}

networkConfig.Metadata = &runtimeres.PlatformMetadataSpec{
Platform: s.Name(),
Hostname: metadata.Hostname,
Region: strings.Join(zoneParts, "-"),
Zone: metadata.Location.ZoneID,
Region: region.String(),
Zone: zone.String(),
InstanceType: metadata.CommercialType,
InstanceID: metadata.ID,
ProviderID: fmt.Sprintf("scaleway://instance/%s/%s", metadata.Location.ZoneID, metadata.ID),
ProviderID: fmt.Sprintf("scaleway://instance/%s/%s", zone.String(), metadata.ID),
}

return networkConfig, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ var rawMetadataV1 []byte
//go:embed testdata/metadata-v2.json
var rawMetadataV2 []byte

//go:embed testdata/metadata-v3.json
var rawMetadataV3 []byte

//go:embed testdata/expected-v1.yaml
var expectedNetworkConfigV1 string

//go:embed testdata/expected-v2.yaml
var expectedNetworkConfigV2 string

//go:embed testdata/expected-v3.yaml
var expectedNetworkConfigV3 string

func TestParseMetadata(t *testing.T) {
p := &scaleway.Scaleway{}

Expand All @@ -47,6 +53,11 @@ func TestParseMetadata(t *testing.T) {
raw: rawMetadataV2,
expected: expectedNetworkConfigV2,
},
{
name: "V3",
raw: rawMetadataV3,
expected: expectedNetworkConfigV3,
},
} {
t.Run(tt.name, func(t *testing.T) {
var metadata instance.Metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ routes:
gateway: ""
outLinkName: eth0
table: main
priority: 1024
priority: 4096
scope: link
type: unicast
flags: ""
Expand Down Expand Up @@ -57,8 +57,8 @@ externalIPs:
metadata:
platform: scaleway
hostname: scw-talos
region: zone-name
zone: zone-name-1
region: fr-par
zone: fr-par-1
instanceType: DEV1-S
instanceId: 11111111-1111-1111-1111-111111111111
providerId: scaleway://instance/zone-name-1/11111111-1111-1111-1111-111111111111
providerId: scaleway://instance/fr-par-1/11111111-1111-1111-1111-111111111111
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ routes:
gateway: ""
outLinkName: eth0
table: main
priority: 1024
priority: 4096
scope: link
type: unicast
flags: ""
Expand Down Expand Up @@ -57,8 +57,8 @@ externalIPs:
metadata:
platform: scaleway
hostname: scw-talos
region: zone-name
zone: zone-name-1
region: fr-par
zone: fr-par-2
instanceType: DEV1-S
instanceId: 11111111-1111-1111-1111-111111111111
providerId: scaleway://instance/zone-name-1/11111111-1111-1111-1111-111111111111
providerId: scaleway://instance/fr-par-2/11111111-1111-1111-1111-111111111111
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
addresses:
- address: 2001:111:222:3333::1/64
linkName: eth0
family: inet6
scope: global
flags: permanent
layer: platform
links:
- name: eth0
logical: false
up: true
mtu: 0
kind: ""
type: netrom
layer: platform
routes:
- family: inet4
dst: 169.254.42.42/32
src: ""
gateway: ""
outLinkName: eth0
table: main
priority: 4096
scope: link
type: unicast
flags: ""
protocol: static
layer: platform
- family: inet6
dst: ""
src: ""
gateway: fe80::dc00:ff:fe12:3456
outLinkName: eth0
table: main
priority: 2048
scope: global
type: unicast
flags: ""
protocol: static
layer: platform
hostnames:
- hostname: scw-talos
domainname: ""
layer: platform
resolvers: []
timeServers: []
operators: []
externalIPs:
- 2001:111:222:3333::1
metadata:
platform: scaleway
hostname: scw-talos
region: nl-ams
zone: nl-ams-1
instanceType: DEV1-S
instanceId: 11111111-1111-1111-1111-111111111111
providerId: scaleway://instance/nl-ams-1/11111111-1111-1111-1111-111111111111
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@
"public_ip": {
"id": "11111111-1111-1111-1111-111111111111",
"address": "11.22.222.222",
"dynamic": false
"dynamic": false,
"family": "inet"
},
"public_ips_v4": [
{
"address": "11.22.222.222",
"dynamic": false,
"family": "inet",
"gateway": null,
"id": "11111111-1111-1111-1111-111111111111",
"ipam_id": null,
"netmask": "32",
"provisioning_mode": "dhcp",
"state": "detached",
"tags": []
}
],
"public_ips_v6": [],
"private_ip": "10.00.222.222",
"ipv6": {
"address": "2001:111:222:3333::1",
"gateway": "2001:111:222:3333::",
"netmask": "64"
},
"location": {
"zone_id": "zone-name-1"
"zone_id": "fr-par-1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"public_ip": {
"id": "11111111-1111-1111-1111-111111111111",
"address": "11.22.222.222",
"dynamic": false
"dynamic": false,
"family": "inet"
},
"public_ips_v4": [
{
Expand All @@ -29,6 +30,6 @@
}
],
"location": {
"zone_id": "zone-name-1"
"zone_id": "fr-par-2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "11111111-1111-1111-1111-111111111111",
"name": "scw-talos",
"commercial_type": "DEV1-S",
"hostname": "scw-talos",
"tags": [],
"state_detail": "booted",
"public_ip": {
"id": "11111111-1111-1111-1111-111111111111",
"address": "2001:111:222:3333::1",
"dynamic": false
},
"public_ips_v4": [],
"public_ips_v6": [
{
"address": "2001:111:222:3333::1",
"dynamic": false,
"family": "inet6",
"gateway": "fe80::dc00:ff:fe12:3456",
"netmask": "64"
}
],
"location": {
"zone_id": "ams1"
}
}

0 comments on commit 77a4a4a

Please sign in to comment.