Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InitCNIWithCache to set the cni cache directory #84

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/ocicni/ocicni.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ func InitCNI(defaultNetName string, confDir string, binDirs ...string) (CNIPlugi
return initCNI(nil, "", defaultNetName, confDir, binDirs...)
}

// InitCNIWithCache works like InitCNI except that it takes the cni cache directory as third param.
func InitCNIWithCache(defaultNetName, confDir, cacheDir string, binDirs ...string) (CNIPlugin, error) {
return initCNI(nil, cacheDir, defaultNetName, confDir, binDirs...)
}

// Internal function to allow faking out exec functions for testing
func initCNI(exec cniinvoke.Exec, cacheDir, defaultNetName string, confDir string, binDirs ...string) (CNIPlugin, error) {
if confDir == "" {
Expand All @@ -208,7 +213,7 @@ func initCNI(exec cniinvoke.Exec, cacheDir, defaultNetName string, confDir strin
}

plugin := &cniNetworkPlugin{
cniConfig: libcni.NewCNIConfig(binDirs, exec),
cniConfig: libcni.NewCNIConfigWithCacheDir(binDirs, cacheDir, exec),
defaultNetName: netName{
name: defaultNetName,
// If defaultNetName is not assigned in initialization,
Expand Down Expand Up @@ -468,7 +473,7 @@ func (plugin *cniNetworkPlugin) forEachNetwork(podNetwork *PodNetwork, fromCache
}
}

rt, err := buildCNIRuntimeConf(plugin.cacheDir, podNetwork, ifName, podNetwork.RuntimeConfig[network.Name])
rt, err := buildCNIRuntimeConf(podNetwork, ifName, podNetwork.RuntimeConfig[network.Name])
if err != nil {
logrus.Errorf("error building CNI runtime config: %v", err)
return err
Expand Down Expand Up @@ -775,13 +780,12 @@ func (network *cniNetwork) deleteFromNetwork(ctx context.Context, rt *libcni.Run
return nil
}

func buildCNIRuntimeConf(cacheDir string, podNetwork *PodNetwork, ifName string, runtimeConfig RuntimeConfig) (*libcni.RuntimeConf, error) {
func buildCNIRuntimeConf(podNetwork *PodNetwork, ifName string, runtimeConfig RuntimeConfig) (*libcni.RuntimeConf, error) {
logrus.Infof("Got pod network %+v", podNetwork)

rt := &libcni.RuntimeConf{
ContainerID: podNetwork.ID,
NetNS: podNetwork.NetNS,
CacheDir: cacheDir,
IfName: ifName,
Args: [][2]string{
{"IgnoreUnknown", "1"},
Expand Down
25 changes: 12 additions & 13 deletions pkg/ocicni/ocicni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ var _ = Describe("ocicni operations", func() {
})

It("build different runtime configs", func() {
cacheDir := "empty"
ifName := "eth0"
podNetwork := &PodNetwork{}

Expand All @@ -370,44 +369,44 @@ var _ = Describe("ocicni operations", func() {
)

// empty runtimeConfig
_, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
_, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())

// runtimeConfig with invalid IP
runtimeConfig = RuntimeConfig{IP: "172.16"}
_, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
_, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).To(HaveOccurred())

// runtimeConfig with valid IP
runtimeConfig = RuntimeConfig{IP: "172.16.0.1"}
rt, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
Expect(len(rt.Args)).To(Equal(5))
Expect(rt.Args[4][1]).To(Equal("172.16.0.1"))

// runtimeConfig with invalid MAC
runtimeConfig = RuntimeConfig{MAC: "f0:a6"}
_, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
_, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).To(HaveOccurred())

// runtimeConfig with valid MAC
runtimeConfig = RuntimeConfig{MAC: "9e:0c:d9:b2:f0:a6"}
rt, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
Expect(len(rt.Args)).To(Equal(5))
Expect(rt.Args[4][1]).To(Equal("9e:0c:d9:b2:f0:a6"))

// runtimeConfig with valid IP and valid MAC
runtimeConfig = RuntimeConfig{IP: "172.16.0.1", MAC: "9e:0c:d9:b2:f0:a6"}
rt, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
Expect(len(rt.Args)).To(Equal(6))
Expect(rt.Args[4][1]).To(Equal("172.16.0.1"))
Expect(rt.Args[5][1]).To(Equal("9e:0c:d9:b2:f0:a6"))

// runtimeConfig with portMappings is nil
runtimeConfig = RuntimeConfig{PortMappings: nil}
_, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
_, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())

// runtimeConfig with valid portMappings
Expand All @@ -417,7 +416,7 @@ var _ = Describe("ocicni operations", func() {
Protocol: "tcp",
HostIP: "192.168.0.1",
}}}
rt, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
pm, ok := rt.CapabilityArgs["portMappings"].([]PortMapping)
Expect(ok).To(Equal(true))
Expand All @@ -429,7 +428,7 @@ var _ = Describe("ocicni operations", func() {

// runtimeConfig with bandwidth is nil
runtimeConfig = RuntimeConfig{Bandwidth: nil}
_, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
_, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())

// runtimeConfig with valid bandwidth
Expand All @@ -439,7 +438,7 @@ var _ = Describe("ocicni operations", func() {
EgressRate: 3,
EgressBurst: 4,
}}
rt, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
bw, ok := rt.CapabilityArgs["bandwidth"].(map[string]uint64)
Expect(ok).To(Equal(true))
Expand All @@ -450,7 +449,7 @@ var _ = Describe("ocicni operations", func() {

// runtimeConfig with ipRanges is empty
runtimeConfig = RuntimeConfig{IpRanges: [][]IpRange{}}
_, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
_, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())

// runtimeConfig with valid ipRanges
Expand All @@ -460,7 +459,7 @@ var _ = Describe("ocicni operations", func() {
RangeEnd: "192.168.0.200",
Gateway: "192.168.0.254",
}}}}
rt, err = buildCNIRuntimeConf(cacheDir, podNetwork, ifName, runtimeConfig)
rt, err = buildCNIRuntimeConf(podNetwork, ifName, runtimeConfig)
Expect(err).NotTo(HaveOccurred())
ir, ok := rt.CapabilityArgs["ipRanges"].([][]IpRange)
Expect(ok).To(Equal(true))
Expand Down