mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-10 13:42:02 +00:00
Merge pull request #114586 from andrewsykim/apiserver-lease-rename
Rename apiserver identity lease labels to apiserver.kubernetes.io/identity
This commit is contained in:
@@ -26,6 +26,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/cryptobyte"
|
||||
|
||||
coordinationv1 "k8s.io/api/coordination/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -45,9 +47,21 @@ const (
|
||||
testLeaseName = "apiserver-lease-test"
|
||||
)
|
||||
|
||||
func expectedAPIServerIdentity(hostname string) string {
|
||||
hash := sha256.Sum256([]byte(hostname))
|
||||
return "kube-apiserver-" + strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:16]))
|
||||
func expectedAPIServerIdentity(t *testing.T, hostname string) string {
|
||||
b := cryptobyte.NewBuilder(nil)
|
||||
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||
b.AddBytes([]byte(hostname))
|
||||
})
|
||||
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||
b.AddBytes([]byte("kube-apiserver"))
|
||||
})
|
||||
hashData, err := b.Bytes()
|
||||
if err != nil {
|
||||
t.Fatalf("error building hash data for apiserver identity: %v", err)
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(hashData)
|
||||
return "apiserver-" + strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(hash[:16]))
|
||||
}
|
||||
|
||||
func TestCreateLeaseOnStart(t *testing.T) {
|
||||
@@ -84,8 +98,8 @@ func TestCreateLeaseOnStart(t *testing.T) {
|
||||
}
|
||||
|
||||
lease := leases.Items[0]
|
||||
if lease.Name != expectedAPIServerIdentity(hostname) {
|
||||
return false, fmt.Errorf("unexpected apiserver identity, got: %v, expected: %v", lease.Name, expectedAPIServerIdentity(hostname))
|
||||
if lease.Name != expectedAPIServerIdentity(t, hostname) {
|
||||
return false, fmt.Errorf("unexpected apiserver identity, got: %v, expected: %v", lease.Name, expectedAPIServerIdentity(t, hostname))
|
||||
}
|
||||
|
||||
if lease.Labels[corev1.LabelHostname] != hostname {
|
||||
@@ -134,12 +148,54 @@ func TestLeaseGarbageCollection(t *testing.T) {
|
||||
t.Run("expired non-identity lease should not be garbage collected",
|
||||
testLeaseNotGarbageCollected(t, kubeclient, expiredLease))
|
||||
|
||||
// identity leases (with k8s.io/component label) created in user namespaces should not be GC'ed
|
||||
// identity leases (with apiserver.kubernetes.io/identity label) created in user namespaces should not be GC'ed
|
||||
expiredNonKubeSystemLease := newTestLease(time.Now().Add(-2*time.Hour), metav1.NamespaceDefault)
|
||||
t.Run("expired non-system identity lease should not be garbage collected",
|
||||
testLeaseNotGarbageCollected(t, kubeclient, expiredNonKubeSystemLease))
|
||||
}
|
||||
|
||||
func TestLeaseGarbageCollectionWithDeprecatedLabels(t *testing.T) {
|
||||
oldIdentityLeaseDurationSeconds := controlplane.IdentityLeaseDurationSeconds
|
||||
oldIdentityLeaseGCPeriod := controlplane.IdentityLeaseGCPeriod
|
||||
oldIdentityLeaseRenewIntervalPeriod := controlplane.IdentityLeaseRenewIntervalPeriod
|
||||
defer func() {
|
||||
// reset the default values for leases after this test
|
||||
controlplane.IdentityLeaseDurationSeconds = oldIdentityLeaseDurationSeconds
|
||||
controlplane.IdentityLeaseGCPeriod = oldIdentityLeaseGCPeriod
|
||||
controlplane.IdentityLeaseRenewIntervalPeriod = oldIdentityLeaseRenewIntervalPeriod
|
||||
}()
|
||||
|
||||
// Shorten lease parameters so GC behavior can be exercised in integration tests
|
||||
controlplane.IdentityLeaseDurationSeconds = 1
|
||||
controlplane.IdentityLeaseGCPeriod = time.Second
|
||||
controlplane.IdentityLeaseRenewIntervalPeriod = time.Second
|
||||
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.APIServerIdentity, true)()
|
||||
result := kubeapiservertesting.StartTestServerOrDie(t, nil, nil, framework.SharedEtcd())
|
||||
defer result.TearDownFn()
|
||||
|
||||
kubeclient, err := kubernetes.NewForConfig(result.ClientConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expiredLease := newTestLeaseWithDeprecatedLabels(time.Now().Add(-2*time.Hour), metav1.NamespaceSystem)
|
||||
t.Run("expired apiserver lease should be garbage collected",
|
||||
testLeaseGarbageCollected(t, kubeclient, expiredLease))
|
||||
|
||||
freshLease := newTestLeaseWithDeprecatedLabels(time.Now().Add(-2*time.Minute), metav1.NamespaceSystem)
|
||||
t.Run("fresh apiserver lease should not be garbage collected",
|
||||
testLeaseNotGarbageCollected(t, kubeclient, freshLease))
|
||||
|
||||
expiredLease.Labels = nil
|
||||
t.Run("expired non-identity lease should not be garbage collected",
|
||||
testLeaseNotGarbageCollected(t, kubeclient, expiredLease))
|
||||
|
||||
// identity leases (with k8s.io/component label) created in user namespaces should not be GC'ed
|
||||
expiredNonKubeSystemLease := newTestLeaseWithDeprecatedLabels(time.Now().Add(-2*time.Hour), metav1.NamespaceDefault)
|
||||
t.Run("expired non-system identity lease should not be garbage collected",
|
||||
testLeaseNotGarbageCollected(t, kubeclient, expiredNonKubeSystemLease))
|
||||
}
|
||||
|
||||
func testLeaseGarbageCollected(t *testing.T, client kubernetes.Interface, lease *coordinationv1.Lease) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
ns := lease.Namespace
|
||||
@@ -203,3 +259,21 @@ func newTestLease(acquireTime time.Time, namespace string) *coordinationv1.Lease
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newTestLeaseWithDeprecatedLabels(acquireTime time.Time, namespace string) *coordinationv1.Lease {
|
||||
return &coordinationv1.Lease{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: testLeaseName,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"k8s.io/component": "kube-apiserver",
|
||||
},
|
||||
},
|
||||
Spec: coordinationv1.LeaseSpec{
|
||||
HolderIdentity: pointer.StringPtr(testLeaseName),
|
||||
LeaseDurationSeconds: pointer.Int32Ptr(3600),
|
||||
AcquireTime: &metav1.MicroTime{Time: acquireTime},
|
||||
RenewTime: &metav1.MicroTime{Time: acquireTime},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user