diff --git a/controllers/cidr_handler.go b/controllers/cidr_handler.go index 7a26c65c..c64e8275 100644 --- a/controllers/cidr_handler.go +++ b/controllers/cidr_handler.go @@ -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) @@ -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) } diff --git a/controllers/hostinterface_handler.go b/controllers/hostinterface_handler.go index 4ba81a21..18cf2570 100644 --- a/controllers/hostinterface_handler.go +++ b/controllers/hostinterface_handler.go @@ -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 @@ -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 @@ -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 } @@ -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 } diff --git a/controllers/ippool_handler.go b/controllers/ippool_handler.go index bc61ef3d..3d9ed3df 100644 --- a/controllers/ippool_handler.go +++ b/controllers/ippool_handler.go @@ -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 } @@ -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) @@ -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 @@ -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) { @@ -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 }