Fix for http client call to wireserver (#431)
* modified wireserver call to non-blocking. Fixed logging issue in ipam * fixed ut * addressing review comments * used inithttpclient function
This commit is contained in:
Родитель
e0ba7ad924
Коммит
534e646fd9
|
@ -19,11 +19,6 @@ import (
|
||||||
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
|
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// Plugin name.
|
|
||||||
name = "azure-vnet-ipam"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ipv4DefaultRouteDstPrefix = net.IPNet{net.IPv4zero, net.IPv4Mask(0, 0, 0, 0)}
|
ipv4DefaultRouteDstPrefix = net.IPNet{net.IPv4zero, net.IPv4Mask(0, 0, 0, 0)}
|
||||||
)
|
)
|
||||||
|
@ -35,7 +30,7 @@ type ipamPlugin struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPlugin creates a new ipamPlugin object.
|
// NewPlugin creates a new ipamPlugin object.
|
||||||
func NewPlugin(config *common.PluginConfig) (*ipamPlugin, error) {
|
func NewPlugin(name string, config *common.PluginConfig) (*ipamPlugin, error) {
|
||||||
// Setup base plugin.
|
// Setup base plugin.
|
||||||
plugin, err := cni.NewPlugin(name, config.Version)
|
plugin, err := cni.NewPlugin(name, config.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -51,7 +51,7 @@ func TestMain(m *testing.M) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the plugin.
|
// Create the plugin.
|
||||||
plugin, err = NewPlugin(&config)
|
plugin, err = NewPlugin("ipamtest", &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to create IPAM plugin, err:%v.\n", err)
|
fmt.Printf("Failed to create IPAM plugin, err:%v.\n", err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -10,6 +10,11 @@ import (
|
||||||
"github.com/Azure/azure-container-networking/cni"
|
"github.com/Azure/azure-container-networking/cni"
|
||||||
"github.com/Azure/azure-container-networking/cni/ipam"
|
"github.com/Azure/azure-container-networking/cni/ipam"
|
||||||
"github.com/Azure/azure-container-networking/common"
|
"github.com/Azure/azure-container-networking/common"
|
||||||
|
"github.com/Azure/azure-container-networking/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
name = "azure-vnet-ipam"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version is populated by make during build.
|
// Version is populated by make during build.
|
||||||
|
@ -20,7 +25,16 @@ func main() {
|
||||||
var config common.PluginConfig
|
var config common.PluginConfig
|
||||||
config.Version = version
|
config.Version = version
|
||||||
|
|
||||||
ipamPlugin, err := ipam.NewPlugin(&config)
|
log.SetName(name)
|
||||||
|
log.SetLevel(log.LevelInfo)
|
||||||
|
if err := log.SetTarget(log.TargetLogfile); err != nil {
|
||||||
|
fmt.Printf("Failed to setup cni logging: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer log.Close()
|
||||||
|
|
||||||
|
ipamPlugin, err := ipam.NewPlugin(name, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to create IPAM plugin, err:%v.\n", err)
|
fmt.Printf("Failed to create IPAM plugin, err:%v.\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -5,6 +5,7 @@ package ipam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -17,9 +18,12 @@ import (
|
||||||
const (
|
const (
|
||||||
// Host URL to query.
|
// Host URL to query.
|
||||||
azureQueryUrl = "http://168.63.129.16/machine/plugins?comp=nmagent&type=getinterfaceinfov1"
|
azureQueryUrl = "http://168.63.129.16/machine/plugins?comp=nmagent&type=getinterfaceinfov1"
|
||||||
|
|
||||||
// Minimum time interval between consecutive queries.
|
// Minimum time interval between consecutive queries.
|
||||||
azureQueryInterval = 10 * time.Second
|
azureQueryInterval = 10 * time.Second
|
||||||
|
// http connection timeout
|
||||||
|
httpConnectionTimeout = 10
|
||||||
|
// http response header timeout
|
||||||
|
responseHeaderTimeout = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
// Microsoft Azure IPAM configuration source.
|
// Microsoft Azure IPAM configuration source.
|
||||||
|
@ -84,14 +88,27 @@ func (s *azureSource) refresh() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
httpClient := common.InitHttpClient(httpConnectionTimeout, responseHeaderTimeout)
|
||||||
|
if httpClient == nil {
|
||||||
|
log.Errorf("[ipam] Failed intializing http client")
|
||||||
|
return fmt.Errorf("Error intializing http client")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("[ipam] Wireserver call %v to retrieve IP List", s.queryUrl)
|
||||||
// Fetch configuration.
|
// Fetch configuration.
|
||||||
resp, err := http.Get(s.queryUrl)
|
resp, err := httpClient.Get(s.queryUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ipam] wireserver call failed with: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
log.Errorf("[ipam] http return error code for wireserver call %+v", resp)
|
||||||
|
return fmt.Errorf("wireserver http error %+v", resp)
|
||||||
|
}
|
||||||
|
|
||||||
// Decode XML document.
|
// Decode XML document.
|
||||||
var doc common.XmlDocument
|
var doc common.XmlDocument
|
||||||
decoder := xml.NewDecoder(resp.Body)
|
decoder := xml.NewDecoder(resp.Body)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче