Skip to content

Commit 4e92f25

Browse files
fixup! Add resourcediscovery package to help discover and model Gateway API resource relationships
1 parent a5bc3fd commit 4e92f25

File tree

2 files changed

+31
-50
lines changed

2 files changed

+31
-50
lines changed

gwctl/pkg/resourcediscovery/discoverer.go

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package resourcediscovery
1818

1919
import (
2020
"context"
21-
"fmt"
2221

2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -28,21 +27,17 @@ import (
2827

2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
31-
"k8s.io/apimachinery/pkg/fields"
3230
"k8s.io/apimachinery/pkg/labels"
3331
"k8s.io/apimachinery/pkg/runtime/schema"
3432
apimachinerytypes "k8s.io/apimachinery/pkg/types"
35-
"k8s.io/client-go/discovery"
3633
"k8s.io/klog/v2"
37-
"k8s.io/utils/strings/slices"
3834
)
3935

4036
// Filter struct defines parameters for filtering resources
4137
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
4641
}
4742

4843
// Discoverer orchestrates the discovery of resources and their associated
@@ -228,7 +223,7 @@ func (d Discoverer) discoverHTTPRoutesFromBackends(ctx context.Context, resource
228223
}
229224
}
230225

231-
// discoverNamespaces discovers Namespaces for resources that exist in the
226+
// discoverNamespaces adds Namespaces for resources that exist in the
232227
// resourceModel.
233228
func (d Discoverer) discoverNamespaces(ctx context.Context, resourceModel *ResourceModel) {
234229
for gatewayID, gatewayNode := range resourceModel.Gateways {
@@ -245,8 +240,7 @@ func (d Discoverer) discoverNamespaces(ctx context.Context, resourceModel *Resou
245240
}
246241
}
247242

248-
// discoverPolicies discovers Policies for resources that exist in the
249-
// resourceModel.
243+
// discoverPolicies adds Policies for resources that exist in the resourceModel.
250244
func (d Discoverer) discoverPolicies(ctx context.Context, resourceModel *ResourceModel) {
251245
resourceModel.addPolicyIfTargetExists(d.PolicyManager.GetPolicies()...)
252246
}
@@ -330,56 +324,35 @@ func fetchHTTPRoutes(ctx context.Context, k8sClients *common.K8sClients, filter
330324
}
331325

332326
// 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.
333330
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-
}
338331
gvr := schema.GroupVersionResource{
339-
Group: apiResource.Group,
340-
Version: apiResource.Version,
341-
Resource: apiResource.Name,
332+
Group: "",
333+
Version: "v1",
334+
Resource: "services",
342335
}
343336

344-
listOptions := metav1.ListOptions{}
345337
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
347345
}
348346

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(),
354350
}
351+
var backendsList *unstructured.UnstructuredList
352+
backendsList, err := k8sClients.DC.Resource(gvr).Namespace(filter.Namespace).List(ctx, listOptions)
355353
if err != nil {
356354
return nil, err
357355
}
358356

359357
return backendsList.Items, nil
360358
}
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-
}

gwctl/pkg/resourcediscovery/resourcemodel.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package resourcediscovery
1818

1919
import (
20+
"fmt"
21+
"sort"
22+
2023
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2124
"sigs.k8s.io/gateway-api/gwctl/pkg/policymanager"
2225

@@ -455,6 +458,11 @@ func convertPoliciesMapToSlice(policies map[policyID]*PolicyNode) []policymanage
455458
for _, policyNode := range policies {
456459
result = append(result, *policyNode.Policy)
457460
}
461+
sort.Slice(result, func(i, j int) bool {
462+
a := fmt.Sprintf("%v/%v/%v", result[i].PolicyCrdID(), result[i].Unstructured().GetNamespace(), result[i].Unstructured().GetName())
463+
b := fmt.Sprintf("%v/%v/%v", result[j].PolicyCrdID(), result[j].Unstructured().GetNamespace(), result[j].Unstructured().GetName())
464+
return a < b
465+
})
458466
return result
459467
}
460468

0 commit comments

Comments
 (0)