@@ -23,7 +23,8 @@ import (
23
23
)
24
24
25
25
const (
26
- API_VERSION = "multinic.fms.io/v1"
26
+ API_VERSION = "multinic.fms.io/v1"
27
+ APISERVER_TIMEOUT = 2 * time .Minute
27
28
)
28
29
29
30
type DynamicHandler struct {
@@ -71,7 +72,9 @@ func (h *DynamicHandler) Create(mapObj map[string]interface{}, namespace string,
71
72
gvr , _ := schema .ParseResourceArg (h .ResourceName )
72
73
log .Println (fmt .Sprintf ("Create %s/%s" , h .ResourceName , mapObj ["metadata" ].(map [string ]interface {})["name" ]))
73
74
start := time .Now ()
74
- res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Create (context .TODO (), obj , options )
75
+ ctx , cancel := context .WithTimeout (context .Background (), APISERVER_TIMEOUT )
76
+ defer cancel ()
77
+ res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Create (ctx , obj , options )
75
78
elapsed := time .Since (start )
76
79
log .Println (fmt .Sprintf ("Create%s elapsed: %d us" , h .Kind , int64 (elapsed / time .Microsecond )))
77
80
return res , err
@@ -82,7 +85,9 @@ func (h *DynamicHandler) Update(mapObj map[string]interface{}, namespace string,
82
85
gvr , _ := schema .ParseResourceArg (h .ResourceName )
83
86
log .Println (fmt .Sprintf ("Update %s/%s" , h .ResourceName , mapObj ["metadata" ].(map [string ]interface {})["name" ]))
84
87
start := time .Now ()
85
- res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Update (context .TODO (), obj , options )
88
+ ctx , cancel := context .WithTimeout (context .Background (), APISERVER_TIMEOUT )
89
+ defer cancel ()
90
+ res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Update (ctx , obj , options )
86
91
elapsed := time .Since (start )
87
92
log .Println (fmt .Sprintf ("Update%s elapsed: %d us" , h .Kind , int64 (elapsed / time .Microsecond )))
88
93
return res , err
@@ -91,7 +96,7 @@ func (h *DynamicHandler) Update(mapObj map[string]interface{}, namespace string,
91
96
func (h * DynamicHandler ) List (namespace string , options metav1.ListOptions ) (* unstructured.UnstructuredList , error ) {
92
97
gvr , _ := schema .ParseResourceArg (h .ResourceName )
93
98
start := time .Now ()
94
- ctx , cancel := context .WithTimeout (context .Background (), 2 * time . Minute )
99
+ ctx , cancel := context .WithTimeout (context .Background (), APISERVER_TIMEOUT )
95
100
defer cancel ()
96
101
res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).List (ctx , options )
97
102
elapsed := time .Since (start )
@@ -102,7 +107,9 @@ func (h *DynamicHandler) List(namespace string, options metav1.ListOptions) (*un
102
107
func (h * DynamicHandler ) Get (name string , namespace string , options metav1.GetOptions ) (* unstructured.Unstructured , error ) {
103
108
gvr , _ := schema .ParseResourceArg (h .ResourceName )
104
109
start := time .Now ()
105
- res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Get (context .TODO (), name , options )
110
+ ctx , cancel := context .WithTimeout (context .Background (), APISERVER_TIMEOUT )
111
+ defer cancel ()
112
+ res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Get (ctx , name , options )
106
113
elapsed := time .Since (start )
107
114
log .Println (fmt .Sprintf ("Get%s elapsed: %d us" , h .Kind , int64 (elapsed / time .Microsecond )))
108
115
return res , err
@@ -112,7 +119,9 @@ func (h *DynamicHandler) Delete(name string, namespace string, options metav1.De
112
119
gvr , _ := schema .ParseResourceArg (h .ResourceName )
113
120
log .Println (fmt .Sprintf ("Delete %s/%s" , h .ResourceName , name ))
114
121
start := time .Now ()
115
- err := h .DYN .Resource (* gvr ).Namespace (namespace ).Delete (context .TODO (), name , options )
122
+ ctx , cancel := context .WithTimeout (context .Background (), APISERVER_TIMEOUT )
123
+ defer cancel ()
124
+ err := h .DYN .Resource (* gvr ).Namespace (namespace ).Delete (ctx , name , options )
116
125
elapsed := time .Since (start )
117
126
log .Println (fmt .Sprintf ("Delete%s elapsed: %d us" , h .Kind , int64 (elapsed / time .Microsecond )))
118
127
return err
@@ -122,7 +131,9 @@ func (h *DynamicHandler) Patch(name string, namespace string, pt types.PatchType
122
131
gvr , _ := schema .ParseResourceArg (h .ResourceName )
123
132
log .Println (fmt .Sprintf ("Patch %s/%s - %s" , h .ResourceName , name , string (data )))
124
133
start := time .Now ()
125
- res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Patch (context .TODO (), name , pt , data , options )
134
+ ctx , cancel := context .WithTimeout (context .Background (), APISERVER_TIMEOUT )
135
+ defer cancel ()
136
+ res , err := h .DYN .Resource (* gvr ).Namespace (namespace ).Patch (ctx , name , pt , data , options )
126
137
elapsed := time .Since (start )
127
138
log .Println (fmt .Sprintf ("Patch%s elapsed: %d us" , h .Kind , int64 (elapsed / time .Microsecond )))
128
139
return res , err
0 commit comments