Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1.7.x] Label Templates: add IP addresses to the Network variables (#885) #894

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ generate-mocks: $(MOCKGEN)
$(MOCKGEN) -copyright_file=scripts/boilerplate.go.txt -destination=pkg/elementalcli/mocks/elementalcli.go -package=mocks github.com/rancher/elemental-operator/pkg/elementalcli Runner
$(MOCKGEN) -copyright_file=scripts/boilerplate.go.txt -destination=pkg/network/mocks/network.go -package=mocks github.com/rancher/elemental-operator/pkg/network Configurator
$(MOCKGEN) -copyright_file=scripts/boilerplate.go.txt -destination=pkg/util/mocks/command_runner.go -package=mocks github.com/rancher/elemental-operator/pkg/util CommandRunner
$(MOCKGEN) -copyright_file=scripts/boilerplate.go.txt -destination=pkg/util/mocks/net_controller.go -package=mocks github.com/rancher/elemental-operator/pkg/util NetController

.PHONY: generate-go
generate-go: generate-mocks $(CONTROLLER_GEN) ## Runs Go related generate targets for the operator
Expand Down
24 changes: 24 additions & 0 deletions pkg/hostinfo/hostinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/rancher/elemental-operator/pkg/log"
"github.com/rancher/elemental-operator/pkg/runtime"
"github.com/rancher/elemental-operator/pkg/util"
)

var ErrCouldNotReadHostInfo = errors.New("could not read host info")
Expand Down Expand Up @@ -254,6 +255,7 @@ func sanitizeHostInfoVal(data string) string {
return data
}

// ExtractLabelsLegacy provides the old (<= 1.7.0) syntax of Label Templates for backward compatibility
func ExtractLabelsLegacy(systemData HostInfo) map[string]interface{} {
memory := map[string]interface{}{}
if systemData.Memory != nil {
Expand Down Expand Up @@ -344,6 +346,7 @@ func ExtractLabelsLegacy(systemData HostInfo) map[string]interface{} {
return labels
}

// ExtractLabels provide the new (>= 1.8.x) syntax of the Label Templates
func ExtractLabels(systemData HostInfo) map[string]interface{} {
labels := map[string]interface{}{}

Expand Down Expand Up @@ -421,10 +424,31 @@ func ExtractLabels(systemData HostInfo) map[string]interface{} {
network["NICs"] = nicsMap
for i, iface := range systemData.Network.NICs {
ifaceNum := strconv.Itoa(i)
ipv4, ipv6 := util.GetIPsByIfName(iface.Name)
ipv4add := ""
ipv4map := map[string]interface{}{}
for i, ip := range ipv4 {
ipv4map[strconv.Itoa(i)] = ip
if ipv4add == "" {
ipv4add = ip
}
}
ipv6add := ""
ipv6map := map[string]interface{}{}
for i, ip := range ipv6 {
ipv6map[strconv.Itoa(i)] = ip
if ipv6add == "" {
ipv6add = ip
}
}
nicsMap[ifaceNum] = map[string]interface{}{
"AdvertisedLinkModes": strings.Join(iface.AdvertisedLinkModes, ","),
"Duplex": sanitizeHostInfoVal(iface.Duplex),
"IsVirtual": strconv.FormatBool(iface.IsVirtual),
"IPv4Address": ipv4add,
"IPv4Addresses": ipv4map,
"IPv6Address": ipv6add,
"IPv6Addresses": ipv6map,
"MacAddress": iface.MacAddress,
"Name": iface.Name,
"PCIAddress": iface.PCIAddress,
Expand Down
72 changes: 72 additions & 0 deletions pkg/util/mocks/net_controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright © 2022 - 2024 SUSE LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"net"
)

type NetController interface {
GetInterfaceAddresses(ifName string) [](net.IPNet)
}

func NewNetController() NetController {
return &unixNet{}
}

var _ NetController = (*unixNet)(nil)

type unixNet struct{}

func (un *unixNet) GetInterfaceAddresses(ifName string) (res []net.IPNet) {
var (
iface *net.Interface
addrs []net.Addr
err error
)
res = []net.IPNet{}

if iface, err = net.InterfaceByName(ifName); err != nil {
return
}
if addrs, err = iface.Addrs(); err != nil {
return
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok {
res = append(res, *ipNet)
}
}
return
}

// GetIPByIfName returns two slices containing the IPv4 and the IPv6 addresses of
// the network interface name (ifName) passed as argument.
// This function cannot fail: if no address can be retrieved two empty slices are
// returned.
func GetIPsByIfName(ifName string) ([]string, []string) {
return getIPsByIfName(ifName, NewNetController())
}

func getIPsByIfName(ifName string, nc NetController) (ipv4, ipv6 []string) {
ipv4 = []string{}
ipv6 = []string{}

ipNets := nc.GetInterfaceAddresses(ifName)
for _, ip := range ipNets {
if ip2v4 := ip.IP.To4(); ip2v4 != nil {
ipv4 = append(ipv4, ip2v4.String())
} else if ip2v6 := ip.IP.To16(); ip2v6 != nil {
ipv6 = append(ipv6, ip2v6.String())
}
}
return
}
94 changes: 94 additions & 0 deletions pkg/util/net_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright © 2022 - 2024 SUSE LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"net"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

utilmocks "github.com/rancher/elemental-operator/pkg/util/mocks"
gomock "go.uber.org/mock/gomock"
)

var _ = Describe("GetIPByIfName", func() {
var ctrl *gomock.Controller
var netctrl *utilmocks.MockNetController

BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
netctrl = utilmocks.NewMockNetController(ctrl)
})
It("should return all IPv4 and IPv6 addresses available", func() {
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return(
[]net.IPNet{
{
IP: []byte{0x20, 0x1, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77},
},
{
IP: []byte{192, 168, 1, 10},
},
{
IP: []byte{1, 1, 1, 1},
},
},
)

ipv4, ipv6 := getIPsByIfName("eth0", netctrl)
Expect(len(ipv4)).To(Equal(2))
Expect(len(ipv6)).To(Equal(1))
Expect(ipv4[0]).To(Equal("192.168.1.10"))
Expect(ipv4[1]).To(Equal("1.1.1.1"))
Expect(ipv6[0]).To(Equal("2001:1111:2222:3333:4444:5555:6666:7777"))
})
It("should return IPv6 address only if IPv4 is not available", func() {
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return(
[]net.IPNet{
{
IP: []byte{0x20, 0x1, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77},
},
},
)

ipv4, ipv6 := getIPsByIfName("eth0", netctrl)
Expect(ipv4).To(BeEmpty())
Expect(len(ipv6)).To(Equal(1))
Expect(ipv6[0]).To(Equal("2001:1111:2222:3333:4444:5555:6666:7777"))
})
It("should return the empty string when no IP addresses are available", func() {
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return([]net.IPNet{})
ipv4, ipv6 := getIPsByIfName("eth0", netctrl)
Expect(ipv4).To(BeEmpty())
Expect(ipv6).To(BeEmpty())
})
It("should return the empty string if returned addresses are invalid", func() {
netctrl.EXPECT().GetInterfaceAddresses("eth0").Return(
[]net.IPNet{
{
IP: []byte{0x20, 0x1, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
0x44, 0x44},
},
},
)
ipv4, ipv6 := getIPsByIfName("eth0", netctrl)
Expect(ipv4).To(BeEmpty())
Expect(ipv6).To(BeEmpty())
})
})
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func JSONObjectToYamlBytes(object map[string]runtime.RawExtension) ([]byte, erro
// This creates a parent "root" key to facilitate parsing the schemaless map
mapSlice := yaml.JSONObjectToYAMLObject(map[string]interface{}{"root": object})
if len(mapSlice) <= 0 {
return nil, errors.New("Could not convert json object to yaml")
return nil, errors.New("could not convert json object to yaml")
}

// Just marshal the value of the "root" key
Expand Down
Loading