Skip to content

Commit 0fb93a2

Browse files
authored
Merge pull request #177 from sunya-ch/v1.2.2
add context timeout and connection close
2 parents f217e2e + d1fa502 commit 0fb93a2

File tree

15 files changed

+420
-94
lines changed

15 files changed

+420
-94
lines changed

cni/plugins/ipam/multi-nic-ipam/ipam.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io/ioutil"
1212
"net/http"
13+
"time"
1314

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

6061
if err != nil {
61-
return response, errors.New(fmt.Sprintf("Marshal fail: %v", err))
62+
return response, fmt.Errorf("marshal fail: %v", err)
6263
} else {
63-
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
64+
client := http.Client{
65+
Timeout: 2 * time.Minute,
66+
}
67+
defer client.CloseIdleConnections()
68+
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
6469
if err != nil {
65-
return response, errors.New(fmt.Sprintf("Post fail: %v", err))
70+
return response, fmt.Errorf("post fail: %v", err)
6671
}
72+
defer res.Body.Close()
6773
if res.StatusCode != http.StatusOK {
6874
return response, errors.New(res.Status)
6975
}
70-
7176
body, err := ioutil.ReadAll(res.Body)
7277
if err != nil {
73-
return response, errors.New(fmt.Sprintf("Read body: %v", err))
78+
return response, fmt.Errorf("read body: %v", err)
7479
}
7580
err = json.Unmarshal(body, &response)
7681
if err == nil && len(response) == 0 {
77-
return response, fmt.Errorf("Response nothing")
82+
return response, fmt.Errorf("response nothing")
7883
}
7984
return response, err
8085
}
@@ -97,23 +102,28 @@ func Deallocate(daemonPort int, podName string, podNamespace string, hostName st
97102
jsonReq, err := json.Marshal(request)
98103

99104
if err != nil {
100-
return response, errors.New(fmt.Sprintf("Marshal fail: %v", err))
105+
return response, fmt.Errorf("marshal fail: %v", err)
101106
} else {
102-
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
107+
client := http.Client{
108+
Timeout: 2 * time.Minute,
109+
}
110+
defer client.CloseIdleConnections()
111+
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
103112
if err != nil {
104-
return response, errors.New(fmt.Sprintf("Post fail: %v", err))
113+
return response, fmt.Errorf("post fail: %v", err)
105114
}
115+
defer res.Body.Close()
106116
if res.StatusCode != http.StatusOK {
107117
return response, errors.New(res.Status)
108118
}
109119

110120
body, err := ioutil.ReadAll(res.Body)
111121
if err != nil {
112-
return response, errors.New(fmt.Sprintf("Read body: %v", err))
122+
return response, fmt.Errorf("read body: %v", err)
113123
}
114124
err = json.Unmarshal(body, &response)
115125
if err == nil && len(response) == 0 {
116-
return response, fmt.Errorf("Response nothing")
126+
return response, fmt.Errorf("response nothing")
117127
}
118128
return response, err
119129
}

cni/plugins/main/multi-nic/selector.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io/ioutil"
1212
"net/http"
13+
"time"
1314

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

5859
if err != nil {
59-
return response, errors.New(fmt.Sprintf("Marshal fail: %v", err))
60+
return response, fmt.Errorf("marshal fail: %v", err)
6061
} else {
61-
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
62+
client := http.Client{
63+
Timeout: 5 * time.Minute,
64+
}
65+
defer client.CloseIdleConnections()
66+
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
6267
if err != nil {
63-
return response, errors.New(fmt.Sprintf("Post fail: %v", err))
68+
return response, fmt.Errorf("post fail: %v", err)
6469
}
70+
defer res.Body.Close()
6571
if res.StatusCode != http.StatusOK {
6672
return response, errors.New(res.Status)
6773
}
6874

6975
body, err := ioutil.ReadAll(res.Body)
7076
if err != nil {
71-
return response, errors.New(fmt.Sprintf("Read body: %v", err))
77+
return response, fmt.Errorf("read body: %v", err)
7278
}
7379
err = json.Unmarshal(body, &response)
7480
if err == nil && len(response.Masters) == 0 {
75-
return response, fmt.Errorf("Response nothing")
81+
return response, fmt.Errorf("response nothing")
7682
}
7783
return response, err
7884
}

controllers/cidr_handler.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ func (h *CIDRHandler) DeleteCIDR(cidr multinicv1.CIDR) error {
201201
}
202202
instance, err := h.GetCIDR(name)
203203
if err == nil {
204-
err = h.Client.Delete(context.Background(), instance)
204+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
205+
defer cancel()
206+
err = h.Client.Delete(ctx, instance)
205207
}
206208
if err != nil {
207209
errorMsg = errorMsg + fmt.Sprintf("%v,", err)
@@ -525,14 +527,18 @@ func (h *CIDRHandler) updateCIDR(cidrSpec multinicv1.CIDRSpec, new bool) (bool,
525527
if err == nil {
526528
updatedCIDR := existCIDR.DeepCopy()
527529
updatedCIDR.Spec = spec
528-
err = h.Client.Update(context.TODO(), updatedCIDR)
530+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
531+
defer cancel()
532+
err = h.Client.Update(ctx, updatedCIDR)
529533
if err == nil {
530534
h.SafeCache.SetCache(def.Name, updatedCIDR.Spec)
531535
}
532536
h.CleanPendingIPPools(ippoolSnapshot, def.Name, updatedCIDR.Spec)
533537
} else {
534538
if new {
535-
err = h.Client.Create(context.TODO(), mapObj)
539+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
540+
defer cancel()
541+
err = h.Client.Create(ctx, mapObj)
536542
if err == nil {
537543
h.SafeCache.SetCache(def.Name, mapObj.Spec)
538544
}

controllers/daemon_connector.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"io/ioutil"
1212
"net/http"
13-
"time"
1413

1514
"bytes"
1615
"errors"
@@ -73,11 +72,13 @@ func (dc DaemonConnector) GetInterfaces(podAddress string) ([]multinicv1.Interfa
7372
var interfaces []multinicv1.InterfaceInfoType
7473
address := podAddress + INTERFACE_PATH
7574
// try connect and get interface from daemon pod
76-
res, err := http.Get(address)
75+
client := http.Client{}
76+
defer client.CloseIdleConnections()
77+
res, err := client.Get(address)
7778
if err != nil {
7879
return []multinicv1.InterfaceInfoType{}, err
7980
}
80-
81+
defer res.Body.Close()
8182
body, err := ioutil.ReadAll(res.Body)
8283
if err != nil {
8384
return []multinicv1.InterfaceInfoType{}, err
@@ -100,12 +101,13 @@ func (dc DaemonConnector) Join(podAddress string, hifs []multinicv1.InterfaceInf
100101
return err
101102
} else {
102103
client := http.Client{
103-
Timeout: 5 * time.Second,
104+
Timeout: vars.ContextTimeout,
104105
}
105106
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
106107
if err != nil {
107108
return err
108109
}
110+
defer res.Body.Close()
109111
if res.StatusCode != http.StatusOK {
110112
return errors.New(res.Status)
111113
}
@@ -145,7 +147,15 @@ func (dc DaemonConnector) putRouteRequest(podAddress string, path string, cidrNa
145147
if err != nil {
146148
return response, err
147149
} else {
148-
res, err := http.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
150+
client := http.Client{
151+
Timeout: vars.ContextTimeout,
152+
}
153+
defer client.CloseIdleConnections()
154+
res, err := client.Post(address, "application/json; charset=utf-8", bytes.NewBuffer(jsonReq))
155+
if err != nil {
156+
return response, fmt.Errorf("post fail: %v", err)
157+
}
158+
defer res.Body.Close()
149159
if err != nil {
150160
response.Message = vars.ConnectionRefusedError
151161
return response, err

controllers/hostinterface_handler.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func (h *HostInterfaceHandler) initHostInterface(hostName string, interfaces []m
6161
// CreateHostInterface creates new HostInterface from an interface list get from daemon pods
6262
func (h *HostInterfaceHandler) CreateHostInterface(hostName string, interfaces []multinicv1.InterfaceInfoType) error {
6363
newHif := h.initHostInterface(hostName, interfaces)
64-
return h.Client.Create(context.TODO(), newHif)
64+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
65+
defer cancel()
66+
return h.Client.Create(ctx, newHif)
6567
}
6668

6769
// UpdateHostInterface updates HostInterface
@@ -73,7 +75,9 @@ func (h *HostInterfaceHandler) UpdateHostInterface(oldObj multinicv1.HostInterfa
7375
Interfaces: interfaces,
7476
},
7577
}
76-
return updateHif, h.Client.Update(context.TODO(), updateHif)
78+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
79+
defer cancel()
80+
return updateHif, h.Client.Update(ctx, updateHif)
7781
}
7882

7983
// GetHostInterface gets HostInterface from hostname
@@ -83,7 +87,9 @@ func (h *HostInterfaceHandler) GetHostInterface(name string) (*multinicv1.HostIn
8387
Name: name,
8488
Namespace: metav1.NamespaceAll,
8589
}
86-
err := h.Client.Get(context.TODO(), namespacedName, instance)
90+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
91+
defer cancel()
92+
err := h.Client.Get(ctx, namespacedName, instance)
8793
return instance, err
8894
}
8995

@@ -107,7 +113,9 @@ func (h *HostInterfaceHandler) ListHostInterface() (map[string]multinicv1.HostIn
107113
func (h *HostInterfaceHandler) DeleteHostInterface(name string) error {
108114
instance, err := h.GetHostInterface(name)
109115
if err == nil {
110-
err = h.Client.Delete(context.TODO(), instance)
116+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
117+
defer cancel()
118+
err = h.Client.Delete(ctx, instance)
111119
}
112120
return err
113121
}

controllers/ippool_handler.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ func (h *IPPoolHandler) DeleteIPPool(netAttachDef string, podCIDR string) error
6969
name := h.GetIPPoolName(netAttachDef, podCIDR)
7070
instance, err := h.GetIPPool(name)
7171
if err == nil {
72-
err = h.Client.Delete(context.TODO(), instance)
72+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
73+
defer cancel()
74+
err = h.Client.Delete(ctx, instance)
7375
}
7476
return err
7577
}
@@ -132,7 +134,9 @@ func (h *IPPoolHandler) UpdateIPPool(netAttachDef string, podCIDR string, vlanCI
132134
ippool.Spec = spec
133135
ippool.Spec.Allocations = prevSpec.Allocations
134136
ippool.ObjectMeta.Labels = labels
135-
err = h.Client.Update(context.TODO(), ippool)
137+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
138+
defer cancel()
139+
err = h.Client.Update(ctx, ippool)
136140
if !reflect.DeepEqual(prevSpec.Excludes, excludesInterface) {
137141
// report if allocated ip addresses have conflicts with the new IPPool (for example, in exclude list)
138142
invalidAllocations := h.checkPoolValidity(excludesInterface, prevSpec.Allocations)
@@ -154,7 +158,9 @@ func (h *IPPoolHandler) UpdateIPPool(netAttachDef string, podCIDR string, vlanCI
154158
},
155159
Spec: spec,
156160
}
157-
err = h.Client.Create(context.Background(), newIPPool)
161+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
162+
defer cancel()
163+
err = h.Client.Create(ctx, newIPPool)
158164
vars.IPPoolLog.V(5).Info(fmt.Sprintf("New IPPool %s: %v, %v", ippoolName, newIPPool, err))
159165
}
160166
return err
@@ -192,7 +198,9 @@ func (h *IPPoolHandler) PatchIPPoolAllocations(ippoolName string, newAllocations
192198
}
193199
patch := client.MergeFrom(ippool.DeepCopy())
194200
ippool.Spec.Allocations = newAllocations
195-
return h.Client.Patch(context.Background(), ippool, patch)
201+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
202+
defer cancel()
203+
return h.Client.Patch(ctx, ippool, patch)
196204
}
197205

198206
func (h *IPPoolHandler) UpdateIPPools(defName string, entries []multinicv1.CIDREntry, excludes []compute.IPValue) {
@@ -234,6 +242,8 @@ func (h *IPPoolHandler) AddLabel(ippool *multinicv1.IPPool) error {
234242
labels := map[string]string{vars.HostNameLabel: hostName, vars.DefNameLabel: netAttachDef}
235243
patch := client.MergeFrom(ippool.DeepCopy())
236244
ippool.ObjectMeta.Labels = labels
237-
err := h.Client.Patch(context.Background(), ippool, patch)
245+
ctx, cancel := context.WithTimeout(context.Background(), vars.ContextTimeout)
246+
defer cancel()
247+
err := h.Client.Patch(ctx, ippool, patch)
238248
return err
239249
}

controllers/multinicnetwork_controller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,18 @@ func (r *MultiNicNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Requ
184184
vars.NetworkLog.V(3).Info(fmt.Sprintf("CIDR %s successfully applied", multinicnetworkName))
185185
}
186186
}
187-
} else if !instance.Spec.IsMultiNICIPAM && routeStatus == multinicv1.RouteNoApplied {
187+
} else if !instance.Spec.IsMultiNICIPAM && (routeStatus == "" || routeStatus == multinicv1.RouteNoApplied) {
188188
// not related to L3
189+
instance.Status.Message = ""
190+
instance.Status.RouteStatus = multinicv1.RouteNoApplied
191+
vars.NetworkLog.V(3).Info(fmt.Sprintf("Update %s status (Non-MultiNICIPAM, RouteNoApplied)", multinicnetworkName))
189192
err = r.CIDRHandler.MultiNicNetworkHandler.UpdateNetConfigStatus(instance, multinicv1.ConfigComplete, "")
190193
if err != nil {
191194
vars.NetworkLog.V(3).Info(fmt.Sprintf("Failed to UpdateNetConfigStatus %s for non-L3: %v", instance.Name, err))
192195
}
193196
} else if routeStatus != multinicv1.AllRouteApplied {
194197
// some route still fails
198+
vars.NetworkLog.V(3).Info(fmt.Sprintf("Update %s status (waiting for route configuration)", multinicnetworkName))
195199
err = r.CIDRHandler.MultiNicNetworkHandler.UpdateNetConfigStatus(instance, multinicv1.WaitForConfig, "")
196200
if err != nil {
197201
vars.NetworkLog.V(3).Info(fmt.Sprintf("Failed to UpdateNetConfigStatus %s at route failure: %v", instance.Name, err))
@@ -234,12 +238,16 @@ func (r *MultiNicNetworkReconciler) GetIPAMConfig(instance *multinicv1.MultiNicN
234238
ipamConfig.MasterNetAddrs = instance.Spec.MasterNetAddrs
235239
return ipamConfig, nil
236240
}
237-
return nil, fmt.Errorf("non-MultiNicIPAM")
241+
vars.NetworkLog.V(3).Info("non-MultiNicIPAM")
242+
return nil, nil
238243
}
239244

240245
// HandleMultiNicIPAM handles ipam if target type
241246
func (r *MultiNicNetworkReconciler) HandleMultiNicIPAM(instance *multinicv1.MultiNicNetwork) error {
242247
ipamConfig, err := r.GetIPAMConfig(instance)
248+
if ipamConfig == nil {
249+
return err
250+
}
243251
if err == nil {
244252
cidrName := instance.GetName()
245253
_, err := r.CIDRHandler.GetCIDR(cidrName)

0 commit comments

Comments
 (0)