buildkit/vendor/github.com/containerd/go-cni
Tonis Tiigi d33756e17b vendor: update containerd to efa0e809
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
2020-08-30 23:04:25 -07:00
..
.travis.yml vendor: update containerd to eb6354a11 2020-07-28 17:48:03 -07:00
LICENSE add cni networking support 2019-07-10 14:42:30 -07:00
README.md vendor: update containerd to efa0e809 2020-08-30 23:04:25 -07:00
cni.go vendor: update containerd to v1.3.0-beta.2 2019-08-21 20:22:55 -07:00
errors.go vendor: update containerd to eb6354a11 2020-07-28 17:48:03 -07:00
go.mod vendor: update containerd to efa0e809 2020-08-30 23:04:25 -07:00
go.sum vendor: update containerd to efa0e809 2020-08-30 23:04:25 -07:00
helper.go vendor: update containerd to v1.3.0-beta.2 2019-08-21 20:22:55 -07:00
namespace.go vendor: update containerd to v1.3.0-beta.2 2019-08-21 20:22:55 -07:00
namespace_opts.go vendor: update containerd (and various packages) 2020-02-28 10:24:10 +09:00
opts.go vendor: update containerd (and various packages) 2020-02-28 10:24:10 +09:00
result.go vendor: update containerd to v1.3.0-beta.2 2019-08-21 20:22:55 -07:00
testutils.go add cni networking support 2019-07-10 14:42:30 -07:00
types.go vendor: update containerd (and various packages) 2020-02-28 10:24:10 +09:00

README.md

Build Status GoDoc

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

go-cni aims to support plugins that implement 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.