utils

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 21 Imported by: 3

Documentation

Index

Constants

View Source
const (
	BridgeSuffix       = "-br"
	BondSuffix         = "-bo"
	DefaultValueMiimon = 100

	LenOfBridgeSuffix = 3 // length of BridgeSuffix
	LenOfBondSuffix   = 3 // length of BondSuffix

	MaxDeviceNameLen = 15

	VlanSubInterfaceSpliter = "."

	// format: e.g. mgmt-br.2025
	ManagementClusterNetworkDevicePrefix = ManagementClusterNetworkName + BridgeSuffix + VlanSubInterfaceSpliter
)
View Source
const (
	DefaultMTU = 1500
	MaxMTU     = 9000
	MinMTU     = 576 // IPv4 does not define this explicitly; IPv6 defines 1280; Some protocol requires 576; hence 576 is used

	HarvesterSystemNamespaceName = "harvester-system" // don't import harvester/pkg/util to avoid loop importing, define it directly
)
View Source
const (
	KeyVlanLabel             = network.GroupName + "/vlan-id"
	KeyVlanConfigLabel       = network.GroupName + "/vlanconfig"
	KeyClusterNetworkLabel   = network.GroupName + "/clusternetwork"
	KeyNodeLabel             = network.GroupName + "/node"
	KeyNetworkType           = network.GroupName + "/type"
	KeyLastNetworkType       = network.GroupName + "/last-type"
	KeyNetworkReady          = network.GroupName + "/ready"
	KeyNetworkRoute          = network.GroupName + "/route"
	KeyNetworkRouteSourceVID = network.GroupName + "/route-source-vid" // the source vid of this route
	KeyMTUSourceVlanConfig   = network.GroupName + "/mtu-source-vc"    // the VC which syncs MTU to CN
	KeyUplinkMTU             = network.GroupName + "/uplink-mtu"       // configured MTU on the VC'uplink

	KeyMatchedNodes = network.GroupName + "/matched-nodes"

	KeyVlanIDSetStr     = network.GroupName + "/vlan-id-set-str"      // all vlan ids under current cluster network, format "1,2,3..."
	KeyVlanIDSetStrHash = network.GroupName + "/vlan-id-set-str-hash" // hash value of above string

	KeyVlanDHCPServerIP = network.GroupName + "/vlan-dhcp-server-ip"

	ValueTrue  = "true"
	ValueFalse = "false"

	HarvesterWitnessNodeLabelKey = "node-role.harvesterhci.io/witness"

	HarvesterMgmtClusterNetworkLabeyKey = network.GroupName + "/" + ManagementClusterNetworkName

	// defined in Harvester pkg/controller/master/storagenetwork/storage_network.go
	// to avoid loop references, needs to wait until Harvester move this to its const and then refers to
	StorageNetworkAnnotation         = "storage-network.settings.harvesterhci.io"
	StorageNetworkNetAttachDefPrefix = "storagenetwork-"
)
View Source
const (
	CNITypeKubeOVN = "kube-ovn"

	CNITypeBridge       = "bridge"
	CNITypeDefaultEmpty = "" // potential empty type, is treated as CNITypeBridge
)
View Source
const (
	MaxVlanID            = 4094
	MinVlanID            = 0
	MinTrunkVlanID       = 1
	DefaultVlanID        = 1
	VlanIDCount          = 4096
	VlanIDStringJoinChar = ","
)
View Source
const ManagementClusterNetworkName = "mgmt"
View Source
const (
	MaxClusterNetworkNameLen = MaxDeviceNameLen - LenOfBridgeSuffix
)
View Source
const (
	VMByNetworkIndex = "vm.harvesterhci.io/vm-by-network"
)

Variables

This section is empty.

Functions

func AreClusterNetworkVlanAnnotationsUnchanged added in v1.7.0

func AreClusterNetworkVlanAnnotationsUnchanged(cn *networkv1.ClusterNetwork, vidstr, vidhash string) bool

func AreEqualMTUs added in v1.6.0

func AreEqualMTUs(MTU1, MTU2 int) bool

func AreJobLabelsDHCPInfoUnchanged added in v1.7.0

func AreJobLabelsDHCPInfoUnchanged(lb map[string]string, l2netconf *NetConf, l3netconf *Layer3NetworkConf) bool

func CheckBridgeName added in v1.7.0

func CheckBridgeName(brName string) error

check if the bridge name is valid

func EnsureSysctlValue added in v0.3.0

func EnsureSysctlValue(name, value string) error

func FilterFirstActiveStorageNetworkNad added in v1.6.0

func FilterFirstActiveStorageNetworkNad(nads []*nadv1.NetworkAttachmentDefinition) *nadv1.NetworkAttachmentDefinition

filter the first active storage network nad from a list of nads

func GenerateBondName added in v1.7.0

func GenerateBondName(prefix string) string

func GenerateBridgeName added in v1.7.0

func GenerateBridgeName(prefix string) string

func GetBridgeNamePrefix added in v1.7.0

func GetBridgeNamePrefix(brName string) (string, error)

get the bridge name exclude the suffix

func GetClusterNetworkDevicePrefix added in v1.7.0

func GetClusterNetworkDevicePrefix(cnName string) string

e.g. cn2-br.2025 is a valid vlan sub interface, and the device prefix is `cn2-br.`

func GetClusterNetworkFromBridgeName added in v1.7.0

func GetClusterNetworkFromBridgeName(brName string) (string, error)

func GetLabelKeyOfClusterNetwork added in v1.7.0

func GetLabelKeyOfClusterNetwork(clusterNetwork string) string

func GetMTUFromAnnotation added in v1.6.0

func GetMTUFromAnnotation(annotation string) (int, error)

func GetMTUFromVlanConfig added in v1.6.0

func GetMTUFromVlanConfig(vc *networkv1.VlanConfig) int

caller handlers the return value of 0 which is normally treated as the default MTU

func GetNadAnnotation added in v1.7.0

func GetNadAnnotation(nad *nadv1.NetworkAttachmentDefinition, key string) string

func GetNadLabel added in v1.7.0

func GetNadLabel(nad *nadv1.NetworkAttachmentDefinition, key string) string

func GetNadNameFromProvider added in v1.6.0

func GetNadNameFromProvider(provider string) (nadName string, nadNamespace string, err error)

func GetProviderFromNad added in v1.6.0

func GetProviderFromNad(nadName string, nadNamespace string) (provider string, err error)

func HasClusterNetworkDevicePrefix added in v1.7.0

func HasClusterNetworkDevicePrefix(link, prefix string) bool

func HasLabelKey added in v1.7.0

func HasLabelKey(lbs map[string]string, key string, value string) bool

func HasMgmtClusterNetworkDevicePrefix added in v1.7.0

func HasMgmtClusterNetworkDevicePrefix(link string) bool

func HasMgmtClusterNetworkLabelKey added in v1.7.0

func HasMgmtClusterNetworkLabelKey(lbs map[string]string) bool

func HasWitnessNodeLabelKey added in v1.7.0

func HasWitnessNodeLabelKey(lbs map[string]string) bool

func IsBridgeNameValid added in v1.7.0

func IsBridgeNameValid(brName string) (bool, error)

func IsClusterNetworkNameValid added in v1.7.0

func IsClusterNetworkNameValid(nm string) (bool, error)

func IsDefaultMTU added in v1.6.0

func IsDefaultMTU(MTU int) bool

func IsManagementClusterNetwork added in v1.7.0

func IsManagementClusterNetwork(cnName string) bool

func IsNadNetworkLabelSet added in v1.7.0

func IsNadNetworkLabelSet(nad *nadv1.NetworkAttachmentDefinition) bool

func IsOverlayNad added in v1.7.0

func IsOverlayNad(nad *nadv1.NetworkAttachmentDefinition) bool

func IsReservedSubnet added in v1.6.0

func IsReservedSubnet(subnetName string, provider string) (bool, error)

func IsStorageNetworkNad added in v1.6.0

func IsStorageNetworkNad(nad *nadv1.NetworkAttachmentDefinition) bool

if this nad is a storagenetwork nad

func IsValidMTU added in v1.6.0

func IsValidMTU(MTU int) bool

func IsVlanNad added in v0.3.2

func IsVlanNad(nad *nadv1.NetworkAttachmentDefinition) bool

func Name added in v0.3.0

func Name(prefix string, s ...string) string

Name function joints prefix with all other strings and crc32 checksum

func NewSelector added in v0.3.0

func NewSelector(selector map[string]string) (labels.Selector, error)

func OutdateLayer3NetworkConfPerMode added in v1.7.0

func OutdateLayer3NetworkConfPerMode(conf string) (string, error)

func OutdateNadLayer3NetworkConf added in v1.7.0

func OutdateNadLayer3NetworkConf(nad *nadv1.NetworkAttachmentDefinition, nadConf *NetConf) (map[string]string, error)

func OutdateNadLayer3NetworkConfWhenRouteModeChanges added in v1.7.0

func OutdateNadLayer3NetworkConfWhenRouteModeChanges(oldNad, newNad *nadv1.NetworkAttachmentDefinition) (map[string]string, error)

caller ensurs this function is only called when oldNad and newNad have same vlan id the first return is not nil only when real change happens

func SetClusterNetworkVlanAnnotations added in v1.7.0

func SetClusterNetworkVlanAnnotations(cn *networkv1.ClusterNetwork, vidstr, vidhash string)

func SetDHCPInfo2JobLabels added in v1.7.0

func SetDHCPInfo2JobLabels(lb map[string]string, l2netconf *NetConf, l3netconf *Layer3NetworkConf)

func SetLogLevel added in v1.7.0

func SetLogLevel(level string)

func SetMgmtClusterNetworkLabelKey added in v1.7.0

func SetMgmtClusterNetworkLabelKey(lbs map[string]string)

func SetNadAnnotation added in v1.7.0

func SetNadAnnotation(nad *nadv1.NetworkAttachmentDefinition, key, value string)

func SetNadLabel added in v1.7.0

func SetNadLabel(nad *nadv1.NetworkAttachmentDefinition, key, value string)

Types

type Connectivity

type Connectivity string
const (
	Connectable   Connectivity = "true"
	Unconnectable Connectivity = "false"
	DHCPFailed    Connectivity = "DHCP failed"
	PingFailed    Connectivity = "ping failed"
)

type Layer3NetworkConf

type Layer3NetworkConf struct {
	Mode         Mode         `json:"mode,omitempty"`
	CIDR         string       `json:"cidr,omitempty"`
	Gateway      string       `json:"gateway,omitempty"`
	ServerIPAddr string       `json:"serverIPAddr,omitempty"`
	Connectivity Connectivity `json:"connectivity,omitempty"`
	Outdated     bool         `json:"outdated,omitempty"`
}

func NewLayer3NetworkConf

func NewLayer3NetworkConf(conf string) (*Layer3NetworkConf, error)

func NewLayer3NetworkConfFromNad added in v1.7.0

func NewLayer3NetworkConfFromNad(nad *nadv1.NetworkAttachmentDefinition) (*Layer3NetworkConf, error)

func (*Layer3NetworkConf) GetDHCPServerIPAddr added in v1.7.0

func (c *Layer3NetworkConf) GetDHCPServerIPAddr() string

func (*Layer3NetworkConf) ToString

func (c *Layer3NetworkConf) ToString() (string, error)

type Mode

type Mode string
const (
	Auto   Mode = "auto"
	Manual Mode = "manual"
)

type NadGetter added in v1.6.0

type NadGetter struct {
	// contains filtered or unexported fields
}

func NewNadGetter added in v1.6.0

func NewNadGetter(nadCache ctlcniv1.NetworkAttachmentDefinitionCache) *NadGetter

func (*NadGetter) GetFirstActiveStorageNetworkNadOnClusterNetwork added in v1.6.0

func (n *NadGetter) GetFirstActiveStorageNetworkNadOnClusterNetwork(cnName string) (*nadv1.NetworkAttachmentDefinition, error)

func (*NadGetter) ListNadsOnClusterNetwork added in v1.6.0

func (n *NadGetter) ListNadsOnClusterNetwork(cnName string) ([]*nadv1.NetworkAttachmentDefinition, error)

list all nads attached to a cluster network

func (*NadGetter) NadNamesOnClusterNetwork added in v1.6.0

func (n *NadGetter) NadNamesOnClusterNetwork(cnName string) ([]string, error)

type NadSelectedNetworks

type NadSelectedNetworks []nadv1.NetworkSelectionElement

func NewNADSelectedNetworks

func NewNADSelectedNetworks(conf string) (NadSelectedNetworks, error)

func (NadSelectedNetworks) ToString

func (n NadSelectedNetworks) ToString() (string, error)

type NetConf added in v0.3.0

type NetConf struct {
	cniv1.NetConf
	BrName       string       `json:"bridge"`
	IsGW         bool         `json:"isGateway"`
	IsDefaultGW  bool         `json:"isDefaultGateway"`
	ForceAddress bool         `json:"forceAddress"`
	IPMasq       bool         `json:"ipMasq"`
	MTU          int          `json:"mtu"`
	HairpinMode  bool         `json:"hairpinMode"`
	PromiscMode  bool         `json:"promiscMode"`
	Vlan         int          `json:"vlan"`
	Provider     string       `json:"provider"`
	VlanTrunk    []*VlanTrunk `json:"vlanTrunk,omitempty"`
}

L2 mode

func DecodeNadConfigToNetConf added in v1.7.0

func DecodeNadConfigToNetConf(nad *nadv1.NetworkAttachmentDefinition) (*NetConf, error)

decode nad config string to a config struct

func (*NetConf) GetClusterNetworkName added in v1.7.0

func (nc *NetConf) GetClusterNetworkName() (string, error)

func (*NetConf) GetNetworkType added in v1.7.0

func (nc *NetConf) GetNetworkType() (NetworkType, error)

func (*NetConf) GetVlan added in v1.7.0

func (nc *NetConf) GetVlan() int

func (*NetConf) GetVlanString added in v1.7.0

func (nc *NetConf) GetVlanString() string

func (*NetConf) IsBridgeCNI added in v1.7.0

func (nc *NetConf) IsBridgeCNI() bool

func (*NetConf) IsKubeOVNCNI added in v1.7.0

func (nc *NetConf) IsKubeOVNCNI() bool

func (*NetConf) IsL2VlanNetwork added in v1.7.0

func (nc *NetConf) IsL2VlanNetwork() bool

func (*NetConf) IsL2VlanTrunkNetwork added in v1.7.0

func (nc *NetConf) IsL2VlanTrunkNetwork() bool

func (*NetConf) IsUntaggedNetwork added in v1.7.0

func (nc *NetConf) IsUntaggedNetwork() bool

func (*NetConf) IsVlanAccessMode added in v1.7.0

func (nc *NetConf) IsVlanAccessMode() bool

the default mode

func (*NetConf) IsVlanConfigValid added in v1.7.0

func (nc *NetConf) IsVlanConfigValid() (bool, error)

if vlanconfig is valid

func (*NetConf) IsVlanTrunkMode added in v1.7.0

func (nc *NetConf) IsVlanTrunkMode() bool

if VlanTrunk is configured

func (*NetConf) SetNetworkInfoToLabels added in v1.7.0

func (nc *NetConf) SetNetworkInfoToLabels(label map[string]string) error

type NetworkType added in v0.3.2

type NetworkType string
const (
	L2VlanNetwork      NetworkType = "L2VlanNetwork"
	L2VlanTrunkNetwork NetworkType = "L2VlanTrunkNetwork"
	UntaggedNetwork    NetworkType = "UntaggedNetwork"
	OverlayNetwork     NetworkType = "OverlayNetwork"

	InvalidNetwork NetworkType = "InvalidNetwork"
)

type VMGetter added in v1.6.0

type VMGetter struct {
	// contains filtered or unexported fields
}

func NewVMGetter added in v1.6.0

func NewVMGetter(vmCache ctlkubevirtv1.VirtualMachineCache) *VMGetter

func (*VMGetter) VMNamesWhoUseNad added in v1.6.0

func (v *VMGetter) VMNamesWhoUseNad(nad *nadv1.NetworkAttachmentDefinition) ([]string, error)

Get the vm name list who uses the nad

func (*VMGetter) VMNamesWhoUseNads added in v1.6.0

func (v *VMGetter) VMNamesWhoUseNads(nads []*nadv1.NetworkAttachmentDefinition) ([]string, error)

Get the vm name list who uses a group of nads, note: duplicated names are removed

func (*VMGetter) WhoUseNad added in v1.6.0

WhoUseNad requires adding network indexer to the vm cache before invoking it

func (*VMGetter) WhoUseNads added in v1.6.0

Get the vm lists who uses a group of nads, note: duplicated vmis may exist when they attache to mutli nads

type VlanIDSet added in v1.7.0

type VlanIDSet struct {
	// contains filtered or unexported fields
}

func GeVlanIDSetFromClusterNetwork added in v1.7.0

func GeVlanIDSetFromClusterNetwork(cnName string, nadCache ctlcniv1.NetworkAttachmentDefinitionCache) (*VlanIDSet, error)

func NewVlanIDSet added in v1.7.0

func NewVlanIDSet() *VlanIDSet

trunk mode

func NewVlanIDSetFromNadList added in v1.7.0

func NewVlanIDSetFromNadList(nads []*nadv1.NetworkAttachmentDefinition) (*VlanIDSet, error)

return vidsets from all bridge nads

func NewVlanIDSetFromSingleVID added in v1.7.0

func NewVlanIDSetFromSingleVID(vid int) (*VlanIDSet, error)

for l2 vlan & untag, it can hold only 1 vid

func (*VlanIDSet) Append added in v1.7.0

func (vis *VlanIDSet) Append(other *VlanIDSet) error

merge another VlandIDSet to current

func (*VlanIDSet) ConvertToTrunkMode added in v1.7.0

func (vis *VlanIDSet) ConvertToTrunkMode()

when run Append() or Diff(), if the vidset is in single mode, convert it to trunk mode first

func (*VlanIDSet) Diff added in v1.7.0

func (vis *VlanIDSet) Diff(existing *VlanIDSet) (added, removed *VlanIDSet, err error)

according to current and the existing vlandidset, compute the to be added and removed vidset

func (*VlanIDSet) GetVlanCount added in v1.7.0

func (vis *VlanIDSet) GetVlanCount() uint32

func (*VlanIDSet) SetUint16VID added in v1.7.0

func (vis *VlanIDSet) SetUint16VID(vid uint16) error

refer pkg/network/iface/vlan.go, which uses uint16 as vid

func (*VlanIDSet) SetVID added in v1.7.0

func (vis *VlanIDSet) SetVID(vid int) error

func (*VlanIDSet) VidSetToString added in v1.7.0

func (vis *VlanIDSet) VidSetToString() string

func (*VlanIDSet) VidSetToStringHash added in v1.7.0

func (vis *VlanIDSet) VidSetToStringHash() (str, hash string)

func (*VlanIDSet) WalkVIDs added in v1.7.0

func (vis *VlanIDSet) WalkVIDs(name string, callback func(vid uint16) error) error

walk vids in range [2..4094]

type VlanTrunk added in v1.7.0

type VlanTrunk struct {
	MinID *int `json:"minID,omitempty"`
	MaxID *int `json:"maxID,omitempty"`
	ID    *int `json:"id,omitempty"`
}

type VmiGetter added in v0.3.2

type VmiGetter struct {
	VmiCache ctlkubevirtv1.VirtualMachineInstanceCache
}

func NewVmiGetter added in v1.6.0

func NewVmiGetter(vmiCache ctlkubevirtv1.VirtualMachineInstanceCache) *VmiGetter

func (*VmiGetter) VmiNamesWhoUseNad added in v1.6.0

func (v *VmiGetter) VmiNamesWhoUseNad(nad *nadv1.NetworkAttachmentDefinition, filterFlag bool, nodesFilter mapset.Set[string]) ([]string, error)

Get the vmi name list who uses the nad

func (*VmiGetter) VmiNamesWhoUseNads added in v1.6.0

func (v *VmiGetter) VmiNamesWhoUseNads(nads []*nadv1.NetworkAttachmentDefinition, filterFlag bool, nodesFilter mapset.Set[string]) ([]string, error)

Get the vmi name list who uses a group of nads, note: duplicated names are removed

func (*VmiGetter) WhoUseNad added in v0.3.2

func (v *VmiGetter) WhoUseNad(nad *nadv1.NetworkAttachmentDefinition, filterFlag bool, nodesFilter mapset.Set[string]) ([]*kubevirtv1.VirtualMachineInstance, error)

WhoUseNad requires adding network indexer to the vmi cache before invoking it when filterFlag is true, it ensures the return vmis are on the given nodesFilter

func (*VmiGetter) WhoUseNads added in v1.6.0

func (v *VmiGetter) WhoUseNads(nads []*nadv1.NetworkAttachmentDefinition, filterFlag bool, nodesFilter mapset.Set[string]) ([]*kubevirtv1.VirtualMachineInstance, error)

Get the vmi lists who uses a group of nads, note: duplicated vmis may exist when they attache to mutli nads

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL