@@ -18,7 +18,6 @@ package resourcediscovery
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
23
22
"sigs.k8s.io/controller-runtime/pkg/client"
24
23
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -28,21 +27,17 @@ import (
28
27
29
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30
29
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
31
- "k8s.io/apimachinery/pkg/fields"
32
30
"k8s.io/apimachinery/pkg/labels"
33
31
"k8s.io/apimachinery/pkg/runtime/schema"
34
32
apimachinerytypes "k8s.io/apimachinery/pkg/types"
35
- "k8s.io/client-go/discovery"
36
33
"k8s.io/klog/v2"
37
- "k8s.io/utils/strings/slices"
38
34
)
39
35
40
36
// Filter struct defines parameters for filtering resources
41
37
type Filter struct {
42
- Namespace string
43
- Name string
44
- Labels map [string ]string
45
- ResourceType string
38
+ Namespace string
39
+ Name string
40
+ Labels map [string ]string
46
41
}
47
42
48
43
// Discoverer orchestrates the discovery of resources and their associated
@@ -228,7 +223,7 @@ func (d Discoverer) discoverHTTPRoutesFromBackends(ctx context.Context, resource
228
223
}
229
224
}
230
225
231
- // discoverNamespaces discovers Namespaces for resources that exist in the
226
+ // discoverNamespaces adds Namespaces for resources that exist in the
232
227
// resourceModel.
233
228
func (d Discoverer ) discoverNamespaces (ctx context.Context , resourceModel * ResourceModel ) {
234
229
for gatewayID , gatewayNode := range resourceModel .Gateways {
@@ -245,8 +240,7 @@ func (d Discoverer) discoverNamespaces(ctx context.Context, resourceModel *Resou
245
240
}
246
241
}
247
242
248
- // discoverPolicies discovers Policies for resources that exist in the
249
- // resourceModel.
243
+ // discoverPolicies adds Policies for resources that exist in the resourceModel.
250
244
func (d Discoverer ) discoverPolicies (ctx context.Context , resourceModel * ResourceModel ) {
251
245
resourceModel .addPolicyIfTargetExists (d .PolicyManager .GetPolicies ()... )
252
246
}
@@ -330,56 +324,35 @@ func fetchHTTPRoutes(ctx context.Context, k8sClients *common.K8sClients, filter
330
324
}
331
325
332
326
// fetchBackends fetches Backends based on a filter.
327
+ //
328
+ // At the moment, this is exclusively used for Backends of type Service, though
329
+ // it still returns a slice of unstructured.Unstructured for future extensions.
333
330
func fetchBackends (ctx context.Context , k8sClients * common.K8sClients , filter Filter ) ([]unstructured.Unstructured , error ) {
334
- apiResource , err := apiResourceFromResourceType (filter .ResourceType , k8sClients .DiscoveryClient )
335
- if err != nil {
336
- return nil , err
337
- }
338
331
gvr := schema.GroupVersionResource {
339
- Group : apiResource . Group ,
340
- Version : apiResource . Version ,
341
- Resource : apiResource . Name ,
332
+ Group : "" ,
333
+ Version : "v1" ,
334
+ Resource : "services" ,
342
335
}
343
336
344
- listOptions := metav1.ListOptions {}
345
337
if filter .Name != "" {
346
- listOptions .FieldSelector = fields .OneTermEqualSelector ("metadata.name" , filter .Name ).String ()
338
+ // Use Get call.
339
+ backend , err := k8sClients .DC .Resource (gvr ).Namespace (filter .Namespace ).Get (ctx , filter .Name , metav1.GetOptions {})
340
+ if err != nil {
341
+ return []unstructured.Unstructured {}, err
342
+ }
343
+
344
+ return []unstructured.Unstructured {* backend }, nil
347
345
}
348
346
349
- var backendsList * unstructured.UnstructuredList
350
- if apiResource .Namespaced {
351
- backendsList , err = k8sClients .DC .Resource (gvr ).Namespace (filter .Namespace ).List (ctx , listOptions )
352
- } else {
353
- backendsList , err = k8sClients .DC .Resource (gvr ).List (ctx , listOptions )
347
+ // Use List call.
348
+ listOptions := metav1.ListOptions {
349
+ LabelSelector : labels .SelectorFromSet (filter .Labels ).String (),
354
350
}
351
+ var backendsList * unstructured.UnstructuredList
352
+ backendsList , err := k8sClients .DC .Resource (gvr ).Namespace (filter .Namespace ).List (ctx , listOptions )
355
353
if err != nil {
356
354
return nil , err
357
355
}
358
356
359
357
return backendsList .Items , nil
360
358
}
361
-
362
- func apiResourceFromResourceType (resourceType string , discoveryClient discovery.DiscoveryInterface ) (metav1.APIResource , error ) {
363
- resourceGroups , err := discoveryClient .ServerPreferredResources ()
364
- if err != nil {
365
- return metav1.APIResource {}, err
366
- }
367
- for _ , resourceGroup := range resourceGroups {
368
- gv , err := schema .ParseGroupVersion (resourceGroup .GroupVersion )
369
- if err != nil {
370
- return metav1.APIResource {}, err
371
- }
372
- for _ , resource := range resourceGroup .APIResources {
373
- var choices []string
374
- choices = append (choices , resource .Kind )
375
- choices = append (choices , resource .Name )
376
- choices = append (choices , resource .ShortNames ... )
377
- choices = append (choices , resource .SingularName )
378
- if slices .Contains (choices , resourceType ) {
379
- resource .Version = gv .Version
380
- return resource , nil
381
- }
382
- }
383
- }
384
- return metav1.APIResource {}, fmt .Errorf ("GVR for %v not found in discovery" , resourceType )
385
- }
0 commit comments