github.com/easystack/cni@v0.8.1/pkg/version/legacy_examples/examples.go (about) 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 "io/ioutil" 21 "net" 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 buildDir = "" 43 var buildDirLock sync.Mutex 44 45 func ensureBuildDirExists() error { 46 buildDirLock.Lock() 47 defer buildDirLock.Unlock() 48 49 if buildDir != "" { 50 return nil 51 } 52 53 var err error 54 buildDir, err = ioutil.TempDir("", "cni-example-plugins") 55 return err 56 } 57 58 // Build builds the example, returning the path to the binary 59 func (e Example) Build() (string, error) { 60 if err := ensureBuildDirExists(); err != nil { 61 return "", err 62 } 63 64 outBinPath := filepath.Join(buildDir, e.Name) 65 if runtime.GOOS == "windows" { 66 outBinPath += ".exe" 67 } 68 69 if err := testhelpers.BuildAt([]byte(e.PluginSource), e.CNIRepoGitRef, outBinPath); err != nil { 70 return "", err 71 } 72 return outBinPath, nil 73 } 74 75 // V010 acts like a CNI plugin from the v0.1.0 era 76 var V010 = Example{ 77 Name: "example_v010", 78 CNIRepoGitRef: "2c482f4", 79 PluginSource: `package main 80 81 import ( 82 "net" 83 84 "github.com/containernetworking/cni/pkg/skel" 85 "github.com/containernetworking/cni/pkg/types" 86 ) 87 88 var result = types.Result{ 89 IP4: &types.IPConfig{ 90 IP: net.IPNet{ 91 IP: net.ParseIP("10.1.2.3"), 92 Mask: net.CIDRMask(24, 32), 93 }, 94 Gateway: net.ParseIP("10.1.2.1"), 95 Routes: []types.Route{ 96 types.Route{ 97 Dst: net.IPNet{ 98 IP: net.ParseIP("0.0.0.0"), 99 Mask: net.CIDRMask(0, 32), 100 }, 101 GW: net.ParseIP("10.1.0.1"), 102 }, 103 }, 104 }, 105 DNS: types.DNS{ 106 Nameservers: []string{"8.8.8.8"}, 107 Domain: "example.com", 108 }, 109 } 110 111 func c(_ *skel.CmdArgs) error { result.Print(); return nil } 112 113 func main() { skel.PluginMain(c, c) } 114 `, 115 } 116 117 // ExpectedResult is the current representation of the plugin result 118 // that is expected from each of the examples. 119 // 120 // As we change the CNI spec, the Result type and this value may change. 121 // The text of the example plugins should not. 122 var ExpectedResult = &types020.Result{ 123 IP4: &types020.IPConfig{ 124 IP: net.IPNet{ 125 IP: net.ParseIP("10.1.2.3"), 126 Mask: net.CIDRMask(24, 32), 127 }, 128 Gateway: net.ParseIP("10.1.2.1"), 129 Routes: []types.Route{ 130 types.Route{ 131 Dst: net.IPNet{ 132 IP: net.ParseIP("0.0.0.0"), 133 Mask: net.CIDRMask(0, 32), 134 }, 135 GW: net.ParseIP("10.1.0.1"), 136 }, 137 }, 138 }, 139 DNS: types.DNS{ 140 Nameservers: []string{"8.8.8.8"}, 141 Domain: "example.com", 142 }, 143 }