From 7f8ea631e520a720e2acddbcf94e63dfe6fb219c Mon Sep 17 00:00:00 2001 From: Bruce Ma Date: Sat, 1 Jun 2019 17:37:24 +0800 Subject: [PATCH] host-local: make Store interface support to get ip list by id Signed-off-by: Bruce Ma --- .../ipam/host-local/backend/disk/backend.go | 31 ++++++++++++++++++- plugins/ipam/host-local/backend/store.go | 1 + .../host-local/backend/testing/fake_store.go | 10 ++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/plugins/ipam/host-local/backend/disk/backend.go b/plugins/ipam/host-local/backend/disk/backend.go index c3e6b496..56f1638a 100644 --- a/plugins/ipam/host-local/backend/disk/backend.go +++ b/plugins/ipam/host-local/backend/disk/backend.go @@ -19,10 +19,10 @@ import ( "net" "os" "path/filepath" + "runtime" "strings" "github.com/containernetworking/plugins/plugins/ipam/host-local/backend" - "runtime" ) const lastIPFilePrefix = "last_reserved_ip." @@ -172,6 +172,35 @@ func (s *Store) ReleaseByID(id string, ifname string) error { return err } +// GetByID returns the IPs which have been allocated to the specific ID +func (s *Store) GetByID(id string, ifname string) []net.IP { + var ips []net.IP + + match := strings.TrimSpace(id) + LineBreak + ifname + // matchOld for backwards compatibility + matchOld := strings.TrimSpace(id) + + // walk through all ips in this network to get the ones which belong to a specific ID + _ = filepath.Walk(s.dataDir, func(path string, info os.FileInfo, err error) error { + if err != nil || info.IsDir() { + return nil + } + data, err := ioutil.ReadFile(path) + if err != nil { + return nil + } + if strings.TrimSpace(string(data)) == match || strings.TrimSpace(string(data)) == matchOld { + _, ipString := filepath.Split(path) + if ip := net.ParseIP(ipString); ip != nil { + ips = append(ips, ip) + } + } + return nil + }) + + return ips +} + func GetEscapedPath(dataDir string, fname string) string { if runtime.GOOS == "windows" { fname = strings.Replace(fname, ":", "_", -1) diff --git a/plugins/ipam/host-local/backend/store.go b/plugins/ipam/host-local/backend/store.go index 4ea845da..7211ddf6 100644 --- a/plugins/ipam/host-local/backend/store.go +++ b/plugins/ipam/host-local/backend/store.go @@ -24,4 +24,5 @@ type Store interface { LastReservedIP(rangeID string) (net.IP, error) Release(ip net.IP) error ReleaseByID(id string, ifname string) error + GetByID(id string, ifname string) []net.IP } diff --git a/plugins/ipam/host-local/backend/testing/fake_store.go b/plugins/ipam/host-local/backend/testing/fake_store.go index 631fca2e..4f3a934e 100644 --- a/plugins/ipam/host-local/backend/testing/fake_store.go +++ b/plugins/ipam/host-local/backend/testing/fake_store.go @@ -81,6 +81,16 @@ func (s *FakeStore) ReleaseByID(id string, ifname string) error { return nil } +func (s *FakeStore) GetByID(id string, ifname string) []net.IP { + var ips []net.IP + for k, v := range s.ipMap { + if v == id { + ips = append(ips, net.ParseIP(k)) + } + } + return ips +} + func (s *FakeStore) SetIPMap(m map[string]string) { s.ipMap = m }