2017-08-02 17:27:17 +00:00
|
|
|
/*
|
2018-01-02 18:51:49 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-03 20:30:30 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2018-07-17 21:24:58 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-07-03 20:30:30 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-08-02 17:27:17 +00:00
|
|
|
core "k8s.io/client-go/testing"
|
|
|
|
|
2017-09-06 20:58:43 +00:00
|
|
|
"github.com/heptio/ark/pkg/apis/ark/v1"
|
2018-05-13 13:28:09 +00:00
|
|
|
cloudprovidermocks "github.com/heptio/ark/pkg/cloudprovider/mocks"
|
2017-10-25 16:42:03 +00:00
|
|
|
"github.com/heptio/ark/pkg/generated/clientset/versioned/fake"
|
2018-07-17 21:24:58 +00:00
|
|
|
informers "github.com/heptio/ark/pkg/generated/informers/externalversions"
|
2018-04-18 17:40:23 +00:00
|
|
|
"github.com/heptio/ark/pkg/util/stringslice"
|
2017-12-12 23:22:46 +00:00
|
|
|
arktest "github.com/heptio/ark/pkg/util/test"
|
2018-07-03 20:30:30 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
2017-09-06 20:58:43 +00:00
|
|
|
func TestBackupSyncControllerRun(t *testing.T) {
|
2017-08-02 17:27:17 +00:00
|
|
|
tests := []struct {
|
2018-05-13 13:28:09 +00:00
|
|
|
name string
|
|
|
|
listBackupsError error
|
|
|
|
cloudBackups []*v1.Backup
|
|
|
|
namespace string
|
|
|
|
existingBackups sets.String
|
2017-08-02 17:27:17 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no cloud backups",
|
|
|
|
},
|
|
|
|
{
|
2018-05-13 13:28:09 +00:00
|
|
|
name: "backup lister returns error on ListBackups",
|
|
|
|
listBackupsError: errors.New("listBackups"),
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normal case",
|
2017-09-06 20:58:43 +00:00
|
|
|
cloudBackups: []*v1.Backup{
|
2017-12-12 23:22:46 +00:00
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
2018-05-02 21:43:44 +00:00
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
2018-05-02 21:43:44 +00:00
|
|
|
namespace: "ns-1",
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
2018-04-18 17:40:23 +00:00
|
|
|
{
|
|
|
|
name: "Finalizer gets removed on sync",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithFinalizers(gcFinalizer).Backup,
|
|
|
|
},
|
2018-05-02 21:43:44 +00:00
|
|
|
namespace: "ns-1",
|
2018-04-18 17:40:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Only target finalizer is removed",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithFinalizers(gcFinalizer, "blah").Backup,
|
|
|
|
},
|
2018-05-02 21:43:44 +00:00
|
|
|
namespace: "ns-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "backups get created in Ark server's namespace",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-2").WithName("backup-2").Backup,
|
|
|
|
},
|
|
|
|
namespace: "heptio-ark",
|
2018-04-18 17:40:23 +00:00
|
|
|
},
|
2018-07-03 20:30:30 +00:00
|
|
|
{
|
|
|
|
name: "normal case with backups that already exist in Kubernetes",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
|
|
|
},
|
|
|
|
existingBackups: sets.NewString("backup-2", "backup-3"),
|
|
|
|
namespace: "ns-1",
|
|
|
|
},
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2017-09-14 21:27:31 +00:00
|
|
|
var (
|
2018-05-13 13:28:09 +00:00
|
|
|
backupLister = &cloudprovidermocks.BackupLister{}
|
2018-07-17 21:24:58 +00:00
|
|
|
client = fake.NewSimpleClientset()
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(client, 0)
|
|
|
|
logger = arktest.NewLogger()
|
2017-09-14 21:27:31 +00:00
|
|
|
)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
c := NewBackupSyncController(
|
|
|
|
client.ArkV1(),
|
2018-05-13 13:28:09 +00:00
|
|
|
backupLister,
|
2017-08-02 17:27:17 +00:00
|
|
|
"bucket",
|
|
|
|
time.Duration(0),
|
2018-05-02 21:43:44 +00:00
|
|
|
test.namespace,
|
2018-07-17 21:24:58 +00:00
|
|
|
sharedInformers.Ark().V1().Backups(),
|
2017-09-14 21:27:31 +00:00
|
|
|
logger,
|
2017-08-02 17:27:17 +00:00
|
|
|
).(*backupSyncController)
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
backupLister.On("ListBackups", "bucket").Return(test.cloudBackups, test.listBackupsError)
|
2017-09-06 20:58:43 +00:00
|
|
|
|
2017-08-02 17:27:17 +00:00
|
|
|
expectedActions := make([]core.Action, 0)
|
|
|
|
|
2018-07-03 20:30:30 +00:00
|
|
|
client.PrependReactor("get", "backups", func(action core.Action) (bool, runtime.Object, error) {
|
|
|
|
getAction := action.(core.GetAction)
|
|
|
|
if test.existingBackups.Has(getAction.GetName()) {
|
|
|
|
return true, nil, nil
|
|
|
|
}
|
|
|
|
// We return nil in place of the found backup object because
|
|
|
|
// we exclusively check for the error and don't use the object
|
|
|
|
// returned by the Get / Backups call.
|
|
|
|
return true, nil, apierrors.NewNotFound(v1.SchemeGroupVersion.WithResource("backups").GroupResource(), getAction.GetName())
|
|
|
|
})
|
|
|
|
|
|
|
|
c.run()
|
|
|
|
|
2017-08-02 17:27:17 +00:00
|
|
|
// we only expect creates for items within the target bucket
|
2017-09-06 20:58:43 +00:00
|
|
|
for _, cloudBackup := range test.cloudBackups {
|
2018-04-18 17:40:23 +00:00
|
|
|
// Verify that the run function stripped the GC finalizer
|
|
|
|
assert.False(t, stringslice.Has(cloudBackup.Finalizers, gcFinalizer))
|
2018-05-02 21:43:44 +00:00
|
|
|
assert.Equal(t, test.namespace, cloudBackup.Namespace)
|
2018-07-03 20:30:30 +00:00
|
|
|
|
|
|
|
actionGet := core.NewGetAction(
|
2017-09-06 20:58:43 +00:00
|
|
|
v1.SchemeGroupVersion.WithResource("backups"),
|
2018-05-02 21:43:44 +00:00
|
|
|
test.namespace,
|
2018-07-03 20:30:30 +00:00
|
|
|
cloudBackup.Name,
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
2018-07-03 20:30:30 +00:00
|
|
|
expectedActions = append(expectedActions, actionGet)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-07-03 20:30:30 +00:00
|
|
|
if test.existingBackups.Has(cloudBackup.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
actionCreate := core.NewCreateAction(
|
|
|
|
v1.SchemeGroupVersion.WithResource("backups"),
|
|
|
|
test.namespace,
|
|
|
|
cloudBackup,
|
|
|
|
)
|
|
|
|
expectedActions = append(expectedActions, actionCreate)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expectedActions, client.Actions())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 21:24:58 +00:00
|
|
|
|
|
|
|
func TestDeleteUnused(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cloudBackups []*v1.Backup
|
|
|
|
k8sBackups []*arktest.TestBackup
|
|
|
|
namespace string
|
|
|
|
expectedDeletes sets.String
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no overlapping backups",
|
|
|
|
namespace: "ns-1",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
|
|
|
},
|
|
|
|
k8sBackups: []*arktest.TestBackup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backupA").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backupB").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backupC").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
},
|
|
|
|
expectedDeletes: sets.NewString("backupA", "backupB", "backupC"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "some overlapping backups",
|
|
|
|
namespace: "ns-1",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
|
|
|
},
|
|
|
|
k8sBackups: []*arktest.TestBackup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backupC").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
},
|
|
|
|
expectedDeletes: sets.NewString("backupC"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all overlapping backups",
|
|
|
|
namespace: "ns-1",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
|
|
|
},
|
|
|
|
k8sBackups: []*arktest.TestBackup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
},
|
|
|
|
expectedDeletes: sets.NewString(),
|
|
|
|
},
|
2018-07-26 22:31:05 +00:00
|
|
|
{
|
|
|
|
name: "no overlapping backups but including backups that are not complete",
|
|
|
|
namespace: "ns-1",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
|
|
|
},
|
|
|
|
k8sBackups: []*arktest.TestBackup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backupA").WithPhase(v1.BackupPhaseCompleted),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("Deleting").WithPhase(v1.BackupPhaseDeleting),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("Failed").WithPhase(v1.BackupPhaseFailed),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("FailedValidation").WithPhase(v1.BackupPhaseFailedValidation),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("InProgress").WithPhase(v1.BackupPhaseInProgress),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("New").WithPhase(v1.BackupPhaseNew),
|
|
|
|
},
|
|
|
|
expectedDeletes: sets.NewString("backupA"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all overlapping backups and all backups that are not complete",
|
|
|
|
namespace: "ns-1",
|
|
|
|
cloudBackups: []*v1.Backup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").Backup,
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").Backup,
|
|
|
|
},
|
|
|
|
k8sBackups: []*arktest.TestBackup{
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-1").WithPhase(v1.BackupPhaseFailed),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-2").WithPhase(v1.BackupPhaseFailedValidation),
|
|
|
|
arktest.NewTestBackup().WithNamespace("ns-1").WithName("backup-3").WithPhase(v1.BackupPhaseInProgress),
|
|
|
|
},
|
|
|
|
expectedDeletes: sets.NewString(),
|
|
|
|
},
|
2018-07-17 21:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var (
|
2018-05-13 13:28:09 +00:00
|
|
|
backupLister = &cloudprovidermocks.BackupLister{}
|
2018-07-17 21:24:58 +00:00
|
|
|
client = fake.NewSimpleClientset()
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(client, 0)
|
|
|
|
logger = arktest.NewLogger()
|
|
|
|
)
|
|
|
|
|
|
|
|
c := NewBackupSyncController(
|
|
|
|
client.ArkV1(),
|
2018-05-13 13:28:09 +00:00
|
|
|
backupLister,
|
2018-07-17 21:24:58 +00:00
|
|
|
"bucket",
|
|
|
|
time.Duration(0),
|
|
|
|
test.namespace,
|
|
|
|
sharedInformers.Ark().V1().Backups(),
|
|
|
|
logger,
|
|
|
|
).(*backupSyncController)
|
|
|
|
|
|
|
|
expectedDeleteActions := make([]core.Action, 0)
|
|
|
|
|
|
|
|
// setup: insert backups into Kubernetes
|
|
|
|
for _, backup := range test.k8sBackups {
|
|
|
|
if test.expectedDeletes.Has(backup.Name) {
|
|
|
|
actionDelete := core.NewDeleteAction(
|
|
|
|
v1.SchemeGroupVersion.WithResource("backups"),
|
|
|
|
test.namespace,
|
|
|
|
backup.Name,
|
|
|
|
)
|
|
|
|
expectedDeleteActions = append(expectedDeleteActions, actionDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add test backup to informer:
|
|
|
|
err := sharedInformers.Ark().V1().Backups().Informer().GetStore().Add(backup.Backup)
|
|
|
|
assert.NoError(t, err, "Error adding backup to informer")
|
|
|
|
|
|
|
|
// add test backup to kubernetes:
|
|
|
|
_, err = client.Ark().Backups(test.namespace).Create(backup.Backup)
|
|
|
|
assert.NoError(t, err, "Error deleting from clientset")
|
|
|
|
}
|
|
|
|
|
|
|
|
// get names of client backups
|
|
|
|
testBackupNames := sets.NewString()
|
|
|
|
for _, cloudBackup := range test.cloudBackups {
|
|
|
|
testBackupNames.Insert(cloudBackup.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.deleteUnused(testBackupNames)
|
|
|
|
|
|
|
|
numBackups, err := numBackups(t, client, c.namespace)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expected := len(test.k8sBackups) - len(test.expectedDeletes)
|
|
|
|
assert.Equal(t, expected, numBackups)
|
|
|
|
|
|
|
|
arktest.CompareActions(t, expectedDeleteActions, getDeleteActions(client.Actions()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeleteActions(actions []core.Action) []core.Action {
|
|
|
|
var deleteActions []core.Action
|
|
|
|
for _, action := range actions {
|
|
|
|
if action.GetVerb() == "delete" {
|
|
|
|
deleteActions = append(deleteActions, action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deleteActions
|
|
|
|
}
|
|
|
|
|
|
|
|
func numBackups(t *testing.T, c *fake.Clientset, ns string) (int, error) {
|
|
|
|
t.Helper()
|
|
|
|
existingK8SBackups, err := c.ArkV1().Backups(ns).List(metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(existingK8SBackups.Items), nil
|
|
|
|
}
|