-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
215 lines (204 loc) · 5.61 KB
/
gen.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package mock
import (
"fmt"
metallbbgp "go.universe.tf/metallb/pkg/bgp"
metallbspr "go.universe.tf/metallb/pkg/speaker"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/ipam/types"
"github.com/cilium/cilium/pkg/k8s"
ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/api/core/v1"
slim_metav1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/meta/v1"
nodetypes "github.com/cilium/cilium/pkg/node/types"
)
// GenTestNodeAndAdvertisements generates a v1.Node with its
// ObjectMeta and PodCidr(s) fields hardcoded along with
// the MetalLB Advertisements that would be announced by
// this node.
//
// The returned Node's name is set to nodetypes.GetName()
// to simulate it being the node the Cilium agent is running on.
//
// See definition for details.
func GenTestNodeAndAdvertisements() (v1.Node, []*metallbbgp.Advertisement) {
const (
CIDR = "1.1.0.0/16"
)
meta := metav1.ObjectMeta{
Name: nodetypes.GetName(),
Namespace: "TestNamespace",
Labels: map[string]string{
"TestLabel": "TestLabel",
},
ResourceVersion: "1",
}
spec := v1.NodeSpec{
PodCIDR: CIDR,
PodCIDRs: []string{CIDR},
}
node := v1.Node{
ObjectMeta: meta,
Spec: spec,
}
advertisements := []*metallbbgp.Advertisement{
{
Prefix: cidr.MustParseCIDR(CIDR).IPNet,
},
}
return node, advertisements
}
// GenTestCiliumNodeAndAdvertisements generates a ciliumv2.CiliumNode with
// numPodCIDRs podCIDRs and the corresponding MetalLB Advertisements that would
// be announced by this node.
//
// The returned Node's name is set to nodetypes.GetName()
// to simulate it being the node the Cilium agent is running on.
//
// See definition for details.
func GenTestCiliumNodeAndAdvertisements(numPodCIDRs int) (ciliumv2.CiliumNode, []*metallbbgp.Advertisement) {
podCIDRs := make([]string, numPodCIDRs)
for i := 0; i < numPodCIDRs; i++ {
podCIDRs[i] = fmt.Sprintf("10.%d.0.0/16", i)
}
meta := metav1.ObjectMeta{
Name: nodetypes.GetName(),
Labels: map[string]string{
"TestLabel": "TestLabel",
},
ResourceVersion: "1",
}
spec := ciliumv2.NodeSpec{
IPAM: types.IPAMSpec{
PodCIDRs: podCIDRs,
},
}
node := ciliumv2.CiliumNode{
ObjectMeta: meta,
Spec: spec,
}
advertisements := make([]*metallbbgp.Advertisement, 0, numPodCIDRs)
for _, podCIDR := range podCIDRs {
advertisements = append(advertisements, &metallbbgp.Advertisement{
Prefix: cidr.MustParseCIDR(podCIDR).IPNet,
})
}
return node, advertisements
}
// GenTestServicePairs generates a slim_corev1.Service and a hardcoded conversion
// to a metallbspr.Service and k8s v1.Service, along with a hardcoded conversion to a k8s.ServiceID.
//
// Since the conversion is hardcoded, the returned types are useful for testing
// any code which transforms one data structure to the another.
//
// See definition for details.
func GenTestServicePairs() (slim_corev1.Service, v1.Service, metallbspr.Service, k8s.ServiceID) {
const (
IP = "10.10.10.10"
)
spec := slim_corev1.ServiceSpec{
Type: "TestType",
ExternalTrafficPolicy: "TestExternalTrafficPolicy",
}
meta := slim_metav1.ObjectMeta{
Name: "TestName",
Namespace: "TestNamespace",
ResourceVersion: "1",
}
ingress := []slim_corev1.LoadBalancerIngress{
{
IP: IP,
},
}
lbStatus := slim_corev1.LoadBalancerStatus{
Ingress: ingress,
}
status := slim_corev1.ServiceStatus{
LoadBalancer: lbStatus,
}
service := slim_corev1.Service{
Spec: spec,
Status: status,
ObjectMeta: meta,
}
metallbService := metallbspr.Service{
Type: string(spec.Type),
TrafficPolicy: string(spec.ExternalTrafficPolicy),
Ingress: []v1.LoadBalancerIngress{
{
IP: ingress[0].IP,
},
},
}
v1Service := v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: meta.Name,
Namespace: meta.Namespace,
ResourceVersion: "1",
},
Spec: v1.ServiceSpec{
Type: "TestType",
ExternalTrafficPolicy: "TestExternalTrafficPolicy",
},
Status: v1.ServiceStatus{
LoadBalancer: v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{
{
IP: IP,
Ports: []v1.PortStatus{},
},
},
},
},
}
serviceID := k8s.ServiceID{
Name: service.Name,
Namespace: service.Namespace,
}
return service, v1Service, metallbService, serviceID
}
// GenTestEndpointsPair generates a k8s.Endpoints and a hardcoded conversion
// to a metallbspr.Endpoints, along with a hardoced conversion to slim_corev1.Endpoints.
//
// Since the conversion are hardcoded, the returned types are useful for testing
// any code which transforms one data structure to the other.
func GenTestEndpointsPairs() (k8s.Endpoints, slim_corev1.Endpoints, metallbspr.Endpoints) {
const (
IP = "1.1.1.1"
NodeName = "TestNode"
)
meta := slim_metav1.ObjectMeta{
Name: "TestName",
Namespace: "TestNamespace",
ResourceVersion: "1",
}
backend := k8s.Backend{
NodeName: NodeName,
}
backends := map[string]*k8s.Backend{
IP: &backend,
}
endpoints := k8s.Endpoints{Backends: backends}
slimEndpoints := slim_corev1.Endpoints{
ObjectMeta: meta,
Subsets: []slim_corev1.EndpointSubset{
{
Addresses: []slim_corev1.EndpointAddress{
{IP: IP, NodeName: &(backend.NodeName)},
},
},
},
}
metallbEndpoints := metallbspr.Endpoints{
Ready: []metallbspr.Endpoint{
{
IP: IP,
NodeName: &(backend.NodeName),
},
},
}
return endpoints, slimEndpoints, metallbEndpoints
}