/
k8sgateway_data.go
112 lines (97 loc) · 3.99 KB
/
k8sgateway_data.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
package data
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s_networking_v1 "sigs.k8s.io/gateway-api/apis/v1"
k8s_networking_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
"github.com/kiali/kiali/kubernetes"
)
func CreateEmptyHTTPRoute(name string, namespace string, hosts []string) *k8s_networking_v1.HTTPRoute {
vs := k8s_networking_v1.HTTPRoute{}
vs.Name = name
vs.Namespace = namespace
for _, host := range hosts {
vs.Spec.Hostnames = append(vs.Spec.Hostnames, k8s_networking_v1.Hostname(host))
}
return &vs
}
func CreateHTTPRoute(name string, namespace string, gateway string, hosts []string) *k8s_networking_v1.HTTPRoute {
return AddParentRefToHTTPRoute(gateway, namespace, CreateEmptyHTTPRoute(name, namespace, hosts))
}
func AddParentRefToHTTPRoute(name, namespace string, rt *k8s_networking_v1.HTTPRoute) *k8s_networking_v1.HTTPRoute {
ns := k8s_networking_v1.Namespace(namespace)
group := k8s_networking_v1.Group(kubernetes.K8sNetworkingGroupVersionV1.Group)
kind := k8s_networking_v1.Kind(kubernetes.K8sActualGatewayType)
rt.Spec.ParentRefs = append(rt.Spec.ParentRefs, k8s_networking_v1.ParentReference{
Name: k8s_networking_v1.ObjectName(name),
Namespace: &ns,
Group: &group,
Kind: &kind})
return rt
}
func AddBackendRefToHTTPRoute(name, namespace string, rt *k8s_networking_v1.HTTPRoute) *k8s_networking_v1.HTTPRoute {
kind := k8s_networking_v1.Kind("Service")
var ns k8s_networking_v1.Namespace
if namespace != "" {
ns = k8s_networking_v1.Namespace(namespace)
}
backendRef := k8s_networking_v1.HTTPBackendRef{
BackendRef: k8s_networking_v1.BackendRef{
BackendObjectReference: k8s_networking_v1.BackendObjectReference{
Kind: &kind,
Name: k8s_networking_v1.ObjectName(name),
Namespace: &ns,
},
},
}
rule := k8s_networking_v1.HTTPRouteRule{}
rule.BackendRefs = append(rule.BackendRefs, backendRef)
rt.Spec.Rules = append(rt.Spec.Rules, rule)
return rt
}
func CreateEmptyK8sGateway(name, namespace string) *k8s_networking_v1.Gateway {
gw := k8s_networking_v1.Gateway{}
gw.Name = name
gw.Namespace = namespace
gw.Kind = kubernetes.K8sActualGatewayType
gw.APIVersion = kubernetes.K8sApiNetworkingVersionV1
gw.Spec.GatewayClassName = "istio"
return &gw
}
func AddListenerToK8sGateway(listener k8s_networking_v1.Listener, gw *k8s_networking_v1.Gateway) *k8s_networking_v1.Gateway {
gw.Spec.Listeners = append(gw.Spec.Listeners, listener)
return gw
}
func AddGwAddressToK8sGateway(address k8s_networking_v1.GatewayAddress, gw *k8s_networking_v1.Gateway) *k8s_networking_v1.Gateway {
gw.Spec.Addresses = append(gw.Spec.Addresses, address)
return gw
}
func CreateListener(name string, hostname string, port int, protocol string) k8s_networking_v1.Listener {
hn := k8s_networking_v1.Hostname(hostname)
listener := k8s_networking_v1.Listener{
Name: k8s_networking_v1.SectionName(name),
Hostname: &hn,
Port: k8s_networking_v1.PortNumber(port),
Protocol: k8s_networking_v1.ProtocolType(protocol),
}
return listener
}
func CreateGWAddress(addrType k8s_networking_v1.AddressType, value string) k8s_networking_v1.GatewayAddress {
address := k8s_networking_v1.GatewayAddress{
Type: &addrType,
Value: value,
}
return address
}
func UpdateConditionWithError(k8sgw *k8s_networking_v1.Gateway) *k8s_networking_v1.Gateway {
condition := metav1.Condition{Type: "Ready", Status: "False", Reason: "", Message: "Fake msg"}
k8sgw.Status.Conditions = append(k8sgw.Status.Conditions, condition)
return k8sgw
}
func CreateReferenceGrant(name string, namespace string, fromNamespace string) *k8s_networking_v1beta1.ReferenceGrant {
rg := k8s_networking_v1beta1.ReferenceGrant{}
rg.Name = name
rg.Namespace = namespace
rg.Spec.From = append(rg.Spec.From, k8s_networking_v1beta1.ReferenceGrantFrom{Kind: kubernetes.K8sActualHTTPRouteType, Group: k8s_networking_v1beta1.GroupName, Namespace: k8s_networking_v1.Namespace(fromNamespace)})
rg.Spec.To = append(rg.Spec.To, k8s_networking_v1beta1.ReferenceGrantTo{Kind: kubernetes.ServiceType})
return &rg
}