From 77a4a4adc7232b4382f2a530f4056a1fff6c50b4 Mon Sep 17 00:00:00 2001 From: Serge Logvinov Date: Mon, 7 Oct 2024 12:12:07 +0000 Subject: [PATCH] fix: scaleway metadata Support legacy-style region values. Disable DHCPv4 for external interface when public IPv4 is disabled. Signed-off-by: Serge Logvinov Signed-off-by: Andrey Smirnov --- .../v1alpha1/platform/scaleway/scaleway.go | 47 ++++++++------- .../platform/scaleway/scaleway_test.go | 11 ++++ .../scaleway/testdata/expected-v1.yaml | 8 +-- .../scaleway/testdata/expected-v2.yaml | 8 +-- .../scaleway/testdata/expected-v3.yaml | 57 +++++++++++++++++++ .../scaleway/testdata/metadata-v1.json | 20 ++++++- .../scaleway/testdata/metadata-v2.json | 5 +- .../scaleway/testdata/metadata-v3.json | 26 +++++++++ 8 files changed, 150 insertions(+), 32 deletions(-) create mode 100644 internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v3.yaml create mode 100644 internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v3.json diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway.go b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway.go index f0e683e667..947031345b 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway.go +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway.go @@ -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" @@ -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) } @@ -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 @@ -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 diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway_test.go b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway_test.go index 71184fa8b5..311733369a 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway_test.go +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/scaleway_test.go @@ -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{} @@ -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 diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v1.yaml b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v1.yaml index 61def78c01..344e45c105 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v1.yaml +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v1.yaml @@ -20,7 +20,7 @@ routes: gateway: "" outLinkName: eth0 table: main - priority: 1024 + priority: 4096 scope: link type: unicast flags: "" @@ -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 diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v2.yaml b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v2.yaml index 9940f8460f..8a3a595b82 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v2.yaml +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v2.yaml @@ -20,7 +20,7 @@ routes: gateway: "" outLinkName: eth0 table: main - priority: 1024 + priority: 4096 scope: link type: unicast flags: "" @@ -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 diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v3.yaml b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v3.yaml new file mode 100644 index 0000000000..309ae2418b --- /dev/null +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/expected-v3.yaml @@ -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 diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v1.json b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v1.json index b7bc97ad89..96f520a2b3 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v1.json +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v1.json @@ -8,8 +8,24 @@ "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", @@ -17,6 +33,6 @@ "netmask": "64" }, "location": { - "zone_id": "zone-name-1" + "zone_id": "fr-par-1" } } \ No newline at end of file diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v2.json b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v2.json index c717aad1cd..8aecc67477 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v2.json +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v2.json @@ -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": [ { @@ -29,6 +30,6 @@ } ], "location": { - "zone_id": "zone-name-1" + "zone_id": "fr-par-2" } } \ No newline at end of file diff --git a/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v3.json b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v3.json new file mode 100644 index 0000000000..f439cd3e7e --- /dev/null +++ b/internal/app/machined/pkg/runtime/v1alpha1/platform/scaleway/testdata/metadata-v3.json @@ -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" + } +} \ No newline at end of file