Skip to content

Commit

Permalink
feat: add support for CI Network config in nocloud
Browse files Browse the repository at this point in the history
Fixes #9351

Signed-off-by: ekarlso <[email protected]>
Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
ekarlso authored and smira committed Sep 30, 2024
1 parent 10f2539 commit aa846cc
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const (
mnt = "/mnt"
)

// NetworkCloudInitConfig wraps nocloud network config to match cloud-init format.
type NetworkCloudInitConfig struct {
Config NetworkConfig `yaml:"network"`
}

// NetworkConfig holds network-config info.
type NetworkConfig struct {
Version int `yaml:"version"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"

"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/maps"
"github.com/siderolabs/go-procfs/procfs"
yaml "gopkg.in/yaml.v3"

Expand Down Expand Up @@ -121,14 +122,18 @@ func (n *Nocloud) NetworkConfiguration(ctx context.Context, st state.State, ch c
return nil
}

var unmarshalledNetworkConfig NetworkConfig
var unmarshalledNetworkConfig *NetworkConfig

if metadataNetworkConfigDl != nil {
if err = yaml.Unmarshal(metadataNetworkConfigDl, &unmarshalledNetworkConfig); err != nil {
nc, err := DecodeNetworkConfig(metadataNetworkConfigDl)
if err != nil {
return err
}

unmarshalledNetworkConfig = nc
}

networkConfig, err := n.ParseMetadata(&unmarshalledNetworkConfig, st, metadata)
networkConfig, err := n.ParseMetadata(unmarshalledNetworkConfig, st, metadata)
if err != nil {
return err
}
Expand All @@ -141,3 +146,38 @@ func (n *Nocloud) NetworkConfiguration(ctx context.Context, st state.State, ch c

return nil
}

// DecodeNetworkConfig decodes the network configuration guessing the format from the content.
func DecodeNetworkConfig(content []byte) (*NetworkConfig, error) {
var decoded map[string]any

err := yaml.Unmarshal(content, &decoded)
if err != nil {
return nil, err
}

if _, ok := decoded["network"]; ok {
var ciNetworkConfig NetworkCloudInitConfig

err = yaml.Unmarshal(content, &ciNetworkConfig)
if err != nil {
return nil, err
}

return &ciNetworkConfig.Config, nil
}

// If it is not plain *v2 cloud-init* config then we attempt to decode *nocloud*
if _, ok := decoded["version"]; ok {
var nc NetworkConfig

err = yaml.Unmarshal(content, &nc)
if err != nil {
return nil, err
}

return &nc, nil
}

return nil, fmt.Errorf("failed to decode network configuration, keys: %v", maps.Keys(decoded))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package nocloud_test
import (
"context"
_ "embed"
"fmt"
"testing"

"github.com/cosi-project/runtime/pkg/state"
Expand All @@ -25,8 +24,11 @@ import (
//go:embed testdata/metadata-v1.yaml
var rawMetadataV1 []byte

//go:embed testdata/metadata-v2.yaml
var rawMetadataV2 []byte
//go:embed testdata/metadata-v2-nocloud.yaml
var rawMetadataV2Nocloud []byte

//go:embed testdata/metadata-v2-cloud-init.yaml
var rawMetadataV2CloudInit []byte

//go:embed testdata/expected-v1.yaml
var expectedNetworkConfigV1 string
Expand All @@ -46,8 +48,13 @@ func TestParseMetadata(t *testing.T) {
expected: expectedNetworkConfigV1,
},
{
name: "V2",
raw: rawMetadataV2,
name: "V2-nocloud",
raw: rawMetadataV2Nocloud,
expected: expectedNetworkConfigV2,
},
{
name: "V2-cloud-init",
raw: rawMetadataV2CloudInit,
expected: expectedNetworkConfigV2,
},
} {
Expand All @@ -74,9 +81,8 @@ func TestParseMetadata(t *testing.T) {
eth2.TypedSpec().Kind = ""
require.NoError(t, st.Create(context.TODO(), eth2))

var m nocloud.NetworkConfig

require.NoError(t, yaml.Unmarshal(tt.raw, &m))
m, err := nocloud.DecodeNetworkConfig(tt.raw)
require.NoError(t, err)

mc := nocloud.MetadataConfig{
Hostname: "talos.fqdn",
Expand All @@ -88,18 +94,16 @@ func TestParseMetadata(t *testing.T) {
InstanceID: "0",
}

networkConfig, err := n.ParseMetadata(&m, st, &mc)
networkConfig, err := n.ParseMetadata(m, st, &mc)
require.NoError(t, err)
networkConfig2, err := n.ParseMetadata(&m, st, &mc2)
networkConfig2, err := n.ParseMetadata(m, st, &mc2)
require.NoError(t, err)

marshaled, err := yaml.Marshal(networkConfig)
require.NoError(t, err)
marshaled2, err := yaml.Marshal(networkConfig2)
require.NoError(t, err)

fmt.Print(string(marshaled))

assert.Equal(t, tt.expected, string(marshaled))
assert.Equal(t, tt.expected, string(marshaled2))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
network:
version: 2
ethernets:
eth0:
match:
macaddress: "00:20:6e:1f:f9:a8"
dhcp4: true
addresses:
- 192.168.14.2/24
- 2001:1::1/64
gateway4: 192.168.14.1
gateway6: 2001:1::2
nameservers:
search: [foo.local, bar.local]
addresses: [8.8.8.8]

ext1:
match:
macaddress: 68:05:ca:b8:f1:f8
ext2:
match:
macaddress: 68:05:ca:b8:f1:f9

bonds:
bond0:
interfaces:
- ext1
- ext2
macaddress: e4:3d:1a:4d:6a:28
mtu: 1500
parameters:
mode: 802.3ad
mii-monitor-interval: 100
down-delay: 200
up-delay: 200
lacp-rate: fast
transmit-hash-policy: layer3+4
addresses:
- 10.10.4.140/29
nameservers:
addresses:
- 1.1.1.1
- 2.2.2.2
routes:
- to: 10.0.0.0/8
via: 10.10.4.147
- to: 192.168.0.0/16
via: 10.10.4.147
- to: 188.42.208.0/21
via: 10.10.4.147
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: 2
ethernets:
eth0:
match:
macaddress: "00:20:6e:1f:f9:a8"
dhcp4: true
addresses:
- 192.168.14.2/24
- 2001:1::1/64
gateway4: 192.168.14.1
gateway6: 2001:1::2
nameservers:
search: [foo.local, bar.local]
addresses: [8.8.8.8]

ext1:
match:
macaddress: 68:05:ca:b8:f1:f8
ext2:
match:
macaddress: 68:05:ca:b8:f1:f9

bonds:
bond0:
interfaces:
- ext1
- ext2
macaddress: e4:3d:1a:4d:6a:28
mtu: 1500
parameters:
mode: 802.3ad
mii-monitor-interval: 100
down-delay: 200
up-delay: 200
lacp-rate: fast
transmit-hash-policy: layer3+4
addresses:
- 10.10.4.140/29
nameservers:
addresses:
- 1.1.1.1
- 2.2.2.2
routes:
- to: 10.0.0.0/8
via: 10.10.4.147
- to: 192.168.0.0/16
via: 10.10.4.147
- to: 188.42.208.0/21
via: 10.10.4.147

0 comments on commit aa846cc

Please sign in to comment.