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 (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
kuberrs "k8s.io/apimachinery/pkg/api/errors"
|
2018-07-03 20:30:30 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2018-07-17 21:24:58 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-08-02 17:27:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2018-07-17 21:24:58 +00:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-07-26 22:31:05 +00:00
|
|
|
api "github.com/heptio/ark/pkg/apis/ark/v1"
|
2017-08-02 17:27:17 +00:00
|
|
|
"github.com/heptio/ark/pkg/cloudprovider"
|
2017-10-25 16:42:03 +00:00
|
|
|
arkv1client "github.com/heptio/ark/pkg/generated/clientset/versioned/typed/ark/v1"
|
2018-07-17 21:24:58 +00:00
|
|
|
informers "github.com/heptio/ark/pkg/generated/informers/externalversions/ark/v1"
|
|
|
|
listers "github.com/heptio/ark/pkg/generated/listers/ark/v1"
|
2017-09-14 21:27:31 +00:00
|
|
|
"github.com/heptio/ark/pkg/util/kube"
|
2018-04-18 17:40:23 +00:00
|
|
|
"github.com/heptio/ark/pkg/util/stringslice"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type backupSyncController struct {
|
2018-07-17 21:24:58 +00:00
|
|
|
client arkv1client.BackupsGetter
|
2018-05-13 13:28:09 +00:00
|
|
|
cloudBackupLister cloudprovider.BackupLister
|
2018-07-17 21:24:58 +00:00
|
|
|
bucket string
|
|
|
|
syncPeriod time.Duration
|
|
|
|
namespace string
|
|
|
|
backupLister listers.BackupLister
|
|
|
|
backupInformerSynced cache.InformerSynced
|
|
|
|
logger logrus.FieldLogger
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
func NewBackupSyncController(
|
|
|
|
client arkv1client.BackupsGetter,
|
2018-05-13 13:28:09 +00:00
|
|
|
cloudBackupLister cloudprovider.BackupLister,
|
2017-09-14 21:27:31 +00:00
|
|
|
bucket string,
|
|
|
|
syncPeriod time.Duration,
|
2018-05-02 21:43:44 +00:00
|
|
|
namespace string,
|
2018-07-17 21:24:58 +00:00
|
|
|
backupInformer informers.BackupInformer,
|
2017-12-12 23:22:46 +00:00
|
|
|
logger logrus.FieldLogger,
|
2017-09-14 21:27:31 +00:00
|
|
|
) Interface {
|
2017-08-02 17:27:17 +00:00
|
|
|
if syncPeriod < time.Minute {
|
2017-09-14 21:27:31 +00:00
|
|
|
logger.Infof("Provided backup sync period %v is too short. Setting to 1 minute", syncPeriod)
|
2017-08-02 17:27:17 +00:00
|
|
|
syncPeriod = time.Minute
|
|
|
|
}
|
|
|
|
return &backupSyncController{
|
2018-07-17 21:24:58 +00:00
|
|
|
client: client,
|
2018-05-13 13:28:09 +00:00
|
|
|
cloudBackupLister: cloudBackupLister,
|
2018-07-17 21:24:58 +00:00
|
|
|
bucket: bucket,
|
|
|
|
syncPeriod: syncPeriod,
|
|
|
|
namespace: namespace,
|
|
|
|
backupLister: backupInformer.Lister(),
|
|
|
|
backupInformerSynced: backupInformer.Informer().HasSynced,
|
|
|
|
logger: logger,
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run is a blocking function that continually runs the object storage -> Ark API
|
|
|
|
// sync process according to the controller's syncPeriod. It will return when it
|
|
|
|
// receives on the ctx.Done() channel.
|
|
|
|
func (c *backupSyncController) Run(ctx context.Context, workers int) error {
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("Running backup sync controller")
|
2018-07-17 21:24:58 +00:00
|
|
|
c.logger.Info("Waiting for caches to sync")
|
|
|
|
if !cache.WaitForCacheSync(ctx.Done(), c.backupInformerSynced) {
|
|
|
|
return errors.New("timed out waiting for caches to sync")
|
|
|
|
}
|
|
|
|
c.logger.Info("Caches are synced")
|
2017-08-02 17:27:17 +00:00
|
|
|
wait.Until(c.run, c.syncPeriod, ctx.Done())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:40:23 +00:00
|
|
|
const gcFinalizer = "gc.ark.heptio.com"
|
|
|
|
|
2017-08-02 17:27:17 +00:00
|
|
|
func (c *backupSyncController) run() {
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("Syncing backups from object storage")
|
2018-05-13 13:28:09 +00:00
|
|
|
backups, err := c.cloudBackupLister.ListBackups(c.bucket)
|
2017-08-02 17:27:17 +00:00
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.WithError(err).Error("error listing backups")
|
2017-08-02 17:27:17 +00:00
|
|
|
return
|
|
|
|
}
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.WithField("backupCount", len(backups)).Info("Got backups from object storage")
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-07-17 21:24:58 +00:00
|
|
|
cloudBackupNames := sets.NewString()
|
2017-08-02 17:27:17 +00:00
|
|
|
for _, cloudBackup := range backups {
|
2017-09-14 21:27:31 +00:00
|
|
|
logContext := c.logger.WithField("backup", kube.NamespaceAndName(cloudBackup))
|
|
|
|
logContext.Info("Syncing backup")
|
|
|
|
|
2018-07-17 21:24:58 +00:00
|
|
|
cloudBackupNames.Insert(cloudBackup.Name)
|
|
|
|
|
2018-04-18 17:40:23 +00:00
|
|
|
// If we're syncing backups made by pre-0.8.0 versions, the server removes all finalizers
|
|
|
|
// faster than the sync finishes. Just process them as we find them.
|
|
|
|
cloudBackup.Finalizers = stringslice.Except(cloudBackup.Finalizers, gcFinalizer)
|
|
|
|
|
2018-05-02 21:43:44 +00:00
|
|
|
cloudBackup.Namespace = c.namespace
|
2017-08-02 17:27:17 +00:00
|
|
|
cloudBackup.ResourceVersion = ""
|
2018-07-03 20:30:30 +00:00
|
|
|
|
|
|
|
// Backup only if backup does not exist in Kubernetes or if we are not able to get the backup for any reason.
|
|
|
|
_, err := c.client.Backups(cloudBackup.Namespace).Get(cloudBackup.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if !kuberrs.IsNotFound(err) {
|
|
|
|
logContext.WithError(errors.WithStack(err)).Error("Error getting backup from client, proceeding with backup sync")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.client.Backups(cloudBackup.Namespace).Create(cloudBackup); err != nil && !kuberrs.IsAlreadyExists(err) {
|
|
|
|
logContext.WithError(errors.WithStack(err)).Error("Error syncing backup from object storage")
|
|
|
|
}
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-17 21:24:58 +00:00
|
|
|
|
|
|
|
c.deleteUnused(cloudBackupNames)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:31:05 +00:00
|
|
|
// deleteUnused deletes backup objects from Kubernetes if they are complete
|
|
|
|
// and there is no corresponding backup in the object storage.
|
2018-07-17 21:24:58 +00:00
|
|
|
func (c *backupSyncController) deleteUnused(cloudBackupNames sets.String) {
|
|
|
|
// Backups objects in Kubernetes
|
|
|
|
backups, err := c.backupLister.Backups(c.namespace).List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
c.logger.WithError(errors.WithStack(err)).Error("Error listing backup from Kubernetes")
|
|
|
|
}
|
|
|
|
if len(backups) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:31:05 +00:00
|
|
|
// For each completed backup object in Kubernetes, delete it if it
|
|
|
|
// does not have a corresponding backup in object storage
|
2018-07-17 21:24:58 +00:00
|
|
|
for _, backup := range backups {
|
2018-07-26 22:31:05 +00:00
|
|
|
if backup.Status.Phase == api.BackupPhaseCompleted && !cloudBackupNames.Has(backup.Name) {
|
2018-07-17 21:24:58 +00:00
|
|
|
if err := c.client.Backups(backup.Namespace).Delete(backup.Name, nil); err != nil {
|
|
|
|
c.logger.WithError(errors.WithStack(err)).Error("Error deleting unused backup from Kubernetes")
|
|
|
|
} else {
|
|
|
|
c.logger.Debugf("Deleted backup: %s", backup.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|