-
Notifications
You must be signed in to change notification settings - Fork 11
/
reconcile.go
115 lines (97 loc) · 2.57 KB
/
reconcile.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
105
106
107
108
109
110
111
112
113
114
115
package storage
import (
"context"
"github.com/k8s-proxmox/proxmox-go/api"
"github.com/k8s-proxmox/proxmox-go/proxmox"
"github.com/k8s-proxmox/proxmox-go/rest"
"sigs.k8s.io/controller-runtime/pkg/log"
infrav1 "github.com/k8s-proxmox/cluster-api-provider-proxmox/api/v1beta1"
)
const (
DefaultBasePath = "/var/lib/vz"
)
func (s *Service) Reconcile(ctx context.Context) error {
log := log.FromContext(ctx)
log.Info("Reconciling storage")
if err := s.createOrGetStorage(ctx); err != nil {
return err
}
log.Info("Reconciled storage")
return nil
}
func (s *Service) Delete(ctx context.Context) error {
log := log.FromContext(ctx)
log.Info("Deleteing storage")
return s.deleteStorage(ctx)
}
// createOrGetStorage gets Proxmox Storage for VMs
func (s *Service) createOrGetStorage(ctx context.Context) error {
log := log.FromContext(ctx)
opts := generateVMStorageOptions(s.scope)
if err := s.getStorage(ctx, opts.Storage); err != nil {
if rest.IsNotFound(err) {
log.Info("storage %s not found. it will be created")
return s.createStorage(ctx, opts)
}
return err
}
s.scope.SetStorage(infrav1.Storage{Name: opts.Storage, Path: opts.Path})
return nil
}
func (s *Service) getStorage(ctx context.Context, name string) error {
if _, err := s.client.Storage(ctx, name); err != nil {
return err
}
return nil
}
func (s *Service) createStorage(ctx context.Context, options api.StorageCreateOptions) error {
if _, err := s.client.CreateStorage(ctx, options.Storage, options.StorageType, options); err != nil {
return err
}
return nil
}
func (s *Service) deleteStorage(ctx context.Context) error {
log := log.FromContext(ctx)
var storage *proxmox.Storage
storage, err := s.client.Storage(ctx, s.scope.Storage().Name)
if err != nil {
if rest.IsNotFound(err) {
log.Info("storage not found or already deleted")
return nil
}
return err
}
nodes, err := s.client.Nodes(ctx)
if err != nil {
return err
}
for _, node := range nodes {
storage.Node = node.Node
// check if storage is empty
contents, err := storage.GetContents(ctx)
if err != nil {
return err
}
if len(contents) > 0 {
log.Info("storage not empty, skipping deletion")
return nil
}
}
// delete
if err := storage.Delete(ctx); err != nil {
return err
}
return nil
}
func generateVMStorageOptions(scope Scope) api.StorageCreateOptions {
storageSpec := scope.Storage()
mkdir := true
options := api.StorageCreateOptions{
Storage: storageSpec.Name,
StorageType: "dir",
Content: "snippets",
Mkdir: &mkdir,
Path: storageSpec.Path,
}
return options
}