-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwroute_types.go
107 lines (90 loc) · 3.07 KB
/
gwroute_types.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package v1
import (
"crypto/rand"
"encoding/hex"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)
func init() {
SchemeBuilder.Register(&GWRoute{}, &GWRouteList{})
}
// GWRouteSpec is our wrapper for the various types of Gateway
// RouteSpecs.
type GWRouteSpec struct {
// ClientRef points back to the client-side object that corresponds
// to this one.
ClientRef ClientRef `json:"clientRef,omitempty"`
HTTP *gatewayv1a2.HTTPRouteSpec `json:"http,omitempty"`
TCP *gatewayv1a2.TCPRouteSpec `json:"tcp,omitempty"`
}
// GWRouteStatus defines the observed state of GWRoute
type GWRouteStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}
// +kubebuilder:object:root=true
// +kubebuilder:resource:shortName=gwr;gwrs
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:JSONPath=".spec.clientRef.clusterID",name=Client Cluster,type=string
// +kubebuilder:printcolumn:JSONPath=".spec.clientRef.namespace",name=Client NS,type=string
// +kubebuilder:printcolumn:JSONPath=".spec.clientRef.name",name=Client Name,type=string
// GWRoute is the Schema for the gwroutes API
type GWRoute struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec GWRouteSpec `json:"spec,omitempty"`
Status GWRouteStatus `json:"status,omitempty"`
}
// GWRouteName makes a name for this route that will be unique within
// this customer's namespace. It should also be somewhat
// human-readable which will hopefully help with debugging.
func (gwr *GWRoute) GWRouteName() string {
raw := make([]byte, 8, 8)
_, _ = rand.Read(raw)
return fmt.Sprintf("%s-%s", gwr.Name, hex.EncodeToString(raw))
}
//+kubebuilder:object:root=true
// GWRouteList contains a list of GWRoute
type GWRouteList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []GWRoute `json:"items"`
}
// Parents returns a slice containing this GWR's ParentReferences.
func (gwr *GWRoute) Parents() (parents []gatewayv1a2.ParentReference) {
if gwr.Spec.HTTP != nil {
parents = append(parents, gwr.Spec.HTTP.ParentRefs...)
}
if gwr.Spec.TCP != nil {
parents = append(parents, gwr.Spec.TCP.ParentRefs...)
}
return
}
// Backends returns a slice containing this GWR's BackendReferences.
func (gwr *GWRoute) Backends() (backends []gatewayv1a2.BackendRef) {
if gwr.Spec.HTTP != nil {
for _, rule := range gwr.Spec.HTTP.Rules {
for _, backend := range rule.BackendRefs {
backends = append(backends, backend.BackendRef)
}
}
}
if gwr.Spec.TCP != nil {
for _, rule := range gwr.Spec.TCP.Rules {
backends = append(backends, rule.BackendRefs...)
}
}
return
}
// isChildOf indicates whether this GWRoute has a ParentRef that
// references gateway.
func (gwr *GWRoute) isChildOf(gateway types.NamespacedName) bool {
for _, ref := range gwr.Parents() {
if string(ref.Name) == gateway.Name {
return true
}
}
return false
}