legacy_examples

package
v1.1.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package legacy_examples contains sample code from prior versions of the CNI library, for use in verifying backwards compatibility.

Index

Constants

This section is empty.

Variables

View Source
var ExpectedResult = &types020.Result{
	CNIVersion: "0.1.0",
	IP4: &types020.IPConfig{
		IP: net.IPNet{
			IP:   net.ParseIP("10.1.2.3"),
			Mask: net.CIDRMask(24, 32),
		},
		Gateway: net.ParseIP("10.1.2.1"),
		Routes: []types.Route{
			{
				Dst: net.IPNet{
					IP:   net.ParseIP("0.0.0.0"),
					Mask: net.CIDRMask(0, 32),
				},
				GW: net.ParseIP("10.1.0.1"),
			},
		},
	},
	DNS: types.DNS{
		Nameservers: []string{"8.8.8.8"},
		Domain:      "example.com",
	},
}

ExpectedResult is the current representation of the plugin result that is expected from each of the examples.

As we change the CNI spec, the Result type and this value may change. The text of the example plugins should not.

View Source
var V010 = Example{
	Name:          "example_v010",
	CNIRepoGitRef: "2c482f4",
	PluginSource: `package main

import (
	"net"

	"github.com/containernetworking/cni/pkg/skel"
	"github.com/containernetworking/cni/pkg/types"
)

var result = types.Result{
	IP4: &types.IPConfig{
		IP: net.IPNet{
			IP:   net.ParseIP("10.1.2.3"),
			Mask: net.CIDRMask(24, 32),
		},
		Gateway: net.ParseIP("10.1.2.1"),
		Routes: []types.Route{
			types.Route{
				Dst: net.IPNet{
					IP:   net.ParseIP("0.0.0.0"),
					Mask: net.CIDRMask(0, 32),
				},
				GW: net.ParseIP("10.1.0.1"),
			},
		},
	},
	DNS: types.DNS{
		Nameservers: []string{"8.8.8.8"},
		Domain:      "example.com",
	},
}

func c(_ *skel.CmdArgs) error { result.Print(); return nil }

func main() { skel.PluginMain(c, c) }
`,
}

V010 acts like a CNI plugin from the v0.1.0 era

View Source
var V010_Runtime = ExampleRuntime{
	NetConfs: []string{"unversioned", "0.1.0"},
	Example: Example{
		Name:          "example_invoker_v010",
		CNIRepoGitRef: "c0d34c69",
		PluginSource: `package main

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/containernetworking/cni/libcni"
)

func main(){
	code :=	exec()
	os.Exit(code)
}

func exec() int {
	confBytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Printf("could not read netconfig from stdin: %+v", err)
		return 1
	}

	netConf, err := libcni.ConfFromBytes(confBytes)
	if err != nil {
		fmt.Printf("could not parse netconfig: %+v", err)
		return 1
	}
	fmt.Printf("Parsed network configuration: %+v\n", netConf.Network)

	if len(os.Args) == 1 {
		fmt.Printf("Expect CNI plugin paths in argv")
		return 1
	}

	runtimeConf := &libcni.RuntimeConf{
		ContainerID: "some-container-id",
		NetNS:       "/some/netns/path",
		IfName:      "eth0",
	}

	cniConfig := &libcni.CNIConfig{Path: os.Args[1:]}

	result, err := cniConfig.AddNetwork(netConf, runtimeConf)
	if err != nil {
		fmt.Printf("AddNetwork failed: %+v", err)
		return 2
	}
	fmt.Printf("AddNetwork result: %+v", result)

	// Validate expected results
	const expectedIP4 string = "1.2.3.30/24"
	if result.IP4.IP.String() != expectedIP4 {
		fmt.Printf("Expected IPv4 address %q, got %q", expectedIP4, result.IP4.IP.String())
		return 3
	}
	const expectedIP6 string = "abcd:1234:ffff::cdde/64"
	if result.IP6.IP.String() != expectedIP6 {
		fmt.Printf("Expected IPv6 address %q, got %q", expectedIP6, result.IP6.IP.String())
		return 4
	}

	err = cniConfig.DelNetwork(netConf, runtimeConf)
	if err != nil {
		fmt.Printf("DelNetwork failed: %v", err)
		return 5
	}

	return 0
}
`,
	},
}

V010_Runtime creates a simple noop network configuration, then executes libcni against the noop test plugin.

Functions

This section is empty.

Types

type Example

type Example struct {
	Name          string
	CNIRepoGitRef string
	PluginSource  string
}

An Example is a Git reference to the CNI repo and a Golang CNI plugin that builds against that version of the repo.

By convention, every Example plugin returns an ADD result that is semantically equivalent to the ExpectedResult.

func (Example) Build

func (e Example) Build() (string, error)

Build builds the example, returning the path to the binary

type ExampleNetConf added in v0.6.0

type ExampleNetConf struct {
	Config string
	// contains filtered or unexported fields
}

func (*ExampleNetConf) Cleanup added in v0.6.0

func (c *ExampleNetConf) Cleanup()

type ExampleRuntime

type ExampleRuntime struct {
	Example
	NetConfs []string // The network configuration names to pass
}

An ExampleRuntime is a small program that uses libcni to invoke a network plugin. It should call ADD and DELETE, verifying all intermediate steps and data structures.

func (*ExampleRuntime) GenerateNetConf added in v0.6.0

func (e *ExampleRuntime) GenerateNetConf(name string) (*ExampleNetConf, error)

Jump to

Keyboard shortcuts

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