Skip to content

Commit

Permalink
fix: determine openstack interface IP correctly
Browse files Browse the repository at this point in the history
netaddr.Netmask changes the source ip to net clean subnet:
    10.1.2.3/24 -> 10.1.2.0/24

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
sergelogvinov authored and smira committed Feb 16, 2022
1 parent 00ccaf1 commit 5a0fd63
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
Expand Up @@ -9,6 +9,7 @@ import (
"context"
stderrors "errors"
"fmt"
"net"
"strconv"
"strings"

Expand Down Expand Up @@ -120,17 +121,14 @@ func (o *Openstack) ParseMetadata(unmarshalledMetadataConfig *MetadataConfig, un

bits, err := strconv.Atoi(ntwrk.Netmask)
if err != nil {
maskIP, err := netaddr.ParseIP(ntwrk.Netmask)
netmask, err := netaddr.ParseIP(ntwrk.Netmask)
if err != nil {
return nil, err
}

mask, _ := maskIP.MarshalBinary() //nolint:errcheck // never fails

ipPrefix, err = ip.Netmask(mask)
if err != nil {
return nil, err
}
mask, _ := netmask.MarshalBinary() //nolint:errcheck // never fails
ones, _ := net.IPMask(mask).Size()
ipPrefix = netaddr.IPPrefixFrom(ip, uint8(ones))
} else {
ipPrefix = netaddr.IPPrefixFrom(ip, uint8(bits))
}
Expand Down
Expand Up @@ -17,13 +17,13 @@ import (
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/openstack"
)

//go:embed metadata.json
//go:embed testdata/metadata.json
var rawMetadata []byte

//go:embed network.json
//go:embed testdata/network.json
var rawNetwork []byte

//go:embed expected.yaml
//go:embed testdata/expected.yaml
var expectedNetworkConfig string

func TestParseMetadata(t *testing.T) {
Expand Down
Expand Up @@ -5,7 +5,13 @@ addresses:
scope: global
flags: permanent
layer: platform
- address: 2000:0:ff00::/56
- address: 10.184.0.244/20
linkName: eth1
family: inet4
scope: global
flags: permanent
layer: platform
- address: 2001:db8::3257:9652/64
linkName: eth1
family: inet6
scope: global
Expand Down Expand Up @@ -51,10 +57,34 @@ routes:
flags: ""
protocol: static
layer: platform
- family: inet4
dst: 192.168.0.0/16
src: ""
gateway: 10.184.0.1
outLinkName: eth1
table: main
priority: 1024
scope: global
type: unicast
flags: ""
protocol: static
layer: platform
- family: inet4
dst: ""
src: ""
gateway: 10.184.0.1
outLinkName: eth1
table: main
priority: 1024
scope: global
type: unicast
flags: ""
protocol: static
layer: platform
- family: inet6
dst: ""
src: ""
gateway: '2000:0:ff00::'
gateway: fd00::1
outLinkName: eth1
table: main
priority: 1024
Expand All @@ -80,11 +110,5 @@ operators:
dhcp4:
routeMetric: 1024
layer: platform
- operator: dhcp4
linkName: eth1
requireUp: true
dhcp4:
routeMetric: 1024
layer: platform
externalIPs:
- 1.2.3.4
Expand Up @@ -44,22 +44,36 @@
"id": "privatnet-ipv4",
"link": "aae16046-6c74-4f33-acf2-a16e9ab093ec",
"network_id": "66374c4d-5123-4f11-8fa9-8a6dea2b4fe7",
"type": "ipv4_dhcp"
"type": "ipv4",
"ip_address": "10.184.0.244",
"netmask": "255.255.240.0",
"routes": [
{
"network": "192.168.0.0",
"netmask": "255.255.0.0",
"gateway": "10.184.0.1"
},
{
"network": "0.0.0.0",
"netmask": "0.0.0.0",
"gateway": "10.184.0.1"
}
]
},
{
"id": "privatnet-ipv6",
"link": "aae16046-6c74-4f33-acf2-a16e9ab093ec",
"network_id": "66374c4d-5123-4f11-8fa9-8a6dea2b4fe7",
"type": "ipv6",
"ip_address": "2001:db8::3257:9652",
"netmask": "ffff:ffff:ffff:ffff::",
"routes": [
{
"network": "::",
"netmask": "::",
"gateway": "2000:0:ff00::"
"gateway": "fd00::1"
}
],
"id": "privatnet-ipv6",
"link": "aae16046-6c74-4f33-acf2-a16e9ab093ec",
"ip_address": "2000:0:ff00::1",
"netmask": "ffff:ffff:ffff:ff00::",
"network_id": "66374c4d-5123-4f11-8fa9-8a6dea2b4fe7",
"type": "ipv6"
]
}
],
"services": [
Expand All @@ -72,4 +86,4 @@
"type": "dns"
}
]
}
}

0 comments on commit 5a0fd63

Please sign in to comment.