# README
go-cni
A generic CNI library to provide APIs for CNI plugin interactions. The library provides APIs to:
- Load CNI network config from different sources
- Setup networks for container namespace
- Remove networks from container namespace
- Query status of CNI network plugin initialization
- Check verifies the network is still in desired state
go-cni aims to support plugins that implement the Container Network Interface.
Usage
package main
import (
"context"
"fmt"
"log"
gocni "github.com/containerd/go-cni"
)
func main() {
id := "example"
netns := "/var/run/netns/example-ns-1"
// CNI allows multiple CNI configurations and the network interface
// will be named by eth0, eth1, ..., ethN.
ifPrefixName := "eth"
defaultIfName := "eth0"
// Initializes library
l, err := gocni.New(
// one for loopback network interface
gocni.WithMinNetworkCount(2),
gocni.WithPluginConfDir("/etc/cni/net.d"),
gocni.WithPluginDir([]string{"/opt/cni/bin"}),
// Sets the prefix for network interfaces, eth by default
gocni.WithInterfacePrefix(ifPrefixName))
if err != nil {
log.Fatalf("failed to initialize cni library: %v", err)
}
// Load the cni configuration
if err := l.Load(gocni.WithLoNetwork, gocni.WithDefaultConf); err != nil {
log.Fatalf("failed to load cni configuration: %v", err)
}
// Setup network for namespace.
labels := map[string]string{
"K8S_POD_NAMESPACE": "namespace1",
"K8S_POD_NAME": "pod1",
"K8S_POD_INFRA_CONTAINER_ID": id,
// Plugin tolerates all Args embedded by unknown labels, like
// K8S_POD_NAMESPACE/NAME/INFRA_CONTAINER_ID...
"IgnoreUnknown": "1",
}
ctx := context.Background()
// Teardown network
defer func() {
if err := l.Remove(ctx, id, netns, gocni.WithLabels(labels)); err != nil {
log.Fatalf("failed to teardown network: %v", err)
}
}()
// Setup network
result, err := l.Setup(ctx, id, netns, gocni.WithLabels(labels))
if err != nil {
log.Fatalf("failed to setup network for namespace: %v", err)
}
// Get IP of the default interface
IP := result.Interfaces[defaultIfName].IPConfigs[0].IP.String()
fmt.Printf("IP of the default interface %s:%s", defaultIfName, IP)
}
Project details
The go-cni is a containerd sub-project, licensed under the Apache 2.0 license. As a containerd sub-project, you will find the:
information in our containerd/project
repository.
# Functions
IsCNINotInitialized returns true if the error is due to cni config not being initialized.
IsInvalidConfig returns true if the error is invalid cni config.
IsInvalidResult return true if the error is due to invalid cni result.
IsNotFound returns true if the error is due to a missing config or result.
IsReadFailure return true if the error is a config read failure.
New creates a new libcni instance.
WithAllConf can be used to detect all network config files from the configured cni config directory and load them.
No description provided by the author
WithCapability support well-known capabilities https://www.cni.dev/docs/conventions/#well-known-capabilities.
WithCapabilityBandWitdh adds support for bandwidth limits.
WithCapabilityCgroupPath passes in the cgroup path capability.
WithCapabilityDNS adds support for dns.
WithCapabilityIPRanges adds support for ip ranges.
WithCapabilityPortMap adds support for port mappings.
WithConf can be used to load config directly from byte.
WithConfFile can be used to load network config from an .conf file.
WithConfIndex can be used to load config directly from byte and set the interface name's index.
WithConfListBytes can be used to load network config list directly from byte.
WithConfListFile can be used to load network config from an .conflist file.
WithDefaultConf can be used to detect the default network config file from the configured cni config directory and load it.
WithInterfacePrefix sets the prefix for network interfaces e.g.
Args.
WithLoNetwork can be used to load the loopback network config.
WithMinNetworkCount can be used to configure the minimum networks to be configured and initialized for the status to report success.
WithPluginConfDir can be used to configure the cni configuration directory.
WithPluginDir can be used to set the locations of the cni plugin binaries.
WithPluginMaxConfNum can be used to configure the max cni plugin config file num.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
BandWidth defines the ingress/egress rate and burst limits.
No description provided by the author
No description provided by the author
No description provided by the author
DNS defines the dns config.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NetworkConf is a source bytes to string conversion of cnilibrary.NetworkConfig.
NetworkConfList is a source bytes to string version of cnilibrary.NetworkConfigList.
No description provided by the author
Result contains the network information returned by CNI.Setup
a) Interfaces list.
# Interfaces
No description provided by the author
# Type aliases
Deprecated: use cni.Opt instead.
Deprecated: use cni.Result instead.
No description provided by the author
Opt sets options for a CNI instance.
No description provided by the author