Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backup): add option validation for backup creation #2702

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
BackupTimes int64 `json:"backupTimes"`
}

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,
BackupTimes: bv.Status.BackupTimes,
}
b.Actions = map[string]string{
"backupList": apiContext.UrlBuilder.ActionLink(b.Resource, "backupList"),
Expand Down
25 changes: 25 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,29 @@ 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)
}
}

backupTimes := 0
if backupVolume != nil {
backupTimes = int(backupVolume.BackupTimes)
}

if backupTimes%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"`

BackupTimes int64 `json:"backupTimes,omitempty" yaml:"backup_times,omitempty"`
}

type BackupVolumeCollection struct {
Expand Down
1 change: 1 addition & 0 deletions controller/backup_volume_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,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.BackupTimes = backupVolumeInfo.BackupTimes
return nil
}

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"`
BackupTimes int64 `json:"backupTimes"`
}

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-20240321072722-823fe2cd73ef
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-20240321072722-823fe2cd73ef h1:qoykZ7hwdDo36NyEYw+Azebq0dmBghOaayPtEBHwjTA=
github.com/ChanYiLin/backupstore v0.0.0-20240321072722-823fe2cd73ef/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
4 changes: 4 additions & 0 deletions k8s/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,10 @@ spec:
backingImageName:
description: The backing image name.
type: string
backupTimes:
description: The number of backups that have been created.
format: int64
type: integer
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
BackupTimes int64 `json:"backupTimes"`
}

// +genclient
Expand Down
4 changes: 4 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.

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-20240321072722-823fe2cd73ef
## explicit; go 1.21
github.com/longhorn/backupstore
github.com/longhorn/backupstore/backupbackingimage
Expand Down
46 changes: 46 additions & 0 deletions webhook/resources/backup/validator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package backup

import (
"fmt"
"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"

longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
)
Expand All @@ -28,3 +35,42 @@ 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 types.LonghornLabelVolumeAccessMode:
return nil
default:
return fmt.Errorf("%v:%v is not a valid option", label, value)
}

return nil
}