From 7586b39a7bb20ccef6c01705199a7d052e3a298c Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Sun, 13 Sep 2015 15:04:36 -0700 Subject: [PATCH] nomad: removing old network index lookup methods --- nomad/structs/structs.go | 45 ++--------------------------------- nomad/structs/structs_test.go | 31 ++++++------------------ 2 files changed, 9 insertions(+), 67 deletions(-) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 3f6b66cce..f5479c3bc 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -3,7 +3,6 @@ package structs import ( "bytes" "fmt" - "net" "time" "github.com/hashicorp/go-msgpack/codec" @@ -551,50 +550,10 @@ func (r *Resources) Copy() *Resources { return newR } -// NetIndex finds the matching net index either using IP or -// CIDR block lookup +// NetIndex finds the matching net index using device name func (r *Resources) NetIndex(n *NetworkResource) int { - if n.IP != "" { - return r.NetIndexByIP(n.IP) - } - return r.NetIndexByCIDR(n.CIDR) -} - -// NetIndexByCIDR scans the list of networks for a matching -// CIDR, returning the index. This currently ONLY handles -// an exact match and not a subset CIDR. -func (r *Resources) NetIndexByCIDR(cidr string) int { for idx, net := range r.Networks { - if net.CIDR == cidr { - return idx - } - } - return -1 -} - -// NetIndexByIP scans the list of networks for a matching -// CIDR by IP, returning the index. -func (r *Resources) NetIndexByIP(ip string) int { - // Parse the IP - parsed := net.ParseIP(ip) - if parsed == nil { - return -1 - } - - for idx, n := range r.Networks { - // Look for exact IP match - if n.IP == ip { - return idx - } - - // Check for CIDR subset - if n.CIDR == "" { - continue - } - - // Check if the CIDR contains the IP - _, cidr, _ := net.ParseCIDR(n.CIDR) - if cidr != nil && cidr.Contains(parsed) { + if net.Device == n.Device { return idx } } diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 676ba421b..259f51135 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -5,38 +5,21 @@ import ( "testing" ) -func TestResource_NetIndexByCIDR(t *testing.T) { +func TestResource_NetIndex(t *testing.T) { r := &Resources{ Networks: []*NetworkResource{ - &NetworkResource{CIDR: "10.0.0.0/8"}, - &NetworkResource{CIDR: "127.0.0.0/24"}, + &NetworkResource{Device: "eth0"}, + &NetworkResource{Device: "lo0"}, + &NetworkResource{Device: ""}, }, } - if idx := r.NetIndexByCIDR("10.0.0.0/8"); idx != 0 { + if idx := r.NetIndex(&NetworkResource{Device: "eth0"}); idx != 0 { t.Fatalf("Bad: %d", idx) } - if idx := r.NetIndexByCIDR("127.0.0.0/24"); idx != 1 { + if idx := r.NetIndex(&NetworkResource{Device: "lo0"}); idx != 1 { t.Fatalf("Bad: %d", idx) } - if idx := r.NetIndexByCIDR("10.0.0.0/16"); idx != -1 { - t.Fatalf("Bad: %d", idx) - } -} - -func TestResource_NetIndexByIP(t *testing.T) { - r := &Resources{ - Networks: []*NetworkResource{ - &NetworkResource{CIDR: "10.0.0.0/8"}, - &NetworkResource{CIDR: "127.0.0.0/24"}, - }, - } - if idx := r.NetIndexByIP("10.1.2.3"); idx != 0 { - t.Fatalf("Bad: %d", idx) - } - if idx := r.NetIndexByIP("127.0.0.1"); idx != 1 { - t.Fatalf("Bad: %d", idx) - } - if idx := r.NetIndexByIP("11.2.3.4"); idx != -1 { + if idx := r.NetIndex(&NetworkResource{Device: "eth1"}); idx != -1 { t.Fatalf("Bad: %d", idx) } }