volume snapshot

This commit is contained in:
zhangmin
2020-05-07 10:47:44 +08:00
parent 87e567eaf5
commit 4f17b7a07f
95 changed files with 15025 additions and 590 deletions

View File

@@ -18,6 +18,7 @@
package persistentvolumeclaim
import (
snapshotinformer "github.com/kubernetes-csi/external-snapshotter/v2/pkg/client/informers/externalversions"
"k8s.io/client-go/informers"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha2"
"strconv"
@@ -36,11 +37,15 @@ const (
)
type persistentVolumeClaimSearcher struct {
informers informers.SharedInformerFactory
informers informers.SharedInformerFactory
snapshotInformers snapshotinformer.SharedInformerFactory
}
func NewPersistentVolumeClaimSearcher(informers informers.SharedInformerFactory) v1alpha2.Interface {
return &persistentVolumeClaimSearcher{informers: informers}
func NewPersistentVolumeClaimSearcher(informers informers.SharedInformerFactory, snapshotInformer snapshotinformer.SharedInformerFactory) v1alpha2.Interface {
return &persistentVolumeClaimSearcher{
informers: informers,
snapshotInformers: snapshotInformer,
}
}
func (s *persistentVolumeClaimSearcher) Get(namespace, name string) (interface{}, error) {
@@ -117,11 +122,12 @@ func (s *persistentVolumeClaimSearcher) Search(namespace string, conditions *par
r := make([]interface{}, 0)
for _, i := range result {
inUse := s.countPods(i.Name, i.Namespace)
isSnapshotAllow := s.isSnapshotAllowed(i.GetAnnotations()["volume.beta.kubernetes.io/storage-provisioner"])
if i.Annotations == nil {
i.Annotations = make(map[string]string)
}
i.Annotations["kubesphere.io/in-use"] = strconv.FormatBool(inUse)
i.Annotations["kubesphere.io/allow-snapshot"] = strconv.FormatBool(isSnapshotAllow)
r = append(r, i)
}
return r, nil
@@ -142,3 +148,19 @@ func (s *persistentVolumeClaimSearcher) countPods(name, namespace string) bool {
return false
}
func (s *persistentVolumeClaimSearcher) isSnapshotAllowed(provisioner string) bool {
if len(provisioner) == 0 {
return false
}
volumeSnapshotClasses, err := s.snapshotInformers.Snapshot().V1beta1().VolumeSnapshotClasses().Lister().List(labels.Everything())
if err != nil {
return false
}
for _, volumeSnapshotClass := range volumeSnapshotClasses {
if volumeSnapshotClass.Driver == provisioner {
return true
}
}
return false
}