2017-08-14 14:14:30 +00:00
|
|
|
/*
|
2018-01-02 18:51:49 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-08-14 14:14:30 +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"
|
2017-12-11 22:10:52 +00:00
|
|
|
"encoding/json"
|
2017-08-14 14:14:30 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-05-14 21:34:24 +00:00
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
2017-09-14 21:27:31 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2017-08-14 14:14:30 +00:00
|
|
|
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2017-12-11 22:10:52 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-08-14 14:14:30 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
|
|
|
|
|
|
|
"github.com/heptio/ark/pkg/apis/ark/v1"
|
|
|
|
"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"
|
2017-08-14 14:14:30 +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"
|
2017-08-14 14:14:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type downloadRequestController struct {
|
|
|
|
downloadRequestClient arkv1client.DownloadRequestsGetter
|
|
|
|
downloadRequestLister listers.DownloadRequestLister
|
|
|
|
downloadRequestListerSynced cache.InformerSynced
|
2018-04-02 22:34:19 +00:00
|
|
|
restoreLister listers.RestoreLister
|
|
|
|
restoreListerSynced cache.InformerSynced
|
2017-09-14 21:27:31 +00:00
|
|
|
backupService cloudprovider.BackupService
|
|
|
|
bucket string
|
|
|
|
syncHandler func(key string) error
|
|
|
|
queue workqueue.RateLimitingInterface
|
|
|
|
clock clock.Clock
|
2017-12-12 23:22:46 +00:00
|
|
|
logger logrus.FieldLogger
|
2017-08-14 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDownloadRequestController creates a new DownloadRequestController.
|
|
|
|
func NewDownloadRequestController(
|
|
|
|
downloadRequestClient arkv1client.DownloadRequestsGetter,
|
|
|
|
downloadRequestInformer informers.DownloadRequestInformer,
|
2018-04-02 22:34:19 +00:00
|
|
|
restoreInformer informers.RestoreInformer,
|
2017-08-14 14:14:30 +00:00
|
|
|
backupService cloudprovider.BackupService,
|
|
|
|
bucket string,
|
2017-12-12 23:22:46 +00:00
|
|
|
logger logrus.FieldLogger,
|
2017-08-14 14:14:30 +00:00
|
|
|
) Interface {
|
|
|
|
c := &downloadRequestController{
|
|
|
|
downloadRequestClient: downloadRequestClient,
|
|
|
|
downloadRequestLister: downloadRequestInformer.Lister(),
|
|
|
|
downloadRequestListerSynced: downloadRequestInformer.Informer().HasSynced,
|
2018-04-02 22:34:19 +00:00
|
|
|
restoreLister: restoreInformer.Lister(),
|
|
|
|
restoreListerSynced: restoreInformer.Informer().HasSynced,
|
2017-09-14 21:27:31 +00:00
|
|
|
backupService: backupService,
|
|
|
|
bucket: bucket,
|
|
|
|
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "downloadrequest"),
|
|
|
|
clock: &clock.RealClock{},
|
|
|
|
logger: logger,
|
2017-08-14 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.syncHandler = c.processDownloadRequest
|
|
|
|
|
|
|
|
downloadRequestInformer.Informer().AddEventHandler(
|
|
|
|
cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(obj interface{}) {
|
|
|
|
key, err := cache.MetaNamespaceKeyFunc(obj)
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
downloadRequest := obj.(*v1.DownloadRequest)
|
|
|
|
c.logger.WithError(errors.WithStack(err)).
|
|
|
|
WithField("downloadRequest", downloadRequest.Name).
|
|
|
|
Error("Error creating queue key, item not added to queue")
|
2017-08-14 14:14:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.queue.Add(key)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run is a blocking function that runs the specified number of worker goroutines
|
|
|
|
// to process items in the work queue. It will return when it receives on the
|
|
|
|
// ctx.Done() channel.
|
|
|
|
func (c *downloadRequestController) Run(ctx context.Context, numWorkers int) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
defer func() {
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("Waiting for workers to finish their work")
|
2017-08-14 14:14:30 +00:00
|
|
|
|
|
|
|
c.queue.ShutDown()
|
|
|
|
|
|
|
|
// We have to wait here in the deferred function instead of at the bottom of the function body
|
|
|
|
// because we have to shut down the queue in order for the workers to shut down gracefully, and
|
|
|
|
// we want to shut down the queue via defer and not at the end of the body.
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("All workers have finished")
|
2017-08-14 14:14:30 +00:00
|
|
|
}()
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("Starting DownloadRequestController")
|
|
|
|
defer c.logger.Info("Shutting down DownloadRequestController")
|
2017-08-14 14:14:30 +00:00
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("Waiting for caches to sync")
|
2018-04-02 22:34:19 +00:00
|
|
|
if !cache.WaitForCacheSync(ctx.Done(), c.downloadRequestListerSynced, c.restoreListerSynced) {
|
2017-08-14 14:14:30 +00:00
|
|
|
return errors.New("timed out waiting for caches to sync")
|
|
|
|
}
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.Info("Caches are synced")
|
2017-08-14 14:14:30 +00:00
|
|
|
|
|
|
|
wg.Add(numWorkers)
|
|
|
|
for i := 0; i < numWorkers; i++ {
|
|
|
|
go func() {
|
|
|
|
wait.Until(c.runWorker, time.Second, ctx.Done())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
wait.Until(c.resync, time.Minute, ctx.Done())
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runWorker runs a worker until the controller's queue indicates it's time to shut down.
|
|
|
|
func (c *downloadRequestController) runWorker() {
|
|
|
|
// continually take items off the queue (waits if it's
|
|
|
|
// empty) until we get a shutdown signal from the queue
|
|
|
|
for c.processNextWorkItem() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processNextWorkItem processes a single item from the queue.
|
|
|
|
func (c *downloadRequestController) processNextWorkItem() bool {
|
|
|
|
key, quit := c.queue.Get()
|
|
|
|
if quit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// always call done on this item, since if it fails we'll add
|
|
|
|
// it back with rate-limiting below
|
|
|
|
defer c.queue.Done(key)
|
|
|
|
|
|
|
|
err := c.syncHandler(key.(string))
|
|
|
|
if err == nil {
|
|
|
|
// If you had no error, tell the queue to stop tracking history for your key. This will reset
|
|
|
|
// things like failure counts for per-item rate limiting.
|
|
|
|
c.queue.Forget(key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.WithError(err).WithField("key", key).Error("Error in syncHandler, re-adding item to queue")
|
|
|
|
|
2017-08-14 14:14:30 +00:00
|
|
|
// we had an error processing the item so add it back
|
|
|
|
// into the queue for re-processing with rate-limiting
|
|
|
|
c.queue.AddRateLimited(key)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// processDownloadRequest is the default per-item sync handler. It generates a pre-signed URL for
|
|
|
|
// a new DownloadRequest or deletes the DownloadRequest if it has expired.
|
|
|
|
func (c *downloadRequestController) processDownloadRequest(key string) error {
|
2017-09-14 21:27:31 +00:00
|
|
|
logContext := c.logger.WithField("key", key)
|
|
|
|
|
|
|
|
logContext.Debug("Running processDownloadRequest")
|
2017-08-14 14:14:30 +00:00
|
|
|
ns, name, err := cache.SplitMetaNamespaceKey(key)
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
return errors.Wrap(err, "error splitting queue key")
|
2017-08-14 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
downloadRequest, err := c.downloadRequestLister.DownloadRequests(ns).Get(name)
|
|
|
|
if apierrors.IsNotFound(err) {
|
2017-09-14 21:27:31 +00:00
|
|
|
logContext.Debug("Unable to find DownloadRequest")
|
2017-08-14 14:14:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
return errors.Wrap(err, "error getting DownloadRequest")
|
2017-08-14 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch downloadRequest.Status.Phase {
|
|
|
|
case "", v1.DownloadRequestPhaseNew:
|
|
|
|
return c.generatePreSignedURL(downloadRequest)
|
|
|
|
case v1.DownloadRequestPhaseProcessed:
|
|
|
|
return c.deleteIfExpired(downloadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const signedURLTTL = 10 * time.Minute
|
|
|
|
|
|
|
|
// generatePreSignedURL generates a pre-signed URL for downloadRequest, changes the phase to
|
|
|
|
// Processed, and persists the changes to storage.
|
|
|
|
func (c *downloadRequestController) generatePreSignedURL(downloadRequest *v1.DownloadRequest) error {
|
2017-10-25 16:57:40 +00:00
|
|
|
update := downloadRequest.DeepCopy()
|
2017-09-12 19:54:08 +00:00
|
|
|
|
2018-04-02 22:34:19 +00:00
|
|
|
var (
|
|
|
|
directory string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch downloadRequest.Spec.Target.Kind {
|
|
|
|
case v1.DownloadTargetKindRestoreLog, v1.DownloadTargetKindRestoreResults:
|
|
|
|
restore, err := c.restoreLister.Restores(downloadRequest.Namespace).Get(downloadRequest.Spec.Target.Name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error getting Restore")
|
|
|
|
}
|
|
|
|
|
|
|
|
directory = restore.Spec.BackupName
|
|
|
|
default:
|
|
|
|
directory = downloadRequest.Spec.Target.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
update.Status.DownloadURL, err = c.backupService.CreateSignedURL(downloadRequest.Spec.Target, c.bucket, directory, signedURLTTL)
|
2017-09-12 19:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
update.Status.Phase = v1.DownloadRequestPhaseProcessed
|
|
|
|
update.Status.Expiration = metav1.NewTime(c.clock.Now().Add(signedURLTTL))
|
|
|
|
|
2017-12-11 22:10:52 +00:00
|
|
|
_, err = patchDownloadRequest(downloadRequest, update, c.downloadRequestClient)
|
2017-09-14 21:27:31 +00:00
|
|
|
return errors.WithStack(err)
|
2017-08-14 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteIfExpired deletes downloadRequest if it has expired.
|
|
|
|
func (c *downloadRequestController) deleteIfExpired(downloadRequest *v1.DownloadRequest) error {
|
2017-09-14 21:27:31 +00:00
|
|
|
logContext := c.logger.WithField("key", kube.NamespaceAndName(downloadRequest))
|
|
|
|
logContext.Info("checking for expiration of DownloadRequest")
|
2017-08-14 14:14:30 +00:00
|
|
|
if downloadRequest.Status.Expiration.Time.Before(c.clock.Now()) {
|
2017-09-14 21:27:31 +00:00
|
|
|
logContext.Debug("DownloadRequest has not expired")
|
2017-08-14 14:14:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
logContext.Debug("DownloadRequest has expired - deleting")
|
|
|
|
return errors.WithStack(c.downloadRequestClient.DownloadRequests(downloadRequest.Namespace).Delete(downloadRequest.Name, nil))
|
2017-08-14 14:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resync requeues all the DownloadRequests in the lister's cache. This is mostly to handle deleting
|
|
|
|
// any expired requests that were not deleted as part of the normal client flow for whatever reason.
|
|
|
|
func (c *downloadRequestController) resync() {
|
|
|
|
list, err := c.downloadRequestLister.List(labels.Everything())
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.WithError(errors.WithStack(err)).Error("error listing download requests")
|
2017-08-14 14:14:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dr := range list {
|
|
|
|
key, err := cache.MetaNamespaceKeyFunc(dr)
|
|
|
|
if err != nil {
|
2017-09-14 21:27:31 +00:00
|
|
|
c.logger.WithError(errors.WithStack(err)).WithField("downloadRequest", dr.Name).Error("error generating key for download request")
|
2017-08-14 14:14:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.queue.Add(key)
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 22:10:52 +00:00
|
|
|
|
|
|
|
func patchDownloadRequest(original, updated *v1.DownloadRequest, client arkv1client.DownloadRequestsGetter) (*v1.DownloadRequest, error) {
|
|
|
|
origBytes, err := json.Marshal(original)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshalling original download request")
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedBytes, err := json.Marshal(updated)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error marshalling updated download request")
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:34:24 +00:00
|
|
|
patchBytes, err := jsonpatch.CreateMergePatch(origBytes, updatedBytes)
|
2017-12-11 22:10:52 +00:00
|
|
|
if err != nil {
|
2018-05-14 21:34:24 +00:00
|
|
|
return nil, errors.Wrap(err, "error creating json merge patch for download request")
|
2017-12-11 22:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 14:43:44 +00:00
|
|
|
res, err := client.DownloadRequests(original.Namespace).Patch(original.Name, types.MergePatchType, patchBytes)
|
2017-12-11 22:10:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error patching download request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|