flannel: rename stateDir to dataDir
Rename StateDir to DataDir for flannel CNI plugin
This commit is contained in:
parent
15de81eac6
commit
6eac0ee904
@ -73,7 +73,7 @@ To use `ipvlan` instead of `bridge`, the following configuration can be specifie
|
|||||||
* `name` (string, required): the name of the network
|
* `name` (string, required): the name of the network
|
||||||
* `type` (string, required): "flannel"
|
* `type` (string, required): "flannel"
|
||||||
* `subnetFile` (string, optional): full path to the subnet file written out by flanneld. Defaults to /run/flannel/subnet.env
|
* `subnetFile` (string, optional): full path to the subnet file written out by flanneld. Defaults to /run/flannel/subnet.env
|
||||||
* `stateDir` (string, optional): path to directory where plugin will store generated network configuration files. Defaults to `/var/lib/cni/flannel`
|
* `dataDir` (string, optional): path to directory where plugin will store generated network configuration files. Defaults to `/var/lib/cni/flannel`
|
||||||
* `delegate` (dictionary, optional): specifies configuration options for the delegated plugin.
|
* `delegate` (dictionary, optional): specifies configuration options for the delegated plugin.
|
||||||
|
|
||||||
flannel plugin will always set the following fields in the delegated plugin configuration:
|
flannel plugin will always set the following fields in the delegated plugin configuration:
|
||||||
|
@ -37,13 +37,13 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultSubnetFile = "/run/flannel/subnet.env"
|
defaultSubnetFile = "/run/flannel/subnet.env"
|
||||||
defaultStateDir = "/var/lib/cni/flannel"
|
defaultDataDir = "/var/lib/cni/flannel"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NetConf struct {
|
type NetConf struct {
|
||||||
types.NetConf
|
types.NetConf
|
||||||
SubnetFile string `json:"subnetFile"`
|
SubnetFile string `json:"subnetFile"`
|
||||||
StateDir string `json:"stateDir"`
|
DataDir string `json:"dataDir"`
|
||||||
Delegate map[string]interface{} `json:"delegate"`
|
Delegate map[string]interface{} `json:"delegate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ func (se *subnetEnv) missing() string {
|
|||||||
func loadFlannelNetConf(bytes []byte) (*NetConf, error) {
|
func loadFlannelNetConf(bytes []byte) (*NetConf, error) {
|
||||||
n := &NetConf{
|
n := &NetConf{
|
||||||
SubnetFile: defaultSubnetFile,
|
SubnetFile: defaultSubnetFile,
|
||||||
StateDir: defaultStateDir,
|
DataDir: defaultDataDir,
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(bytes, n); err != nil {
|
if err := json.Unmarshal(bytes, n); err != nil {
|
||||||
return nil, fmt.Errorf("failed to load netconf: %v", err)
|
return nil, fmt.Errorf("failed to load netconf: %v", err)
|
||||||
@ -132,29 +132,29 @@ func loadFlannelSubnetEnv(fn string) (*subnetEnv, error) {
|
|||||||
return se, nil
|
return se, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveScratchNetConf(containerID, stateDir string, netconf []byte) error {
|
func saveScratchNetConf(containerID, dataDir string, netconf []byte) error {
|
||||||
if err := os.MkdirAll(stateDir, 0700); err != nil {
|
if err := os.MkdirAll(dataDir, 0700); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path := filepath.Join(stateDir, containerID)
|
path := filepath.Join(dataDir, containerID)
|
||||||
return ioutil.WriteFile(path, netconf, 0600)
|
return ioutil.WriteFile(path, netconf, 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
func consumeScratchNetConf(containerID, stateDir string) ([]byte, error) {
|
func consumeScratchNetConf(containerID, dataDir string) ([]byte, error) {
|
||||||
path := filepath.Join(stateDir, containerID)
|
path := filepath.Join(dataDir, containerID)
|
||||||
defer os.Remove(path)
|
defer os.Remove(path)
|
||||||
|
|
||||||
return ioutil.ReadFile(path)
|
return ioutil.ReadFile(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func delegateAdd(cid, stateDir string, netconf map[string]interface{}) error {
|
func delegateAdd(cid, dataDir string, netconf map[string]interface{}) error {
|
||||||
netconfBytes, err := json.Marshal(netconf)
|
netconfBytes, err := json.Marshal(netconf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error serializing delegate netconf: %v", err)
|
return fmt.Errorf("error serializing delegate netconf: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// save the rendered netconf for cmdDel
|
// save the rendered netconf for cmdDel
|
||||||
if err = saveScratchNetConf(cid, stateDir, netconfBytes); err != nil {
|
if err = saveScratchNetConf(cid, dataDir, netconfBytes); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +234,7 @@ func cmdAdd(args *skel.CmdArgs) error {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return delegateAdd(args.ContainerID, n.StateDir, n.Delegate)
|
return delegateAdd(args.ContainerID, n.DataDir, n.Delegate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdDel(args *skel.CmdArgs) error {
|
func cmdDel(args *skel.CmdArgs) error {
|
||||||
@ -243,7 +243,7 @@ func cmdDel(args *skel.CmdArgs) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
netconfBytes, err := consumeScratchNetConf(args.ContainerID, nc.StateDir)
|
netconfBytes, err := consumeScratchNetConf(args.ContainerID, nc.DataDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ var _ = Describe("Flannel", func() {
|
|||||||
originalNS ns.NetNS
|
originalNS ns.NetNS
|
||||||
input string
|
input string
|
||||||
subnetFile string
|
subnetFile string
|
||||||
stateDir string
|
dataDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
@ -49,7 +49,7 @@ var _ = Describe("Flannel", func() {
|
|||||||
"name": "cni-flannel",
|
"name": "cni-flannel",
|
||||||
"type": "flannel",
|
"type": "flannel",
|
||||||
"subnetFile": "%s",
|
"subnetFile": "%s",
|
||||||
"stateDir": "%s"
|
"dataDir": "%s"
|
||||||
}`
|
}`
|
||||||
|
|
||||||
const flannelSubnetEnv = `
|
const flannelSubnetEnv = `
|
||||||
@ -73,18 +73,18 @@ FLANNEL_IPMASQ=true
|
|||||||
subnetFile = writeSubnetEnv(flannelSubnetEnv)
|
subnetFile = writeSubnetEnv(flannelSubnetEnv)
|
||||||
|
|
||||||
// flannel state dir
|
// flannel state dir
|
||||||
stateDir, err = ioutil.TempDir("", "stateDir")
|
dataDir, err = ioutil.TempDir("", "dataDir")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
input = fmt.Sprintf(inputTemplate, subnetFile, stateDir)
|
input = fmt.Sprintf(inputTemplate, subnetFile, dataDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
os.Remove(subnetFile)
|
os.Remove(subnetFile)
|
||||||
os.Remove(stateDir)
|
os.Remove(dataDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("CNI lifecycle", func() {
|
Describe("CNI lifecycle", func() {
|
||||||
It("uses stateDir for storing network configuration", func() {
|
It("uses dataDir for storing network configuration", func() {
|
||||||
const IFNAME = "eth0"
|
const IFNAME = "eth0"
|
||||||
|
|
||||||
targetNs, err := ns.NewNS()
|
targetNs, err := ns.NewNS()
|
||||||
@ -107,8 +107,8 @@ FLANNEL_IPMASQ=true
|
|||||||
})
|
})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
By("check that plugin writes to net config to stateDir")
|
By("check that plugin writes to net config to dataDir")
|
||||||
path := fmt.Sprintf("%s/%s", stateDir, "some-container-id")
|
path := fmt.Sprintf("%s/%s", dataDir, "some-container-id")
|
||||||
Expect(path).Should(BeAnExistingFile())
|
Expect(path).Should(BeAnExistingFile())
|
||||||
|
|
||||||
netConfBytes, err := ioutil.ReadFile(path)
|
netConfBytes, err := ioutil.ReadFile(path)
|
||||||
@ -147,18 +147,18 @@ FLANNEL_IPMASQ=true
|
|||||||
})
|
})
|
||||||
|
|
||||||
Describe("loadFlannelNetConf", func() {
|
Describe("loadFlannelNetConf", func() {
|
||||||
Context("when subnetFile and stateDir are specified", func() {
|
Context("when subnetFile and dataDir are specified", func() {
|
||||||
It("loads flannel network config", func() {
|
It("loads flannel network config", func() {
|
||||||
conf, err := loadFlannelNetConf([]byte(input))
|
conf, err := loadFlannelNetConf([]byte(input))
|
||||||
Expect(err).ShouldNot(HaveOccurred())
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
Expect(conf.Name).To(Equal("cni-flannel"))
|
Expect(conf.Name).To(Equal("cni-flannel"))
|
||||||
Expect(conf.Type).To(Equal("flannel"))
|
Expect(conf.Type).To(Equal("flannel"))
|
||||||
Expect(conf.SubnetFile).To(Equal(subnetFile))
|
Expect(conf.SubnetFile).To(Equal(subnetFile))
|
||||||
Expect(conf.StateDir).To(Equal(stateDir))
|
Expect(conf.DataDir).To(Equal(dataDir))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
Context("when defaulting subnetFile and stateDir", func() {
|
Context("when defaulting subnetFile and dataDir", func() {
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
input = `{
|
input = `{
|
||||||
"name": "cni-flannel",
|
"name": "cni-flannel",
|
||||||
@ -172,7 +172,7 @@ FLANNEL_IPMASQ=true
|
|||||||
Expect(conf.Name).To(Equal("cni-flannel"))
|
Expect(conf.Name).To(Equal("cni-flannel"))
|
||||||
Expect(conf.Type).To(Equal("flannel"))
|
Expect(conf.Type).To(Equal("flannel"))
|
||||||
Expect(conf.SubnetFile).To(Equal(defaultSubnetFile))
|
Expect(conf.SubnetFile).To(Equal(defaultSubnetFile))
|
||||||
Expect(conf.StateDir).To(Equal(defaultStateDir))
|
Expect(conf.DataDir).To(Equal(defaultDataDir))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user