chore(nmlite/ifstate): store change reasons as a slice of enums

This commit is contained in:
Siyuan 2025-11-21 12:24:25 +00:00
parent ad4f7c09e1
commit 4edf753956
1 changed files with 42 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package nmlite
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"github.com/jetkvm/kvm/internal/network/types" "github.com/jetkvm/kvm/internal/network/types"
@ -9,6 +10,40 @@ import (
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
type IfStateChangeReason uint
const (
IfStateOperStateChanged IfStateChangeReason = 1
IfStateOnlineStateChanged IfStateChangeReason = 2
IfStateMACAddressChanged IfStateChangeReason = 3
IfStateIPAddressesChanged IfStateChangeReason = 4
)
type IfStateChangeReasons []IfStateChangeReason
func (r IfStateChangeReason) String() string {
switch r {
case IfStateOperStateChanged:
return "oper state changed"
case IfStateOnlineStateChanged:
return "online state changed"
case IfStateMACAddressChanged:
return "MAC address changed"
case IfStateIPAddressesChanged:
return "IP addresses changed"
default:
return fmt.Sprintf("unknown change reason %d", r)
}
}
func (rs IfStateChangeReasons) String() string {
reasons := []string{}
for _, r := range rs {
reasons = append(reasons, r.String())
}
return strings.Join(reasons, ", ")
}
// updateInterfaceState updates the current interface state // updateInterfaceState updates the current interface state
func (im *InterfaceManager) updateInterfaceState() error { func (im *InterfaceManager) updateInterfaceState() error {
nl, err := im.link() nl, err := im.link()
@ -18,7 +53,7 @@ func (im *InterfaceManager) updateInterfaceState() error {
var ( var (
stateChanged bool stateChanged bool
changeReason string changeReasons IfStateChangeReasons
) )
attrs := nl.Attrs() attrs := nl.Attrs()
@ -32,7 +67,7 @@ func (im *InterfaceManager) updateInterfaceState() error {
if im.state.Up != isUp { if im.state.Up != isUp {
im.state.Up = isUp im.state.Up = isUp
stateChanged = true stateChanged = true
changeReason = "oper state changed" changeReasons = append(changeReasons, IfStateOperStateChanged)
} }
// Check if the interface is online // Check if the interface is online
@ -40,14 +75,14 @@ func (im *InterfaceManager) updateInterfaceState() error {
if im.state.Online != isOnline { if im.state.Online != isOnline {
im.state.Online = isOnline im.state.Online = isOnline
stateChanged = true stateChanged = true
changeReason = "online state changed" changeReasons = append(changeReasons, IfStateOnlineStateChanged)
} }
// Check if the MAC address has changed // Check if the MAC address has changed
if im.state.MACAddress != attrs.HardwareAddr.String() { if im.state.MACAddress != attrs.HardwareAddr.String() {
im.state.MACAddress = attrs.HardwareAddr.String() im.state.MACAddress = attrs.HardwareAddr.String()
stateChanged = true stateChanged = true
changeReason = "MAC address changed" changeReasons = append(changeReasons, IfStateMACAddressChanged)
} }
// Update IP addresses // Update IP addresses
@ -55,7 +90,7 @@ func (im *InterfaceManager) updateInterfaceState() error {
im.logger.Error().Err(err).Msg("failed to update IP addresses") im.logger.Error().Err(err).Msg("failed to update IP addresses")
} else if ipChanged { } else if ipChanged {
stateChanged = true stateChanged = true
changeReason = "IP addresses changed" changeReasons = append(changeReasons, IfStateIPAddressesChanged)
} }
im.state.LastUpdated = time.Now() im.state.LastUpdated = time.Now()
@ -64,7 +99,7 @@ func (im *InterfaceManager) updateInterfaceState() error {
// Notify callback if state changed // Notify callback if state changed
if stateChanged && im.onStateChange != nil { if stateChanged && im.onStateChange != nil {
im.logger.Debug(). im.logger.Debug().
Str("changeReason", changeReason). Stringer("changeReasons", changeReasons).
Interface("state", im.state). Interface("state", im.state).
Msg("notifying state change") Msg("notifying state change")
im.onStateChange(*im.state) im.onStateChange(*im.state)