File: main.go

package info (click to toggle)
golang-github-appc-cni 0.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 804 kB
  • sloc: sh: 177; makefile: 6
file content (129 lines) | stat: -rw-r--r-- 3,107 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2016 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
Noop plugin is a CNI plugin designed for use as a test-double.

When calling, set the CNI_ARGS env var equal to the path of a file containing
the JSON encoding of a Debug.
*/

package main

import (
	"errors"
	"fmt"
	"os"
	"strings"

	"github.com/containernetworking/cni/pkg/skel"
	"github.com/containernetworking/cni/pkg/version"
	noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug"
)

// parse extra args i.e. FOO=BAR;ABC=123
func parseExtraArgs(args string) (map[string]string, error) {
	m := make(map[string]string)

	items := strings.Split(args, ";")
	for _, item := range items {
		kv := strings.Split(item, "=")
		if len(kv) != 2 {
			return nil, fmt.Errorf("CNI_ARGS invalid key/value pair: %s\n", kv)
		}
		m[kv[0]] = kv[1]
	}
	return m, nil
}

func debugBehavior(args *skel.CmdArgs, command string) error {
	extraArgs, err := parseExtraArgs(args.Args)
	if err != nil {
		return err
	}

	debugFilePath, ok := extraArgs["DEBUG"]
	if !ok {
		fmt.Printf(`{}`)
		os.Stderr.WriteString("CNI_ARGS empty, no debug behavior\n")
		return nil
	}

	debug, err := noop_debug.ReadDebug(debugFilePath)
	if err != nil {
		return err
	}

	debug.CmdArgs = *args
	debug.Command = command

	if debug.ReportResult == "" {
		debug.ReportResult = fmt.Sprintf(` { "result": %q }`, noop_debug.EmptyReportResultMessage)
	}

	err = debug.WriteDebug(debugFilePath)
	if err != nil {
		return err
	}

	os.Stderr.WriteString(debug.ReportStderr)

	if debug.ReportError != "" {
		return errors.New(debug.ReportError)
	} else {
		os.Stdout.WriteString(debug.ReportResult)
	}

	return nil
}

func debugGetSupportedVersions() []string {
	vers := []string{"0.-42.0", "0.1.0", "0.2.0"}
	cniArgs := os.Getenv("CNI_ARGS")
	if cniArgs == "" {
		return vers
	}

	extraArgs, err := parseExtraArgs(cniArgs)
	if err != nil {
		panic("test setup error: invalid CNI_ARGS format")
	}

	debugFilePath, ok := extraArgs["DEBUG"]
	if !ok {
		panic("test setup error: missing DEBUG in CNI_ARGS")
	}

	debug, err := noop_debug.ReadDebug(debugFilePath)
	if err != nil {
		panic("test setup error: unable to read debug file: " + err.Error())
	}
	if debug.ReportVersionSupport == nil {
		return vers
	}
	return debug.ReportVersionSupport
}

func cmdAdd(args *skel.CmdArgs) error {
	return debugBehavior(args, "ADD")
}

func cmdDel(args *skel.CmdArgs) error {
	return debugBehavior(args, "DEL")
}

func main() {
	supportedVersions := debugGetSupportedVersions()
	skel.PluginMain(cmdAdd, cmdDel, version.PluginSupports(supportedVersions...))
}