Skip to content

Commit

Permalink
feat(backup): add option validation for backup creation
Browse files Browse the repository at this point in the history
ref: longhorn/longhorn 7070

Signed-off-by: Jack Lin <jack.lin@suse.com>
  • Loading branch information
ChanYiLin committed Mar 26, 2024
1 parent 340c10a commit 97fecb4
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 9 deletions.
2 changes: 2 additions & 0 deletions api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type BackupVolume struct {
BackingImageName string `json:"backingImageName"`
BackingImageChecksum string `json:"backingImageChecksum"`
StorageClassName string `json:"storageClassName"`
BackupCount string `json:"backupCount"`
}

type Backup struct {
Expand Down Expand Up @@ -1742,6 +1743,7 @@ func toBackupVolumeResource(bv *longhorn.BackupVolume, apiContext *api.ApiContex
BackingImageName: bv.Status.BackingImageName,
BackingImageChecksum: bv.Status.BackingImageChecksum,
StorageClassName: bv.Status.StorageClassName,
BackupCount: bv.Status.BackupCount,
}
b.Actions = map[string]string{
"backupList": apiContext.UrlBuilder.ActionLink(b.Resource, "backupList"),
Expand Down
28 changes: 28 additions & 0 deletions app/recurring_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/util"

btypes "github.com/longhorn/backupstore/types"
longhornclient "github.com/longhorn/longhorn-manager/client"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -611,6 +613,32 @@ func (job *Job) doRecurringBackup() (err error) {
return err
}

if intervalStr, exists := job.labels[types.GetLonghornLabelKey(btypes.LonghornBackupOptionFullBackupInterval)]; exists {
interval, err := strconv.Atoi(intervalStr)
if err != nil {
return errors.Wrapf(err, "interval %v is not number", intervalStr)
}

backupVolume, err := job.api.BackupVolume.ById(job.volumeName)
if err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to get backup volume %v", job.volumeName)
}
}

backupCount := 0
if backupVolume != nil && backupVolume.BackupCount != "" {
backupCount, err = strconv.Atoi(backupVolume.BackupCount)
if err != nil {
return errors.Wrapf(err, "backup count %v is not number", backupVolume.BackupCount)
}
}

if backupCount%interval == 0 {
job.labels[types.GetLonghornLabelKey(btypes.LonghornBackupOptionBackupMode)] = btypes.LonghornBackupModeFull
}
}

if _, err := job.api.Volume.ActionSnapshotBackup(volume, &longhornclient.SnapshotInput{
Labels: job.labels,
Name: job.snapshotName,
Expand Down
2 changes: 2 additions & 0 deletions client/generated_backup_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type BackupVolume struct {
Size string `json:"size,omitempty" yaml:"size,omitempty"`

StorageClassName string `json:"storageClassName,omitempty" yaml:"storage_class_name,omitempty"`

BackupCount string `json:"backupCount,omitempty" yaml:"backup_count,omitempty"`
}

type BackupVolumeCollection struct {
Expand Down
3 changes: 3 additions & 0 deletions controller/backup_volume_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ func (bvc *BackupVolumeController) reconcile(backupVolumeName string) (err error
return nil
}

logrus.Infof("[DEBUG]: backupVolumeInfo: %v", backupVolumeInfo)

// Update the Backup CR spec.syncRequestAt to request the
// backup_controller to reconcile the Backup CR if the last backup changed
if backupVolume.Status.LastBackupName != backupVolumeInfo.LastBackupName {
Expand All @@ -408,6 +410,7 @@ func (bvc *BackupVolumeController) reconcile(backupVolumeName string) (err error
backupVolume.Status.BackingImageChecksum = backupVolumeInfo.BackingImageChecksum
backupVolume.Status.StorageClassName = backupVolumeInfo.StorageClassName
backupVolume.Status.LastSyncedAt = syncTime
backupVolume.Status.BackupCount = backupVolumeInfo.BackupCount
return nil
}

Expand Down
1 change: 1 addition & 0 deletions engineapi/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (btc *BackupTargetClient) BackupVolumeDelete(destURL, volumeName string, cr
// parseBackupVolumeConfig parses a backup volume config
func parseBackupVolumeConfig(output string) (*BackupVolume, error) {
backupVolume := new(BackupVolume)
logrus.Infof("[DEBUG] parseBackupVolumeConfig output: %v", output)
if err := json.Unmarshal([]byte(output), backupVolume); err != nil {
return nil, errors.Wrapf(err, "error parsing one backup volume config: \n%s", output)
}
Expand Down
1 change: 1 addition & 0 deletions engineapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type BackupVolume struct {
BackingImageName string `json:"backingImageName"`
BackingImageChecksum string `json:"backingImageChecksum"`
StorageClassName string `json:"storageClassName"`
BackupCount string `json:"backupCount"`
}

type Backup struct {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/longhorn/longhorn-manager
go 1.21

replace (
github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77 => github.com/ChanYiLin/backupstore v0.0.0-20240322024320-ab6fa5ee775f
k8s.io/api => k8s.io/api v0.28.5
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.28.5
k8s.io/apimachinery => k8s.io/apimachinery v0.28.5
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ChanYiLin/backupstore v0.0.0-20240322024320-ab6fa5ee775f h1:H3+X1DZVKbxMIqeFQsnVGznAjOHJSEAvFSW8ZdNjfqM=
github.com/ChanYiLin/backupstore v0.0.0-20240322024320-ab6fa5ee775f/go.mod h1:4cbJWtlrD2cGTQxQLtdlPTYopiJiusXH7CpOBrn/s3k=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
Expand Down Expand Up @@ -1045,14 +1047,10 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhn
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/longhorn/backing-image-manager v1.6.0 h1:Jmlc8+W63l0VZoVhPwNLniAk+eBC4CNaadoqpqA51KE=
github.com/longhorn/backing-image-manager v1.6.0/go.mod h1:IH0mgbK+Dr13xkY+LhDaufyd9YIpiKqYo1AeRLFYGrk=
github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77 h1:iJRq59kA22f9HIjFtY/lz5rKCorZJrrYXju70XoWdmE=
github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77/go.mod h1:4cbJWtlrD2cGTQxQLtdlPTYopiJiusXH7CpOBrn/s3k=
github.com/longhorn/go-common-libs v0.0.0-20240307063052-6e77996eda29 h1:tyzIDCMjQGQzhqAtdJaeEMAaNUZJD/sHERXp+tYc+ms=
github.com/longhorn/go-common-libs v0.0.0-20240307063052-6e77996eda29/go.mod h1:ePLGb2r/PJBUIVoVhLOt4bLOeu0S72ZB+fWDWwC8H28=
github.com/longhorn/go-iscsi-helper v0.0.0-20240308033847-bc3aab599425 h1:koSD52H0VkzJAh3OIZCdgQ9mqoRXklkeuhqmuwQ1WzU=
github.com/longhorn/go-iscsi-helper v0.0.0-20240308033847-bc3aab599425/go.mod h1:2aM6KBix3Khd56I4rihOBOOPOm0/YvYMjtr1KNclQsI=
github.com/longhorn/go-spdk-helper v0.0.0-20240301101140-6eb6aa5fc09d h1:vajqcFlGHmyQcqhBbGMRo33GNcrKMgNY8ca87rGuKnU=
github.com/longhorn/go-spdk-helper v0.0.0-20240301101140-6eb6aa5fc09d/go.mod h1:Zv0UpwuqZpijy5/vSW2PvR8zVPBX8CJPF3XeZAuc4Ek=
github.com/longhorn/go-spdk-helper v0.0.0-20240308030201-9b252d6f7250 h1:bc9BtfvSuXvmztYiBCebvPWPxakbiCWBvydbeqat+Ek=
github.com/longhorn/go-spdk-helper v0.0.0-20240308030201-9b252d6f7250/go.mod h1:re2QHb6FUU9G/CL3AnXDbzFjLNPwmweBvnTfeVpkZ1E=
github.com/longhorn/longhorn-engine v1.6.0 h1:6CH2vvwCgFBIGW4TegcI79CL1Ego1nvLZIC3ioRjjdM=
Expand Down Expand Up @@ -2231,8 +2229,6 @@ k8s.io/mount-utils v0.28.5/go.mod h1:ceMAZ+Nzlk8zOwN205YXXGJRGmf1o0/XIwsKnG44p0I
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0gQBEuevE/AaBsHY=
k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
Expand Down
3 changes: 3 additions & 0 deletions k8s/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,9 @@ spec:
backingImageName:
description: The backing image name.
type: string
backupCount:
description: The number of backups that have been created.
type: string
createdAt:
description: The backup volume creation time.
type: string
Expand Down
3 changes: 3 additions & 0 deletions k8s/pkg/apis/longhorn/v1beta2/backupvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type BackupVolumeStatus struct {
// +optional
// +nullable
LastSyncedAt metav1.Time `json:"lastSyncedAt"`
// The number of backups that have been created.
// +optional
BackupCount string `json:"backupCount"`
}

// +genclient
Expand Down
7 changes: 7 additions & 0 deletions vendor/github.com/longhorn/backupstore/backupstore.go

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

17 changes: 15 additions & 2 deletions vendor/github.com/longhorn/backupstore/deltablock.go

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

1 change: 1 addition & 0 deletions vendor/github.com/longhorn/backupstore/inspect.go

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

1 change: 1 addition & 0 deletions vendor/github.com/longhorn/backupstore/list.go

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

16 changes: 16 additions & 0 deletions vendor/github.com/longhorn/backupstore/types/types.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ github.com/longhorn/backing-image-manager/pkg/meta
github.com/longhorn/backing-image-manager/pkg/rpc
github.com/longhorn/backing-image-manager/pkg/types
github.com/longhorn/backing-image-manager/pkg/util
# github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77
# github.com/longhorn/backupstore v0.0.0-20240219094812-3a87ee02df77 => github.com/ChanYiLin/backupstore v0.0.0-20240322024320-ab6fa5ee775f
## explicit; go 1.21
github.com/longhorn/backupstore
github.com/longhorn/backupstore/backupbackingimage
Expand Down
53 changes: 53 additions & 0 deletions webhook/resources/backup/validator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package backup

import (
"fmt"
"strconv"
"strings"

admissionregv1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/runtime"

btypes "github.com/longhorn/backupstore/types"
"github.com/longhorn/longhorn-manager/datastore"
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/webhook/admission"
werror "github.com/longhorn/longhorn-manager/webhook/error"
"github.com/pkg/errors"

longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
)
Expand All @@ -28,3 +37,47 @@ func (b *backupValidator) Resource() admission.Resource {
OperationTypes: []admissionregv1.OperationType{},
}
}

func (b *backupValidator) Create(request *admission.Request, newObj runtime.Object) error {
backup, ok := newObj.(*longhorn.Backup)
if !ok {
return werror.NewInvalidError(fmt.Sprintf("%v is not a *longhorn.Backup", newObj), "")
}

if backup.Spec.Labels != nil {
for key, value := range backup.Spec.Labels {
if !strings.HasPrefix(key, types.LonghornLabelKeyPrefix) {
continue
}
if err := validateBackupOption(key, value); err != nil {
return werror.NewInvalidError(err.Error(), "")
}
}
}

return nil

}

func validateBackupOption(label, value string) error {
option := label[strings.LastIndex(label, "/")+1:]

switch option {
case btypes.LonghornBackupOptionBackupMode:
if value != btypes.LonghornBackupModeFull &&
value != btypes.LonghornBackupModeIncremental {
return fmt.Errorf("%v:%v is not a valid option", label, value)
}
case btypes.LonghornBackupOptionFullBackupInterval:
_, err := strconv.Atoi(value)
if err != nil {
return errors.Wrapf(err, "%v:%v is not number", label, value)
}
case types.LonghornLabelVolumeAccessMode:
return nil
default:
return fmt.Errorf("%v:%v is not a valid option", label, value)
}

return nil
}

0 comments on commit 97fecb4

Please sign in to comment.