Skip to content

Commit

Permalink
types: add 1.0.0
Browse files Browse the repository at this point in the history
Only change this time is removal of the IPConfig "Version" field
which was deemed redundant.

Signed-off-by: Dan Williams <dcbw@redhat.com>
  • Loading branch information
dcbw committed Aug 28, 2020
1 parent 843fa59 commit 843df8a
Show file tree
Hide file tree
Showing 10 changed files with 949 additions and 269 deletions.
136 changes: 85 additions & 51 deletions libcni/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func stringInList(s string, list []string) bool {
return false
}

func newPluginInfo(configValue, prevResult string, injectDebugFilePath bool, result string, runtimeConfig map[string]interface{}, capabilities []string) pluginInfo {
func newPluginInfo(cniVersion, configValue, prevResult string, injectDebugFilePath bool, result string, runtimeConfig map[string]interface{}, capabilities []string) pluginInfo {
debugFile, err := ioutil.TempFile("", "cni_debug")
Expect(err).NotTo(HaveOccurred())
Expect(debugFile.Close()).To(Succeed())
Expand Down Expand Up @@ -97,7 +97,7 @@ func newPluginInfo(configValue, prevResult string, injectDebugFilePath bool, res
err = json.Unmarshal([]byte(config), &newConfig)
Expect(err).NotTo(HaveOccurred())
newConfig["name"] = "some-list"
newConfig["cniVersion"] = current.ImplementedSpecVersion
newConfig["cniVersion"] = cniVersion

// Only include standard runtime config and capability args that this plugin advertises
newRuntimeConfig := make(map[string]interface{})
Expand All @@ -121,6 +121,27 @@ func newPluginInfo(configValue, prevResult string, injectDebugFilePath bool, res
}
}

func makePluginList(cniVersion, ipResult string, runtimeConfig map[string]interface{}) (*libcni.NetworkConfigList, []pluginInfo) {
plugins := make([]pluginInfo, 3)
plugins[0] = newPluginInfo(cniVersion, "some-value", "", true, ipResult, runtimeConfig, []string{"portMappings", "otherCapability"})
plugins[1] = newPluginInfo(cniVersion, "some-other-value", ipResult, true, "PASSTHROUGH", runtimeConfig, []string{"otherCapability"})
plugins[2] = newPluginInfo(cniVersion, "yet-another-value", ipResult, true, "INJECT-DNS", runtimeConfig, []string{})

configList := []byte(fmt.Sprintf(`{
"name": "some-list",
"cniVersion": "%s",
"plugins": [
%s,
%s,
%s
]
}`, cniVersion, plugins[0].config, plugins[1].config, plugins[2].config))

netConfigList, err := libcni.ConfListFromBytes(configList)
Expect(err).NotTo(HaveOccurred())
return netConfigList, plugins
}

func resultCacheFilePath(cacheDirPath, netName string, rt *libcni.RuntimeConf) string {
fName := fmt.Sprintf("%s-%s-%s", netName, rt.ContainerID, rt.IfName)
return filepath.Join(cacheDirPath, "results", fName)
Expand Down Expand Up @@ -158,8 +179,8 @@ var _ = Describe("Invoking plugins", func() {

debug = &noop_debug.Debug{
ReportResult: `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"cniVersion": "1.0.0",
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`,
}
Expand Down Expand Up @@ -264,7 +285,6 @@ var _ = Describe("Invoking plugins", func() {
var (
debugFilePath string
debug *noop_debug.Debug
cniBinPath string
pluginConfig string
cniConfig *libcni.CNIConfig
netConfig *libcni.NetworkConfig
Expand All @@ -282,8 +302,8 @@ var _ = Describe("Invoking plugins", func() {

debug = &noop_debug.Debug{
ReportResult: `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"cniVersion": "1.0.0",
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`,
}
Expand All @@ -293,7 +313,7 @@ var _ = Describe("Invoking plugins", func() {
{HostPort: 8080, ContainerPort: 80, Protocol: "tcp"},
}

cniBinPath = filepath.Dir(pluginPaths["noop"])
cniBinPath := filepath.Dir(pluginPaths["noop"])
pluginConfig = fmt.Sprintf(`{
"type": "noop",
"name": "apitest",
Expand Down Expand Up @@ -351,7 +371,6 @@ var _ = Describe("Invoking plugins", func() {
CNIVersion: current.ImplementedSpecVersion,
IPs: []*current.IPConfig{
{
Version: "4",
Address: net.IPNet{
IP: net.ParseIP("10.1.2.3"),
Mask: net.IPv4Mask(255, 255, 255, 0),
Expand Down Expand Up @@ -437,8 +456,8 @@ var _ = Describe("Invoking plugins", func() {
err := os.MkdirAll(filepath.Dir(cacheFile), 0700)
Expect(err).NotTo(HaveOccurred())
cachedJson := `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"cniVersion": "1.0.0",
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`
err = ioutil.WriteFile(cacheFile, []byte(cachedJson), 0600)
Expand Down Expand Up @@ -503,13 +522,12 @@ var _ = Describe("Invoking plugins", func() {
Context("less than 0.4.0", func() {
It("fails as CHECK is not supported before 0.4.0", func() {
// Generate plugin config with older version
pluginConfig = `{
var err error
netConfig, err = libcni.ConfFromBytes([]byte(`{
"type": "noop",
"name": "apitest",
"cniVersion": "0.3.1"
}`
var err error
netConfig, err = libcni.ConfFromBytes([]byte(pluginConfig))
}`))
Expect(err).NotTo(HaveOccurred())
err = cniConfig.CheckNetwork(ctx, netConfig, runtimeConfig)
Expect(err).To(MatchError("configuration version \"0.3.1\" does not support the CHECK command"))
Expand All @@ -523,8 +541,8 @@ var _ = Describe("Invoking plugins", func() {
Context("containing only a cached result", func() {
It("only passes a prevResult to the plugin", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"cniVersion": "1.0.0",
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`), 0600)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -541,13 +559,33 @@ var _ = Describe("Invoking plugins", func() {

Context("equal to 0.4.0", func() {
It("passes a prevResult to the plugin", func() {
err := ioutil.WriteFile(cacheFile, []byte(`{
var err error
netConfig, err = libcni.ConfFromBytes([]byte(`{
"type": "noop",
"name": "apitest",
"cniVersion": "0.4.0",
"capabilities": {
"portMappings": true,
"somethingElse": true,
"noCapability": false
}
}`))
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(cacheFile, []byte(`{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`), 0600)
Expect(err).NotTo(HaveOccurred())

debug.ReportResult = `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`
Expect(debug.WriteDebug(debugFilePath)).To(Succeed())

err = cniConfig.CheckNetwork(ctx, netConfig, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
debug, err := noop_debug.ReadDebug(debugFilePath)
Expand Down Expand Up @@ -610,8 +648,8 @@ var _ = Describe("Invoking plugins", func() {
err := os.MkdirAll(filepath.Dir(cacheFile), 0700)
Expect(err).NotTo(HaveOccurred())
cachedJson := `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"cniVersion": "1.0.0",
"ips": [{"address": "10.1.2.3/24"}],
"dns": {}
}`
err = ioutil.WriteFile(cacheFile, []byte(cachedJson), 0600)
Expand Down Expand Up @@ -721,12 +759,11 @@ var _ = Describe("Invoking plugins", func() {
Expect(err).NotTo(HaveOccurred())

// Generate plugin config with older version
pluginConfig = `{
netConfig, err = libcni.ConfFromBytes([]byte(`{
"type": "noop",
"name": "apitest",
"cniVersion": "0.3.1"
}`
netConfig, err = libcni.ConfFromBytes([]byte(pluginConfig))
}`))
Expect(err).NotTo(HaveOccurred())
err = cniConfig.DelNetwork(ctx, netConfig, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -812,7 +849,7 @@ var _ = Describe("Invoking plugins", func() {

Expect(versionInfo).NotTo(BeNil())
Expect(versionInfo.SupportedVersions()).To(Equal([]string{
"0.-42.0", "0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0",
"0.-42.0", "0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0",
}))
})

Expand Down Expand Up @@ -856,15 +893,14 @@ var _ = Describe("Invoking plugins", func() {
cniConfig *libcni.CNIConfig
netConfigList *libcni.NetworkConfigList
runtimeConfig *libcni.RuntimeConf
rcMap map[string]interface{}
ctx context.Context
ipResult string

expectedCmdArgs skel.CmdArgs
)

BeforeEach(func() {
var err error

capabilityArgs := map[string]interface{}{
"portMappings": []portMapping{
{HostPort: 8080, ContainerPort: 80, Protocol: "tcp"},
Expand All @@ -890,7 +926,7 @@ var _ = Describe("Invoking plugins", func() {
Path: cniBinPath,
}

rc := map[string]interface{}{
rcMap = map[string]interface{}{
"containerId": runtimeConfig.ContainerID,
"netNs": runtimeConfig.NetNS,
"ifName": runtimeConfig.IfName,
Expand All @@ -901,24 +937,9 @@ var _ = Describe("Invoking plugins", func() {
"otherCapability": capabilityArgs["otherCapability"],
}

ipResult = fmt.Sprintf(`{"cniVersion": "%s", "dns":{},"ips":[{"version": "4", "address": "10.1.2.3/24"}]}`, current.ImplementedSpecVersion)
plugins = make([]pluginInfo, 3)
plugins[0] = newPluginInfo("some-value", "", true, ipResult, rc, []string{"portMappings", "otherCapability"})
plugins[1] = newPluginInfo("some-other-value", ipResult, true, "PASSTHROUGH", rc, []string{"otherCapability"})
plugins[2] = newPluginInfo("yet-another-value", ipResult, true, "INJECT-DNS", rc, []string{})

configList := []byte(fmt.Sprintf(`{
"name": "some-list",
"cniVersion": "%s",
"plugins": [
%s,
%s,
%s
]
}`, current.ImplementedSpecVersion, plugins[0].config, plugins[1].config, plugins[2].config))
ipResult = fmt.Sprintf(`{"cniVersion": "%s", "dns":{},"ips":[{"address": "10.1.2.3/24"}]}`, current.ImplementedSpecVersion)
netConfigList, plugins = makePluginList(current.ImplementedSpecVersion, ipResult, rcMap)

netConfigList, err = libcni.ConfListFromBytes(configList)
Expect(err).NotTo(HaveOccurred())
ctx = context.TODO()
})

Expand All @@ -941,7 +962,6 @@ var _ = Describe("Invoking plugins", func() {
// IP4 added by first plugin
IPs: []*current.IPConfig{
{
Version: "4",
Address: net.IPNet{
IP: net.ParseIP("10.1.2.3"),
Mask: net.IPv4Mask(255, 255, 255, 0),
Expand Down Expand Up @@ -1209,14 +1229,21 @@ var _ = Describe("Invoking plugins", func() {

Context("is 0.4.0", func() {
It("passes a cached result to the first plugin", func() {
cachedJson := `{
ipResult = `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`
err := ioutil.WriteFile(cacheFile, []byte(cachedJson), 0600)
err := ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

netConfigList, plugins = makePluginList("0.4.0", ipResult, rcMap)

for _, p := range plugins {
p.debug.ReportResult = ipResult
Expect(p.debug.WriteDebug(p.debugFilePath)).To(Succeed())
}

err = cniConfig.CheckNetworkList(ctx, netConfigList, runtimeConfig)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -1229,7 +1256,7 @@ var _ = Describe("Invoking plugins", func() {
Expect(err).NotTo(HaveOccurred())
stdinPrevResult, err := json.Marshal(data["prevResult"])
Expect(err).NotTo(HaveOccurred())
Expect(stdinPrevResult).To(MatchJSON(cachedJson))
Expect(stdinPrevResult).To(MatchJSON(ipResult))
})
})

Expand Down Expand Up @@ -1326,14 +1353,21 @@ var _ = Describe("Invoking plugins", func() {

Context("is 0.4.0", func() {
It("passes a cached result to the first plugin", func() {
cachedJson := `{
ipResult = `{
"cniVersion": "0.4.0",
"ips": [{"version": "4", "address": "10.1.2.3/24"}],
"dns": {}
}`
err := ioutil.WriteFile(cacheFile, []byte(cachedJson), 0600)
err := ioutil.WriteFile(cacheFile, []byte(ipResult), 0600)
Expect(err).NotTo(HaveOccurred())

netConfigList, plugins = makePluginList("0.4.0", ipResult, rcMap)

for _, p := range plugins {
p.debug.ReportResult = ipResult
Expect(p.debug.WriteDebug(p.debugFilePath)).To(Succeed())
}

err = cniConfig.DelNetworkList(ctx, netConfigList, runtimeConfig)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -1346,7 +1380,7 @@ var _ = Describe("Invoking plugins", func() {
Expect(err).NotTo(HaveOccurred())
stdinPrevResult, err := json.Marshal(data["prevResult"])
Expect(err).NotTo(HaveOccurred())
Expect(stdinPrevResult).To(MatchJSON(cachedJson))
Expect(stdinPrevResult).To(MatchJSON(ipResult))
})
})

Expand Down
5 changes: 2 additions & 3 deletions pkg/invoke/delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ var _ = Describe("Delegate", func() {
BeforeEach(func() {
netConf, _ = json.Marshal(map[string]string{
"name": "delegate-test",
"cniVersion": "0.4.0",
"cniVersion": current.ImplementedSpecVersion,
})

expectedResult = &current.Result{
CNIVersion: "0.4.0",
CNIVersion: current.ImplementedSpecVersion,
IPs: []*current.IPConfig{
{
Version: "4",
Address: net.IPNet{
IP: net.ParseIP("10.1.2.3"),
Mask: net.CIDRMask(24, 32),
Expand Down
Loading

0 comments on commit 843df8a

Please sign in to comment.