Skip to content

Commit

Permalink
PVC changes + additionalPrinterColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
mrajashree committed Aug 28, 2020
1 parent a553429 commit fa5fe85
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 107 deletions.
30 changes: 27 additions & 3 deletions crds/backup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ kind: CustomResourceDefinition
metadata:
name: backups.resources.cattle.io
spec:
additionalPrinterColumns:
- JSONPath: .status.storageLocation
name: Storage-Location
type: string
- JSONPath: .status.backupType
name: Backup-Type
type: string
- JSONPath: .status.numSnapshots
name: Backups-Saved
type: string
- JSONPath: .status.prefix
name: Backupfile-Prefix
type: string
- JSONPath: .status.conditions[?(@.type=="Ready")].message
name: Status
type: string
group: resources.cattle.io
names:
kind: Backup
Expand All @@ -16,12 +32,12 @@ spec:
spec:
properties:
encryptionConfigName:
description: 'Name of secret containing the encryption config, must
description: 'Name of the Secret containing the encryption config, must
be in the namespace of the chart: cattle-resources-system'
type: string
resourceSetName:
description: Name of resourceSet CR to use for backup, must be in the
same namespace
description: Name of the ResourceSet CR to use for backup, must be in
the same namespace
type: string
retentionCount:
type: integer
Expand Down Expand Up @@ -55,6 +71,8 @@ spec:
type: object
status:
properties:
backupType:
type: string
conditions:
items:
properties:
Expand All @@ -73,6 +91,8 @@ spec:
type: object
nullable: true
type: array
filename:
type: string
lastSnapshotTs:
type: string
nextSnapshotAt:
Expand All @@ -81,6 +101,10 @@ spec:
type: integer
observedGeneration:
type: integer
prefix:
type: string
storageLocation:
type: string
summary:
type: string
type: object
Expand Down
35 changes: 0 additions & 35 deletions crds/faketest.yaml

This file was deleted.

15 changes: 14 additions & 1 deletion crds/restore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ kind: CustomResourceDefinition
metadata:
name: restores.resources.cattle.io
spec:
additionalPrinterColumns:
- JSONPath: .status.numRetries
name: Retries
type: string
- JSONPath: .status.backupSource
name: Backup-Source
type: string
- JSONPath: .status.conditions[?(@.type=="Ready")].message
name: Status
type: string
group: resources.cattle.io
names:
kind: Restore
Expand All @@ -18,7 +28,8 @@ spec:
backupFilename:
type: string
deleteTimeout:
maximum: 5
maximum: 10
deleteTimeoutSeconds:
type: integer
encryptionConfigName:
type: string
Expand Down Expand Up @@ -52,6 +63,8 @@ spec:
type: object
status:
properties:
backupSource:
type: string
conditions:
items:
properties:
Expand Down
5 changes: 1 addition & 4 deletions examples/create-deflocation-backup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ apiVersion: resources.cattle.io/v1
kind: Backup
metadata:
name: test-default-location-recurring-backup
namespace: default
spec:
resourceSetName: ecm-resource-set
encryptionConfigName: test-encryptionconfig
schedule: "@every 1m"
retention: "4m"
encryptionConfigName: test-encryptionconfig
8 changes: 8 additions & 0 deletions examples/create-deflocation-restore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: resources.cattle.io/v1
kind: Restore
metadata:
name: restore-s3-demo
namespace: default
spec:
backupFilename: backup-ns-test-default-location-recurring-backup-6a9c60bf-73ff-461f-93d8-506f174b1d65-2020-08-28T00#39#05Z.tar.gz
encryptionConfigName: test-encryptionconfig
10 changes: 0 additions & 10 deletions examples/create-local-backup.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions examples/create-local-recurring-backup.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions examples/create-local-restore.yaml

This file was deleted.

1 change: 0 additions & 1 deletion examples/create-s3-backup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apiVersion: resources.cattle.io/v1
kind: Backup
metadata:
name: s3-backup-demo
namespace: default
spec:
storageLocation:
s3:
Expand Down
2 changes: 1 addition & 1 deletion examples/create-s3-restore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: restore-s3-demo
namespace: default
spec:
backupFilename: default-s3-backup-demo-84bf8dd8-0ef3-4240-8ad1-fc7ec308e216-2020-08-24T12#49#25-07#00.tar.gz
backupFilename: default-s3-backup-demo-df56e4f4-d5f6-4b9d-8a78-b8c89757df30-2020-08-27T09#59#53-07#00.tar.gz
storageLocation:
s3:
credentialSecretName: creds
Expand Down
16 changes: 2 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
func init() {
flag.StringVar(&KubeConfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.Parse()
OperatorPVCName = os.Getenv("DEFAULT_PVC_BACKUP_STORAGE_LOCATION")
OperatorPVCName = os.Getenv("DEFAULT_PVC_NAME")
OperatorS3BackupStorageLocation = os.Getenv("DEFAULT_S3_BACKUP_STORAGE_LOCATION")
ChartNamespace = os.Getenv("CHART_NAMESPACE")
}
Expand Down Expand Up @@ -107,26 +107,14 @@ func main() {
logrus.Fatalf("Error setting default location %v: %v", dmPath, err)
}
logrus.Infof("No temporary backup location provided, saving backups at %v", dmPath)
OperatorPVCName = dmPath
defaultMountPath = dmPath
}
} else {
// else, this log tells user that each backup needs to contain StorageLocation details
logrus.Infof("No PVC or S3 details provided for storing backups by default. User must specify storageLocation" +
"on each Backup CR")
// TODO: add to the operator status,
// TODO: add printer columns for backup indicate storageLocation
// TODO: document this, and add to chart notes
}
} else if OperatorPVCName != "" {
// Get the PVC using the name, and it should be in the chart's namespace
pvc, err := core.Core().V1().PersistentVolumeClaim().Get(ChartNamespace, OperatorPVCName, k8sv1.GetOptions{})
if err != nil {
logrus.Fatalf("Error getting pvc details %v: %v", OperatorPVCName, err)
}
// pvc must have only one access mode, that is RWO
if !(len(pvc.Spec.AccessModes) == 1 && pvc.Spec.AccessModes[0] == "ReadWriteOnce") {
logrus.Fatalf("PVC %v access mode must be ReadWriteOnce", OperatorPVCName)
}
defaultMountPath = LocalBackupStorageLocation
} else if OperatorS3BackupStorageLocation != "" {
// read the secret from chart's namespace, with OperatorS3BackupStorageLocation as the name
Expand Down
9 changes: 7 additions & 2 deletions pkg/apis/resources.cattle.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ type BackupStatus struct {
NextSnapshotAt string `json:"nextSnapshotAt"`
NumSnapshots int `json:"numSnapshots"`
ObservedGeneration int64 `json:"observedGeneration"`
StorageLocation string `json:"storageLocation"`
BackupType string `json:"backupType"`
Filename string `json:"filename"`
Prefix string `json:"prefix"`
Summary string `json:"summary"`
}

Expand Down Expand Up @@ -103,7 +107,7 @@ type RestoreSpec struct {
BackupFilename string `json:"backupFilename"`
StorageLocation *StorageLocation `json:"storageLocation"`
Prune *bool `json:"prune"` //prune by default
DeleteTimeout int `json:"deleteTimeout,omitempty"`
DeleteTimeoutSeconds int `json:"deleteTimeoutSeconds,omitempty"`
EncryptionConfigName string `json:"encryptionConfigName,omitempty"`
}

Expand All @@ -112,5 +116,6 @@ type RestoreStatus struct {
RestoreCompletionTS string `json:"restoreCompletionTs"`
ObservedGeneration int64 `json:"observedGeneration"`
NumRetries int `json:"numRetries"`
Summary string `json:"summary,omitempty"`
BackupSource string `json:"backupSource"`
Summary string `json:"summary"`
}
23 changes: 17 additions & 6 deletions pkg/controllers/backup/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (h *handler) OnBackupChange(_ string, backup *v1.Backup) (*v1.Backup, error
}
}

backupFileName, err := h.generateBackupFilename(backup)
backupFileName, prefix, err := h.generateBackupFilename(backup)
if err != nil {
return h.setReconcilingCondition(backup, err)
}
Expand Down Expand Up @@ -148,14 +148,15 @@ func (h *handler) OnBackupChange(_ string, backup *v1.Backup) (*v1.Backup, error
return h.setReconcilingCondition(backup, err)
}
}

storageLocationType := backup.Status.StorageLocation
updateErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
var err error
backup, err = h.backups.Get(backup.Namespace, backup.Name, k8sv1.GetOptions{})
if err != nil {
return err
}

condition.Cond(v1.BackupConditionReady).SetStatusBool(backup, true)
condition.Cond(v1.BackupConditionReady).Message(backup, "Completed")
condition.Cond(v1.BackupConditionUploaded).SetStatusBool(backup, true)
backup.Status.LastSnapshotTS = time.Now().Format(time.RFC3339)
backup.Status.NumSnapshots++
Expand All @@ -164,9 +165,14 @@ func (h *handler) OnBackupChange(_ string, backup *v1.Backup) (*v1.Backup, error
backup.Status.NextSnapshotAt = nextBackupAt.Format(time.RFC3339)
after := nextBackupAt.Sub(time.Now().Round(time.Minute))
h.backups.EnqueueAfter(backup.Namespace, backup.Name, after)
backup.Status.BackupType = "Recurring"
} else {
backup.Status.BackupType = "One-time"
}
backup.Status.ObservedGeneration = backup.Generation

backup.Status.StorageLocation = storageLocationType
backup.Status.Filename = backupFileName + ".tar.gz"
backup.Status.Prefix = prefix
_, err = h.backups.UpdateStatus(backup)
return err
})
Expand Down Expand Up @@ -248,28 +254,32 @@ func (h *handler) performBackup(backup *v1.Backup, tmpBackupPath, backupFileName
if err := CreateTarAndGzip(tmpBackupPath, h.defaultBackupMountPath, gzipFile, backup.Name); err != nil {
return err
}
backup.Status.StorageLocation = util.PVCBackup
} else if h.defaultS3BackupLocation != nil {
// not checking for nil, since if this wasn't provided, the default local location would get used
if err := h.uploadToS3(backup, h.defaultS3BackupLocation, util.ChartNamespace, tmpBackupPath, gzipFile); err != nil {
return err
}
backup.Status.StorageLocation = util.S3Backup
} else {
return fmt.Errorf("backup %v needs to specify S3 details, or configure storage location at the operator level", backup.Name)
}
} else if storageLocation.S3 != nil {
backup.Status.StorageLocation = util.S3Backup
if err := h.uploadToS3(backup, storageLocation.S3, backup.Namespace, tmpBackupPath, gzipFile); err != nil {
return err
}
}
return nil
}

func (h *handler) generateBackupFilename(backup *v1.Backup) (string, error) {
func (h *handler) generateBackupFilename(backup *v1.Backup) (string, string, error) {
currSnapshotTS := time.Now().Format(time.RFC3339)
// on OS X writing file with `:` converts colon to forward slash
currTSForFilename := strings.Replace(currSnapshotTS, ":", "#", -1)
backupFileName := fmt.Sprintf("%s-%s-%s-%s", backup.Namespace, backup.Name, h.kubeSystemNS, currTSForFilename)
return backupFileName, nil
prefix := fmt.Sprintf("%s-%s-%s", backup.Namespace, backup.Name, h.kubeSystemNS)
return backupFileName, prefix, nil
}

// https://github.com/kubernetes-sigs/cli-utils/tree/master/pkg/kstatus
Expand All @@ -284,6 +294,7 @@ func (h *handler) setReconcilingCondition(backup *v1.Backup, originalErr error)

condition.Cond(v1.BackupConditionReconciling).SetStatusBool(updBackup, true)
condition.Cond(v1.BackupConditionReconciling).SetError(updBackup, "", originalErr)
condition.Cond(v1.BackupConditionReady).Message(updBackup, "Retrying")

_, err = h.backups.UpdateStatus(updBackup)
return err
Expand Down
Loading

0 comments on commit fa5fe85

Please sign in to comment.