1
16
17 package reconcilers
18
19 import (
20 "fmt"
21
22 corev1 "k8s.io/api/core/v1"
23 discoveryv1 "k8s.io/api/discovery/v1"
24 apiequality "k8s.io/apimachinery/pkg/api/equality"
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/runtime"
27 utilerrors "k8s.io/apimachinery/pkg/util/errors"
28 "k8s.io/client-go/kubernetes/fake"
29 k8stesting "k8s.io/client-go/testing"
30 )
31
32 func makeEndpointsArray(name string, ips []string, ports []corev1.EndpointPort) []runtime.Object {
33 return []runtime.Object{
34 makeEndpoints(name, ips, ports),
35 makeEndpointSlice(name, ips, ports),
36 }
37 }
38
39 func makeEndpoints(name string, ips []string, ports []corev1.EndpointPort) *corev1.Endpoints {
40 endpoints := &corev1.Endpoints{
41 ObjectMeta: metav1.ObjectMeta{
42 Namespace: metav1.NamespaceDefault,
43 Name: name,
44 Labels: map[string]string{
45 discoveryv1.LabelSkipMirror: "true",
46 },
47 },
48 }
49 if len(ips) > 0 || len(ports) > 0 {
50 endpoints.Subsets = []corev1.EndpointSubset{{
51 Addresses: make([]corev1.EndpointAddress, len(ips)),
52 Ports: ports,
53 }}
54 for i := range ips {
55 endpoints.Subsets[0].Addresses[i].IP = ips[i]
56 }
57 }
58 return endpoints
59 }
60
61 func makeEndpointSlice(name string, ips []string, ports []corev1.EndpointPort) *discoveryv1.EndpointSlice {
62 slice := &discoveryv1.EndpointSlice{
63 ObjectMeta: metav1.ObjectMeta{
64 Namespace: metav1.NamespaceDefault,
65 Name: name,
66 Labels: map[string]string{
67 discoveryv1.LabelServiceName: name,
68 },
69 },
70 AddressType: discoveryv1.AddressTypeIPv4,
71 Endpoints: make([]discoveryv1.Endpoint, len(ips)),
72 Ports: make([]discoveryv1.EndpointPort, len(ports)),
73 }
74 ready := true
75 for i := range ips {
76 slice.Endpoints[i].Addresses = []string{ips[i]}
77 slice.Endpoints[i].Conditions.Ready = &ready
78 }
79 for i := range ports {
80 slice.Ports[i].Name = &ports[i].Name
81 slice.Ports[i].Protocol = &ports[i].Protocol
82 slice.Ports[i].Port = &ports[i].Port
83 }
84 return slice
85 }
86
87 func verifyCreatesAndUpdates(fakeClient *fake.Clientset, expectedCreates, expectedUpdates []runtime.Object) error {
88 errors := []error{}
89
90 updates := []k8stesting.UpdateAction{}
91 creates := []k8stesting.CreateAction{}
92 for _, action := range fakeClient.Actions() {
93 if action.GetVerb() == "update" {
94 updates = append(updates, action.(k8stesting.UpdateAction))
95 } else if action.GetVerb() == "create" {
96 creates = append(creates, action.(k8stesting.CreateAction))
97 }
98 }
99
100 if len(creates) != len(expectedCreates) {
101 errors = append(errors, fmt.Errorf("expected %d creates got %d", len(expectedCreates), len(creates)))
102 }
103 for i := 0; i < len(creates) || i < len(expectedCreates); i++ {
104 var expected, actual runtime.Object
105 if i < len(creates) {
106 actual = creates[i].GetObject()
107 }
108 if i < len(expectedCreates) {
109 expected = expectedCreates[i]
110 }
111 if !apiequality.Semantic.DeepEqual(expected, actual) {
112 errors = append(errors, fmt.Errorf("expected create %d to be:\n%#v\ngot:\n%#v\n", i, expected, actual))
113 }
114 }
115
116 if len(updates) != len(expectedUpdates) {
117 errors = append(errors, fmt.Errorf("expected %d updates got %d", len(expectedUpdates), len(updates)))
118 }
119 for i := 0; i < len(updates) || i < len(expectedUpdates); i++ {
120 var expected, actual runtime.Object
121 if i < len(updates) {
122 actual = updates[i].GetObject()
123 }
124 if i < len(expectedUpdates) {
125 expected = expectedUpdates[i]
126 }
127 if !apiequality.Semantic.DeepEqual(expected, actual) {
128 errors = append(errors, fmt.Errorf("expected update %d to be:\n%#v\ngot:\n%#v\n", i, expected, actual))
129 }
130 }
131
132 return utilerrors.NewAggregate(errors)
133 }
134
View as plain text