Skip to content

Commit

Permalink
Merge pull request #6484 from kaovilai/unkopia-for-plugins
Browse files Browse the repository at this point in the history
Move kopia logging from util/logging to remove indirect dependency in velero plugins
  • Loading branch information
Lyndon-Li authored and danfengliu committed Jul 19, 2023
2 parents 017d6ce + 0572287 commit f2321a1
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 42 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/6484-kaovilai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Velero Plugins no longer need kopia indirect dependency in their go.mod
4 changes: 2 additions & 2 deletions pkg/util/logging/kopia_log.go → pkg/kopia/kopia_log.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package kopia

/*
Copyright the Velero contributors.
Expand All @@ -14,8 +16,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package logging

import (
"context"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package logging
package kopia

import (
"testing"
Expand Down
28 changes: 14 additions & 14 deletions pkg/repository/udmrepo/kopialib/lib_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/vmware-tanzu/velero/pkg/kopia"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/util/logging"
)

type kopiaRepoService struct {
Expand Down Expand Up @@ -91,7 +91,7 @@ func NewKopiaRepoService(logger logrus.FieldLogger) udmrepo.BackupRepoService {
}

func (ks *kopiaRepoService) Init(ctx context.Context, repoOption udmrepo.RepoOptions, createNew bool) error {
repoCtx := logging.SetupKopiaLog(ctx, ks.logger)
repoCtx := kopia.SetupKopiaLog(ctx, ks.logger)

if createNew {
if err := CreateBackupRepo(repoCtx, repoOption); err != nil {
Expand All @@ -113,7 +113,7 @@ func (ks *kopiaRepoService) Open(ctx context.Context, repoOption udmrepo.RepoOpt
return nil, errors.Wrapf(err, "repo config %s doesn't exist", repoConfig)
}

repoCtx := logging.SetupKopiaLog(ctx, ks.logger)
repoCtx := kopia.SetupKopiaLog(ctx, ks.logger)

r, err := openKopiaRepo(repoCtx, repoConfig, repoOption.RepoPassword)
if err != nil {
Expand Down Expand Up @@ -156,7 +156,7 @@ func (ks *kopiaRepoService) Maintain(ctx context.Context, repoOption udmrepo.Rep
return errors.Wrapf(err, "repo config %s doesn't exist", repoConfig)
}

repoCtx := logging.SetupKopiaLog(ctx, ks.logger)
repoCtx := kopia.SetupKopiaLog(ctx, ks.logger)

r, err := openKopiaRepo(repoCtx, repoConfig, repoOption.RepoPassword)
if err != nil {
Expand Down Expand Up @@ -206,7 +206,7 @@ func (ks *kopiaRepoService) DefaultMaintenanceFrequency() time.Duration {
}

func (km *kopiaMaintenance) runMaintenance(ctx context.Context, rep repo.DirectRepositoryWriter) error {
err := snapshotmaintenance.Run(logging.SetupKopiaLog(ctx, km.logger), rep, km.mode, false, maintenance.SafetyFull)
err := snapshotmaintenance.Run(kopia.SetupKopiaLog(ctx, km.logger), rep, km.mode, false, maintenance.SafetyFull)
if err != nil {
return errors.Wrapf(err, "error to run maintenance under mode %s", km.mode)
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func (kr *kopiaRepository) OpenObject(ctx context.Context, id udmrepo.ID) (udmre
return nil, errors.Wrapf(err, "error to parse object ID from %v", id)
}

reader, err := kr.rawRepo.OpenObject(logging.SetupKopiaLog(ctx, kr.logger), objID)
reader, err := kr.rawRepo.OpenObject(kopia.SetupKopiaLog(ctx, kr.logger), objID)
if err != nil {
return nil, errors.Wrap(err, "error to open object")
}
Expand All @@ -253,7 +253,7 @@ func (kr *kopiaRepository) GetManifest(ctx context.Context, id udmrepo.ID, mani
return errors.New("repo is closed or not open")
}

metadata, err := kr.rawRepo.GetManifest(logging.SetupKopiaLog(ctx, kr.logger), manifest.ID(id), mani.Payload)
metadata, err := kr.rawRepo.GetManifest(kopia.SetupKopiaLog(ctx, kr.logger), manifest.ID(id), mani.Payload)
if err != nil {
return errors.Wrap(err, "error to get manifest")
}
Expand All @@ -268,7 +268,7 @@ func (kr *kopiaRepository) FindManifests(ctx context.Context, filter udmrepo.Man
return nil, errors.New("repo is closed or not open")
}

metadata, err := kr.rawRepo.FindManifests(logging.SetupKopiaLog(ctx, kr.logger), filter.Labels)
metadata, err := kr.rawRepo.FindManifests(kopia.SetupKopiaLog(ctx, kr.logger), filter.Labels)
if err != nil {
return nil, errors.Wrap(err, "error to find manifests")
}
Expand All @@ -286,7 +286,7 @@ func (kr *kopiaRepository) Time() time.Time {

func (kr *kopiaRepository) Close(ctx context.Context) error {
if kr.rawWriter != nil {
err := kr.rawWriter.Close(logging.SetupKopiaLog(ctx, kr.logger))
err := kr.rawWriter.Close(kopia.SetupKopiaLog(ctx, kr.logger))
if err != nil {
return errors.Wrap(err, "error to close repo writer")
}
Expand All @@ -295,7 +295,7 @@ func (kr *kopiaRepository) Close(ctx context.Context) error {
}

if kr.rawRepo != nil {
err := kr.rawRepo.Close(logging.SetupKopiaLog(ctx, kr.logger))
err := kr.rawRepo.Close(kopia.SetupKopiaLog(ctx, kr.logger))
if err != nil {
return errors.Wrap(err, "error to close repo")
}
Expand All @@ -311,7 +311,7 @@ func (kr *kopiaRepository) NewObjectWriter(ctx context.Context, opt udmrepo.Obje
return nil
}

writer := kr.rawWriter.NewObjectWriter(logging.SetupKopiaLog(ctx, kr.logger), object.WriterOptions{
writer := kr.rawWriter.NewObjectWriter(kopia.SetupKopiaLog(ctx, kr.logger), object.WriterOptions{
Description: opt.Description,
Prefix: index.IDPrefix(opt.Prefix),
AsyncWrites: opt.AsyncWrites,
Expand All @@ -332,7 +332,7 @@ func (kr *kopiaRepository) PutManifest(ctx context.Context, manifest udmrepo.Rep
return "", errors.New("repo writer is closed or not open")
}

id, err := kr.rawWriter.PutManifest(logging.SetupKopiaLog(ctx, kr.logger), manifest.Metadata.Labels, manifest.Payload)
id, err := kr.rawWriter.PutManifest(kopia.SetupKopiaLog(ctx, kr.logger), manifest.Metadata.Labels, manifest.Payload)
if err != nil {
return "", errors.Wrap(err, "error to put manifest")
}
Expand All @@ -345,7 +345,7 @@ func (kr *kopiaRepository) DeleteManifest(ctx context.Context, id udmrepo.ID) er
return errors.New("repo writer is closed or not open")
}

err := kr.rawWriter.DeleteManifest(logging.SetupKopiaLog(ctx, kr.logger), manifest.ID(id))
err := kr.rawWriter.DeleteManifest(kopia.SetupKopiaLog(ctx, kr.logger), manifest.ID(id))
if err != nil {
return errors.Wrap(err, "error to delete manifest")
}
Expand All @@ -358,7 +358,7 @@ func (kr *kopiaRepository) Flush(ctx context.Context) error {
return errors.New("repo writer is closed or not open")
}

err := kr.rawWriter.Flush(logging.SetupKopiaLog(ctx, kr.logger))
err := kr.rawWriter.Flush(kopia.SetupKopiaLog(ctx, kr.logger))
if err != nil {
return errors.Wrap(err, "error to flush repo")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/uploader/kopia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (

"github.com/sirupsen/logrus"

"github.com/vmware-tanzu/velero/pkg/kopia"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/uploader"
"github.com/vmware-tanzu/velero/pkg/util/logging"

"github.com/kopia/kopia/fs"
"github.com/kopia/kopia/fs/localfs"
Expand Down Expand Up @@ -121,7 +121,7 @@ func Backup(ctx context.Context, fsUploader SnapshotUploader, repoWriter repo.Re
return nil, false, errors.Wrap(err, "Unable to get local filesystem entry")
}

kopiaCtx := logging.SetupKopiaLog(ctx, log)
kopiaCtx := kopia.SetupKopiaLog(ctx, log)
snapID, snapshotSize, err := SnapshotSource(kopiaCtx, repoWriter, fsUploader, sourceInfo, rootDir, forceFull, parentSnapshot, tags, log, "Kopia Uploader")
if err != nil {
return nil, false, err
Expand Down Expand Up @@ -306,7 +306,7 @@ func findPreviousSnapshotManifest(ctx context.Context, rep repo.Repository, sour
func Restore(ctx context.Context, rep repo.RepositoryWriter, progress *Progress, snapshotID, dest string, log logrus.FieldLogger, cancleCh chan struct{}) (int64, int32, error) {
log.Info("Start to restore...")

kopiaCtx := logging.SetupKopiaLog(ctx, log)
kopiaCtx := kopia.SetupKopiaLog(ctx, log)

snapshot, err := snapshot.LoadSnapshot(kopiaCtx, rep, manifest.ID(snapshotID))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func BackupRestoreTest(useVolumeSnapshots bool) {

Expect(VeleroInstall(context.Background(), &veleroCfg, false)).To(Succeed())
}
Expect(VeleroAddPluginsForProvider(context.TODO(), veleroCfg.VeleroCLI, veleroCfg.VeleroNamespace, veleroCfg.AdditionalBSLProvider)).To(Succeed())
Expect(VeleroAddPluginsForProvider(context.TODO(), veleroCfg.VeleroCLI, veleroCfg.VeleroNamespace, veleroCfg.AdditionalBSLProvider, []string{veleroCfg.AddBSLPlugins})).To(Succeed())

// Create Secret for additional BSL
secretName := fmt.Sprintf("bsl-credentials-%s", UUIDgen)
Expand Down
26 changes: 22 additions & 4 deletions test/e2e/basic/storage-class-changing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type StorageClasssChanging struct {
volume string
podName string
mappedNS string
replica int32
}

const SCCBaseName string = "scc-"
Expand Down Expand Up @@ -78,10 +79,27 @@ func (s *StorageClasssChanging) CreateResources() error {
fmt.Sprintf("Failed to create namespace %s", s.namespace))
})

By(fmt.Sprintf("Create pod %s in namespace %s", s.podName, s.namespace), func() {
_, err := CreatePod(s.Client, s.namespace, s.podName, s.srcStorageClass, "", []string{s.volume}, nil, nil)
Expect(err).To(Succeed())
})
// By(fmt.Sprintf("Create pod %s in namespace %s", s.podName, s.namespace), func() {
// _, err := CreatePod(s.Client, s.namespace, s.podName, s.srcStorageClass, "", []string{s.volume}, nil, nil)
// Expect(err).To(Succeed())
// })

//Create deployment
fmt.Printf("Creating deployment in namespaces ...%s\n", s.namespace)

// VolumeSource: v1.VolumeSource{
// PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
// ClaimName: "aaa",
// },
// },

deployment := NewDeployment(s.CaseBaseName, s.namespace, s.replica, s.labels, nil).Result()
deployment, err := CreateDeployment(s.Client.ClientGo, s.namespace, deployment)
Expect(err).To(Succeed(), fmt.Sprintf("failed to delete the namespace %q", s.namespace))

err = WaitForReadyDeployment(s.Client.ClientGo, s.namespace, deployment.Name)
Expect(err).To(Succeed(), fmt.Sprintf("failed to delete the namespace %q", s.namespace))

By(fmt.Sprintf("Create ConfigMap %s in namespace %s", s.configmaptName, s.VeleroCfg.VeleroNamespace), func() {
_, err := CreateConfigMap(s.Client.ClientGo, s.VeleroCfg.VeleroNamespace, s.configmaptName, s.labels, s.data)
Expect(err).To(Succeed(), fmt.Sprintf("failed to create configmap in the namespace %q", s.VeleroCfg.VeleroNamespace))
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/bsl-mgmt/deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func BslDeletionTest(useVolumeSnapshots bool) {
}

By(fmt.Sprintf("Add an additional plugin for provider %s", veleroCfg.AdditionalBSLProvider), func() {
Expect(VeleroAddPluginsForProvider(context.TODO(), veleroCfg.VeleroCLI, veleroCfg.VeleroNamespace, veleroCfg.AdditionalBSLProvider)).To(Succeed())
Expect(VeleroAddPluginsForProvider(context.TODO(), veleroCfg.VeleroCLI, veleroCfg.VeleroNamespace, veleroCfg.AdditionalBSLProvider, []string{veleroCfg.AddBSLPlugins})).To(Succeed())
})

additionalBsl := fmt.Sprintf("bsl-%s", UUIDgen)
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func MigrationTest(useVolumeSnapshots bool, veleroCLI2Version VeleroCLI2Version)
OriginVeleroCfg.UseVolumeSnapshots = useVolumeSnapshots
OriginVeleroCfg.UseNodeAgent = !useVolumeSnapshots

version, err := GetVeleroVersion(oneHourTimeout, OriginVeleroCfg.VeleroCLI, true)
Expect(err).To(Succeed(), "Fail to get Velero version")
OriginVeleroCfg.VeleroVersion = version

// self represents v1.12
if veleroCLI2Version.VeleroVersion == "self" {
if OriginVeleroCfg.SnapshotMoveData {
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func BackupUpgradeRestoreTest(useVolumeSnapshots bool, veleroCLI2Version VeleroC
veleroCfg.GCFrequency = ""
By(fmt.Sprintf("Install the expected old version Velero (%s) for upgrade",
veleroCLI2Version.VeleroVersion), func() {

//Set VeleroImage and RestoreHelperImage to blank
//VeleroImage and RestoreHelperImage should be the default value in originalCli
tmpCfgForOldVeleroInstall := veleroCfg
Expand All @@ -128,6 +129,10 @@ func BackupUpgradeRestoreTest(useVolumeSnapshots bool, veleroCLI2Version VeleroC
tmpCfgForOldVeleroInstall.RestoreHelperImage = ""
tmpCfgForOldVeleroInstall.Plugins = ""
tmpCfgForOldVeleroInstall.UploaderType = ""
version, err := GetVeleroVersion(oneHourTimeout, tmpCfgForOldVeleroInstall.VeleroCLI, true)
Expect(err).To(Succeed(), "Fail to get Velero version")
tmpCfgForOldVeleroInstall.VeleroVersion = version

if supportUploaderType {
tmpCfgForOldVeleroInstall.UseRestic = false
tmpCfgForOldVeleroInstall.UseNodeAgent = !useVolumeSnapshots
Expand Down
51 changes: 48 additions & 3 deletions test/e2e/util/csi/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ func GetCsiSnapshotHandle(client TestClient, backupName string) ([]string, error
}
return snapshotHandleList, nil
}
func GetCsiSnapshotHandleV1(client TestClient, backupName string) ([]string, error) {
_, snapshotClient, err := GetClients()
if err != nil {
return nil, err
}
vscList, err1 := snapshotClient.SnapshotV1().VolumeSnapshotContents().List(context.TODO(), metav1.ListOptions{})
if err1 != nil {
return nil, err
}

var snapshotHandleList []string
for _, i := range vscList.Items {
if i.Status == nil {
fmt.Println("SnapshotHandle Status s nil")
continue
}
if i.Status.SnapshotHandle == nil {
fmt.Println("SnapshotHandle is nil")
continue
}

if i.Labels == nil {
fmt.Println("VolumeSnapshotContents label is nil")
continue
}

if i.Labels["velero.io/backup-name"] == backupName {
tmp := strings.Split(*i.Status.SnapshotHandle, "/")
snapshotHandleList = append(snapshotHandleList, tmp[len(tmp)-1])
}
}

if len(snapshotHandleList) == 0 {
fmt.Printf("No VolumeSnapshotContent from backup %s", backupName)
}
return snapshotHandleList, nil
}
func GetVolumeSnapshotContentNameByPod(client TestClient, podName, namespace, backupName string) (string, error) {
pvcList, err := GetPvcByPodName(context.Background(), namespace, podName)
if err != nil {
Expand Down Expand Up @@ -128,11 +165,19 @@ func GetVolumeSnapshotContentNameByPod(client TestClient, podName, namespace, ba
return "", errors.New(fmt.Sprintf("Fail to get VolumeSnapshotContentName for pod %s under namespace %s", podName, namespace))
}

func CheckVolumeSnapshotCR(client TestClient, backupName string, expectedCount int) ([]string, error) {
func CheckVolumeSnapshotCR(client TestClient, backupName string, expectedCount int, apiVersion string) ([]string, error) {
var err error
var snapshotContentNameList []string
if snapshotContentNameList, err = GetCsiSnapshotHandle(client, backupName); err != nil {
return nil, errors.Wrap(err, "Fail to get Azure CSI snapshot content")
if apiVersion == "v1beta1" {
if snapshotContentNameList, err = GetCsiSnapshotHandle(client, backupName); err != nil {
return nil, errors.Wrap(err, "Fail to get Azure CSI snapshot content")
}
} else if apiVersion == "v1" {
if snapshotContentNameList, err = GetCsiSnapshotHandleV1(client, backupName); err != nil {
return nil, errors.Wrap(err, "Fail to get Azure CSI snapshot content")
}
} else {
return nil, errors.New("API version is invalid")
}
if len(snapshotContentNameList) != expectedCount {
return nil, errors.New(fmt.Sprintf("Snapshot count %d is not as expect %d", len(snapshotContentNameList), expectedCount))
Expand Down
14 changes: 13 additions & 1 deletion test/e2e/util/k8s/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (d *DeploymentBuilder) Result() *apps.Deployment {
}

// newDeployment returns a RollingUpdate Deployment with a fake container image
func NewDeployment(name, ns string, replicas int32, labels map[string]string, containers []v1.Container) *DeploymentBuilder {
func NewDeployment(name, ns string, replicas int32, labels map[string]string, containers []v1.Container, volumes []v1.Volume) *DeploymentBuilder {
if containers == nil {
containers = []v1.Container{
{
Expand All @@ -70,6 +70,17 @@ func NewDeployment(name, ns string, replicas int32, labels map[string]string, co
},
}
}
if volumes == nil {

volumes = []v1.Volume{
{
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
}

return &DeploymentBuilder{
&apps.Deployment{
TypeMeta: metav1.TypeMeta{
Expand Down Expand Up @@ -98,6 +109,7 @@ func NewDeployment(name, ns string, replicas int32, labels map[string]string, co
FSGroupChangePolicy: func(policy v1.PodFSGroupChangePolicy) *v1.PodFSGroupChangePolicy { return &policy }(v1.FSGroupChangeAlways),
},
Containers: containers,
Volumes: volumes,
},
},
},
Expand Down
8 changes: 7 additions & 1 deletion test/e2e/util/velero/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func VeleroInstall(ctx context.Context, veleroCfg *VeleroConfig, isStandbyCluste
return errors.WithMessagef(err, "Failed to get Velero InstallOptions for plugin provider %s", veleroCfg.ObjectStoreProvider)
}
veleroInstallOptions.UseVolumeSnapshots = veleroCfg.UseVolumeSnapshots
veleroInstallOptions.UseNodeAgent = veleroCfg.UseNodeAgent
if !veleroCfg.UseRestic {
veleroInstallOptions.UseNodeAgent = veleroCfg.UseNodeAgent
}
veleroInstallOptions.UseRestic = veleroCfg.UseRestic
veleroInstallOptions.Image = veleroCfg.VeleroImage
veleroInstallOptions.Namespace = veleroCfg.VeleroNamespace
veleroInstallOptions.UploaderType = veleroCfg.UploaderType
Expand Down Expand Up @@ -198,6 +201,9 @@ func installVeleroServer(ctx context.Context, cli, cloudProvider string, options
if len(options.Image) > 0 {
args = append(args, "--image", options.Image)
}
if options.UseRestic {
args = append(args, "--use-restic")
}
if options.UseNodeAgent {
args = append(args, "--use-node-agent")
}
Expand Down
Loading

0 comments on commit f2321a1

Please sign in to comment.