-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathippool.go
More file actions
280 lines (245 loc) · 10 KB
/
ippool.go
File metadata and controls
280 lines (245 loc) · 10 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package framework
import (
"context"
"errors"
"fmt"
"math/big"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/test/e2e/framework"
apiv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
v1 "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned/typed/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/util"
)
// IPPoolClient is a struct for ippool client.
type IPPoolClient struct {
f *Framework
v1.IPPoolInterface
}
func (f *Framework) IPPoolClient() *IPPoolClient {
return &IPPoolClient{
f: f,
IPPoolInterface: f.KubeOVNClientSet.KubeovnV1().IPPools(),
}
}
func (c *IPPoolClient) Get(name string) *apiv1.IPPool {
ginkgo.GinkgoHelper()
ippool, err := c.IPPoolInterface.Get(context.TODO(), name, metav1.GetOptions{})
ExpectNoError(err)
return ippool
}
// Create creates a new ippool according to the framework specifications
func (c *IPPoolClient) Create(ippool *apiv1.IPPool) *apiv1.IPPool {
ginkgo.GinkgoHelper()
s, err := c.IPPoolInterface.Create(context.TODO(), ippool, metav1.CreateOptions{})
ExpectNoError(err, "Error creating ippool")
return s.DeepCopy()
}
// CreateSync creates a new ippool according to the framework specifications, and waits for it to be ready.
func (c *IPPoolClient) CreateSync(ippool *apiv1.IPPool) *apiv1.IPPool {
ginkgo.GinkgoHelper()
s := c.Create(ippool)
ExpectTrue(c.WaitToBeReady(s.Name, timeout))
// Get the newest ippool after it becomes ready
return c.Get(s.Name).DeepCopy()
}
// Update updates the ippool
func (c *IPPoolClient) Update(ippool *apiv1.IPPool, options metav1.UpdateOptions, timeout time.Duration) *apiv1.IPPool {
ginkgo.GinkgoHelper()
var updatedIPPool *apiv1.IPPool
err := wait.PollUntilContextTimeout(context.Background(), poll, timeout, true, func(ctx context.Context) (bool, error) {
s, err := c.IPPoolInterface.Update(ctx, ippool, options)
if err != nil {
// On conflict, refresh the resource and retry
if apierrors.IsConflict(err) {
latest, getErr := c.IPPoolInterface.Get(ctx, ippool.Name, metav1.GetOptions{})
if getErr != nil {
return handleWaitingAPIError(getErr, false, "get ippool %q for conflict retry", ippool.Name)
}
// Copy spec changes to the latest version
latest.Spec = ippool.Spec
ippool = latest
}
return handleWaitingAPIError(err, false, "update ippool %q", ippool.Name)
}
updatedIPPool = s
return true, nil
})
if err == nil {
return updatedIPPool.DeepCopy()
}
if errors.Is(err, context.DeadlineExceeded) {
Failf("timed out while retrying to update ippool %s", ippool.Name)
}
Failf("error occurred while retrying to update ippool %s: %v", ippool.Name, err)
return nil
}
// UpdateSync updates the ippool and waits for the ippool to be ready for `timeout`.
// If the ippool doesn't become ready before the timeout, it will fail the test.
func (c *IPPoolClient) UpdateSync(ippool *apiv1.IPPool, options metav1.UpdateOptions, timeout time.Duration) *apiv1.IPPool {
ginkgo.GinkgoHelper()
s := c.Update(ippool, options, timeout)
ExpectTrue(c.WaitToBeReady(s.Name, timeout))
// Get the newest ippool after it becomes ready
return c.Get(s.Name).DeepCopy()
}
// Patch patches the ippool
func (c *IPPoolClient) Patch(original, modified *apiv1.IPPool, timeout time.Duration) *apiv1.IPPool {
ginkgo.GinkgoHelper()
patch, err := util.GenerateMergePatchPayload(original, modified)
ExpectNoError(err)
var patchedIPPool *apiv1.IPPool
err = wait.PollUntilContextTimeout(context.Background(), poll, timeout, true, func(ctx context.Context) (bool, error) {
s, err := c.IPPoolInterface.Patch(ctx, original.Name, types.MergePatchType, patch, metav1.PatchOptions{}, "")
if err != nil {
return handleWaitingAPIError(err, false, "patch ippool %q", original.Name)
}
patchedIPPool = s
return true, nil
})
if err == nil {
return patchedIPPool.DeepCopy()
}
if errors.Is(err, context.DeadlineExceeded) {
Failf("timed out while retrying to patch ippool %s", original.Name)
}
Failf("error occurred while retrying to patch ippool %s: %v", original.Name, err)
return nil
}
// PatchSync patches the ippool and waits for the ippool to be ready for `timeout`.
// If the ippool doesn't become ready before the timeout, it will fail the test.
func (c *IPPoolClient) PatchSync(original, modified *apiv1.IPPool) *apiv1.IPPool {
ginkgo.GinkgoHelper()
s := c.Patch(original, modified, timeout)
ExpectTrue(c.WaitToBeReady(s.Name, timeout))
// Get the newest ippool after it becomes ready
return c.Get(s.Name).DeepCopy()
}
// Delete deletes an ippool if the ippool exists
func (c *IPPoolClient) Delete(name string) {
ginkgo.GinkgoHelper()
err := c.IPPoolInterface.Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
Failf("Failed to delete ippool %q: %v", name, err)
}
}
// DeleteSync deletes the ippool and waits for the ippool to disappear for `timeout`.
// If the ippool doesn't disappear before the timeout, it will fail the test.
func (c *IPPoolClient) DeleteSync(name string) {
ginkgo.GinkgoHelper()
c.Delete(name)
gomega.Expect(c.WaitToDisappear(name, poll, timeout)).To(gomega.Succeed(), "wait for ippool %q to disappear", name)
}
func isIPPoolConditionSetAsExpected(ippool *apiv1.IPPool, conditionType apiv1.ConditionType, wantTrue, silent bool) bool {
for _, cond := range ippool.Status.Conditions {
if cond.Type == conditionType {
if (wantTrue && (cond.Status == corev1.ConditionTrue)) || (!wantTrue && (cond.Status != corev1.ConditionTrue)) {
return true
}
if !silent {
Logf("Condition %s of ippool %s is %v instead of %t. Reason: %v, message: %v",
conditionType, ippool.Name, cond.Status == corev1.ConditionTrue, wantTrue, cond.Reason, cond.Message)
}
return false
}
}
if !silent {
Logf("Couldn't find condition %v on ippool %v", conditionType, ippool.Name)
}
return false
}
// IsIPPoolConditionSetAsExpected returns a wantTrue value if the ippool has a match to the conditionType,
// otherwise returns an opposite value of the wantTrue with detailed logging.
func IsIPPoolConditionSetAsExpected(ippool *apiv1.IPPool, conditionType apiv1.ConditionType, wantTrue bool) bool {
return isIPPoolConditionSetAsExpected(ippool, conditionType, wantTrue, false)
}
// WaitConditionToBe returns whether ippool "name's" condition state matches wantTrue
// within timeout. If wantTrue is true, it will ensure the ippool condition status is
// ConditionTrue; if it's false, it ensures the ippool condition is in any state other
// than ConditionTrue (e.g. not true or unknown).
func (c *IPPoolClient) WaitConditionToBe(name string, conditionType apiv1.ConditionType, wantTrue bool, timeout time.Duration) bool {
Logf("Waiting up to %v for ippool %s condition %s to be %t", timeout, name, conditionType, wantTrue)
for start := time.Now(); time.Since(start) < timeout; time.Sleep(poll) {
ippool := c.Get(name)
if IsIPPoolConditionSetAsExpected(ippool, conditionType, wantTrue) {
Logf("IPPool %s reach desired %t condition status", name, wantTrue)
return true
}
Logf("IPPool %s still not reach desired %t condition status", name, wantTrue)
}
Logf("IPPool %s didn't reach desired %s condition status (%t) within %v", name, conditionType, wantTrue, timeout)
return false
}
// WaitToBeReady returns whether the ippool is ready within timeout.
func (c *IPPoolClient) WaitToBeReady(name string, timeout time.Duration) bool {
return c.WaitConditionToBe(name, apiv1.Ready, true, timeout)
}
// WaitToBeUpdated returns whether the ippool is updated within timeout.
func (c *IPPoolClient) WaitToBeUpdated(ippool *apiv1.IPPool, timeout time.Duration) bool {
Logf("Waiting up to %v for ippool %s to be updated", timeout, ippool.Name)
rv, _ := big.NewInt(0).SetString(ippool.ResourceVersion, 10)
for start := time.Now(); time.Since(start) < timeout; time.Sleep(poll) {
s := c.Get(ippool.Name)
if current, _ := big.NewInt(0).SetString(s.ResourceVersion, 10); current.Cmp(rv) > 0 {
Logf("IPPool %s updated", ippool.Name)
return true
}
Logf("IPPool %s still not updated", ippool.Name)
}
Logf("IPPool %s was not updated within %v", ippool.Name, timeout)
return false
}
// WaitUntil waits the given timeout duration for the specified condition to be met.
func (c *IPPoolClient) WaitUntil(name string, cond func(s *apiv1.IPPool) (bool, error), condDesc string, interval, timeout time.Duration) *apiv1.IPPool {
ginkgo.GinkgoHelper()
var ippool *apiv1.IPPool
err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(_ context.Context) (bool, error) {
Logf("Waiting for ippool %s to meet condition %q", name, condDesc)
ippool = c.Get(name).DeepCopy()
met, err := cond(ippool)
if err != nil {
return false, fmt.Errorf("failed to check condition for ippool %s: %w", name, err)
}
return met, nil
})
if err == nil {
return ippool
}
if errors.Is(err, context.DeadlineExceeded) {
Failf("timed out while waiting for ippool %s to meet condition %q", name, condDesc)
}
Failf("error occurred while waiting for ippool %s to meet condition %q: %v", name, condDesc, err)
return nil
}
// WaitToDisappear waits the given timeout duration for the specified ippool to disappear.
func (c *IPPoolClient) WaitToDisappear(name string, _, timeout time.Duration) error {
err := framework.Gomega().Eventually(context.Background(), framework.HandleRetry(func(ctx context.Context) (*apiv1.IPPool, error) {
ippool, err := c.IPPoolInterface.Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return nil, nil
}
return ippool, err
})).WithTimeout(timeout).Should(gomega.BeNil())
if err != nil {
return fmt.Errorf("expected ippool %s to not be found: %w", name, err)
}
return nil
}
func MakeIPPool(name, subnet string, ips, namespaces []string) *apiv1.IPPool {
return &apiv1.IPPool{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: apiv1.IPPoolSpec{
Subnet: subnet,
IPs: ips,
Namespaces: namespaces,
},
}
}