Skip to content

Commit

Permalink
Merge pull request #177 from sunya-ch/v1.2.2
Browse files Browse the repository at this point in the history
add context timeout and connection close
  • Loading branch information
sunya-ch authored Jan 23, 2024
2 parents f217e2e + d1fa502 commit 0fb93a2
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 94 deletions.
32 changes: 21 additions & 11 deletions cni/plugins/ipam/multi-nic-ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

"bytes"
"errors"
Expand Down Expand Up @@ -58,23 +59,27 @@ func RequestIP(daemonIP string, daemonPort int, podName string, podNamespace str
jsonReq, err := json.Marshal(request)

if err != nil {
return response, errors.New(fmt.Sprintf("Marshal fail: %v", err))
return response, fmt.Errorf("marshal fail: %v", err)
} else {
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
client := http.Client{
Timeout: 2 * time.Minute,
}
defer client.CloseIdleConnections()
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
if err != nil {
return response, errors.New(fmt.Sprintf("Post fail: %v", err))
return response, fmt.Errorf("post fail: %v", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return response, errors.New(res.Status)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, errors.New(fmt.Sprintf("Read body: %v", err))
return response, fmt.Errorf("read body: %v", err)
}
err = json.Unmarshal(body, &response)
if err == nil && len(response) == 0 {
return response, fmt.Errorf("Response nothing")
return response, fmt.Errorf("response nothing")
}
return response, err
}
Expand All @@ -97,23 +102,28 @@ func Deallocate(daemonPort int, podName string, podNamespace string, hostName st
jsonReq, err := json.Marshal(request)

if err != nil {
return response, errors.New(fmt.Sprintf("Marshal fail: %v", err))
return response, fmt.Errorf("marshal fail: %v", err)
} else {
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
client := http.Client{
Timeout: 2 * time.Minute,
}
defer client.CloseIdleConnections()
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
if err != nil {
return response, errors.New(fmt.Sprintf("Post fail: %v", err))
return response, fmt.Errorf("post fail: %v", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return response, errors.New(res.Status)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, errors.New(fmt.Sprintf("Read body: %v", err))
return response, fmt.Errorf("read body: %v", err)
}
err = json.Unmarshal(body, &response)
if err == nil && len(response) == 0 {
return response, fmt.Errorf("Response nothing")
return response, fmt.Errorf("response nothing")
}
return response, err
}
Expand Down
16 changes: 11 additions & 5 deletions cni/plugins/main/multi-nic/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

"bytes"
"errors"
Expand Down Expand Up @@ -56,23 +57,28 @@ func selectNICs(daemonIP string, daemonPort int, podName string, podNamespace st
jsonReq, err := json.Marshal(request)

if err != nil {
return response, errors.New(fmt.Sprintf("Marshal fail: %v", err))
return response, fmt.Errorf("marshal fail: %v", err)
} else {
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
client := http.Client{
Timeout: 5 * time.Minute,
}
defer client.CloseIdleConnections()
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
if err != nil {
return response, errors.New(fmt.Sprintf("Post fail: %v", err))
return response, fmt.Errorf("post fail: %v", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return response, errors.New(res.Status)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, errors.New(fmt.Sprintf("Read body: %v", err))
return response, fmt.Errorf("read body: %v", err)
}
err = json.Unmarshal(body, &response)
if err == nil && len(response.Masters) == 0 {
return response, fmt.Errorf("Response nothing")
return response, fmt.Errorf("response nothing")
}
return response, err
}
Expand Down
12 changes: 9 additions & 3 deletions controllers/cidr_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ func (h *CIDRHandler) DeleteCIDR(cidr multinicv1.CIDR) error {
}
instance, err := h.GetCIDR(name)
if err == nil {
err = h.Client.Delete(context.Background(), instance)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Delete(ctx, instance)
}
if err != nil {
errorMsg = errorMsg + fmt.Sprintf("%v,", err)
Expand Down Expand Up @@ -525,14 +527,18 @@ func (h *CIDRHandler) updateCIDR(cidrSpec multinicv1.CIDRSpec, new bool) (bool,
if err == nil {
updatedCIDR := existCIDR.DeepCopy()
updatedCIDR.Spec = spec
err = h.Client.Update(context.TODO(), updatedCIDR)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Update(ctx, updatedCIDR)
if err == nil {
h.SafeCache.SetCache(def.Name, updatedCIDR.Spec)
}
h.CleanPendingIPPools(ippoolSnapshot, def.Name, updatedCIDR.Spec)
} else {
if new {
err = h.Client.Create(context.TODO(), mapObj)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Create(ctx, mapObj)
if err == nil {
h.SafeCache.SetCache(def.Name, mapObj.Spec)
}
Expand Down
20 changes: 15 additions & 5 deletions controllers/daemon_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

"bytes"
"errors"
Expand Down Expand Up @@ -73,11 +72,13 @@ func (dc DaemonConnector) GetInterfaces(podAddress string) ([]multinicv1.Interfa
var interfaces []multinicv1.InterfaceInfoType
address := podAddress + INTERFACE_PATH
// try connect and get interface from daemon pod
res, err := http.Get(address)
client := http.Client{}
defer client.CloseIdleConnections()
res, err := client.Get(address)
if err != nil {
return []multinicv1.InterfaceInfoType{}, err
}

defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []multinicv1.InterfaceInfoType{}, err
Expand All @@ -100,12 +101,13 @@ func (dc DaemonConnector) Join(podAddress string, hifs []multinicv1.InterfaceInf
return err
} else {
client := http.Client{
Timeout: 5 * time.Second,
Timeout: vars.ContextTimeout,
}
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return errors.New(res.Status)
}
Expand Down Expand Up @@ -145,7 +147,15 @@ func (dc DaemonConnector) putRouteRequest(podAddress string, path string, cidrNa
if err != nil {
return response, err
} else {
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
client := http.Client{
Timeout: vars.ContextTimeout,
}
defer client.CloseIdleConnections()
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
if err != nil {
return response, fmt.Errorf("post fail: %v", err)
}
defer res.Body.Close()
if err != nil {
response.Message = vars.ConnectionRefusedError
return response, err
Expand Down
16 changes: 12 additions & 4 deletions controllers/hostinterface_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func (h *HostInterfaceHandler) initHostInterface(hostName string, interfaces []m
// CreateHostInterface creates new HostInterface from an interface list get from daemon pods
func (h *HostInterfaceHandler) CreateHostInterface(hostName string, interfaces []multinicv1.InterfaceInfoType) error {
newHif := h.initHostInterface(hostName, interfaces)
return h.Client.Create(context.TODO(), newHif)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
return h.Client.Create(ctx, newHif)
}

// UpdateHostInterface updates HostInterface
Expand All @@ -73,7 +75,9 @@ func (h *HostInterfaceHandler) UpdateHostInterface(oldObj multinicv1.HostInterfa
Interfaces: interfaces,
},
}
return updateHif, h.Client.Update(context.TODO(), updateHif)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
return updateHif, h.Client.Update(ctx, updateHif)
}

// GetHostInterface gets HostInterface from hostname
Expand All @@ -83,7 +87,9 @@ func (h *HostInterfaceHandler) GetHostInterface(name string) (*multinicv1.HostIn
Name: name,
Namespace: metav1.NamespaceAll,
}
err := h.Client.Get(context.TODO(), namespacedName, instance)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err := h.Client.Get(ctx, namespacedName, instance)
return instance, err
}

Expand All @@ -107,7 +113,9 @@ func (h *HostInterfaceHandler) ListHostInterface() (map[string]multinicv1.HostIn
func (h *HostInterfaceHandler) DeleteHostInterface(name string) error {
instance, err := h.GetHostInterface(name)
if err == nil {
err = h.Client.Delete(context.TODO(), instance)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Delete(ctx, instance)
}
return err
}
Expand Down
20 changes: 15 additions & 5 deletions controllers/ippool_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func (h *IPPoolHandler) DeleteIPPool(netAttachDef string, podCIDR string) error
name := h.GetIPPoolName(netAttachDef, podCIDR)
instance, err := h.GetIPPool(name)
if err == nil {
err = h.Client.Delete(context.TODO(), instance)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Delete(ctx, instance)
}
return err
}
Expand Down Expand Up @@ -132,7 +134,9 @@ func (h *IPPoolHandler) UpdateIPPool(netAttachDef string, podCIDR string, vlanCI
ippool.Spec = spec
ippool.Spec.Allocations = prevSpec.Allocations
ippool.ObjectMeta.Labels = labels
err = h.Client.Update(context.TODO(), ippool)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Update(ctx, ippool)
if !reflect.DeepEqual(prevSpec.Excludes, excludesInterface) {
// report if allocated ip addresses have conflicts with the new IPPool (for example, in exclude list)
invalidAllocations := h.checkPoolValidity(excludesInterface, prevSpec.Allocations)
Expand All @@ -154,7 +158,9 @@ func (h *IPPoolHandler) UpdateIPPool(netAttachDef string, podCIDR string, vlanCI
},
Spec: spec,
}
err = h.Client.Create(context.Background(), newIPPool)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err = h.Client.Create(ctx, newIPPool)
vars.IPPoolLog.V(5).Info(fmt.Sprintf("New IPPool %s: %v, %v", ippoolName, newIPPool, err))
}
return err
Expand Down Expand Up @@ -192,7 +198,9 @@ func (h *IPPoolHandler) PatchIPPoolAllocations(ippoolName string, newAllocations
}
patch := client.MergeFrom(ippool.DeepCopy())
ippool.Spec.Allocations = newAllocations
return h.Client.Patch(context.Background(), ippool, patch)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
return h.Client.Patch(ctx, ippool, patch)
}

func (h *IPPoolHandler) UpdateIPPools(defName string, entries []multinicv1.CIDREntry, excludes []compute.IPValue) {
Expand Down Expand Up @@ -234,6 +242,8 @@ func (h *IPPoolHandler) AddLabel(ippool *multinicv1.IPPool) error {
labels := map[string]string{vars.HostNameLabel: hostName, vars.DefNameLabel: netAttachDef}
patch := client.MergeFrom(ippool.DeepCopy())
ippool.ObjectMeta.Labels = labels
err := h.Client.Patch(context.Background(), ippool, patch)
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
defer cancel()
err := h.Client.Patch(ctx, ippool, patch)
return err
}
12 changes: 10 additions & 2 deletions controllers/multinicnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ func (r *MultiNicNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Requ
vars.NetworkLog.V(3).Info(fmt.Sprintf("CIDR %s successfully applied", multinicnetworkName))
}
}
} else if !instance.Spec.IsMultiNICIPAM && routeStatus == multinicv1.RouteNoApplied {
} else if !instance.Spec.IsMultiNICIPAM && (routeStatus == "" || routeStatus == multinicv1.RouteNoApplied) {
// not related to L3
instance.Status.Message = ""
instance.Status.RouteStatus = multinicv1.RouteNoApplied
vars.NetworkLog.V(3).Info(fmt.Sprintf("Update %s status (Non-MultiNICIPAM, RouteNoApplied)", multinicnetworkName))
err = r.CIDRHandler.MultiNicNetworkHandler.UpdateNetConfigStatus(instance, multinicv1.ConfigComplete, "")
if err != nil {
vars.NetworkLog.V(3).Info(fmt.Sprintf("Failed to UpdateNetConfigStatus %s for non-L3: %v", instance.Name, err))
}
} else if routeStatus != multinicv1.AllRouteApplied {
// some route still fails
vars.NetworkLog.V(3).Info(fmt.Sprintf("Update %s status (waiting for route configuration)", multinicnetworkName))
err = r.CIDRHandler.MultiNicNetworkHandler.UpdateNetConfigStatus(instance, multinicv1.WaitForConfig, "")
if err != nil {
vars.NetworkLog.V(3).Info(fmt.Sprintf("Failed to UpdateNetConfigStatus %s at route failure: %v", instance.Name, err))
Expand Down Expand Up @@ -234,12 +238,16 @@ func (r *MultiNicNetworkReconciler) GetIPAMConfig(instance *multinicv1.MultiNicN
ipamConfig.MasterNetAddrs = instance.Spec.MasterNetAddrs
return ipamConfig, nil
}
return nil, fmt.Errorf("non-MultiNicIPAM")
vars.NetworkLog.V(3).Info("non-MultiNicIPAM")
return nil, nil
}

// HandleMultiNicIPAM handles ipam if target type
func (r *MultiNicNetworkReconciler) HandleMultiNicIPAM(instance *multinicv1.MultiNicNetwork) error {
ipamConfig, err := r.GetIPAMConfig(instance)
if ipamConfig == nil {
return err
}
if err == nil {
cidrName := instance.GetName()
_, err := r.CIDRHandler.GetCIDR(cidrName)
Expand Down
Loading

0 comments on commit 0fb93a2

Please sign in to comment.