| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package lease |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/google/go-cmp/cmp" |
| 27 |
coordinationv1 "k8s.io/api/coordination/v1" |
| 28 |
corev1 "k8s.io/api/core/v1" |
| 29 |
apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 30 |
apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 31 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 |
"k8s.io/apimachinery/pkg/runtime" |
| 33 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 34 |
"k8s.io/apimachinery/pkg/types" |
| 35 |
clientset "k8s.io/client-go/kubernetes" |
| 36 |
"k8s.io/client-go/kubernetes/fake" |
| 37 |
clienttesting "k8s.io/client-go/testing" |
| 38 |
testingclock "k8s.io/utils/clock/testing" |
| 39 |
"k8s.io/utils/pointer" |
| 40 |
|
| 41 |
"k8s.io/klog/v2" |
| 42 |
"k8s.io/klog/v2/ktesting" |
| 43 |
) |
| 44 |
|
| 45 |
func TestNewNodeLease(t *testing.T) { |
| 46 |
fakeClock := testingclock.NewFakeClock(time.Now()) |
| 47 |
node := &corev1.Node{ |
| 48 |
ObjectMeta: metav1.ObjectMeta{ |
| 49 |
Name: "foo", |
| 50 |
UID: types.UID("foo-uid"), |
| 51 |
}, |
| 52 |
} |
| 53 |
cases := []struct { |
| 54 |
desc string |
| 55 |
controller *controller |
| 56 |
base *coordinationv1.Lease |
| 57 |
expect *coordinationv1.Lease |
| 58 |
}{ |
| 59 |
{ |
| 60 |
desc: "nil base without node", |
| 61 |
controller: &controller{ |
| 62 |
client: fake.NewSimpleClientset(), |
| 63 |
leaseName: node.Name, |
| 64 |
holderIdentity: node.Name, |
| 65 |
leaseDurationSeconds: 10, |
| 66 |
clock: fakeClock, |
| 67 |
}, |
| 68 |
base: nil, |
| 69 |
expect: &coordinationv1.Lease{ |
| 70 |
ObjectMeta: metav1.ObjectMeta{ |
| 71 |
Name: node.Name, |
| 72 |
Namespace: corev1.NamespaceNodeLease, |
| 73 |
}, |
| 74 |
Spec: coordinationv1.LeaseSpec{ |
| 75 |
HolderIdentity: pointer.StringPtr(node.Name), |
| 76 |
LeaseDurationSeconds: pointer.Int32Ptr(10), |
| 77 |
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()}, |
| 78 |
}, |
| 79 |
}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
desc: "nil base with node", |
| 83 |
controller: &controller{ |
| 84 |
client: fake.NewSimpleClientset(node), |
| 85 |
leaseName: node.Name, |
| 86 |
holderIdentity: node.Name, |
| 87 |
leaseDurationSeconds: 10, |
| 88 |
clock: fakeClock, |
| 89 |
}, |
| 90 |
base: nil, |
| 91 |
expect: &coordinationv1.Lease{ |
| 92 |
ObjectMeta: metav1.ObjectMeta{ |
| 93 |
Name: node.Name, |
| 94 |
Namespace: corev1.NamespaceNodeLease, |
| 95 |
OwnerReferences: []metav1.OwnerReference{ |
| 96 |
{ |
| 97 |
APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version, |
| 98 |
Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind, |
| 99 |
Name: node.Name, |
| 100 |
UID: node.UID, |
| 101 |
}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
Spec: coordinationv1.LeaseSpec{ |
| 105 |
HolderIdentity: pointer.StringPtr(node.Name), |
| 106 |
LeaseDurationSeconds: pointer.Int32Ptr(10), |
| 107 |
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()}, |
| 108 |
}, |
| 109 |
}, |
| 110 |
}, |
| 111 |
{ |
| 112 |
desc: "non-nil base without owner ref, renew time is updated", |
| 113 |
controller: &controller{ |
| 114 |
client: fake.NewSimpleClientset(node), |
| 115 |
holderIdentity: node.Name, |
| 116 |
leaseDurationSeconds: 10, |
| 117 |
clock: fakeClock, |
| 118 |
}, |
| 119 |
base: &coordinationv1.Lease{ |
| 120 |
ObjectMeta: metav1.ObjectMeta{ |
| 121 |
Name: node.Name, |
| 122 |
Namespace: corev1.NamespaceNodeLease, |
| 123 |
}, |
| 124 |
Spec: coordinationv1.LeaseSpec{ |
| 125 |
HolderIdentity: pointer.StringPtr(node.Name), |
| 126 |
LeaseDurationSeconds: pointer.Int32Ptr(10), |
| 127 |
RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-10 * time.Second)}, |
| 128 |
}, |
| 129 |
}, |
| 130 |
expect: &coordinationv1.Lease{ |
| 131 |
ObjectMeta: metav1.ObjectMeta{ |
| 132 |
Name: node.Name, |
| 133 |
Namespace: corev1.NamespaceNodeLease, |
| 134 |
OwnerReferences: []metav1.OwnerReference{ |
| 135 |
{ |
| 136 |
APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version, |
| 137 |
Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind, |
| 138 |
Name: node.Name, |
| 139 |
UID: node.UID, |
| 140 |
}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
Spec: coordinationv1.LeaseSpec{ |
| 144 |
HolderIdentity: pointer.StringPtr(node.Name), |
| 145 |
LeaseDurationSeconds: pointer.Int32Ptr(10), |
| 146 |
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()}, |
| 147 |
}, |
| 148 |
}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
desc: "non-nil base with owner ref, renew time is updated", |
| 152 |
controller: &controller{ |
| 153 |
client: fake.NewSimpleClientset(node), |
| 154 |
holderIdentity: node.Name, |
| 155 |
leaseDurationSeconds: 10, |
| 156 |
clock: fakeClock, |
| 157 |
}, |
| 158 |
base: &coordinationv1.Lease{ |
| 159 |
ObjectMeta: metav1.ObjectMeta{ |
| 160 |
Name: node.Name, |
| 161 |
Namespace: corev1.NamespaceNodeLease, |
| 162 |
OwnerReferences: []metav1.OwnerReference{ |
| 163 |
{ |
| 164 |
APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version, |
| 165 |
Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind, |
| 166 |
Name: node.Name, |
| 167 |
UID: node.UID, |
| 168 |
}, |
| 169 |
}, |
| 170 |
}, |
| 171 |
Spec: coordinationv1.LeaseSpec{ |
| 172 |
HolderIdentity: pointer.StringPtr(node.Name), |
| 173 |
LeaseDurationSeconds: pointer.Int32Ptr(10), |
| 174 |
RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-10 * time.Second)}, |
| 175 |
}, |
| 176 |
}, |
| 177 |
expect: &coordinationv1.Lease{ |
| 178 |
ObjectMeta: metav1.ObjectMeta{ |
| 179 |
Name: node.Name, |
| 180 |
Namespace: corev1.NamespaceNodeLease, |
| 181 |
OwnerReferences: []metav1.OwnerReference{ |
| 182 |
{ |
| 183 |
APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version, |
| 184 |
Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind, |
| 185 |
Name: node.Name, |
| 186 |
UID: node.UID, |
| 187 |
}, |
| 188 |
}, |
| 189 |
}, |
| 190 |
Spec: coordinationv1.LeaseSpec{ |
| 191 |
HolderIdentity: pointer.StringPtr(node.Name), |
| 192 |
LeaseDurationSeconds: pointer.Int32Ptr(10), |
| 193 |
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()}, |
| 194 |
}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
} |
| 198 |
|
| 199 |
for _, tc := range cases { |
| 200 |
t.Run(tc.desc, func(t *testing.T) { |
| 201 |
logger, _ := ktesting.NewTestContext(t) |
| 202 |
tc.controller.newLeasePostProcessFunc = setNodeOwnerFunc(logger, tc.controller.client, node.Name) |
| 203 |
tc.controller.leaseNamespace = corev1.NamespaceNodeLease |
| 204 |
newLease, _ := tc.controller.newLease(tc.base) |
| 205 |
if newLease == tc.base { |
| 206 |
t.Fatalf("the new lease must be newly allocated, but got same address as base") |
| 207 |
} |
| 208 |
if !apiequality.Semantic.DeepEqual(tc.expect, newLease) { |
| 209 |
t.Errorf("unexpected result from newLease: %s", cmp.Diff(tc.expect, newLease)) |
| 210 |
} |
| 211 |
}) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func TestRetryUpdateNodeLease(t *testing.T) { |
| 216 |
node := &corev1.Node{ |
| 217 |
ObjectMeta: metav1.ObjectMeta{ |
| 218 |
Name: "foo", |
| 219 |
UID: types.UID("foo-uid"), |
| 220 |
}, |
| 221 |
} |
| 222 |
gr := schema.GroupResource{Group: "v1", Resource: "lease"} |
| 223 |
noConnectionUpdateErr := apierrors.NewServerTimeout(gr, "put", 1) |
| 224 |
optimistcLockUpdateErr := apierrors.NewConflict(gr, "lease", fmt.Errorf("conflict")) |
| 225 |
cases := []struct { |
| 226 |
desc string |
| 227 |
updateReactor func(action clienttesting.Action) (bool, runtime.Object, error) |
| 228 |
getReactor func(action clienttesting.Action) (bool, runtime.Object, error) |
| 229 |
onRepeatedHeartbeatFailure func() |
| 230 |
expectErr bool |
| 231 |
client *fake.Clientset |
| 232 |
}{ |
| 233 |
{ |
| 234 |
desc: "no errors", |
| 235 |
updateReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 236 |
return true, &coordinationv1.Lease{}, nil |
| 237 |
}, |
| 238 |
getReactor: nil, |
| 239 |
onRepeatedHeartbeatFailure: nil, |
| 240 |
expectErr: false, |
| 241 |
client: fake.NewSimpleClientset(node), |
| 242 |
}, |
| 243 |
{ |
| 244 |
desc: "connection errors", |
| 245 |
updateReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 246 |
return true, nil, noConnectionUpdateErr |
| 247 |
}, |
| 248 |
getReactor: nil, |
| 249 |
onRepeatedHeartbeatFailure: nil, |
| 250 |
expectErr: true, |
| 251 |
client: fake.NewSimpleClientset(node), |
| 252 |
}, |
| 253 |
{ |
| 254 |
desc: "optimistic lock errors", |
| 255 |
updateReactor: func() func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 256 |
i := 0 |
| 257 |
return func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 258 |
i++ |
| 259 |
switch i { |
| 260 |
case 1: |
| 261 |
return true, nil, noConnectionUpdateErr |
| 262 |
case 2: |
| 263 |
return true, nil, optimistcLockUpdateErr |
| 264 |
default: |
| 265 |
return true, &coordinationv1.Lease{}, nil |
| 266 |
} |
| 267 |
} |
| 268 |
}(), |
| 269 |
getReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 270 |
return true, &coordinationv1.Lease{}, nil |
| 271 |
}, |
| 272 |
onRepeatedHeartbeatFailure: func() { t.Fatalf("onRepeatedHeartbeatFailure called") }, |
| 273 |
expectErr: false, |
| 274 |
client: fake.NewSimpleClientset(node), |
| 275 |
}, |
| 276 |
{ |
| 277 |
desc: "node not found errors", |
| 278 |
updateReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 279 |
t.Fatalf("lease was updated when node does not exist!") |
| 280 |
return true, nil, nil |
| 281 |
}, |
| 282 |
getReactor: nil, |
| 283 |
onRepeatedHeartbeatFailure: nil, |
| 284 |
expectErr: true, |
| 285 |
client: fake.NewSimpleClientset(), |
| 286 |
}, |
| 287 |
} |
| 288 |
for _, tc := range cases { |
| 289 |
t.Run(tc.desc, func(t *testing.T) { |
| 290 |
logger, ctx := ktesting.NewTestContext(t) |
| 291 |
cl := tc.client |
| 292 |
if tc.updateReactor != nil { |
| 293 |
cl.PrependReactor("update", "leases", tc.updateReactor) |
| 294 |
} |
| 295 |
if tc.getReactor != nil { |
| 296 |
cl.PrependReactor("get", "leases", tc.getReactor) |
| 297 |
} |
| 298 |
c := &controller{ |
| 299 |
clock: testingclock.NewFakeClock(time.Now()), |
| 300 |
client: cl, |
| 301 |
leaseClient: cl.CoordinationV1().Leases(corev1.NamespaceNodeLease), |
| 302 |
holderIdentity: node.Name, |
| 303 |
leaseNamespace: corev1.NamespaceNodeLease, |
| 304 |
leaseDurationSeconds: 10, |
| 305 |
onRepeatedHeartbeatFailure: tc.onRepeatedHeartbeatFailure, |
| 306 |
newLeasePostProcessFunc: setNodeOwnerFunc(logger, cl, node.Name), |
| 307 |
} |
| 308 |
if err := c.retryUpdateLease(ctx, nil); tc.expectErr != (err != nil) { |
| 309 |
t.Fatalf("got %v, expected %v", err != nil, tc.expectErr) |
| 310 |
} |
| 311 |
}) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func TestUpdateUsingLatestLease(t *testing.T) { |
| 316 |
nodeName := "foo" |
| 317 |
node := &corev1.Node{ |
| 318 |
ObjectMeta: metav1.ObjectMeta{ |
| 319 |
Name: nodeName, |
| 320 |
UID: types.UID("foo-uid"), |
| 321 |
}, |
| 322 |
} |
| 323 |
|
| 324 |
notFoundErr := apierrors.NewNotFound(coordinationv1.Resource("lease"), nodeName) |
| 325 |
internalErr := apierrors.NewInternalError(errors.New("unreachable code")) |
| 326 |
|
| 327 |
makeLease := func(name, resourceVersion string) *coordinationv1.Lease { |
| 328 |
return &coordinationv1.Lease{ |
| 329 |
ObjectMeta: metav1.ObjectMeta{ |
| 330 |
Namespace: corev1.NamespaceNodeLease, |
| 331 |
Name: name, |
| 332 |
ResourceVersion: resourceVersion, |
| 333 |
}, |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
cases := []struct { |
| 338 |
desc string |
| 339 |
existingObjs []runtime.Object |
| 340 |
latestLease *coordinationv1.Lease |
| 341 |
updateReactor func(action clienttesting.Action) (bool, runtime.Object, error) |
| 342 |
getReactor func(action clienttesting.Action) (bool, runtime.Object, error) |
| 343 |
createReactor func(action clienttesting.Action) (bool, runtime.Object, error) |
| 344 |
expectLatestLease bool |
| 345 |
expectLeaseResourceVersion string |
| 346 |
}{ |
| 347 |
{ |
| 348 |
desc: "latestLease is nil and need to create", |
| 349 |
existingObjs: []runtime.Object{node}, |
| 350 |
latestLease: nil, |
| 351 |
updateReactor: nil, |
| 352 |
getReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 353 |
return true, nil, notFoundErr |
| 354 |
}, |
| 355 |
createReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 356 |
return true, makeLease(nodeName, "1"), nil |
| 357 |
}, |
| 358 |
expectLatestLease: true, |
| 359 |
expectLeaseResourceVersion: "1", |
| 360 |
}, |
| 361 |
{ |
| 362 |
desc: "latestLease is nil and need to create, node doesn't exist", |
| 363 |
existingObjs: nil, |
| 364 |
latestLease: nil, |
| 365 |
updateReactor: nil, |
| 366 |
getReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 367 |
return true, nil, notFoundErr |
| 368 |
}, |
| 369 |
createReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 370 |
return true, makeLease(nodeName, "1"), nil |
| 371 |
}, |
| 372 |
expectLatestLease: false, |
| 373 |
expectLeaseResourceVersion: "1", |
| 374 |
}, |
| 375 |
{ |
| 376 |
desc: "latestLease is nil and need to update", |
| 377 |
existingObjs: []runtime.Object{node}, |
| 378 |
latestLease: nil, |
| 379 |
updateReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 380 |
return true, makeLease(nodeName, "2"), nil |
| 381 |
}, |
| 382 |
getReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 383 |
return true, makeLease(nodeName, "1"), nil |
| 384 |
}, |
| 385 |
expectLatestLease: true, |
| 386 |
expectLeaseResourceVersion: "2", |
| 387 |
}, |
| 388 |
{ |
| 389 |
desc: "latestLease exist and need to update", |
| 390 |
existingObjs: []runtime.Object{node}, |
| 391 |
latestLease: makeLease(nodeName, "1"), |
| 392 |
updateReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 393 |
return true, makeLease(nodeName, "2"), nil |
| 394 |
}, |
| 395 |
expectLatestLease: true, |
| 396 |
expectLeaseResourceVersion: "2", |
| 397 |
}, |
| 398 |
{ |
| 399 |
desc: "update with latest lease failed", |
| 400 |
existingObjs: []runtime.Object{node}, |
| 401 |
latestLease: makeLease(nodeName, "1"), |
| 402 |
updateReactor: func() func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 403 |
i := 0 |
| 404 |
return func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 405 |
i++ |
| 406 |
switch i { |
| 407 |
case 1: |
| 408 |
return true, nil, notFoundErr |
| 409 |
case 2: |
| 410 |
return true, makeLease(nodeName, "3"), nil |
| 411 |
default: |
| 412 |
t.Fatalf("unexpect call update lease") |
| 413 |
return true, nil, internalErr |
| 414 |
} |
| 415 |
} |
| 416 |
}(), |
| 417 |
getReactor: func(action clienttesting.Action) (bool, runtime.Object, error) { |
| 418 |
return true, makeLease(nodeName, "2"), nil |
| 419 |
}, |
| 420 |
expectLatestLease: true, |
| 421 |
expectLeaseResourceVersion: "3", |
| 422 |
}, |
| 423 |
} |
| 424 |
for _, tc := range cases { |
| 425 |
t.Run(tc.desc, func(t *testing.T) { |
| 426 |
logger, ctx := ktesting.NewTestContext(t) |
| 427 |
cl := fake.NewSimpleClientset(tc.existingObjs...) |
| 428 |
if tc.updateReactor != nil { |
| 429 |
cl.PrependReactor("update", "leases", tc.updateReactor) |
| 430 |
} |
| 431 |
if tc.getReactor != nil { |
| 432 |
cl.PrependReactor("get", "leases", tc.getReactor) |
| 433 |
} |
| 434 |
if tc.createReactor != nil { |
| 435 |
cl.PrependReactor("create", "leases", tc.createReactor) |
| 436 |
} |
| 437 |
c := &controller{ |
| 438 |
clock: testingclock.NewFakeClock(time.Now()), |
| 439 |
client: cl, |
| 440 |
leaseClient: cl.CoordinationV1().Leases(corev1.NamespaceNodeLease), |
| 441 |
holderIdentity: node.Name, |
| 442 |
leaseNamespace: corev1.NamespaceNodeLease, |
| 443 |
leaseDurationSeconds: 10, |
| 444 |
latestLease: tc.latestLease, |
| 445 |
newLeasePostProcessFunc: setNodeOwnerFunc(logger, cl, node.Name), |
| 446 |
} |
| 447 |
|
| 448 |
c.sync(ctx) |
| 449 |
|
| 450 |
if tc.expectLatestLease { |
| 451 |
if tc.expectLeaseResourceVersion != c.latestLease.ResourceVersion { |
| 452 |
t.Fatalf("latestLease RV got %v, expected %v", c.latestLease.ResourceVersion, tc.expectLeaseResourceVersion) |
| 453 |
} |
| 454 |
} else { |
| 455 |
if c.latestLease != nil { |
| 456 |
t.Fatalf("unexpected latestLease: %v", c.latestLease) |
| 457 |
} |
| 458 |
} |
| 459 |
}) |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
func setNodeOwnerFunc(logger klog.Logger, c clientset.Interface, nodeName string) func(lease *coordinationv1.Lease) error { |
| 466 |
return func(lease *coordinationv1.Lease) error { |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
if len(lease.OwnerReferences) == 0 { |
| 472 |
if node, err := c.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{}); err == nil { |
| 473 |
lease.OwnerReferences = []metav1.OwnerReference{ |
| 474 |
{ |
| 475 |
APIVersion: corev1.SchemeGroupVersion.WithKind("Node").Version, |
| 476 |
Kind: corev1.SchemeGroupVersion.WithKind("Node").Kind, |
| 477 |
Name: nodeName, |
| 478 |
UID: node.UID, |
| 479 |
}, |
| 480 |
} |
| 481 |
} else { |
| 482 |
logger.Error(err, "failed to get node when trying to set owner ref to the node lease", "node", nodeName) |
| 483 |
return err |
| 484 |
} |
| 485 |
} |
| 486 |
return nil |
| 487 |
} |
| 488 |
} |