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
|
||||
* `type` (string, required): "flannel"
|
||||
* `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.
|
||||
|
||||
flannel plugin will always set the following fields in the delegated plugin configuration:
|
||||
|
@ -37,13 +37,13 @@ import (
|
||||
|
||||
const (
|
||||
defaultSubnetFile = "/run/flannel/subnet.env"
|
||||
defaultStateDir = "/var/lib/cni/flannel"
|
||||
defaultDataDir = "/var/lib/cni/flannel"
|
||||
)
|
||||
|
||||
type NetConf struct {
|
||||
types.NetConf
|
||||
SubnetFile string `json:"subnetFile"`
|
||||
StateDir string `json:"stateDir"`
|
||||
DataDir string `json:"dataDir"`
|
||||
Delegate map[string]interface{} `json:"delegate"`
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ func (se *subnetEnv) missing() string {
|
||||
func loadFlannelNetConf(bytes []byte) (*NetConf, error) {
|
||||
n := &NetConf{
|
||||
SubnetFile: defaultSubnetFile,
|
||||
StateDir: defaultStateDir,
|
||||
DataDir: defaultDataDir,
|
||||
}
|
||||
if err := json.Unmarshal(bytes, n); err != nil {
|
||||
return nil, fmt.Errorf("failed to load netconf: %v", err)
|
||||
@ -132,29 +132,29 @@ func loadFlannelSubnetEnv(fn string) (*subnetEnv, error) {
|
||||
return se, nil
|
||||
}
|
||||
|
||||
func saveScratchNetConf(containerID, stateDir string, netconf []byte) error {
|
||||
if err := os.MkdirAll(stateDir, 0700); err != nil {
|
||||
func saveScratchNetConf(containerID, dataDir string, netconf []byte) error {
|
||||
if err := os.MkdirAll(dataDir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
path := filepath.Join(stateDir, containerID)
|
||||
path := filepath.Join(dataDir, containerID)
|
||||
return ioutil.WriteFile(path, netconf, 0600)
|
||||
}
|
||||
|
||||
func consumeScratchNetConf(containerID, stateDir string) ([]byte, error) {
|
||||
path := filepath.Join(stateDir, containerID)
|
||||
func consumeScratchNetConf(containerID, dataDir string) ([]byte, error) {
|
||||
path := filepath.Join(dataDir, containerID)
|
||||
defer os.Remove(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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error serializing delegate netconf: %v", err)
|
||||
}
|
||||
|
||||
// save the rendered netconf for cmdDel
|
||||
if err = saveScratchNetConf(cid, stateDir, netconfBytes); err != nil {
|
||||
if err = saveScratchNetConf(cid, dataDir, netconfBytes); err != nil {
|
||||
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 {
|
||||
@ -243,7 +243,7 @@ func cmdDel(args *skel.CmdArgs) error {
|
||||
return err
|
||||
}
|
||||
|
||||
netconfBytes, err := consumeScratchNetConf(args.ContainerID, nc.StateDir)
|
||||
netconfBytes, err := consumeScratchNetConf(args.ContainerID, nc.DataDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ var _ = Describe("Flannel", func() {
|
||||
originalNS ns.NetNS
|
||||
input string
|
||||
subnetFile string
|
||||
stateDir string
|
||||
dataDir string
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
@ -49,7 +49,7 @@ var _ = Describe("Flannel", func() {
|
||||
"name": "cni-flannel",
|
||||
"type": "flannel",
|
||||
"subnetFile": "%s",
|
||||
"stateDir": "%s"
|
||||
"dataDir": "%s"
|
||||
}`
|
||||
|
||||
const flannelSubnetEnv = `
|
||||
@ -73,18 +73,18 @@ FLANNEL_IPMASQ=true
|
||||
subnetFile = writeSubnetEnv(flannelSubnetEnv)
|
||||
|
||||
// flannel state dir
|
||||
stateDir, err = ioutil.TempDir("", "stateDir")
|
||||
dataDir, err = ioutil.TempDir("", "dataDir")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
input = fmt.Sprintf(inputTemplate, subnetFile, stateDir)
|
||||
input = fmt.Sprintf(inputTemplate, subnetFile, dataDir)
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
os.Remove(subnetFile)
|
||||
os.Remove(stateDir)
|
||||
os.Remove(dataDir)
|
||||
})
|
||||
|
||||
Describe("CNI lifecycle", func() {
|
||||
It("uses stateDir for storing network configuration", func() {
|
||||
It("uses dataDir for storing network configuration", func() {
|
||||
const IFNAME = "eth0"
|
||||
|
||||
targetNs, err := ns.NewNS()
|
||||
@ -107,8 +107,8 @@ FLANNEL_IPMASQ=true
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
By("check that plugin writes to net config to stateDir")
|
||||
path := fmt.Sprintf("%s/%s", stateDir, "some-container-id")
|
||||
By("check that plugin writes to net config to dataDir")
|
||||
path := fmt.Sprintf("%s/%s", dataDir, "some-container-id")
|
||||
Expect(path).Should(BeAnExistingFile())
|
||||
|
||||
netConfBytes, err := ioutil.ReadFile(path)
|
||||
@ -147,18 +147,18 @@ FLANNEL_IPMASQ=true
|
||||
})
|
||||
|
||||
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() {
|
||||
conf, err := loadFlannelNetConf([]byte(input))
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
Expect(conf.Name).To(Equal("cni-flannel"))
|
||||
Expect(conf.Type).To(Equal("flannel"))
|
||||
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() {
|
||||
input = `{
|
||||
"name": "cni-flannel",
|
||||
@ -172,7 +172,7 @@ FLANNEL_IPMASQ=true
|
||||
Expect(conf.Name).To(Equal("cni-flannel"))
|
||||
Expect(conf.Type).To(Equal("flannel"))
|
||||
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