Skip to content

Commit

Permalink
Improve log and error messages (#29)
Browse files Browse the repository at this point in the history
- Make sure all IPAM requests and responses are logged
- Improve error messages in responses to IPAM requests
  • Loading branch information
yuewko authored and johnbelamaric committed Jan 26, 2017
1 parent e9039fc commit bbcd79e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
30 changes: 20 additions & 10 deletions infoblox-ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,27 @@ func (ibDrv *InfobloxDriver) RequestAddress(r interface{}) (map[string]interface
if fixedAddr != nil {
if v.Address != "" {
if fixedAddr.IPAddress != v.Address {
log.Printf("Requested MAC address '%s' is already associated with a difference IP address '%s' (requested: '%s')",
msg := fmt.Sprintf("Requested MAC '%s' is already associated with a difference IP '%s' (requested: '%s')",
macAddr, fixedAddr.IPAddress, v.Address)

return nil, nil
log.Printf("RequestAddress: %s", msg)
return nil, errors.New(msg)
}
}
}

var err error
if fixedAddr == nil {
fixedAddr, _ = ibDrv.objMgr.AllocateIP(network.NetviewName, network.Cidr, v.Address, macAddr, "")
fixedAddr, err = ibDrv.objMgr.AllocateIP(network.NetviewName, network.Cidr, v.Address, macAddr, "")
}

var res map[string]interface{}
if fixedAddr == nil || err != nil {
res = map[string]interface{}{}
} else {
res = map[string]interface{}{"Address": fmt.Sprintf("%s/%s", fixedAddr.IPAddress, getPrefixLength(network.Cidr))}
}

return map[string]interface{}{"Address": fmt.Sprintf("%s/%s", fixedAddr.IPAddress, getPrefixLength(network.Cidr))}, nil
return res, err
}

func (ibDrv *InfobloxDriver) ReleaseAddress(r interface{}) (map[string]interface{}, error) {
Expand All @@ -108,8 +116,9 @@ func (ibDrv *InfobloxDriver) requestSpecificNetwork(netview string, pool string,
}
if network != nil {
if n, ok := network.Ea["Network Name"]; !ok || n != networkName {
log.Printf("requestSpecificNetwork: network is already used '%s'", *network)
return nil, nil
msg := fmt.Sprintf("Network (%s) already in use", network.Cidr)
log.Printf("requestSpecificNetwork: %s", msg)
return nil, errors.New(msg)
}
} else {
networkByName, err := ibDrv.objMgr.GetNetwork(netview, "", ibclient.EA{"Network Name": networkName})
Expand All @@ -118,8 +127,9 @@ func (ibDrv *InfobloxDriver) requestSpecificNetwork(netview string, pool string,
}
if networkByName != nil {
if networkByName.Cidr != pool {
log.Printf("requestSpecificNetwork: network name has different Cidr '%s'", networkByName.Cidr)
return nil, nil
msg := fmt.Sprintf("Network name (%s) has different CIDR (%s)", networkName, networkByName.Cidr)
log.Printf("requestSpecificNetwork: %s", msg)
return nil, errors.New(msg)
}
}
}
Expand Down Expand Up @@ -258,7 +268,7 @@ func (ibDrv *InfobloxDriver) ReleasePool(r interface{}) (map[string]interface{},

ref, _ := ibDrv.objMgr.DeleteNetwork(v.PoolID, networkFromRef.NetviewName)
if len(ref) > 0 {
log.Printf("Network %s deleted from Infoblox\n", v.PoolID)
log.Printf("Network %s successfully deleted from Infoblox\n", v.PoolID)
}
}

Expand Down
32 changes: 24 additions & 8 deletions ipam-driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"reflect"
"strings"
)

func getDockerID() (dockerID string, err error) {
Expand Down Expand Up @@ -107,6 +108,16 @@ func setupSocket(pluginDir string, driverName string) string {
return socketFile
}

func urlToRequestType(url string) string {
parts := strings.Split(url, ".")
n := len(parts)
if n > 0 {
n = n - 1
}

return parts[n]
}

type ipamCall struct {
url string
f func(r interface{}) (map[string]interface{}, error)
Expand Down Expand Up @@ -180,7 +191,8 @@ func main() {

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
url := r.URL.String()
log.Printf("Plugin: %s\n", url)
requestType := urlToRequestType(url)

if c, ok := handlers[url]; ok {

//var req interface{}
Expand All @@ -194,19 +206,23 @@ func main() {
}
}

log.Printf("'%s' request: '%s'\n", requestType, req)
res, err := c.f(req)
if err != nil || res == nil {
if err != nil {
log.Printf("IPAM Driver error '%s'", err)
} else if res == nil {
if res == nil {
log.Printf("IPAM Driver returned nil result")
res = make(map[string]interface{})
}
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
} else {
if err := json.NewEncoder(w).Encode(res); err != nil {
log.Printf("%s: Bad Response Error: %s\n", url, err)
if err != nil {
log.Printf("IPAM Driver error '%s'", err)
res["Error"] = err.Error()
}
}
log.Printf("'%s' result: '%s'\n", requestType, res)

if err := json.NewEncoder(w).Encode(res); err != nil {
log.Printf("%s: Bad Response Error: %s\n", url, err)
}
}
fmt.Fprintf(w, "{ \"Error\": \"%s\"}", url)
})
Expand Down

0 comments on commit bbcd79e

Please sign in to comment.