@@ -73,29 +73,33 @@ func NewAPIServerHandler(manager cmanager.Manager, log logr.Logger, resolver Res
73
73
74
74
// CRUD endpoints (global objects)
75
75
ws .Route (ws .POST ("/api/v1/{resource}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Create ))
76
- ws .Route (ws .GET ("/api/v1/{resource}" ).To (apiServer .apiV1List ))
76
+ ws .Route (ws .GET ("/api/v1/{resource}" ).If (isList ).To (apiServer .apiV1List ))
77
+ ws .Route (ws .GET ("/api/v1/{resource}" ).If (isWatch ).To (apiServer .apiV1Watch ))
77
78
ws .Route (ws .GET ("/api/v1/{resource}/{name}" ).To (apiServer .apiV1Get ))
78
79
ws .Route (ws .PUT ("/api/v1/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Update ))
79
80
ws .Route (ws .PATCH ("/api/v1/{resource}/{name}" ).Consumes (string (types .MergePatchType ), string (types .StrategicMergePatchType )).To (apiServer .apiV1Patch ))
80
81
ws .Route (ws .DELETE ("/api/v1/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf , runtime .ContentTypeJSON ).To (apiServer .apiV1Delete ))
81
82
82
83
ws .Route (ws .POST ("/apis/{group}/{version}/{resource}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Create ))
83
- ws .Route (ws .GET ("/apis/{group}/{version}/{resource}" ).To (apiServer .apiV1List ))
84
+ ws .Route (ws .GET ("/apis/{group}/{version}/{resource}" ).If (isList ).To (apiServer .apiV1List ))
85
+ ws .Route (ws .GET ("/apis/{group}/{version}/{resource}" ).If (isWatch ).To (apiServer .apiV1Watch ))
84
86
ws .Route (ws .GET ("/apis/{group}/{version}/{resource}/{name}" ).To (apiServer .apiV1Get ))
85
87
ws .Route (ws .PUT ("/apis/{group}/{version}/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Update ))
86
88
ws .Route (ws .PATCH ("/apis/{group}/{version}/{resource}/{name}" ).Consumes (string (types .MergePatchType ), string (types .StrategicMergePatchType )).To (apiServer .apiV1Patch ))
87
89
ws .Route (ws .DELETE ("/apis/{group}/{version}/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf , runtime .ContentTypeJSON ).To (apiServer .apiV1Delete ))
88
90
89
91
// CRUD endpoints (namespaced objects)
90
92
ws .Route (ws .POST ("/api/v1/namespaces/{namespace}/{resource}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Create ))
91
- ws .Route (ws .GET ("/api/v1/namespaces/{namespace}/{resource}" ).To (apiServer .apiV1List ))
93
+ ws .Route (ws .GET ("/api/v1/namespaces/{namespace}/{resource}" ).If (isList ).To (apiServer .apiV1List ))
94
+ ws .Route (ws .GET ("/api/v1/namespaces/{namespace}/{resource}" ).If (isWatch ).To (apiServer .apiV1Watch ))
92
95
ws .Route (ws .GET ("/api/v1/namespaces/{namespace}/{resource}/{name}" ).To (apiServer .apiV1Get ))
93
96
ws .Route (ws .PUT ("/api/v1/namespaces/{namespace}/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Update ))
94
97
ws .Route (ws .PATCH ("/api/v1/namespaces/{namespace}/{resource}/{name}" ).Consumes (string (types .MergePatchType ), string (types .StrategicMergePatchType )).To (apiServer .apiV1Patch ))
95
98
ws .Route (ws .DELETE ("/api/v1/namespaces/{namespace}/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf , runtime .ContentTypeJSON ).To (apiServer .apiV1Delete ))
96
99
97
100
ws .Route (ws .POST ("/apis/{group}/{version}/namespaces/{namespace}/{resource}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Create ))
98
- ws .Route (ws .GET ("/apis/{group}/{version}/namespaces/{namespace}/{resource}" ).To (apiServer .apiV1List ))
101
+ ws .Route (ws .GET ("/apis/{group}/{version}/namespaces/{namespace}/{resource}" ).If (isList ).To (apiServer .apiV1List ))
102
+ ws .Route (ws .GET ("/apis/{group}/{version}/namespaces/{namespace}/{resource}" ).If (isWatch ).To (apiServer .apiV1Watch ))
99
103
ws .Route (ws .GET ("/apis/{group}/{version}/namespaces/{namespace}/{resource}/{name}" ).To (apiServer .apiV1Get ))
100
104
ws .Route (ws .PUT ("/apis/{group}/{version}/namespaces/{namespace}/{resource}/{name}" ).Consumes (runtime .ContentTypeProtobuf ).To (apiServer .apiV1Update ))
101
105
ws .Route (ws .PATCH ("/apis/{group}/{version}/namespaces/{namespace}/{resource}/{name}" ).Consumes (string (types .MergePatchType ), string (types .StrategicMergePatchType )).To (apiServer .apiV1Patch ))
@@ -230,16 +234,6 @@ func (h *apiServerHandler) apiV1List(req *restful.Request, resp *restful.Respons
230
234
return
231
235
}
232
236
233
- // If the request is a Watch handle it using watchForResource.
234
- if isWatch (req ) {
235
- err = h .watchForResource (req , resp , resourceGroup , * gvk )
236
- if err != nil {
237
- _ = resp .WriteErrorString (http .StatusInternalServerError , err .Error ())
238
- return
239
- }
240
- return
241
- }
242
-
243
237
// Reads and returns the requested data.
244
238
list := & unstructured.UnstructuredList {}
245
239
list .SetAPIVersion (gvk .GroupVersion ().String ())
@@ -260,6 +254,29 @@ func (h *apiServerHandler) apiV1List(req *restful.Request, resp *restful.Respons
260
254
}
261
255
}
262
256
257
+ func (h * apiServerHandler ) apiV1Watch (req * restful.Request , resp * restful.Response ) {
258
+ // Gets the resource group the request targets (the resolver is aware of the mapping host<->resourceGroup)
259
+ resourceGroup , err := h .resourceGroupResolver (req .Request .Host )
260
+ if err != nil {
261
+ _ = resp .WriteErrorString (http .StatusInternalServerError , err .Error ())
262
+ return
263
+ }
264
+
265
+ // Maps the requested resource to a gvk.
266
+ gvk , err := requestToGVK (req )
267
+ if err != nil {
268
+ _ = resp .WriteErrorString (http .StatusInternalServerError , err .Error ())
269
+ return
270
+ }
271
+
272
+ // If the request is a Watch handle it using watchForResource.
273
+ err = h .watchForResource (req , resp , resourceGroup , * gvk )
274
+ if err != nil {
275
+ _ = resp .WriteErrorString (http .StatusInternalServerError , err .Error ())
276
+ return
277
+ }
278
+ }
279
+
263
280
func (h * apiServerHandler ) apiV1Get (req * restful.Request , resp * restful.Response ) {
264
281
ctx := req .Request .Context ()
265
282
@@ -539,3 +556,13 @@ func getAPIResourceList(req *restful.Request) *metav1.APIResourceList {
539
556
}
540
557
return corev1APIResourceList
541
558
}
559
+
560
+ // isWatch is true if the request contains `watch="true"` as a query parameter.
561
+ func isWatch (req * http.Request ) bool {
562
+ return req .URL .Query ().Get ("watch" ) == "true"
563
+ }
564
+
565
+ // isList is true if the request does not have `watch="true` as a query parameter.
566
+ func isList (req * http.Request ) bool {
567
+ return ! isWatch (req )
568
+ }
0 commit comments