-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathroutes.go
More file actions
49 lines (42 loc) · 1.59 KB
/
routes.go
File metadata and controls
49 lines (42 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gateway
import (
"context"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)
// Returns true if the parentRef is a reference to a Gateway. If the parentRef does not specify a Group or Kind, it is assumed to be a reference to a Gateway.
func parentRefIsGateway(parentRef gatewayv1.ParentReference) bool {
group := ptr.Deref(parentRef.Group, gatewayv1.GroupName)
kind := ptr.Deref(parentRef.Kind, gatewayv1.Kind("Gateway"))
return group == gatewayv1.GroupName && kind == "Gateway"
}
// routeReferencesNgrokGateway returns true if at least one parentRef targets a
// Gateway whose GatewayClass is managed by this controller.
func routeReferencesNgrokGateway(ctx context.Context, c client.Client, namespace string, parentRefs []gatewayv1.ParentReference) (bool, error) {
for _, parentRef := range parentRefs {
if !parentRefIsGateway(parentRef) {
continue
}
ns := string(ptr.Deref(parentRef.Namespace, gatewayv1.Namespace(namespace)))
gw := &gatewayv1.Gateway{}
if err := c.Get(ctx, types.NamespacedName{Name: string(parentRef.Name), Namespace: ns}, gw); err != nil {
if client.IgnoreNotFound(err) != nil {
return false, err
}
continue // gateway not found, cannot confirm ownership
}
gwc := &gatewayv1.GatewayClass{}
if err := c.Get(ctx, client.ObjectKey{Name: string(gw.Spec.GatewayClassName)}, gwc); err != nil {
if client.IgnoreNotFound(err) != nil {
return false, err
}
continue
}
if ShouldHandleGatewayClass(gwc) {
return true, nil
}
}
return false, nil
}