From 5e6ddfac44ea70474a02b1ed4dfedd2762cf0134 Mon Sep 17 00:00:00 2001 From: wangyelei Date: Wed, 11 Oct 2023 19:47:43 +0800 Subject: [PATCH] remove invalid code --- .../dataprotection/backup_controller_test.go | 4 +- internal/controllerutil/volumesnapshot.go | 6 ++- .../dataprotection/action/action_create_vs.go | 41 ---------------- internal/dataprotection/builder/builder.go | 41 ---------------- .../dataprotection/builder/builder_test.go | 36 -------------- internal/dataprotection/builder/suite_test.go | 47 ------------------- internal/dataprotection/utils/utils.go | 2 +- internal/testutil/apps/constant.go | 3 +- internal/testutil/apps/native_object_util.go | 4 +- internal/testutil/k8s/storage_util.go | 12 ++--- internal/testutil/type.go | 5 +- 11 files changed, 21 insertions(+), 180 deletions(-) delete mode 100644 internal/dataprotection/builder/builder.go delete mode 100644 internal/dataprotection/builder/builder_test.go delete mode 100644 internal/dataprotection/builder/suite_test.go diff --git a/controllers/dataprotection/backup_controller_test.go b/controllers/dataprotection/backup_controller_test.go index dd45be85acc..066bec6c2ef 100644 --- a/controllers/dataprotection/backup_controller_test.go +++ b/controllers/dataprotection/backup_controller_test.go @@ -37,8 +37,10 @@ import ( dptypes "github.com/apecloud/kubeblocks/internal/dataprotection/types" dputils "github.com/apecloud/kubeblocks/internal/dataprotection/utils" "github.com/apecloud/kubeblocks/internal/generics" + "github.com/apecloud/kubeblocks/internal/testutil" testapps "github.com/apecloud/kubeblocks/internal/testutil/apps" testdp "github.com/apecloud/kubeblocks/internal/testutil/dataprotection" + testk8s "github.com/apecloud/kubeblocks/internal/testutil/k8s" ) var _ = Describe("Backup Controller test", func() { @@ -259,7 +261,7 @@ var _ = Describe("Backup Controller test", func() { BeforeEach(func() { // mock VolumeSnapshotClass for volume snapshot - testapps.CreateVolumeSnapshotClass(&testCtx) + testk8s.CreateVolumeSnapshotClass(&testCtx, testutil.DefaultStorageProvisoner) By("create a backup from backupPolicy " + testdp.BackupPolicyName) backup = testdp.NewFakeBackup(&testCtx, func(backup *dpv1alpha1.Backup) { diff --git a/internal/controllerutil/volumesnapshot.go b/internal/controllerutil/volumesnapshot.go index d6ec98e0e8a..99e8278d5f8 100644 --- a/internal/controllerutil/volumesnapshot.go +++ b/internal/controllerutil/volumesnapshot.go @@ -171,8 +171,12 @@ func IsVolumeSnapshotEnabled(ctx context.Context, cli client.Client, storageClas return false, client.IgnoreNotFound(err) } + vsCli := VolumeSnapshotCompatClient{ + Client: cli, + Ctx: ctx, + } vscList := snapshotv1.VolumeSnapshotClassList{} - if err := cli.List(ctx, &vscList); err != nil { + if err := vsCli.List(&vscList); err != nil { return false, err } for _, vsc := range vscList.Items { diff --git a/internal/dataprotection/action/action_create_vs.go b/internal/dataprotection/action/action_create_vs.go index f4faf360149..b8ed5ab7c78 100644 --- a/internal/dataprotection/action/action_create_vs.go +++ b/internal/dataprotection/action/action_create_vs.go @@ -20,21 +20,18 @@ along with this program. If not, see . package action import ( - "context" "fmt" "strings" vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" - storagev1 "k8s.io/api/storage/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1" intctrlutil "github.com/apecloud/kubeblocks/internal/controllerutil" - "github.com/apecloud/kubeblocks/internal/dataprotection/builder" dptypes "github.com/apecloud/kubeblocks/internal/dataprotection/types" "github.com/apecloud/kubeblocks/internal/dataprotection/utils" ) @@ -157,14 +154,6 @@ func (c *CreateVolumeSnapshotAction) createVolumeSnapshotIfNotExist(ctx Context, return nil } - // create volume snapshot - if pvc.Spec.StorageClassName != nil && *pvc.Spec.StorageClassName != "" { - vsc, err = createVolumeSnapshotClassIfNotExist(ctx.Ctx, ctx.Client, vsCli, *pvc.Spec.StorageClassName) - if err != nil { - return err - } - } - c.ObjectMeta.Name = key.Name c.ObjectMeta.Namespace = key.Namespace @@ -195,36 +184,6 @@ func (c *CreateVolumeSnapshotAction) createVolumeSnapshotIfNotExist(ctx Context, return nil } -func createVolumeSnapshotClassIfNotExist( - ctx context.Context, - cli client.Client, - vsCli intctrlutil.VolumeSnapshotCompatClient, - scName string) (*vsv1.VolumeSnapshotClass, error) { - scObj := storagev1.StorageClass{} - // ignore if not found storage class, use the default volume snapshot class - if err := cli.Get(ctx, client.ObjectKey{Name: scName}, &scObj); client.IgnoreNotFound(err) != nil { - return nil, err - } - - vscList := vsv1.VolumeSnapshotClassList{} - if err := vsCli.List(&vscList); err != nil { - return nil, err - } - for _, item := range vscList.Items { - if item.Driver == scObj.Provisioner { - return item.DeepCopy(), nil - } - } - - // not found matched volume snapshot class, create one - vscName := fmt.Sprintf("vsc-%s-%s", scName, scObj.UID[:8]) - newVsc := builder.BuildVolumeSnapshotClass(vscName, scObj.Provisioner) - if err := vsCli.Create(newVsc); err != nil { - return nil, err - } - return newVsc, nil -} - func ensureVolumeSnapshotReady( vsCli intctrlutil.VolumeSnapshotCompatClient, key client.ObjectKey) (bool, *vsv1.VolumeSnapshot, error) { diff --git a/internal/dataprotection/builder/builder.go b/internal/dataprotection/builder/builder.go deleted file mode 100644 index cbbe980f497..00000000000 --- a/internal/dataprotection/builder/builder.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright (C) 2022-2023 ApeCloud Co., Ltd - -This file is part of KubeBlocks project - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . -*/ - -package builder - -import ( - vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - "github.com/apecloud/kubeblocks/internal/constant" -) - -func BuildVolumeSnapshotClass(name string, driver string) *vsv1.VolumeSnapshotClass { - vsc := &vsv1.VolumeSnapshotClass{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Labels: map[string]string{ - constant.KBManagedByKey: constant.AppName, - }, - }, - Driver: driver, - DeletionPolicy: vsv1.VolumeSnapshotContentDelete, - } - return vsc -} diff --git a/internal/dataprotection/builder/builder_test.go b/internal/dataprotection/builder/builder_test.go deleted file mode 100644 index b10522e9964..00000000000 --- a/internal/dataprotection/builder/builder_test.go +++ /dev/null @@ -1,36 +0,0 @@ -/* -Copyright (C) 2022-2023 ApeCloud Co., Ltd - -This file is part of KubeBlocks project - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . -*/ - -package builder - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = Describe("builder", func() { - It("builds volume snapshot class correctly", func() { - className := "vsc-test" - driverName := "csi-driver-test" - obj := BuildVolumeSnapshotClass(className, driverName) - Expect(obj).ShouldNot(BeNil()) - Expect(obj.Name).Should(Equal(className)) - Expect(obj.Driver).Should(Equal(driverName)) - }) -}) diff --git a/internal/dataprotection/builder/suite_test.go b/internal/dataprotection/builder/suite_test.go deleted file mode 100644 index 7cb19699de3..00000000000 --- a/internal/dataprotection/builder/suite_test.go +++ /dev/null @@ -1,47 +0,0 @@ -/* -Copyright (C) 2022-2023 ApeCloud Co., Ltd - -This file is part of KubeBlocks project - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . -*/ - -package builder - -import ( - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -// These tests use Ginkgo (BDD-style Go testing framework). Refer to -// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. - -func TestAPIs(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecs(t, "Data Protection Builder Suite") -} - -var _ = BeforeSuite(func() { - // +kubebuilder:scaffold:scheme - - go func() { - defer GinkgoRecover() - }() -}) - -var _ = AfterSuite(func() { -}) diff --git a/internal/dataprotection/utils/utils.go b/internal/dataprotection/utils/utils.go index 58fc1dee3a3..94cce5e0547 100644 --- a/internal/dataprotection/utils/utils.go +++ b/internal/dataprotection/utils/utils.go @@ -175,7 +175,7 @@ func VolumeSnapshotEnabled(ctx context.Context, cli client.Client, pod *corev1.P return false, err } if !enabled { - return false, fmt.Errorf(`cannot find CSI of persistentVolumeClaim "%s" to do volume snapshot on pod "%s"`, storageClassMap[k], pod.Name) + return false, fmt.Errorf(`cannot find any VolumeSnapshotClass of persistentVolumeClaim "%s" to do volume snapshot on pod "%s"`, storageClassMap[k], pod.Name) } } return true, nil diff --git a/internal/testutil/apps/constant.go b/internal/testutil/apps/constant.go index 0141780f489..b1e3303ae9b 100644 --- a/internal/testutil/apps/constant.go +++ b/internal/testutil/apps/constant.go @@ -60,8 +60,7 @@ const ( Class2c4gName = "general-2c4g" DefaultResourceConstraintName = "kb-resource-constraint" - StogrageProvisioner = "kubernetes.io/no-provisioner" - StogrageClassName = "test-sc" + StogrageClassName = "test-sc" ) var ( diff --git a/internal/testutil/apps/native_object_util.go b/internal/testutil/apps/native_object_util.go index ff1a4a1d80b..a6983011814 100644 --- a/internal/testutil/apps/native_object_util.go +++ b/internal/testutil/apps/native_object_util.go @@ -78,7 +78,7 @@ func CreateStorageClass(testCtx *testutil.TestContext, storageClassName string, storage.IsDefaultStorageClassAnnotation: "false", }, }, - Provisioner: StogrageProvisioner, + Provisioner: testutil.DefaultStorageProvisoner, AllowVolumeExpansion: &allowVolumeExpansion, } return CreateK8sResource(testCtx, storageClass).(*storagev1.StorageClass) @@ -89,7 +89,7 @@ func CreateVolumeSnapshotClass(testCtx *testutil.TestContext) { ObjectMeta: metav1.ObjectMeta{ Name: "default-vs", }, - Driver: StogrageProvisioner, + Driver: testutil.DefaultStorageProvisoner, DeletionPolicy: vsv1.VolumeSnapshotContentDelete, } CreateK8sResource(testCtx, volumeSnapshotClass) diff --git a/internal/testutil/k8s/storage_util.go b/internal/testutil/k8s/storage_util.go index 53b01248b7a..e0a5f81edaa 100644 --- a/internal/testutil/k8s/storage_util.go +++ b/internal/testutil/k8s/storage_util.go @@ -32,9 +32,9 @@ import ( ) var ( - DefaultStorageClassName string = "default-sc-for-testing" - defaultVolumeSnapshotClassName string = "default-vsc-for-testing" - defaultProvisioner string = "testing.kubeblocks.io" + DefaultStorageClassName = "default-sc-for-testing" + defaultVolumeSnapshotClassName = "default-vsc-for-testing" + defaultProvisioner = "testing.kubeblocks.io" ) func GetDefaultStorageClass(testCtx *testutil.TestContext) *storagev1.StorageClass { @@ -75,7 +75,7 @@ func MockEnableVolumeSnapshot(testCtx *testutil.TestContext, storageClassName st } vsc := getVolumeSnapshotClass(testCtx, sc) if vsc == nil { - createVolumeSnapshotClass(testCtx, sc) + CreateVolumeSnapshotClass(testCtx, sc.Provisioner) } gomega.Expect(IsMockVolumeSnapshotEnabled(testCtx, storageClassName)).Should(gomega.BeTrue()) } @@ -138,12 +138,12 @@ func getVolumeSnapshotClass(testCtx *testutil.TestContext, storageClass *storage return nil } -func createVolumeSnapshotClass(testCtx *testutil.TestContext, storageClass *storagev1.StorageClass) *snapshotv1.VolumeSnapshotClass { +func CreateVolumeSnapshotClass(testCtx *testutil.TestContext, storageProvisioner string) *snapshotv1.VolumeSnapshotClass { vsc := &snapshotv1.VolumeSnapshotClass{ ObjectMeta: metav1.ObjectMeta{ Name: defaultVolumeSnapshotClassName, }, - Driver: storageClass.Provisioner, + Driver: storageProvisioner, DeletionPolicy: snapshotv1.VolumeSnapshotContentDelete, } gomega.Expect(testCtx.Cli.Create(testCtx.Ctx, vsc)).Should(gomega.Succeed()) diff --git a/internal/testutil/type.go b/internal/testutil/type.go index 2ae9c51d466..cf8db46e152 100644 --- a/internal/testutil/type.go +++ b/internal/testutil/type.go @@ -57,8 +57,9 @@ type TestContext struct { var ErrUninitError = fmt.Errorf("cli uninitialized error") const ( - envExistingClusterType = "EXISTING_CLUSTER_TYPE" - envUseExistingCluster = "USE_EXISTING_CLUSTER" + envExistingClusterType = "EXISTING_CLUSTER_TYPE" + envUseExistingCluster = "USE_EXISTING_CLUSTER" + DefaultStorageProvisoner = "kubernetes.io/no-provisioner" ) func init() {