Giter Site home page Giter Site logo

go-cni's Introduction

go-cni

PkgGoDev Build Status codecov Go Report Card

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 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.

go-cni's People

Contributors

abhi avatar akihirosuda avatar alexellis avatar aojea avatar austinvazquez avatar claudiubelu avatar crosbymichael avatar dmcgowan avatar estesp avatar fuweid avatar hainesc avatar joewrightss avatar johscheuer avatar jterry75 avatar junnplus avatar kuramal avatar kzys avatar marc-schwind avatar mikebrow avatar nickethier avatar pigletfly avatar random-liu avatar squeed avatar thajeztah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-cni's Issues

ip may be leaked when call RunPodSandbox failed

environments

  • containerd 1.4.0
  • cni v0.6.0

cni configuration

{
  "name":"cni0",
  "cniVersion":"0.3.1",
  "plugins":[
    {
      "type":"flannel",
      "delegate":{
        "forceAddress":true,
        "hairpinMode": true,
        "isDefaultGateway":true
      }
    },
    {
      "type":"portmap",
      "capabilities":{
        "portMappings":true
      }
    }
  ]
}

containerd log

Oct 13 19:57:27 node-6 containerd: time="2020-10-13T19:57:27.000650503+08:00" level=error msg="Failed to destroy network for sandbox \"48579e8be48f4e17144c81e6750947513bd790985c3513a78a84d3569d94b33c\"" error="delegateDel: error invoking ConflistDel - \"cni0\": conflistDel: error in getting result from DelNetworkList: could not teardown ipv4 snat: running [/usr/sbin/iptables -t nat -N CNI-SN-789e45a28000a6e5f5d67 --wait]: exit status 4: iptables: Resource temporarily unavailable.\n"

expected

ip recycled

real

ip leaked

look like, this should be cni question, but cni plugins should return failed also

cni 101 missing converters

note... until cni adds a converter (see below) for 1.0.1 we can't move up the version we are using in containerd or we won't be able to do a live update of containerd.. because when you try to remove the pod and it goes to destroy the network.. you'll get one of these errors:

FATA[0000] stopping the pod sandbox "9b": rpc error: code = Unknown desc = failed to destroy network for sandbox "9b068ec35dfaffe5e92287db5d24731a30569d13ab88addfeb07361956d407e4": failed to get network "containerd-net" cached result: failed to convert cached result to config version "1.0.1": no converter for CNI result version 0.4.0 to 1.0.1 

here's the list of existing converters: https://github.com/containernetworking/cni/blob/master/pkg/types/100/types.go#L43

Question: Possible to find CNI Pod?

I apologize in advance if this isn't the right place for this question. I'm trying to identify the pod responsible for providing the CNI to a node. I have a use case where I need to destroy all pods on a node, without using Kubernetes, and I need to destroy the CNI pod last. The reason I need to do this is because in the case of cilium, if I destroy the cilium pod first, I get the following error when attempting to destroy subsequent pods:

StopPodSandbox "c0e0d734fbf5d44c44cac7ca535475a0a747b55abee1782c842fc270b1a254e6" from runtime service failed: rpc error: code = Unknown desc = failed to destroy network for sandbox "c0e0d734fbf5d44c44cac7ca535475a0a747b55abee1782c842fc270b1a254e6": unable to connect to Cilium daemon: failed to create cilium agent client after 30.000000 seconds timeout: Get http:///var/run/cilium/cilium.sock/v1/config: dial unix /var/run/cilium/cilium.sock: connect: no such file or directory

I don't know if I will face something similar with other CNIs, and I'd like to discover the CNI pod dynamically so that I can destroy it last. So maybe I have 2 questions:

  1. Is it possible to discover which pod is providing the CNI to a given node?
  2. If so, can I use this package to do so?

I am posting these questions here since I am using containerd as the CRI, so I figured the maintainers of this package would know best. I appreciate any kind of help you guys can offer 🙂.

go test data race in parallel network setup

After bumping go-cni from 1.1.0 to 1.1.1 we saw data race warning from go test -race in our containerd integration test suit. The tests are with no change only go-cni bumped.

==================
WARNING: DATA RACE
Read at 0x00c00046e598 by goroutine 45:
  github.com/containernetworking/cni/libcni.(*CNIConfig).ensureExec()
      github.com/containernetworking/[email protected]/libcni/api.go:183 +0x99
  github.com/containernetworking/cni/libcni.(*CNIConfig).addNetwork()
      github.com/containernetworking/[email protected]/libcni/api.go:394 +0x88
  github.com/containernetworking/cni/libcni.(*CNIConfig).AddNetworkList()
      github.com/containernetworking/[email protected]/libcni/api.go:422 +0x18f
  github.com/containerd/go-cni.(*Network).Attach()
      github.com/containerd/[email protected]/namespace.go:33 +0xbd
  github.com/containerd/go-cni.asynchAttach()
      github.com/containerd/[email protected]/cni.go:165 +0xd2
  github.com/containerd/go-cni.(*libcni).attachNetworks·dwrap·5()
      github.com/containerd/[email protected]/cni.go:177 +0x99

Previous write at 0x00c00046e598 by goroutine 44:
  github.com/containernetworking/cni/libcni.(*CNIConfig).ensureExec()
      github.com/containernetworking/[email protected]/libcni/api.go:184 +0x191
  github.com/containernetworking/cni/libcni.(*CNIConfig).addNetwork()
      github.com/containernetworking/[email protected]/libcni/api.go:394 +0x88
  github.com/containernetworking/cni/libcni.(*CNIConfig).AddNetworkList()
      github.com/containernetworking/[email protected]/libcni/api.go:422 +0x18f
  github.com/containerd/go-cni.(*Network).Attach()
      github.com/containerd/[email protected]/namespace.go:33 +0xbd
  github.com/containerd/go-cni.asynchAttach()
      github.com/containerd/[email protected]/cni.go:165 +0xd2
  github.com/containerd/go-cni.(*libcni).attachNetworks·dwrap·5()
      github.com/containerd/[email protected]/cni.go:177 +0x99

Goroutine 45 (running) created at:
  github.com/containerd/go-cni.(*libcni).attachNetworks()
      github.com/containerd/[email protected]/cni.go:177 +0xf0
  github.com/containerd/go-cni.(*libcni).Setup()
      github.com/containerd/[email protected]/cni.go:150 +0xd0
  github.com/concourse/concourse/worker/runtime.cniNetwork.Add()
      github.com/concourse/concourse/worker/runtime/cni_network.go:382 +0xf3
  github.com/concourse/concourse/worker/runtime.(*cniNetwork).Add()
      <autogenerated>:1 +0x127

In the testing, while creating a containerd container, it tries to get the id and path of a containerd task to pass to cni Setup(). Both contaner and task are newly created for individual test. The only thing shared between our tests is the CNI client for setting up networks.

can't install with go modules

go get github.com/containerd/go-cni

results in:

go: finding github.com/containerd/go-cni latest
go: downloading github.com/containerd/go-cni v0.0.0-20190610170741-5a4663dad645
go: extracting github.com/containerd/go-cni v0.0.0-20190610170741-5a4663dad645
# github.com/containerd/go-cni
../../go/pkg/mod/github.com/containerd/[email protected]/namespace.go:31:32: not enough arguments in call to n.cni.AddNetworkList
	have (*libcni.NetworkConfigList, *libcni.RuntimeConf)
	want (context.Context, *libcni.NetworkConfigList, *libcni.RuntimeConf)
../../go/pkg/mod/github.com/containerd/[email protected]/namespace.go:39:29: not enough arguments in call to n.cni.DelNetworkList
	have (*libcni.NetworkConfigList, *libcni.RuntimeConf)
	want (context.Context, *libcni.NetworkConfigList, *libcni.RuntimeConf)

Loading configuration with multiple plugins fail with "WithConfFile"

Whenever I try to load a config with cni.Load(gocni.WithLoNetwork, gocni.WithConfFile(filepath.Join(CNIConfDir, DefaultCNIConfFilename))) that has multiple plugins like:

{
    "cniVersion": "0.4.0",
    "name": "mybridge",
    "type": "bridge",
    "plugins": [
      {
        "type": "bridge",
        "bridge": "myb0",
        "isGateway": true,
        "ipMasq": true,
        "ipam": {
            "type": "host-local",
            "subnet": "10.62.0.0/16",
            "routes": [
                { "dst": "0.0.0.0/0" }
            ]
        }
      },
      {
        "type": "firewall"
      }
    ]
}

I get an error like cni config load failed: error parsing configuration: missing 'type'.

If I load the same config with cni.Load(gocni.WithLoNetwork, gocni.WithDefaultConf), it successes.

Shouldn't both have same behavior where by using WithConfFile I can guarantee that I'm loading the correct config?

cannot use mockCNI (type *MockCNI) as type libcni.CNI

With github.com/containernetworking/cni 0.7.0 and Golang 1.12.2:

Testing    in: /builddir/build/BUILD/go-cni-891c2a41e18144b2d7921f971d6c9789a68046b2/_build/src
         PATH: /builddir/build/BUILD/go-cni-891c2a41e18144b2d7921f971d6c9789a68046b2/_build/bin:/builddir/.local/bin:/builddir/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
       GOPATH: /builddir/build/BUILD/go-cni-891c2a41e18144b2d7921f971d6c9789a68046b2/_build:/usr/share/gocode
  GO111MODULE: off
      command: go test -buildmode pie -compiler gc -ldflags "-X github.com/containerd/go-cni/version.commit=891c2a41e18144b2d7921f971d6c9789a68046b2 -X github.com/containerd/go-cni/version=0 -extldflags '-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld '"
      testing: github.com/containerd/go-cni
github.com/containerd/go-cni
# github.com/containerd/go-cni [github.com/containerd/go-cni.test]
./namespace.go:31:32: not enough arguments in call to n.cni.AddNetworkList
	have (*libcni.NetworkConfigList, *libcni.RuntimeConf)
	want (context.Context, *libcni.NetworkConfigList, *libcni.RuntimeConf)
./namespace.go:39:29: not enough arguments in call to n.cni.DelNetworkList
	have (*libcni.NetworkConfigList, *libcni.RuntimeConf)
	want (context.Context, *libcni.NetworkConfigList, *libcni.RuntimeConf)
./cni_test.go:50:20: cannot use mockCNI (type *MockCNI) as type libcni.CNI in assignment:
	*MockCNI does not implement libcni.CNI (wrong type for AddNetwork method)
		have AddNetwork(*libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
		want AddNetwork(context.Context, *libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
./cni_test.go:66:20: cannot use mockCNI (type *MockCNI) as type libcni.CNI in assignment:
	*MockCNI does not implement libcni.CNI (wrong type for AddNetwork method)
		have AddNetwork(*libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
		want AddNetwork(context.Context, *libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
./cni_test.go:122:20: cannot use mockCNI (type *MockCNI) as type libcni.CNI in assignment:
	*MockCNI does not implement libcni.CNI (wrong type for AddNetwork method)
		have AddNetwork(*libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
		want AddNetwork(context.Context, *libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
./cni_test.go:123:20: cannot use mockCNI (type *MockCNI) as type libcni.CNI in assignment:
	*MockCNI does not implement libcni.CNI (wrong type for AddNetwork method)
		have AddNetwork(*libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
		want AddNetwork(context.Context, *libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
./cni_test.go:151:20: cannot use mockCNI (type *MockCNI) as type libcni.CNI in assignment:
	*MockCNI does not implement libcni.CNI (wrong type for AddNetwork method)
		have AddNetwork(*libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
		want AddNetwork(context.Context, *libcni.NetworkConfig, *libcni.RuntimeConf) (types.Result, error)
FAIL	github.com/containerd/go-cni [build failed]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.