| 1 |
// Copyright 2016 CNI authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package legacy_examples |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
|
| 21 |
noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug" |
| 22 |
) |
| 23 |
|
| 24 |
// An ExampleRuntime is a small program that uses libcni to invoke a network plugin. |
| 25 |
// It should call ADD and DELETE, verifying all intermediate steps |
| 26 |
// and data structures. |
| 27 |
type ExampleRuntime struct { |
| 28 |
Example |
| 29 |
NetConfs []string // The network configuration names to pass |
| 30 |
} |
| 31 |
|
| 32 |
type exampleNetConfTemplate struct { |
| 33 |
conf string |
| 34 |
result string |
| 35 |
} |
| 36 |
|
| 37 |
// NetConfs are various versioned network configuration files. Examples should |
| 38 |
// specify which version they expect |
| 39 |
var netConfTemplates = map[string]exampleNetConfTemplate{ |
| 40 |
"unversioned": { |
| 41 |
conf: `{ |
| 42 |
"name": "default", |
| 43 |
"type": "noop", |
| 44 |
"debugFile": "%s" |
| 45 |
}`, |
| 46 |
result: `{ |
| 47 |
"ip4": { |
| 48 |
"ip": "1.2.3.30/24", |
| 49 |
"gateway": "1.2.3.1", |
| 50 |
"routes": [ |
| 51 |
{ |
| 52 |
"dst": "15.5.6.0/24", |
| 53 |
"gw": "15.5.6.8" |
| 54 |
} |
| 55 |
] |
| 56 |
}, |
| 57 |
"ip6": { |
| 58 |
"ip": "abcd:1234:ffff::cdde/64", |
| 59 |
"gateway": "abcd:1234:ffff::1", |
| 60 |
"routes": [ |
| 61 |
{ |
| 62 |
"dst": "1111:dddd::/80", |
| 63 |
"gw": "1111:dddd::aaaa" |
| 64 |
} |
| 65 |
] |
| 66 |
}, |
| 67 |
"dns":{} |
| 68 |
}`, |
| 69 |
}, |
| 70 |
"0.1.0": { |
| 71 |
conf: `{ |
| 72 |
"cniVersion": "0.1.0", |
| 73 |
"name": "default", |
| 74 |
"type": "noop", |
| 75 |
"debugFile": "%s" |
| 76 |
}`, |
| 77 |
result: `{ |
| 78 |
"cniVersion": "0.1.0", |
| 79 |
"ip4": { |
| 80 |
"ip": "1.2.3.30/24", |
| 81 |
"gateway": "1.2.3.1", |
| 82 |
"routes": [ |
| 83 |
{ |
| 84 |
"dst": "15.5.6.0/24", |
| 85 |
"gw": "15.5.6.8" |
| 86 |
} |
| 87 |
] |
| 88 |
}, |
| 89 |
"ip6": { |
| 90 |
"ip": "abcd:1234:ffff::cdde/64", |
| 91 |
"gateway": "abcd:1234:ffff::1", |
| 92 |
"routes": [ |
| 93 |
{ |
| 94 |
"dst": "1111:dddd::/80", |
| 95 |
"gw": "1111:dddd::aaaa" |
| 96 |
} |
| 97 |
] |
| 98 |
}, |
| 99 |
"dns":{} |
| 100 |
}`, |
| 101 |
}, |
| 102 |
} |
| 103 |
|
| 104 |
func (e *ExampleRuntime) GenerateNetConf(name string) (*ExampleNetConf, error) { |
| 105 |
template, ok := netConfTemplates[name] |
| 106 |
if !ok { |
| 107 |
return nil, fmt.Errorf("unknown example net config template %q", name) |
| 108 |
} |
| 109 |
|
| 110 |
debugFile, err := os.CreateTemp("", "cni_debug") |
| 111 |
if err != nil { |
| 112 |
return nil, fmt.Errorf("failed to create noop plugin debug file: %w", err) |
| 113 |
} |
| 114 |
debugFilePath := debugFile.Name() |
| 115 |
|
| 116 |
debug := &noop_debug.Debug{ |
| 117 |
ReportResult: template.result, |
| 118 |
} |
| 119 |
if err := debug.WriteDebug(debugFilePath); err != nil { |
| 120 |
os.Remove(debugFilePath) |
| 121 |
return nil, fmt.Errorf("failed to write noop plugin debug file %q: %w", debugFilePath, err) |
| 122 |
} |
| 123 |
conf := &ExampleNetConf{ |
| 124 |
Config: fmt.Sprintf(template.conf, debugFilePath), |
| 125 |
debugFilePath: debugFilePath, |
| 126 |
} |
| 127 |
|
| 128 |
return conf, nil |
| 129 |
} |
| 130 |
|
| 131 |
type ExampleNetConf struct { |
| 132 |
Config string |
| 133 |
debugFilePath string |
| 134 |
} |
| 135 |
|
| 136 |
func (c *ExampleNetConf) Cleanup() { |
| 137 |
os.Remove(c.debugFilePath) |
| 138 |
} |
| 139 |
|
| 140 |
// V010_Runtime creates a simple noop network configuration, then |
| 141 |
// executes libcni against the noop test plugin. |
| 142 |
var V010_Runtime = ExampleRuntime{ |
| 143 |
NetConfs: []string{"unversioned", "0.1.0"}, |
| 144 |
Example: Example{ |
| 145 |
Name: "example_invoker_v010", |
| 146 |
CNIRepoGitRef: "c0d34c69", // version with ns.Do |
| 147 |
PluginSource: `package main |
| 148 |
|
| 149 |
import ( |
| 150 |
"fmt" |
| 151 |
"io" |
| 152 |
"os" |
| 153 |
|
| 154 |
"github.com/containernetworking/cni/libcni" |
| 155 |
) |
| 156 |
|
| 157 |
func main(){ |
| 158 |
code := exec() |
| 159 |
os.Exit(code) |
| 160 |
} |
| 161 |
|
| 162 |
func exec() int { |
| 163 |
confBytes, err := io.ReadAll(os.Stdin) |
| 164 |
if err != nil { |
| 165 |
fmt.Printf("could not read netconfig from stdin: %+v", err) |
| 166 |
return 1 |
| 167 |
} |
| 168 |
|
| 169 |
netConf, err := libcni.ConfFromBytes(confBytes) |
| 170 |
if err != nil { |
| 171 |
fmt.Printf("could not parse netconfig: %+v", err) |
| 172 |
return 1 |
| 173 |
} |
| 174 |
fmt.Printf("Parsed network configuration: %+v\n", netConf.Network) |
| 175 |
|
| 176 |
if len(os.Args) == 1 { |
| 177 |
fmt.Printf("Expect CNI plugin paths in argv") |
| 178 |
return 1 |
| 179 |
} |
| 180 |
|
| 181 |
runtimeConf := &libcni.RuntimeConf{ |
| 182 |
ContainerID: "some-container-id", |
| 183 |
NetNS: "/some/netns/path", |
| 184 |
IfName: "eth0", |
| 185 |
} |
| 186 |
|
| 187 |
cniConfig := &libcni.CNIConfig{Path: os.Args[1:]} |
| 188 |
|
| 189 |
result, err := cniConfig.AddNetwork(netConf, runtimeConf) |
| 190 |
if err != nil { |
| 191 |
fmt.Printf("AddNetwork failed: %+v", err) |
| 192 |
return 2 |
| 193 |
} |
| 194 |
fmt.Printf("AddNetwork result: %+v", result) |
| 195 |
|
| 196 |
// Validate expected results |
| 197 |
const expectedIP4 string = "1.2.3.30/24" |
| 198 |
if result.IP4.IP.String() != expectedIP4 { |
| 199 |
fmt.Printf("Expected IPv4 address %q, got %q", expectedIP4, result.IP4.IP.String()) |
| 200 |
return 3 |
| 201 |
} |
| 202 |
const expectedIP6 string = "abcd:1234:ffff::cdde/64" |
| 203 |
if result.IP6.IP.String() != expectedIP6 { |
| 204 |
fmt.Printf("Expected IPv6 address %q, got %q", expectedIP6, result.IP6.IP.String()) |
| 205 |
return 4 |
| 206 |
} |
| 207 |
|
| 208 |
err = cniConfig.DelNetwork(netConf, runtimeConf) |
| 209 |
if err != nil { |
| 210 |
fmt.Printf("DelNetwork failed: %v", err) |
| 211 |
return 5 |
| 212 |
} |
| 213 |
|
| 214 |
return 0 |
| 215 |
} |
| 216 |
`, |
| 217 |
}, |
| 218 |
} |