/
pvc.go
104 lines (90 loc) · 3.12 KB
/
pvc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright 2022 KubeCube Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pvc
import (
"encoding/json"
"errors"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
resourcemanage "github.com/kubecube-io/kubecube/pkg/apiserver/cubeapi/resourcemanage/handle"
"github.com/kubecube-io/kubecube/pkg/apiserver/cubeapi/resourcemanage/resources"
"github.com/kubecube-io/kubecube/pkg/apiserver/cubeapi/resourcemanage/resources/enum"
"github.com/kubecube-io/kubecube/pkg/clients"
"github.com/kubecube-io/kubecube/pkg/clog"
"github.com/kubecube-io/kubecube/pkg/utils/errcode"
"github.com/kubecube-io/kubecube/pkg/utils/filter"
)
type PvcExtend struct {
v1.PersistentVolumeClaim
Pods []v1.Pod `json:"pods,omitempty"`
Total int `json:"total,omitempty"`
}
func init() {
resourcemanage.SetExtendHandler(enum.PvcResourceType, PvcHandle)
}
func PvcHandle(param resourcemanage.ExtendParams) (interface{}, error) {
access := resources.NewSimpleAccess(param.Cluster, param.Username, param.Namespace)
if allow := access.AccessAllow("", "persistentvolumeclaims", "list"); !allow {
return nil, errors.New(errcode.ForbiddenErr.Message)
}
kubernetes := clients.Interface().Kubernetes(param.Cluster)
if kubernetes == nil {
return nil, errors.New(errcode.ClusterNotFoundError(param.Cluster).Message)
}
pvc := NewPvc(kubernetes, param.Namespace, param.Filter)
return pvc.GetPvc()
}
//GetPvc list pvcs, and add extend info that which pod mount this pvc
func (p *Pvc) GetPvc() (filter.K8sJson, error) {
result := make(filter.K8sJson)
pvcList := v1.PersistentVolumeClaimList{}
err := p.client.Cache().List(p.ctx, &pvcList, &client.ListOptions{
Namespace: p.namespace,
})
if err != nil {
return nil, err
}
result["total"] = len(pvcList.Items)
pvcListJson, err := json.Marshal(pvcList)
if err != nil {
return nil, err
}
filterResult := p.filter.FilterResult(pvcListJson)
pvcList = v1.PersistentVolumeClaimList{}
err = json.Unmarshal(filterResult, &pvcList)
if err != nil {
return nil, err
}
var pvcExtendList []PvcExtend
for _, pvc := range pvcList.Items {
workloadMap, err := p.GetPvcWorkloads(pvc.Name)
if err != nil {
return nil, err
}
// if response has pods, and result is pod array, then add it as extendInfo
if podRes, ok := workloadMap["pods"]; ok {
if pods, ok := podRes.([]v1.Pod); ok {
extend := PvcExtend{
PersistentVolumeClaim: pvc,
Pods: pods,
Total: len(pods),
}
pvcExtendList = append(pvcExtendList, extend)
} else {
clog.Error("get pvc mounted pod err, res: %+v", workloadMap)
}
}
}
result["items"] = pvcExtendList
return result, nil
}