Merge pull request #4718 from f10atin9/snapshot

Add "snapshot-count" annotation for volumesnapshotClass
This commit is contained in:
KubeSphere CI Bot
2022-03-10 16:11:14 +08:00
committed by GitHub
2 changed files with 39 additions and 3 deletions

View File

@@ -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"
@@ -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)
}
@@ -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
}

View File

@@ -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 {
@@ -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() {
@@ -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() {
@@ -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))
})
})