-
Notifications
You must be signed in to change notification settings - Fork 308
/
store.go
65 lines (53 loc) · 2.18 KB
/
store.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
package vm
import (
"fmt"
"strings"
"github.com/rancher/apiserver/pkg/apierror"
"github.com/rancher/apiserver/pkg/types"
v1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/schemas/validation"
"github.com/rancher/wrangler/pkg/slice"
kubevirtv1 "kubevirt.io/api/core/v1"
ctlkubevirtv1 "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io/v1"
"github.com/harvester/harvester/pkg/util"
)
type vmStore struct {
types.Store
vms ctlkubevirtv1.VirtualMachineClient
vmCache ctlkubevirtv1.VirtualMachineCache
pvcs v1.PersistentVolumeClaimClient
pvcCache v1.PersistentVolumeClaimCache
}
func (s *vmStore) Delete(request *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
removedDisks := request.Query["removedDisks"]
vm, err := s.vmCache.Get(request.Namespace, request.Name)
if err != nil {
return types.APIObject{}, apierror.NewAPIError(validation.ServerError, fmt.Sprintf("Failed to get vm %s/%s, %v", request.Namespace, request.Name, err))
}
var removedPVCs []string
if vm.Spec.Template != nil {
for _, vol := range vm.Spec.Template.Spec.Volumes {
if vol.PersistentVolumeClaim == nil {
continue
}
if slice.ContainsString(removedDisks, vol.Name) {
removedPVCs = append(removedPVCs, vol.PersistentVolumeClaim.ClaimName)
}
}
}
// Set removed PVCs in annotations. VMController is in charge of the cleanup.
if err = s.setRemovedPVCs(vm, removedPVCs); err != nil {
return types.APIObject{}, apierror.NewAPIError(validation.ServerError, fmt.Sprintf("Failed to set removedPersistentVolumeClaims to virtualMachine %s/%s, %v", request.Namespace, request.Name, err))
}
apiObj, err := s.Store.Delete(request, request.Schema, id)
if err != nil {
return types.APIObject{}, apierror.NewAPIError(validation.ServerError, fmt.Sprintf("Failed to remove vm %s/%s, %v", request.Namespace, request.Name, err))
}
return apiObj, nil
}
func (s *vmStore) setRemovedPVCs(vm *kubevirtv1.VirtualMachine, removedPVCs []string) error {
vmCopy := vm.DeepCopy()
vmCopy.Annotations[util.RemovedPVCsAnnotationKey] = strings.Join(removedPVCs, ",")
_, err := s.vms.Update(vmCopy)
return err
}