k3s/vendor/github.com/containerd/go-cni/README.md

93 lines
2.9 KiB
Markdown
Raw Normal View History

2020-08-10 17:43:49 +00:00
[![Build Status](https://travis-ci.org/containerd/go-cni.svg?branch=master)](https://travis-ci.org/containerd/go-cni) [![GoDoc](https://godoc.org/github.com/containerd/go-cni?status.svg)](https://godoc.org/github.com/containerd/go-cni)
2019-01-12 04:58:27 +00:00
# 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](https://github.com/containernetworking/cni)
## Usage
2019-09-27 21:51:53 +00:00
```go
package main
import (
"context"
"fmt"
"log"
gocni "github.com/containerd/go-cni"
)
2019-01-12 04:58:27 +00:00
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"
2019-01-12 04:58:27 +00:00
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)
}
2019-01-12 04:58:27 +00:00
// Load the cni configuration
if err := l.Load(gocni.WithLoNetwork, gocni.WithDefaultConf); err != nil {
log.Fatalf("failed to load cni configuration: %v", err)
2019-01-12 04:58:27 +00:00
}
2019-01-12 04:58:27 +00:00
// 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",
2019-01-12 04:58:27 +00:00
}
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))
2019-01-12 04:58:27 +00:00
if err != nil {
log.Fatalf("failed to setup network for namespace: %v", err)
2019-01-12 04:58:27 +00:00
}
2019-01-12 04:58:27 +00:00
// 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)
}
```
2019-09-27 21:51:53 +00:00
## Project details
The go-cni is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE).
As a containerd sub-project, you will find the:
* [Project governance](https://github.com/containerd/project/blob/master/GOVERNANCE.md),
* [Maintainers](https://github.com/containerd/project/blob/master/MAINTAINERS),
* and [Contributing guidelines](https://github.com/containerd/project/blob/master/CONTRIBUTING.md)
information in our [`containerd/project`](https://github.com/containerd/project) repository.