Add "snapshot-count" annotation for volumesnapshotClass

Signed-off-by: f10atin9 <f10atin9@kubesphere.io>
This commit is contained in:
f10atin9
2022-03-03 15:55:32 +08:00
parent f018a23023
commit 2e34bdf9a2

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,11 @@ func (v *volumeSnapshotClassGetter) List(namespace string, query *query.Query) (
var result []runtime.Object
for _, snapshotClass := range all {
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 +94,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().VolumeSnapshots("").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
}