Skip to content

Commit

Permalink
Merge pull request #366 from ehazlett/ipam-host-local-initial-reserve…
Browse files Browse the repository at this point in the history
…d-ip

Do not error if last reserved not found after initial creation
  • Loading branch information
rosenhouse authored Mar 6, 2017
2 parents 1f6efc2 + ac50624 commit 4ce9b01
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
3 changes: 2 additions & 1 deletion plugins/ipam/host-local/backend/allocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log"
"net"
"os"

"github.com/containernetworking/cni/pkg/ip"
"github.com/containernetworking/cni/pkg/types"
Expand Down Expand Up @@ -253,7 +254,7 @@ func (a *IPAllocator) getSearchRange() (net.IP, net.IP) {
var endIP net.IP
startFromLastReservedIP := false
lastReservedIP, err := a.store.LastReservedIP()
if err != nil {
if err != nil && !os.IsNotExist(err) {
log.Printf("Error retriving last reserved ip: %v", err)
} else if lastReservedIP != nil {
subnet := net.IPNet{
Expand Down
3 changes: 1 addition & 2 deletions plugins/ipam/host-local/backend/disk/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package disk

import (
"fmt"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (s *Store) LastReservedIP() (net.IP, error) {
ipfile := filepath.Join(s.dataDir, lastIPFile)
data, err := ioutil.ReadFile(ipfile)
if err != nil {
return nil, fmt.Errorf("Failed to retrieve last reserved ip: %v", err)
return nil, err
}
return net.ParseIP(string(data)), nil
}
Expand Down
27 changes: 27 additions & 0 deletions plugins/ipam/host-local/host_local_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2016 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestHostLocal(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "HostLocal Suite")
}
45 changes: 42 additions & 3 deletions plugins/ipam/host-local/host_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ var _ = Describe("host-local Operations", func() {
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)

err = ioutil.WriteFile(filepath.Join(tmpDir, "resolv.conf"), []byte("nameserver 192.0.2.3"), 0644)
Expect(err).NotTo(HaveOccurred())

conf := fmt.Sprintf(`{
"cniVersion": "0.1.0",
"name": "mynet",
Expand All @@ -117,9 +120,10 @@ var _ = Describe("host-local Operations", func() {
"ipam": {
"type": "host-local",
"subnet": "10.1.2.0/24",
"dataDir": "%s"
"dataDir": "%s",
"resolvConf": "%s/resolv.conf"
}
}`, tmpDir)
}`, tmpDir, tmpDir)

args := &skel.CmdArgs{
ContainerID: "dummy",
Expand Down Expand Up @@ -175,7 +179,7 @@ var _ = Describe("host-local Operations", func() {
defer os.RemoveAll(tmpDir)

conf := fmt.Sprintf(`{
"cniVersion": "0.2.0",
"cniVersion": "0.3.0",
"name": "mynet",
"type": "ipvlan",
"master": "foo0",
Expand Down Expand Up @@ -216,4 +220,39 @@ var _ = Describe("host-local Operations", func() {
_, err = os.Stat(ipFilePath)
Expect(err).To(HaveOccurred())
})

It("does not output an error message upon initial subnet creation", func() {
const ifname string = "eth0"
const nspath string = "/some/where"

tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
Expect(err).NotTo(HaveOccurred())
defer os.RemoveAll(tmpDir)

conf := fmt.Sprintf(`{
"cniVersion": "0.2.0",
"name": "mynet",
"type": "ipvlan",
"master": "foo0",
"ipam": {
"type": "host-local",
"subnet": "10.1.2.0/24",
"dataDir": "%s"
}
}`, tmpDir)

args := &skel.CmdArgs{
ContainerID: "testing",
Netns: nspath,
IfName: ifname,
StdinData: []byte(conf),
}

// Allocate the IP
_, out, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
Expect(strings.Index(string(out), "Error retriving last reserved ip")).To(Equal(-1))
})
})

0 comments on commit 4ce9b01

Please sign in to comment.