Skip to content

Commit

Permalink
register: track NIC IPaddresses in MachineInventories annotations
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fgiudici committed Nov 14, 2024
1 parent 7b879b1 commit 1c5c958
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package register

import (
"bufio"
"bytes"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"time"

Expand Down Expand Up @@ -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))
Expand All @@ -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{}

Expand Down

0 comments on commit 1c5c958

Please sign in to comment.