-
Notifications
You must be signed in to change notification settings - Fork 294
/
helpers_v1alpha2.go
194 lines (147 loc) · 4.73 KB
/
helpers_v1alpha2.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
// This file contains code derived from Contour,
// https://github.com/projectcontour/contour
// and is provided here subject to the following:
// Copyright Project Contour Authors
// SPDX-License-Identifier: Apache-2.0
package gatewayapi
import (
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)
// TODO: [gwapiv1a2-gwapiv1]
// This file can be removed once TLSRoute graduates to gwapiv1.
func GroupPtrV1Alpha2(group string) *gwapiv1a2.Group {
gwGroup := gwapiv1a2.Group(group)
return &gwGroup
}
func KindPtrV1Alpha2(kind string) *gwapiv1a2.Kind {
gwKind := gwapiv1a2.Kind(kind)
return &gwKind
}
func NamespacePtrV1Alpha2(namespace string) *gwapiv1a2.Namespace {
gwNamespace := gwapiv1a2.Namespace(namespace)
return &gwNamespace
}
func SectionNamePtrV1Alpha2(sectionName string) *gwapiv1a2.SectionName {
gwSectionName := gwapiv1a2.SectionName(sectionName)
return &gwSectionName
}
func PortNumPtrV1Alpha2(port int) *gwapiv1a2.PortNumber {
pn := gwapiv1a2.PortNumber(port)
return &pn
}
func UpgradeParentReferences(old []gwapiv1a2.ParentReference) []gwapiv1.ParentReference {
newParentReferences := make([]gwapiv1.ParentReference, len(old))
for i, o := range old {
newParentReferences[i] = UpgradeParentReference(o)
}
return newParentReferences
}
// UpgradeParentReference converts gwapiv1a2.ParentReference to gwapiv1.ParentReference
func UpgradeParentReference(old gwapiv1a2.ParentReference) gwapiv1.ParentReference {
upgraded := gwapiv1.ParentReference{}
if old.Group != nil {
upgraded.Group = GroupPtr(string(*old.Group))
}
if old.Kind != nil {
upgraded.Kind = KindPtr(string(*old.Kind))
}
if old.Namespace != nil {
upgraded.Namespace = NamespacePtr(string(*old.Namespace))
}
upgraded.Name = old.Name
if old.SectionName != nil {
upgraded.SectionName = SectionNamePtr(string(*old.SectionName))
}
if old.Port != nil {
upgraded.Port = PortNumPtr(int32(*old.Port))
}
return upgraded
}
func DowngradeParentReference(old gwapiv1.ParentReference) gwapiv1a2.ParentReference {
downgraded := gwapiv1a2.ParentReference{}
if old.Group != nil {
downgraded.Group = GroupPtrV1Alpha2(string(*old.Group))
}
if old.Kind != nil {
downgraded.Kind = KindPtrV1Alpha2(string(*old.Kind))
}
if old.Namespace != nil {
downgraded.Namespace = NamespacePtrV1Alpha2(string(*old.Namespace))
}
downgraded.Name = old.Name
if old.SectionName != nil {
downgraded.SectionName = SectionNamePtrV1Alpha2(string(*old.SectionName))
}
if old.Port != nil {
downgraded.Port = PortNumPtrV1Alpha2(int(*old.Port))
}
return downgraded
}
func UpgradeRouteParentStatuses(routeParentStatuses []gwapiv1a2.RouteParentStatus) []gwapiv1.RouteParentStatus {
var res []gwapiv1.RouteParentStatus
for _, rps := range routeParentStatuses {
res = append(res, gwapiv1.RouteParentStatus{
ParentRef: UpgradeParentReference(rps.ParentRef),
ControllerName: rps.ControllerName,
Conditions: rps.Conditions,
})
}
return res
}
func DowngradeRouteParentStatuses(routeParentStatuses []gwapiv1.RouteParentStatus) []gwapiv1a2.RouteParentStatus {
var res []gwapiv1a2.RouteParentStatus
for _, rps := range routeParentStatuses {
res = append(res, gwapiv1a2.RouteParentStatus{
ParentRef: DowngradeParentReference(rps.ParentRef),
ControllerName: rps.ControllerName,
Conditions: rps.Conditions,
})
}
return res
}
// UpgradeBackendRef converts gwapiv1a2.BackendRef to gwapiv1.BackendRef
func UpgradeBackendRef(old gwapiv1a2.BackendRef) gwapiv1.BackendRef {
upgraded := gwapiv1.BackendRef{}
if old.Group != nil {
upgraded.Group = GroupPtr(string(*old.Group))
}
if old.Kind != nil {
upgraded.Kind = KindPtr(string(*old.Kind))
}
if old.Namespace != nil {
upgraded.Namespace = NamespacePtr(string(*old.Namespace))
}
upgraded.Name = old.Name
if old.Port != nil {
upgraded.Port = PortNumPtr(int32(*old.Port))
}
return upgraded
}
func DowngradeBackendRef(old gwapiv1.BackendRef) gwapiv1a2.BackendRef {
downgraded := gwapiv1a2.BackendRef{}
if old.Group != nil {
downgraded.Group = GroupPtrV1Alpha2(string(*old.Group))
}
if old.Kind != nil {
downgraded.Kind = KindPtrV1Alpha2(string(*old.Kind))
}
if old.Namespace != nil {
downgraded.Namespace = NamespacePtrV1Alpha2(string(*old.Namespace))
}
downgraded.Name = old.Name
if old.Port != nil {
downgraded.Port = PortNumPtrV1Alpha2(int(*old.Port))
}
return downgraded
}
func NamespaceDerefOrAlpha(namespace *gwapiv1a2.Namespace, defaultNamespace string) string {
if namespace != nil && *namespace != "" {
return string(*namespace)
}
return defaultNamespace
}