Skip to content

Commit

Permalink
Add BSL status phase based on validation
Browse files Browse the repository at this point in the history
Signed-off-by: Carlisia <carlisia@vmware.com>
  • Loading branch information
Carlisia committed May 1, 2020
1 parent 9db74ba commit 9e158f5
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 61 deletions.
98 changes: 37 additions & 61 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import (
clientset "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned"
informers "github.com/vmware-tanzu/velero/pkg/generated/informers/externalversions"
"github.com/vmware-tanzu/velero/pkg/metrics"
"github.com/vmware-tanzu/velero/pkg/persistence"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt"
"github.com/vmware-tanzu/velero/pkg/podexec"
"github.com/vmware-tanzu/velero/pkg/restic"
Expand All @@ -85,15 +84,16 @@ const (
defaultProfilerAddress = "localhost:6060"

// keys used to map out available controllers with disable-controllers flag
BackupControllerKey = "backup"
BackupSyncControllerKey = "backup-sync"
ScheduleControllerKey = "schedule"
GcControllerKey = "gc"
BackupDeletionControllerKey = "backup-deletion"
RestoreControllerKey = "restore"
DownloadRequestControllerKey = "download-request"
ResticRepoControllerKey = "restic-repo"
ServerStatusRequestControllerKey = "server-status-request"
BackupControllerKey = "backup"
BackupSyncControllerKey = "backup-sync"
ScheduleControllerKey = "schedule"
GcControllerKey = "gc"
BackupDeletionControllerKey = "backup-deletion"
RestoreControllerKey = "restore"
DownloadRequestControllerKey = "download-request"
ResticRepoControllerKey = "restic-repo"
ServerStatusRequestControllerKey = "server-status-request"
BackupStorageLocationControllerKey = "backup-storage-location"

defaultControllerWorkers = 1
// the default TTL for a backup
Expand All @@ -111,6 +111,7 @@ var disableControllerList = []string{
DownloadRequestControllerKey,
ResticRepoControllerKey,
ServerStatusRequestControllerKey,
BackupStorageLocationControllerKey,
}

type serverConfig struct {
Expand Down Expand Up @@ -333,15 +334,6 @@ func (s *server) run() error {
return err
}

if err := s.validateBackupStorageLocations(); err != nil {
return err
}

if _, err := s.veleroClient.VeleroV1().BackupStorageLocations(s.namespace).Get(s.config.defaultBackupLocation, metav1.GetOptions{}); err != nil {
s.logger.WithError(errors.WithStack(err)).
Warnf("A backup storage location named %s has been specified for the server to use by default, but no corresponding backup storage location exists. Backups with a location not matching the default will need to explicitly specify an existing location", s.config.defaultBackupLocation)
}

if err := s.initRestic(); err != nil {
return err
}
Expand Down Expand Up @@ -429,39 +421,6 @@ func (s *server) veleroResourcesExist() error {
return nil
}

// validateBackupStorageLocations checks to ensure all backup storage locations exist
// and have a compatible layout, and returns an error if not.
func (s *server) validateBackupStorageLocations() error {
s.logger.Info("Checking that all backup storage locations are valid")

pluginManager := clientmgmt.NewManager(s.logger, s.logLevel, s.pluginRegistry)
defer pluginManager.CleanupClients()

locations, err := s.veleroClient.VeleroV1().BackupStorageLocations(s.namespace).List(metav1.ListOptions{})
if err != nil {
return errors.WithStack(err)
}

var invalid []string
for _, location := range locations.Items {
backupStore, err := persistence.NewObjectBackupStore(&location, pluginManager, s.logger)
if err != nil {
invalid = append(invalid, errors.Wrapf(err, "error getting backup store for location %q", location.Name).Error())
continue
}

if err := backupStore.IsValid(); err != nil {
invalid = append(invalid, errors.Wrapf(err, "backup store for location %q is invalid", location.Name).Error())
}
}

if len(invalid) > 0 {
return errors.Errorf("some backup storage locations are invalid: %s", strings.Join(invalid, "; "))
}

return nil
}

// - Custom Resource Definitions come before Custom Resource so that they can be
// restored with their corresponding CRD.
// - Namespaces go second because all namespaced resources depend on them.
Expand Down Expand Up @@ -793,16 +752,33 @@ func (s *server) runControllers(defaultVolumeSnapshotLocations map[string]string
}
}

backupStorageLocationControllerRunInfo := func() controllerRunInfo {
backupStorageLocationController := controller.NewBackupStorageLocationController(
s.namespace,
s.config.defaultBackupLocation,
s.veleroClient.VeleroV1(),
s.sharedInformerFactory.Velero().V1().BackupStorageLocations().Lister(),
newPluginManager,
s.logger,
)

return controllerRunInfo{
controller: backupStorageLocationController,
numWorkers: defaultControllerWorkers,
}
}

enabledControllers := map[string]func() controllerRunInfo{
BackupSyncControllerKey: backupSyncControllerRunInfo,
BackupControllerKey: backupControllerRunInfo,
ScheduleControllerKey: scheduleControllerRunInfo,
GcControllerKey: gcControllerRunInfo,
BackupDeletionControllerKey: deletionControllerRunInfo,
RestoreControllerKey: restoreControllerRunInfo,
ResticRepoControllerKey: resticRepoControllerRunInfo,
DownloadRequestControllerKey: downloadrequestControllerRunInfo,
ServerStatusRequestControllerKey: serverStatusRequestControllerRunInfo,
BackupSyncControllerKey: backupSyncControllerRunInfo,
BackupControllerKey: backupControllerRunInfo,
ScheduleControllerKey: scheduleControllerRunInfo,
GcControllerKey: gcControllerRunInfo,
BackupDeletionControllerKey: deletionControllerRunInfo,
RestoreControllerKey: restoreControllerRunInfo,
ResticRepoControllerKey: resticRepoControllerRunInfo,
DownloadRequestControllerKey: downloadrequestControllerRunInfo,
ServerStatusRequestControllerKey: serverStatusRequestControllerRunInfo,
BackupStorageLocationControllerKey: backupStorageLocationControllerRunInfo,
}

if s.config.restoreOnly {
Expand Down
162 changes: 162 additions & 0 deletions pkg/controller/backup_storage_location_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright 2020 the Velero contributors.
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 (
"encoding/json"
"strings"
"time"

jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov1client "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1"
velerov1listers "github.com/vmware-tanzu/velero/pkg/generated/listers/velero/v1"
"github.com/vmware-tanzu/velero/pkg/persistence"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
)

type backupStorageLocationController struct {
*genericController

namespace string
defaultBackupLocation string
backupLocationClient velerov1client.BackupStorageLocationsGetter
backupStorageLocationLister velerov1listers.BackupStorageLocationLister
newPluginManager func(logrus.FieldLogger) clientmgmt.Manager
newBackupStore func(*velerov1api.BackupStorageLocation, persistence.ObjectStoreGetter, logrus.FieldLogger) (persistence.BackupStore, error)
}

func NewBackupStorageLocationController(
namespace string,
defaultBackupLocation string,
backupLocationClient velerov1client.BackupStorageLocationsGetter,
backupStorageLocationLister velerov1listers.BackupStorageLocationLister,
newPluginManager func(logrus.FieldLogger) clientmgmt.Manager,
logger logrus.FieldLogger,
) Interface {
c := backupStorageLocationController{
genericController: newGenericController("backup-storage-location", logger),
namespace: namespace,
defaultBackupLocation: defaultBackupLocation,
backupLocationClient: backupLocationClient,
backupStorageLocationLister: backupStorageLocationLister,

// use variables to refer to these functions so they can be
// replaced with fakes for testing.
newPluginManager: newPluginManager,
newBackupStore: persistence.NewObjectBackupStore,
}

c.resyncFunc = c.run
c.resyncPeriod = 30 * time.Second

return c
}

func (c *backupStorageLocationController) run() {
c.logger.Info("Checking that there is at least 1 backup storage location that is ready")

locations, err := c.backupStorageLocationLister.BackupStorageLocations(c.namespace).List(labels.Everything())
if err != nil {
c.logger.WithError(err).Error("Error listing backup storage locations, at least one available backup storage location is required")
return
}

if len(locations) == 0 {
c.logger.Error("No backup storage locations found, at least one available backup storage location is required")
return
}

pluginManager := c.newPluginManager(c.logger)
defer pluginManager.CleanupClients()

var unavailable []string
for _, location := range locations {
backupStore, err := c.newBackupStore(location, pluginManager, c.logger)
if err != nil {
unavailable = append(unavailable, errors.Wrapf(err, "error getting backup store for location %q", location.Name).Error())
continue
}

locationName := location.Name
if err := backupStore.IsValid(); err != nil {
// update status
_, err2 := c.patchBackupStorageLocation(location, func(r *v1.BackupStorageLocation) {
r.Status.Phase = velerov1api.BackupStorageLocationPhaseUnavailable
})
if err2 != nil {
unavailable = append(unavailable, errors.Wrapf(err2, "error updating backup storage for location %q to %s status", locationName, velerov1api.BackupStorageLocationPhaseUnavailable).Error())
} else {
unavailable = append(unavailable, errors.Wrapf(err, "location %q is unavailable", locationName).Error())
}
} else {
// update status
_, err := c.patchBackupStorageLocation(location, func(r *v1.BackupStorageLocation) {
r.Status.Phase = velerov1api.BackupStorageLocationPhaseAvailable
})
if err != nil {
unavailable = append(unavailable, errors.Wrapf(err, "error updating backup storage for location %q to %s status", locationName, velerov1api.BackupStorageLocationPhaseAvailable).Error())
}
}
}

if len(unavailable) == len(locations) { // no BSL available
c.logger.Errorf("There are no backup storage locations available, at least one available backup storage location is required: %s", strings.Join(unavailable, "; "))
} else if len(unavailable) > 0 { // some but not all BSL unavailable
for _, location := range locations {
if location.Name == c.defaultBackupLocation && location.Status.Phase == velerov1api.BackupStorageLocationPhaseUnavailable {
c.logger.Warnf("The specified default backup storage location named %q is unavailable; for convenience, be sure to configure it properly or make another location that is available the default", c.defaultBackupLocation)
break
}
}
c.logger.Warnf("Unavailable backup storage locations detected: %s", strings.Join(unavailable, "; "))
}
}

func (c backupStorageLocationController) patchBackupStorageLocation(req *velerov1api.BackupStorageLocation, mutate func(*v1.BackupStorageLocation)) (*velerov1api.BackupStorageLocation, error) {
// Record original json
oldData, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrap(err, "error marshalling original BackupStorageLocations")
}

// Mutate
mutate(req)

// Record new json
newData, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrap(err, "error marshalling updated BackupStorageLocations")
}

patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return nil, errors.Wrap(err, "error creating json merge patch for BackupStorageLocations")
}

bsl, err := c.backupLocationClient.BackupStorageLocations(req.Namespace).Patch(req.Name, types.MergePatchType, patchBytes)
if err != nil {
return nil, errors.Wrap(err, "error patching BackupStorageLocations")
}

return bsl, nil
}

0 comments on commit 9e158f5

Please sign in to comment.