Coordinated Leader Election Alpha API

This commit is contained in:
Jefftree
2024-07-21 20:02:00 +00:00
parent ab470aad01
commit 3999b98c88
100 changed files with 8329 additions and 100 deletions

View File

@@ -22,6 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/coordination"
"k8s.io/utils/ptr"
)
func TestValidateLease(t *testing.T) {
@@ -41,14 +42,58 @@ func TestValidateLeaseSpec(t *testing.T) {
holder := "holder"
leaseDuration := int32(0)
leaseTransitions := int32(-1)
spec := &coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: &leaseDuration,
LeaseTransitions: &leaseTransitions,
preferredHolder := "holder2"
testcases := []struct {
spec coordination.LeaseSpec
err bool
}{
{
// valid
coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: ptr.To[int32](10),
LeaseTransitions: ptr.To[int32](1),
},
false,
},
{
// valid with PreferredHolder
coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: ptr.To[int32](10),
LeaseTransitions: ptr.To[int32](1),
Strategy: ptr.To(coordination.OldestEmulationVersion),
PreferredHolder: ptr.To("someotherholder"),
},
false,
},
{
coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: &leaseDuration,
LeaseTransitions: &leaseTransitions,
},
true,
},
{
coordination.LeaseSpec{
HolderIdentity: &holder,
LeaseDurationSeconds: &leaseDuration,
LeaseTransitions: &leaseTransitions,
PreferredHolder: &preferredHolder,
},
true,
},
}
errs := ValidateLeaseSpec(spec, field.NewPath("foo"))
if len(errs) != 2 {
t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
for _, tc := range testcases {
errs := ValidateLeaseSpec(&tc.spec, field.NewPath("foo"))
if tc.err && len(errs) == 0 {
t.Error("Expected err, got no err")
} else if !tc.err && len(errs) != 0 {
t.Errorf("Expected no err, got err %v", errs)
}
}
}
@@ -102,3 +147,188 @@ func TestValidateLeaseSpecUpdate(t *testing.T) {
t.Errorf("unexpected list of errors for valid update: %#v", errs.ToAggregate().Error())
}
}
func TestValidateLeaseCandidate(t *testing.T) {
lease := &coordination.LeaseCandidate{
ObjectMeta: metav1.ObjectMeta{
Name: "invalidName++",
Namespace: "==invalid_Namespace==",
},
}
errs := ValidateLeaseCandidate(lease)
if len(errs) == 0 {
t.Errorf("expected invalid LeaseCandidate")
}
}
func TestValidateLeaseCandidateSpec(t *testing.T) {
testcases := []struct {
name string
shouldErr bool
spec *coordination.LeaseCandidateSpec
}{
{
"valid",
false,
&coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.0",
EmulationVersion: "1.30.0",
LeaseName: "test",
PreferredStrategies: []coordination.CoordinatedLeaseStrategy{coordination.OldestEmulationVersion},
},
},
{
"valid custom strategy should not require binaryVersion and emulationVersion",
false,
&coordination.LeaseCandidateSpec{
LeaseName: "test",
PreferredStrategies: []coordination.CoordinatedLeaseStrategy{"custom.com/foo"},
},
},
{
"no lease name",
true,
&coordination.LeaseCandidateSpec{
EmulationVersion: "1.30.0",
},
},
{
"bad binaryVersion",
true,
&coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.1.6",
LeaseName: "test",
},
},
{
"emulation should be greater than or equal to binary version",
true,
&coordination.LeaseCandidateSpec{
EmulationVersion: "1.30.0",
BinaryVersion: "1.29.0",
LeaseName: "test",
},
},
{
"preferredStrategies bad",
true,
&coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.1",
EmulationVersion: "1.30.1",
LeaseName: "test",
PreferredStrategies: []coordination.CoordinatedLeaseStrategy{"foo"},
},
},
{
"preferredStrategies good but emulationVersion missing",
true,
&coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.1",
LeaseName: "test",
PreferredStrategies: []coordination.CoordinatedLeaseStrategy{coordination.OldestEmulationVersion},
},
},
}
for _, tc := range testcases {
errs := ValidateLeaseCandidateSpec(tc.spec, field.NewPath("foo"))
if len(errs) > 0 && !tc.shouldErr {
t.Errorf("unexpected list of errors: %#v", errs.ToAggregate().Error())
} else if len(errs) == 0 && tc.shouldErr {
t.Errorf("Expected err, got no error for tc: %s", tc.name)
}
}
}
func TestValidateLeaseCandidateUpdate(t *testing.T) {
testcases := []struct {
name string
old coordination.LeaseCandidate
update coordination.LeaseCandidate
err bool
}{
{
name: "valid update",
old: coordination.LeaseCandidate{
Spec: coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.0",
EmulationVersion: "1.30.0",
LeaseName: "test",
},
},
update: coordination.LeaseCandidate{
Spec: coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.0",
EmulationVersion: "1.30.0",
LeaseName: "test",
},
},
err: false,
},
{
name: "update LeaseName should fail",
old: coordination.LeaseCandidate{
Spec: coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.0",
EmulationVersion: "1.30.0",
LeaseName: "test",
},
},
update: coordination.LeaseCandidate{
Spec: coordination.LeaseCandidateSpec{
BinaryVersion: "1.30.0",
EmulationVersion: "1.30.0",
LeaseName: "test-update",
},
},
err: true,
},
}
for _, tc := range testcases {
tc.old.ResourceVersion = "1"
tc.update.ResourceVersion = "1"
errs := ValidateLeaseCandidateUpdate(&tc.update, &tc.old)
if tc.err && len(errs) == 0 {
t.Errorf("Expected err, got no err for tc: %s", tc.name)
} else if !tc.err && len(errs) != 0 {
t.Errorf("Expected no err, got err %v for tc: %s", errs, tc.name)
}
}
}
func TestValidateCoordinatedLeaseStrategy(t *testing.T) {
testcases := []struct {
strategy coordination.CoordinatedLeaseStrategy
err bool
}{
{
coordination.CoordinatedLeaseStrategy("foobar"),
true,
},
{
coordination.CoordinatedLeaseStrategy("example.com/foobar/toomanyslashes"),
true,
},
{
coordination.CoordinatedLeaseStrategy(coordination.OldestEmulationVersion),
false,
},
{
coordination.CoordinatedLeaseStrategy("example.com/foobar"),
false,
},
}
for _, tc := range testcases {
errs := ValidateCoordinatedLeaseStrategy(tc.strategy, field.NewPath("foo"))
if tc.err && len(errs) == 0 {
t.Error("Expected err, got no err")
} else if !tc.err && len(errs) != 0 {
t.Errorf("Expected no err, got err %v", errs)
}
}
}