From 1c5c9580d0e5d4f0d1613b1621075706a0585147 Mon Sep 17 00:00:00 2001 From: Francesco Giudici Date: Tue, 12 Nov 2024 18:20:33 +0100 Subject: [PATCH] register: track NIC IPaddresses in MachineInventories annotations The IP address information can be useful to admins and users. Anyway, the main reason to track the network interfaces ip addresses is to later introduce support to the CATTLE_ADDRESS and the CATTLE_INTERNAL_ADDRESS env variables. Note that we don't want to track kubernetes and/or CNI interfaces: we just track the interfaces with an IP address and that have an associated connection activated by NetworkManager. Signed-off-by: Francesco Giudici --- pkg/register/register.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/register/register.go b/pkg/register/register.go index 2b5c8da4..1ac9edc1 100644 --- a/pkg/register/register.go +++ b/pkg/register/register.go @@ -18,6 +18,7 @@ package register import ( "bufio" + "bytes" "crypto/tls" "crypto/x509" "errors" @@ -25,6 +26,7 @@ import ( "io" "net/http" "os" + "os/exec" "strings" "time" @@ -325,6 +327,14 @@ func sendAnnotations(conn *websocket.Conn, reg elementalv1.Registration) error { log.Debugf("sending local IP: %s", data["registration-ip"]) } + netIfaces := hostinfo.GetIPAddresses() + nmDevs := getNMActivatedDevices() + for _, ifName := range nmDevs { + if ipAddr, ok := netIfaces[ifName]; ok { + data["net."+ifName+".ip"] = ipAddr + } + } + err := SendJSONData(conn, MsgAnnotations, data) if err != nil { log.Debugf("annotation data:\n%s", litter.Sdump(data)) @@ -342,6 +352,29 @@ func getLocalIPAddress(conn *websocket.Conn) (string, error) { return tcpAddr[0:idxPortNumStart], nil } +func getNMActivatedDevices() (nmDevs []string) { + nmDevs = []string{} + tempBuf := &bytes.Buffer{} + cmd := exec.Command("nmcli", "-g", "DEVICE,STATE", "device") + cmd.Stdout = tempBuf + + if err := cmd.Run(); err != nil { + return + } + + scanner := bufio.NewScanner(tempBuf) + for scanner.Scan() { + devState := strings.Split(scanner.Text(), ":") + if len(devState) != 2 { + continue + } + if devState[1] == "connected" { + nmDevs = append(nmDevs, devState[0]) + } + } + return +} + func getOsReleaseInfo() (map[string]string, error) { data := map[string]string{}