-
Notifications
You must be signed in to change notification settings - Fork 297
/
helpers_v1alpha2.go
145 lines (112 loc) · 3.77 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
// 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 (
"sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/apis/v1beta1"
)
// TODO: [v1alpha2-v1beta1]
// This file can be removed once TLSRoute graduates to v1beta1.
func GroupPtrV1Alpha2(group string) *v1alpha2.Group {
gwGroup := v1alpha2.Group(group)
return &gwGroup
}
func KindPtrV1Alpha2(kind string) *v1alpha2.Kind {
gwKind := v1alpha2.Kind(kind)
return &gwKind
}
func NamespacePtrV1Alpha2(namespace string) *v1alpha2.Namespace {
gwNamespace := v1alpha2.Namespace(namespace)
return &gwNamespace
}
func SectionNamePtrV1Alpha2(sectionName string) *v1alpha2.SectionName {
gwSectionName := v1alpha2.SectionName(sectionName)
return &gwSectionName
}
func PortNumPtrV1Alpha2(port int) *v1alpha2.PortNumber {
pn := v1alpha2.PortNumber(port)
return &pn
}
func UpgradeParentReferences(old []v1alpha2.ParentReference) []v1beta1.ParentReference {
newParentReferences := make([]v1beta1.ParentReference, len(old))
for i, o := range old {
newParentReferences[i] = UpgradeParentReference(o)
}
return newParentReferences
}
// UpgradeParentReference converts v1alpha2.ParentReference to v1beta1.ParentReference
func UpgradeParentReference(old v1alpha2.ParentReference) v1beta1.ParentReference {
upgraded := v1beta1.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 = v1beta1.ObjectName(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 v1beta1.ParentReference) v1alpha2.ParentReference {
downgraded := v1alpha2.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 = v1alpha2.ObjectName(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 []v1alpha2.RouteParentStatus) []v1beta1.RouteParentStatus {
var res []v1beta1.RouteParentStatus
for _, rps := range routeParentStatuses {
res = append(res, v1beta1.RouteParentStatus{
ParentRef: UpgradeParentReference(rps.ParentRef),
ControllerName: v1beta1.GatewayController(rps.ControllerName),
Conditions: rps.Conditions,
})
}
return res
}
func DowngradeRouteParentStatuses(routeParentStatuses []v1beta1.RouteParentStatus) []v1alpha2.RouteParentStatus {
var res []v1alpha2.RouteParentStatus
for _, rps := range routeParentStatuses {
res = append(res, v1alpha2.RouteParentStatus{
ParentRef: DowngradeParentReference(rps.ParentRef),
ControllerName: v1alpha2.GatewayController(rps.ControllerName),
Conditions: rps.Conditions,
})
}
return res
}
func NamespaceDerefOrAlpha(namespace *v1alpha2.Namespace, defaultNamespace string) string {
if namespace != nil && *namespace != "" {
return string(*namespace)
}
return defaultNamespace
}