cni

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const CNINetworksDir = "/var/lib/cni/networks"

CNINetworksDir is the standard directory where CNI stores network state and IPAM allocations. This is the default location used by CNI plugins for host-local IPAM.

Variables

This section is empty.

Functions

func BuildDefaultConflist

func BuildDefaultConflist(name, bridge, subnet string) ([]byte, error)

BuildDefaultConflist generates a default conflist JSON using provided parameters.

func SafeBridgeName added in v0.2.0

func SafeBridgeName(name string) string

SafeBridgeName derives a Linux-legal bridge device name from a CNI network name. Names within the IFNAMSIZ limit pass through unchanged; longer ones are deterministically truncated to a 6-char prefix plus an 8-hex-char FNV-1a suffix (total 15 chars) to preserve readability while staying unique. The empty string is preserved so callers can detect "unset".

Types

type BootstrapReport

type BootstrapReport struct {
	CniConfigDir        string
	CniCacheDir         string
	CniBinDir           string
	ConfigDirExistsPre  bool
	CacheDirExistsPre   bool
	BinDirExistsPre     bool
	ConfigDirCreated    bool
	CacheDirCreated     bool
	BinDirCreated       bool
	ConfigDirExistsPost bool
	CacheDirExistsPost  bool
	BinDirExistsPost    bool
}

BootstrapReport captures CNI environment checks and actions.

func BootstrapCNI

func BootstrapCNI(cfgDir, cacheDir, binDir string) (BootstrapReport, error)

BootstrapCNI ensures required directories exist and the bin directory is present. If any directory is empty, the defaults from config.go are used.

type BridgeIPAMConfig

type BridgeIPAMConfig struct {
	Type   string                `json:"type"`
	Ranges [][]map[string]string `json:"ranges"`
	Routes []RouteModel          `json:"routes"`
}

BridgeIPAMConfig represents the IPAM configuration for the bridge plugin.

type BridgePluginModel

type BridgePluginModel struct {
	Type      string           `json:"type"`
	Bridge    string           `json:"bridge"`
	IsGateway bool             `json:"isGateway"`
	IPMasq    bool             `json:"ipMasq"`
	IPAM      BridgeIPAMConfig `json:"ipam"`
}

BridgePluginModel represents the bridge plugin configuration in a conflist.

type Conf

type Conf struct {
	CniConfigDir string
	CniBinDir    string
	CniCacheDir  string
}

Conf holds CNI configuration paths.

type ConflistModel

type ConflistModel struct {
	CNIVersion string        `json:"cniVersion"`
	Name       string        `json:"name"`
	Plugins    []interface{} `json:"plugins"`
}

ConflistModel represents the structure of a CNI conflist file.

type LoopbackPluginModel

type LoopbackPluginModel struct {
	Type string `json:"type"`
}

LoopbackPluginModel represents the loopback plugin configuration in a conflist.

type Manager

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

Manager manages CNI network operations.

func NewManager

func NewManager(cniBinDir, cniConfigDir, cniCacheDir string) (*Manager, error)

NewManager creates a new CNI manager with the provided directories. cniBinDir: where plugins live, e.g. /opt/cni/bin cniConfigDir: where network configs live, e.g. /opt/cni/net.d cniCacheDir: where CNI stores cache, e.g. /opt/cni/cache.

func (*Manager) AddContainerToNetwork

func (m *Manager) AddContainerToNetwork(ctx context.Context, containerID, netnsPath string) error

AddContainerToNetwork adds a container to the CNI network.

func (*Manager) CreateNetwork

func (m *Manager) CreateNetwork(networkName, bridgeName, subnetCIDR string) (string, error)

CreateNetwork is a backward-compatible helper using discrete params.

func (*Manager) CreateNetworkWithConfig

func (m *Manager) CreateNetworkWithConfig(cfg NetworkConfig) (string, error)

CreateNetworkWithConfig creates a CNI network conflist file using the provided NetworkConfig.

func (*Manager) DelContainerFromNetwork

func (m *Manager) DelContainerFromNetwork(ctx context.Context, containerID, netnsPath string) error

DelContainerFromNetwork removes a container from the CNI network.

func (*Manager) DeleteNetwork

func (m *Manager) DeleteNetwork(networkName, configPath string) error

DeleteNetwork removes a CNI network config file from the filesystem.

func (*Manager) ExistsNetworkConfig

func (m *Manager) ExistsNetworkConfig(networkName, configPath string) (bool, string, error)

ExistsNetworkConfig checks if a network config exists and matches the expected network name.

func (*Manager) LoadNetworkConfigList

func (m *Manager) LoadNetworkConfigList(configPath string) error

LoadNetworkConfigList loads a CNI network config list from the given path.

func (*Manager) ReadBridgeName added in v0.2.0

func (m *Manager) ReadBridgeName(configPath string) (string, error)

ReadBridgeName parses the conflist at configPath and returns the `bridge` field of the first plugin whose `type` is "bridge". Returns errdefs.ErrNetworkNotFound if the file is missing, errdefs.ErrBridgePluginMissing if no bridge plugin is present, and a wrapped JSON error for malformed content. The returned string may be empty when the bridge plugin has no `bridge` field, which callers should treat as a mismatch.

type NetworkConfig

type NetworkConfig struct {
	// Name is the CNI network name and the filename (without extension).
	Name string
	// BridgeName is the bridge device name. Defaults to "cni0" when empty.
	BridgeName string
	// SubnetCIDR is the IPAM subnet CIDR. Defaults to "10.88.0.0/16" when empty.
	SubnetCIDR string
}

NetworkConfig represents the desired parameters for creating a CNI network.

func NewCNINetworkConfig

func NewCNINetworkConfig(name string) NetworkConfig

NewCNINetworkConfig builds a NetworkConfig with sensible defaults.

type RouteModel

type RouteModel struct {
	Dst string `json:"dst"`
}

RouteModel represents a route in the IPAM configuration.

Jump to

Keyboard shortcuts

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