Skip to content

Commit

Permalink
add missing context deadline on controller
Browse files Browse the repository at this point in the history
Signed-off-by: Sunyanan Choochotkaew <[email protected]>
  • Loading branch information
sunya-ch committed Jan 22, 2024
1 parent a974f04 commit d1fa502
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
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
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
}

0 comments on commit d1fa502

Please sign in to comment.