Skip to content

Commit

Permalink
Merge pull request kubesphere#4718 from f10atin9/snapshot
Browse files Browse the repository at this point in the history
Add "snapshot-count" annotation for volumesnapshotClass
  • Loading branch information
ks-ci-bot authored Mar 10, 2022
2 parents f018a23 + d871f0e commit 84df2e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ limitations under the License.
package volumesnapshotclass

import (
"strconv"
"strings"

"k8s.io/apimachinery/pkg/labels"

v1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
"github.com/kubernetes-csi/external-snapshotter/client/v4/informers/externalversions"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -53,6 +56,12 @@ func (v *volumeSnapshotClassGetter) List(namespace string, query *query.Query) (

var result []runtime.Object
for _, snapshotClass := range all {
snapshotClass = snapshotClass.DeepCopy()
count := v.countVolumeSnapshots(snapshotClass.Name)
if snapshotClass.Annotations == nil {
snapshotClass.Annotations = make(map[string]string)
}
snapshotClass.Annotations["kubesphere.io/snapshot-count"] = strconv.Itoa(count)
result = append(result, snapshotClass)
}

Expand Down Expand Up @@ -86,3 +95,18 @@ func (v *volumeSnapshotClassGetter) filter(object runtime.Object, filter query.F
return v1alpha3.DefaultObjectMetaFilter(snapshotClass.ObjectMeta, filter)
}
}

func (v *volumeSnapshotClassGetter) countVolumeSnapshots(name string) int {
snapshots, err := v.informers.Snapshot().V1().VolumeSnapshots().Lister().List(labels.Everything())
if err != nil {
return 0
}
var count int

for _, snapshot := range snapshots {
if snapshot.Spec.VolumeSnapshotClassName != nil && *snapshot.Spec.VolumeSnapshotClassName == name {
count++
}
}
return count
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func TestListVolumeSnapshotClass(t *testing.T) {
snapshotClass3.CreationTimestamp = v1.NewTime(snapshotClass1.CreationTimestamp.Add(time.Hour * 3))
snapshotClass3.DeletionPolicy = snapshotv1.VolumeSnapshotContentRetain

sc1Expected := snapshotClass1.DeepCopy()
sc1Expected.Annotations = make(map[string]string)
sc1Expected.Annotations["kubesphere.io/snapshot-count"] = "0"

sc2Expected := snapshotClass2.DeepCopy()
sc2Expected.Annotations = make(map[string]string)
sc2Expected.Annotations["kubesphere.io/snapshot-count"] = "0"

sc3Expected := snapshotClass3.DeepCopy()
sc3Expected.Annotations = make(map[string]string)
sc3Expected.Annotations["kubesphere.io/snapshot-count"] = "0"

volumeSnapshotClasses := []interface{}{snapshotClass1, snapshotClass2, snapshotClass3}

for _, s := range volumeSnapshotClasses {
Expand All @@ -86,7 +98,7 @@ func TestListVolumeSnapshotClass(t *testing.T) {
snapshotClassList, err := getter.List("", query1)
Expect(err).To(BeNil())
Expect(snapshotClassList.TotalItems).To(Equal(1))
Expect(snapshotClassList.Items[0]).To(Equal(snapshotClass1))
Expect(snapshotClassList.Items[0]).To(Equal(sc1Expected))
})

It("match driver", func() {
Expand All @@ -95,7 +107,7 @@ func TestListVolumeSnapshotClass(t *testing.T) {
snapshotClassList, err := getter.List("", query1)
Expect(err).To(BeNil())
Expect(snapshotClassList.TotalItems).To(Equal(1))
Expect(snapshotClassList.Items[0]).To(Equal(snapshotClass2))
Expect(snapshotClassList.Items[0]).To(Equal(sc2Expected))
})

It("match deletionPolicy", func() {
Expand All @@ -104,7 +116,7 @@ func TestListVolumeSnapshotClass(t *testing.T) {
snapshotClassList, err := getter.List("", query1)
Expect(err).To(BeNil())
Expect(snapshotClassList.TotalItems).To(Equal(1))
Expect(snapshotClassList.Items[0]).To(Equal(snapshotClass3))
Expect(snapshotClassList.Items[0]).To(Equal(sc3Expected))
})

})
Expand Down

0 comments on commit 84df2e9

Please sign in to comment.