Skip to content

Commit

Permalink
Add BSL controller
Browse files Browse the repository at this point in the history
Signed-off-by: Carlisia <carlisia@vmware.com>
  • Loading branch information
Carlisia committed Jun 10, 2020
1 parent 0017553 commit 38715c8
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 65 deletions.
11 changes: 11 additions & 0 deletions config/crd/bases/velero.io_backupstoragelocations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ spec:
provider:
description: Provider is the provider of the backup storage.
type: string
validationFrequency:
description: StoreValidationFrequency defines how frequently to validate
the corresponding object storage. A value of 0 disables validation.
nullable: true
type: string
required:
- objectStorage
- provider
Expand Down Expand Up @@ -103,6 +108,12 @@ spec:
format: date-time
nullable: true
type: string
lastValidationTime:
description: LastValidationTime is the last time the backup store location
was validated the cluster.
format: date-time
nullable: true
type: string
phase:
description: Phase is the current state of the BackupStorageLocation.
enum:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/crds/crds.go

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
- velero.io
resources:
- backupstoragelocations
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- velero.io
resources:
- backupstoragelocations/status
verbs:
- get
- patch
- update
16 changes: 16 additions & 0 deletions pkg/apis/velero/v1/backupstoragelocation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type BackupStorageLocationSpec struct {
// +optional
// +nullable
BackupSyncPeriod *metav1.Duration `json:"backupSyncPeriod,omitempty"`

// StoreValidationFrequency defines how frequently to validate the corresponding object storage. A value of 0 disables validation.
// +optional
// +nullable
ValidationFrequency *metav1.Duration `json:"validationFrequency,omitempty"`
}

// BackupStorageLocationStatus defines the observed state of BackupStorageLocation
Expand All @@ -54,6 +59,12 @@ type BackupStorageLocationStatus struct {
// +nullable
LastSyncedTime *metav1.Time `json:"lastSyncedTime,omitempty"`

// LastValidationTime is the last time the backup store location was validated
// the cluster.
// +optional
// +nullable
LastValidationTime *metav1.Time `json:"lastValidationTime,omitempty"`

// LastSyncedRevision is the value of the `metadata/revision` file in the backup
// storage location the last time the BSL's contents were synced into the cluster.
//
Expand All @@ -75,6 +86,8 @@ type BackupStorageLocationStatus struct {
// +kubebuilder:storageversion
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="Backup Storage Location status such as Available/Unavailable"
// +kubebuilder:rbac:groups=velero.io,resources=backupstoragelocations,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=velero.io,resources=backupstoragelocations/status,verbs=get;update;patch

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -89,6 +102,8 @@ type BackupStorageLocation struct {
}

// +kubebuilder:object:root=true
// +kubebuilder:rbac:groups=velero.io,resources=backupstoragelocations,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=velero.io,resources=backupstoragelocations/status,verbs=get;update;patch

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down Expand Up @@ -121,6 +136,7 @@ type ObjectStorageLocation struct {

// BackupStorageLocationPhase is the lifecycle phase of a Velero BackupStorageLocation.
// +kubebuilder:validation:Enum=Available;Unavailable
// +kubebuilder:default=Unavailable
type BackupStorageLocationPhase string

const (
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/velero/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions pkg/backupstoragelocation/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package backupstoragelocation

import (
"time"

"github.com/vmware-tanzu/velero/pkg/persistence"

"github.com/sirupsen/logrus"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt"
)

type Processor struct {
DefaultStorageLocation string
DefaultStoreValidationFrequency time.Duration
NewPluginManager func(logrus.FieldLogger) clientmgmt.Manager
}

func (p *Processor) IsReadyToValidate(location *velerov1api.BackupStorageLocation, log logrus.FieldLogger) bool {
storeValidationFrequency := p.DefaultStoreValidationFrequency
// If the bsl validation frequency is not specifically set, skip this block and use the server's default
if location.Spec.ValidationFrequency != nil {
storeValidationFrequency = location.Spec.ValidationFrequency.Duration
if storeValidationFrequency == 0 {
log.Debug("Validation period for this backup location is set to 0, skipping validation")
return false
}

if storeValidationFrequency < 0 {
log.Debug("Validation period must be non-negative")
storeValidationFrequency = p.DefaultStoreValidationFrequency
}
}

lastValidation := location.Status.LastValidationTime
if lastValidation != nil {
nextValidation := lastValidation.Add(storeValidationFrequency)
if time.Now().UTC().Before(nextValidation) {
//location.Status.Phase = velerov1api.BackupStorageLocationPhaseUnverified
//c.updateCurrentTallyOfAvailability(location)
return false
}
}

return true
}

func (p *Processor) IsValidFor(location *velerov1api.BackupStorageLocation, log logrus.FieldLogger) error {
pluginManager := p.NewPluginManager(log)
defer pluginManager.CleanupClients()

var newBackupStore func(*velerov1api.BackupStorageLocation, persistence.ObjectStoreGetter, logrus.FieldLogger) (persistence.BackupStore, error)
newBackupStore = persistence.NewObjectBackupStore
backupStore, err := newBackupStore(location, pluginManager, log)
if err != nil {
return err
}

if err := backupStore.IsValid(); err != nil {
return err
}

return nil
}

0 comments on commit 38715c8

Please sign in to comment.