Skip to content

Commit 5544d9c

Browse files
authored
Merge pull request #47 from dcbw/tuning-prev-result
tuning: pass prevResult through
2 parents 20bc33a + 998a0f6 commit 5544d9c

3 files changed

Lines changed: 173 additions & 7 deletions

File tree

plugins/meta/tuning/tuning.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,47 @@ import (
3434
// TuningConf represents the network tuning configuration.
3535
type TuningConf struct {
3636
types.NetConf
37-
SysCtl map[string]string `json:"sysctl"`
37+
SysCtl map[string]string `json:"sysctl"`
38+
RawPrevResult map[string]interface{} `json:"prevResult,omitempty"`
39+
PrevResult *current.Result `json:"-"`
40+
}
41+
42+
func parseConf(data []byte) (*TuningConf, error) {
43+
conf := TuningConf{}
44+
if err := json.Unmarshal(data, &conf); err != nil {
45+
return nil, fmt.Errorf("failed to load netconf: %v", err)
46+
}
47+
48+
// Parse previous result.
49+
if conf.RawPrevResult != nil {
50+
resultBytes, err := json.Marshal(conf.RawPrevResult)
51+
if err != nil {
52+
return nil, fmt.Errorf("could not serialize prevResult: %v", err)
53+
}
54+
res, err := version.NewResult(conf.CNIVersion, resultBytes)
55+
if err != nil {
56+
return nil, fmt.Errorf("could not parse prevResult: %v", err)
57+
}
58+
conf.RawPrevResult = nil
59+
conf.PrevResult, err = current.NewResultFromResult(res)
60+
if err != nil {
61+
return nil, fmt.Errorf("could not convert result to current version: %v", err)
62+
}
63+
}
64+
65+
return &conf, nil
3866
}
3967

4068
func cmdAdd(args *skel.CmdArgs) error {
41-
tuningConf := TuningConf{}
42-
if err := json.Unmarshal(args.StdinData, &tuningConf); err != nil {
43-
return fmt.Errorf("failed to load netconf: %v", err)
69+
tuningConf, err := parseConf(args.StdinData)
70+
if err != nil {
71+
return err
4472
}
4573

4674
// The directory /proc/sys/net is per network namespace. Enter in the
4775
// network namespace before writing on it.
4876

49-
err := ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error {
77+
err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error {
5078
for key, value := range tuningConf.SysCtl {
5179
fileName := filepath.Join("/proc/sys", strings.Replace(key, ".", "/", -1))
5280
fileName = filepath.Clean(fileName)
@@ -68,8 +96,7 @@ func cmdAdd(args *skel.CmdArgs) error {
6896
return err
6997
}
7098

71-
result := current.Result{}
72-
return result.Print()
99+
return types.PrintResult(tuningConf.PrevResult, tuningConf.CNIVersion)
73100
}
74101

75102
func cmdDel(args *skel.CmdArgs) error {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 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 main
16+
17+
import (
18+
. "github.com/onsi/ginkgo"
19+
. "github.com/onsi/gomega"
20+
21+
"testing"
22+
)
23+
24+
func TestTuning(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "tuning Suite")
27+
}

plugins/meta/tuning/tuning_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2017 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 main
16+
17+
import (
18+
"github.com/containernetworking/cni/pkg/skel"
19+
"github.com/containernetworking/cni/pkg/types/current"
20+
"github.com/containernetworking/plugins/pkg/ns"
21+
"github.com/containernetworking/plugins/pkg/testutils"
22+
23+
"github.com/vishvananda/netlink"
24+
25+
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/gomega"
27+
)
28+
29+
var _ = Describe("tuning plugin", func() {
30+
var originalNS ns.NetNS
31+
const IFNAME string = "dummy0"
32+
33+
BeforeEach(func() {
34+
// Create a new NetNS so we don't modify the host
35+
var err error
36+
originalNS, err = ns.NewNS()
37+
Expect(err).NotTo(HaveOccurred())
38+
39+
err = originalNS.Do(func(ns.NetNS) error {
40+
defer GinkgoRecover()
41+
42+
err = netlink.LinkAdd(&netlink.Dummy{
43+
LinkAttrs: netlink.LinkAttrs{
44+
Name: IFNAME,
45+
},
46+
})
47+
Expect(err).NotTo(HaveOccurred())
48+
_, err = netlink.LinkByName(IFNAME)
49+
Expect(err).NotTo(HaveOccurred())
50+
return nil
51+
})
52+
Expect(err).NotTo(HaveOccurred())
53+
})
54+
55+
AfterEach(func() {
56+
Expect(originalNS.Close()).To(Succeed())
57+
})
58+
59+
It("passes prevResult through unchanged", func() {
60+
conf := []byte(`{
61+
"name": "test",
62+
"type": "tuning",
63+
"cniVersion": "0.3.1",
64+
"sysctl": {
65+
"net.ipv4.conf.all.log_martians": "1"
66+
},
67+
"prevResult": {
68+
"interfaces": [
69+
{"name": "dummy0", "sandbox":"netns"}
70+
],
71+
"ips": [
72+
{
73+
"version": "4",
74+
"address": "10.0.0.2/24",
75+
"gateway": "10.0.0.1",
76+
"interface": 0
77+
}
78+
]
79+
}
80+
}`)
81+
82+
targetNs, err := ns.NewNS()
83+
Expect(err).NotTo(HaveOccurred())
84+
defer targetNs.Close()
85+
86+
args := &skel.CmdArgs{
87+
ContainerID: "dummy",
88+
Netns: targetNs.Path(),
89+
IfName: IFNAME,
90+
StdinData: conf,
91+
}
92+
93+
err = originalNS.Do(func(ns.NetNS) error {
94+
defer GinkgoRecover()
95+
96+
r, _, err := testutils.CmdAddWithResult(targetNs.Path(), IFNAME, []byte(conf), func() error {
97+
return cmdAdd(args)
98+
})
99+
Expect(err).NotTo(HaveOccurred())
100+
101+
result, err := current.GetResult(r)
102+
Expect(err).NotTo(HaveOccurred())
103+
104+
Expect(len(result.Interfaces)).To(Equal(1))
105+
Expect(result.Interfaces[0].Name).To(Equal(IFNAME))
106+
Expect(len(result.IPs)).To(Equal(1))
107+
Expect(result.IPs[0].Address.String()).To(Equal("10.0.0.2/24"))
108+
return nil
109+
})
110+
Expect(err).NotTo(HaveOccurred())
111+
})
112+
})

0 commit comments

Comments
 (0)