| 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 contains sample code from prior versions of |
| 16 |
// the CNI library, for use in verifying backwards compatibility. |
| 17 |
package legacy_examples |
| 18 |
|
| 19 |
import ( |
| 20 |
"net" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"runtime" |
| 24 |
"sync" |
| 25 |
|
| 26 |
"github.com/containernetworking/cni/pkg/types" |
| 27 |
"github.com/containernetworking/cni/pkg/types/020" |
| 28 |
"github.com/containernetworking/cni/pkg/version/testhelpers" |
| 29 |
) |
| 30 |
|
| 31 |
// An Example is a Git reference to the CNI repo and a Golang CNI plugin that |
| 32 |
// builds against that version of the repo. |
| 33 |
// |
| 34 |
// By convention, every Example plugin returns an ADD result that is |
| 35 |
// semantically equivalent to the ExpectedResult. |
| 36 |
type Example struct { |
| 37 |
Name string |
| 38 |
CNIRepoGitRef string |
| 39 |
PluginSource string |
| 40 |
} |
| 41 |
|
| 42 |
var ( |
| 43 |
buildDir = "" |
| 44 |
buildDirLock sync.Mutex |
| 45 |
) |
| 46 |
|
| 47 |
func ensureBuildDirExists() error { |
| 48 |
buildDirLock.Lock() |
| 49 |
defer buildDirLock.Unlock() |
| 50 |
|
| 51 |
if buildDir != "" { |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
var err error |
| 56 |
buildDir, err = os.MkdirTemp("", "cni-example-plugins") |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
// Build builds the example, returning the path to the binary |
| 61 |
func (e Example) Build() (string, error) { |
| 62 |
if err := ensureBuildDirExists(); err != nil { |
| 63 |
return "", err |
| 64 |
} |
| 65 |
|
| 66 |
outBinPath := filepath.Join(buildDir, e.Name) |
| 67 |
if runtime.GOOS == "windows" { |
| 68 |
outBinPath += ".exe" |
| 69 |
} |
| 70 |
|
| 71 |
if err := testhelpers.BuildAt([]byte(e.PluginSource), e.CNIRepoGitRef, outBinPath); err != nil { |
| 72 |
return "", err |
| 73 |
} |
| 74 |
return outBinPath, nil |
| 75 |
} |
| 76 |
|
| 77 |
// V010 acts like a CNI plugin from the v0.1.0 era |
| 78 |
var V010 = Example{ |
| 79 |
Name: "example_v010", |
| 80 |
CNIRepoGitRef: "2c482f4", |
| 81 |
PluginSource: `package main |
| 82 |
|
| 83 |
import ( |
| 84 |
"net" |
| 85 |
|
| 86 |
"github.com/containernetworking/cni/pkg/skel" |
| 87 |
"github.com/containernetworking/cni/pkg/types" |
| 88 |
) |
| 89 |
|
| 90 |
var result = types.Result{ |
| 91 |
IP4: &types.IPConfig{ |
| 92 |
IP: net.IPNet{ |
| 93 |
IP: net.ParseIP("10.1.2.3"), |
| 94 |
Mask: net.CIDRMask(24, 32), |
| 95 |
}, |
| 96 |
Gateway: net.ParseIP("10.1.2.1"), |
| 97 |
Routes: []types.Route{ |
| 98 |
types.Route{ |
| 99 |
Dst: net.IPNet{ |
| 100 |
IP: net.ParseIP("0.0.0.0"), |
| 101 |
Mask: net.CIDRMask(0, 32), |
| 102 |
}, |
| 103 |
GW: net.ParseIP("10.1.0.1"), |
| 104 |
}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
DNS: types.DNS{ |
| 108 |
Nameservers: []string{"8.8.8.8"}, |
| 109 |
Domain: "example.com", |
| 110 |
}, |
| 111 |
} |
| 112 |
|
| 113 |
func c(_ *skel.CmdArgs) error { result.Print(); return nil } |
| 114 |
|
| 115 |
func main() { skel.PluginMain(c, c) } |
| 116 |
`, |
| 117 |
} |
| 118 |
|
| 119 |
// ExpectedResult is the current representation of the plugin result |
| 120 |
// that is expected from each of the examples. |
| 121 |
// |
| 122 |
// As we change the CNI spec, the Result type and this value may change. |
| 123 |
// The text of the example plugins should not. |
| 124 |
var ExpectedResult = &types020.Result{ |
| 125 |
CNIVersion: "0.1.0", |
| 126 |
IP4: &types020.IPConfig{ |
| 127 |
IP: net.IPNet{ |
| 128 |
IP: net.ParseIP("10.1.2.3"), |
| 129 |
Mask: net.CIDRMask(24, 32), |
| 130 |
}, |
| 131 |
Gateway: net.ParseIP("10.1.2.1"), |
| 132 |
Routes: []types.Route{ |
| 133 |
{ |
| 134 |
Dst: net.IPNet{ |
| 135 |
IP: net.ParseIP("0.0.0.0"), |
| 136 |
Mask: net.CIDRMask(0, 32), |
| 137 |
}, |
| 138 |
GW: net.ParseIP("10.1.0.1"), |
| 139 |
}, |
| 140 |
}, |
| 141 |
}, |
| 142 |
DNS: types.DNS{ |
| 143 |
Nameservers: []string{"8.8.8.8"}, |
| 144 |
Domain: "example.com", |
| 145 |
}, |
| 146 |
} |