2021-01-13 01:27:41 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-12-11 23:21:34 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/crud"
|
2021-06-22 15:23:19 +00:00
|
|
|
"go.pinniped.dev/test/testlib"
|
2020-12-11 23:21:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageGarbageCollection(t *testing.T) {
|
|
|
|
// Run this test in parallel with the other integration tests because it does a lot of waiting
|
|
|
|
// and will not impact other tests, or be impacted by other tests, when run in parallel.
|
|
|
|
t.Parallel()
|
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
env := testlib.IntegrationEnv(t)
|
|
|
|
client := testlib.NewKubernetesClientset(t)
|
2020-12-11 23:21:34 +00:00
|
|
|
secrets := client.CoreV1().Secrets(env.SupervisorNamespace)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secretAlreadyExpired := createSecret(ctx, t, secrets, "past", time.Now().Add(-time.Second))
|
|
|
|
secretWhichWillExpireBeforeTheTestEnds := createSecret(ctx, t, secrets, "near-future", time.Now().Add(30*time.Second))
|
|
|
|
secretNotYetExpired := createSecret(ctx, t, secrets, "far-future", time.Now().Add(10*time.Minute))
|
|
|
|
|
|
|
|
// Start a background goroutine which will end as soon as the test ends.
|
2020-12-18 17:36:28 +00:00
|
|
|
// Keep updating a secret which has the "storage.pinniped.dev/garbage-collect-after" annotation
|
|
|
|
// in the same namespace just to get the controller to respond faster.
|
|
|
|
// This is just a performance optimization to make this test pass faster because otherwise
|
|
|
|
// this test has to wait ~3 minutes for the controller's next full-resync.
|
2021-05-04 16:38:47 +00:00
|
|
|
stopCh := make(chan struct{})
|
|
|
|
errCh := make(chan error)
|
|
|
|
go updateSecretEveryTwoSeconds(stopCh, errCh, secrets, secretNotYetExpired)
|
2020-12-11 23:21:34 +00:00
|
|
|
t.Cleanup(func() {
|
2021-05-04 16:38:47 +00:00
|
|
|
close(stopCh)
|
|
|
|
|
|
|
|
if updateErr := <-errCh; updateErr != nil {
|
|
|
|
panic(updateErr)
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Wait long enough for the next periodic sweep of the GC controller for the secrets to be deleted, which
|
|
|
|
// is the worst-case length of time that we should ever need to wait. Because of the goroutine above,
|
|
|
|
// in practice we should only need to wait about 30 seconds, which is the GC controller's self-imposed
|
|
|
|
// rate throttling time period.
|
|
|
|
slightlyLongerThanGCControllerFullResyncPeriod := 3*time.Minute + 30*time.Second
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
|
2021-06-16 22:51:23 +00:00
|
|
|
_, err := secrets.Get(ctx, secretAlreadyExpired.Name, metav1.GetOptions{})
|
|
|
|
requireEventually.Truef(k8serrors.IsNotFound(err), "wanted a NotFound error but got %v", err)
|
|
|
|
}, slightlyLongerThanGCControllerFullResyncPeriod, 250*time.Millisecond)
|
|
|
|
|
2021-06-22 15:23:19 +00:00
|
|
|
testlib.RequireEventually(t, func(requireEventually *require.Assertions) {
|
2021-06-16 22:51:23 +00:00
|
|
|
_, err := secrets.Get(ctx, secretWhichWillExpireBeforeTheTestEnds.Name, metav1.GetOptions{})
|
|
|
|
requireEventually.Truef(k8serrors.IsNotFound(err), "wanted a NotFound error but got %v", err)
|
|
|
|
}, slightlyLongerThanGCControllerFullResyncPeriod, 250*time.Millisecond)
|
2020-12-11 23:21:34 +00:00
|
|
|
|
|
|
|
// The unexpired secret should not have been deleted within the timeframe of this test run.
|
2021-06-16 22:51:23 +00:00
|
|
|
_, err := secrets.Get(ctx, secretNotYetExpired.Name, metav1.GetOptions{})
|
2020-12-11 23:21:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:38:47 +00:00
|
|
|
func updateSecretEveryTwoSeconds(stopCh chan struct{}, errCh chan error, secrets corev1client.SecretInterface, secret *v1.Secret) {
|
2020-12-11 23:21:34 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-05-04 16:38:47 +00:00
|
|
|
var updateErr error
|
|
|
|
defer func() {
|
|
|
|
errCh <- updateErr
|
|
|
|
}()
|
|
|
|
|
2020-12-11 23:21:34 +00:00
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
// Got a signal, so stop running.
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Channel had no message, so keep running.
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
i++
|
2020-12-18 17:36:28 +00:00
|
|
|
secret.Data["foo"] = []byte(fmt.Sprintf("bar-%d", i))
|
|
|
|
secret, updateErr = secrets.Update(ctx, secret, metav1.UpdateOptions{})
|
2021-05-04 16:38:47 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case updateErr == nil:
|
|
|
|
// continue to next update
|
|
|
|
|
|
|
|
case k8serrors.IsConflict(updateErr), k8serrors.IsNotFound(updateErr):
|
|
|
|
select {
|
|
|
|
case _, ok := <-stopCh:
|
|
|
|
if !ok { // stopCh is closed meaning that test is already finished so these errors are expected
|
|
|
|
updateErr = nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return // even if the error is expected, we must stop
|
|
|
|
default:
|
|
|
|
return // unexpected error
|
|
|
|
}
|
2020-12-11 23:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSecret(ctx context.Context, t *testing.T, secrets corev1client.SecretInterface, name string, expiresAt time.Time) *v1.Secret {
|
|
|
|
secret, err := secrets.Create(ctx, newSecret("pinniped-storage-gc-integration-test-"+name+"-", expiresAt), metav1.CreateOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Make sure the Secret is deleted when the test ends.
|
|
|
|
t.Cleanup(func() {
|
2021-03-05 01:25:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2020-12-11 23:21:34 +00:00
|
|
|
defer cancel()
|
|
|
|
err := secrets.Delete(ctx, secret.Name, metav1.DeleteOptions{})
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if !notFound {
|
|
|
|
// it's okay if the Secret was already deleted, but other errors are cleanup failures
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSecret(namePrefix string, expiresAt time.Time) *v1.Secret {
|
|
|
|
annotations := map[string]string{}
|
|
|
|
if !expiresAt.Equal(time.Time{}) {
|
|
|
|
// Mark the secret for garbage collection.
|
|
|
|
annotations[crud.SecretLifetimeAnnotationKey] = expiresAt.UTC().Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
return &v1.Secret{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
GenerateName: namePrefix,
|
|
|
|
Annotations: annotations,
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{"some-key": []byte("fake-data")},
|
2020-12-18 17:36:28 +00:00
|
|
|
Type: "storage.pinniped.dev/gc-test-integration-test", // the garbage collector controller doesn't care about the type
|
2020-12-11 23:21:34 +00:00
|
|
|
}
|
|
|
|
}
|