Skip to content

Commit bccc6ad

Browse files
committed
storage reconciler
1 parent 394fa5b commit bccc6ad

File tree

12 files changed

+90
-32
lines changed

12 files changed

+90
-32
lines changed

api/v1beta1/proxmoxcluster_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type ProxmoxClusterSpec struct {
3737

3838
CredentialsRef *ObjectReference `json:"credentialsRef"`
3939

40-
// Foo is an example field of ProxmoxCluster. Edit proxmoxcluster_types.go to remove/update
41-
Foo string `json:"foo,omitempty"`
40+
// storage is for proxmox storage used by vm instances
41+
Storage Storage `json:"storage"`
4242
}
4343

4444
// ProxmoxClusterStatus defines the observed state of ProxmoxCluster

api/v1beta1/proxmoxmachine_types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ type ProxmoxMachineSpec struct {
5252
Foo string `json:"foo,omitempty"`
5353
}
5454

55-
// CloudInit defines options related to the bootstrapping systems where
56-
// CloudInit is used.
57-
type CloudInit struct {
58-
}
59-
6055
// ProxmoxMachineStatus defines the observed state of ProxmoxMachine
6156
type ProxmoxMachineStatus struct {
6257
// Ready is true when the provider resource is ready.

api/v1beta1/type.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ type ObjectReference struct {
1616
Name string `json:"name"`
1717
}
1818

19+
// CloudInit defines options related to the bootstrapping systems where
20+
// CloudInit is used.
21+
type CloudInit struct {
22+
}
23+
24+
type Storage struct {
25+
Name string `json:"name"`
26+
Path string `json:"path,omitempty"`
27+
}
28+
1929
type InstanceStatus string
2030

2131
var (

cloud/interfaces.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type ClusterGetter interface {
3636
// Network() *infrav1.Network
3737
// AdditionalLabels() infrav1.Labels
3838
// FailureDomains() clusterv1.FailureDomains
39-
// ControlPlaneEndpoint() clusterv1.APIEndpoint
39+
ControlPlaneEndpoint() clusterv1.APIEndpoint
40+
Storage() infrav1.Storage
4041
}
4142

4243
type ClusterSettter interface {
@@ -56,6 +57,7 @@ type MachineGetter interface {
5657
GetProviderID() string
5758
GetBootstrapData() (string, error)
5859
GetInstanceStatus() *infrav1.InstanceStatus
60+
GetStorage() infrav1.Storage
5961
}
6062

6163
// MachineSetter is an interface which can set machine information.

cloud/scope/cluster.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,26 @@ func (s *ClusterScope) Name() string {
8585
return s.Cluster.Name
8686
}
8787

88+
func (s *ClusterScope) Namespace() string {
89+
return s.Cluster.Namespace
90+
}
91+
92+
func (s *ClusterScope) ControlPlaneEndpoint() clusterv1.APIEndpoint {
93+
return s.ProxmoxCluster.Spec.ControlPlaneEndpoint
94+
}
95+
96+
func (s *ClusterScope) Storage() infrav1.Storage {
97+
return s.ProxmoxCluster.Spec.Storage
98+
}
99+
88100
func (s *ClusterScope) CloudClient() *service.Service {
89101
return s.ProxmoxServices.Compute
90102
}
91103

104+
func (s *ClusterScope) RemoteClient() *SSHClient {
105+
return s.ProxmoxServices.Remote
106+
}
107+
92108
func (s *ClusterScope) Close() error {
93109
return s.PatchObject()
94110
}
@@ -97,6 +113,10 @@ func (s *ClusterScope) SetReady() {
97113
s.ProxmoxCluster.Status.Ready = true
98114
}
99115

116+
func (s *ClusterScope) SetControlPlaneEndpoint(endpoint clusterv1.APIEndpoint) {
117+
s.ProxmoxCluster.Spec.ControlPlaneEndpoint = endpoint
118+
}
119+
100120
// PatchObject persists the cluster configuration and status.
101121
func (s *ClusterScope) PatchObject() error {
102122
return s.patchHelper.Patch(context.TODO(), s.ProxmoxCluster)

cloud/scope/machine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func (m *MachineScope) RemoteClient() *SSHClient {
8787
return m.ClusterGetter.Remote
8888
}
8989

90+
func (m *MachineScope) GetStorage() infrav1.Storage {
91+
return m.ClusterGetter.ProxmoxCluster.Spec.Storage
92+
}
93+
9094
func (m *MachineScope) Name() string {
9195
return m.ProxmoxMachine.Name
9296
}

cloud/services/compute/instance/reconcile.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import (
2020
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
2121
)
2222

23-
const (
24-
vmStorage = "local-capi" // to do
25-
)
26-
2723
func (s *Service) Reconcile(ctx context.Context) error {
2824
log := log.FromContext(ctx)
2925
log.Info("Reconciling instance resources")
@@ -119,23 +115,23 @@ func (s *Service) CreateInstance(ctx context.Context, bootstrap string) (*vm.Vir
119115
}
120116

121117
// cloud init snippet // to do ssh key
122-
if err := setCloudConfig(ctx, s.scope.Name(), bootstrap, s.remote); err != nil {
118+
if err := setCloudConfig(ctx, s.scope.Name(), s.scope.GetStorage().Name, bootstrap, s.remote); err != nil {
123119
return nil, err
124120
}
125121

126122
// create vm
127-
vmoption := generateVMOptions(s.scope.Name(), vmStorage)
123+
vmoption := generateVMOptions(s.scope.Name(), s.scope.GetStorage().Name)
128124
vm, err := node.CreateVirtualMachine(vmid, vmoption)
129125
if err != nil {
130126
log.Error(err, "failed to create virtual machine")
131127
return nil, err
132128
}
133129

134-
if err := applyCICustom(vmid, s.scope.Name(), vmStorage, s.remote); err != nil {
130+
if err := applyCICustom(vmid, s.scope.Name(), s.scope.GetStorage().Name, s.remote); err != nil {
135131
return nil, err
136132
}
137133

138-
if err := setCloudImage(ctx, vmid, s.remote); err != nil {
134+
if err := setCloudImage(ctx, vmid, s.scope.GetStorage().Name, s.remote); err != nil {
139135
return nil, err
140136
}
141137

@@ -188,7 +184,7 @@ func (s *Service) Delete(ctx context.Context) error {
188184
return instance.Delete()
189185
}
190186

191-
func setCloudImage(ctx context.Context, vmid int, ssh scope.SSHClient) error {
187+
func setCloudImage(ctx context.Context, vmid int, storageName string, ssh scope.SSHClient) error {
192188
log := log.FromContext(ctx)
193189
log.Info("setting cloud image")
194190

@@ -199,7 +195,7 @@ func setCloudImage(ctx context.Context, vmid int, ssh scope.SSHClient) error {
199195
// return nil, errors.Errorf("failed to download image")
200196
// }
201197

202-
destPath := fmt.Sprintf("/var/lib/vz/%s/images/%d/vm-%d-disk-0.raw", vmStorage, vmid, vmid)
198+
destPath := fmt.Sprintf("/var/lib/vz/%s/images/%d/vm-%d-disk-0.raw", storageName, vmid, vmid)
203199
out, err = ssh.RunCommand(fmt.Sprintf("/usr/bin/qemu-img convert -O raw /root/jammy-server-cloudimg-amd64-disk-kvm.img %s", destPath))
204200
if err != nil {
205201
return err
@@ -209,7 +205,7 @@ func setCloudImage(ctx context.Context, vmid int, ssh scope.SSHClient) error {
209205
return nil
210206
}
211207

212-
func setCloudConfig(ctx context.Context, vmName, bootstrap string, ssh scope.SSHClient) error {
208+
func setCloudConfig(ctx context.Context, vmName, storageName, bootstrap string, ssh scope.SSHClient) error {
213209
log := log.FromContext(ctx)
214210
log.Info("setting cloud config")
215211

@@ -226,7 +222,7 @@ func setCloudConfig(ctx context.Context, vmName, bootstrap string, ssh scope.SSH
226222
log.Info(configYaml)
227223

228224
// to do: should be set via API
229-
out, err := ssh.RunWithStdin(fmt.Sprintf("tee /var/lib/vz/%s/snippets/%s.yml", vmStorage, vmName), configYaml)
225+
out, err := ssh.RunWithStdin(fmt.Sprintf("tee /var/lib/vz/%s/snippets/%s.yml", storageName, vmName), configYaml)
230226
if err != nil {
231227
return errors.Errorf("ssh command error : %s : %v", out, err)
232228
}

cloud/services/compute/storage/reconcile.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"github.com/sp-yduck/proxmox/pkg/api"
88
"github.com/sp-yduck/proxmox/pkg/service/node/storage"
99
"sigs.k8s.io/controller-runtime/pkg/log"
10+
11+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
1012
)
1113

1214
const (
13-
vmStorage = "local-capi"
15+
defaultBasePath = "/var/lib/vz"
1416
)
1517

1618
func (s *Service) Reconcile(ctx context.Context) error {
@@ -21,17 +23,17 @@ func (s *Service) Reconcile(ctx context.Context) error {
2123
}
2224

2325
func (s *Service) Delete(ctx context.Context) error {
26+
// to do
2427
return nil
2528
}
2629

2730
// createOrGetStorage gets Proxmox Storage for VMs
2831
func (s *Service) createOrGetStorage(ctx context.Context) error {
2932
log := log.FromContext(ctx)
3033
log.Info("Reconciling vm storage")
31-
_, err := s.client.Storage(vmStorage)
32-
if err != nil {
34+
if err := s.getStorage(); err != nil {
3335
if api.IsNotFound(err) {
34-
if _, err := s.client.CreateStorage(vmStorage, "dir", defaultVMStorageOptions(vmStorage)); err != nil {
36+
if err := s.createStorage(); err != nil {
3537
return err
3638
}
3739
}
@@ -40,11 +42,39 @@ func (s *Service) createOrGetStorage(ctx context.Context) error {
4042
return nil
4143
}
4244

45+
func (s *Service) getStorage() error {
46+
storageSpec := s.scope.Storage()
47+
if _, err := s.client.Storage(storageSpec.Name); err != nil {
48+
return err
49+
}
50+
return nil
51+
}
52+
53+
func (s *Service) createStorage() error {
54+
storageSpec := s.scope.Storage()
55+
opts := storage.StorageCreateOptions{
56+
Content: "images,snippets",
57+
Mkdir: true,
58+
Path: generateStoragePath(storageSpec),
59+
}
60+
if _, err := s.client.CreateStorage(storageSpec.Name, "dif", opts); err != nil {
61+
return err
62+
}
63+
return nil
64+
}
65+
66+
func generateStoragePath(storage infrav1.Storage) string {
67+
if storage.Path == "" {
68+
return fmt.Sprintf("%s/%s", defaultBasePath, storage.Name)
69+
}
70+
return storage.Path
71+
}
72+
4373
func defaultVMStorageOptions(name string) storage.StorageCreateOptions {
4474
options := storage.StorageCreateOptions{
4575
Content: "images,snippets",
4676
Mkdir: true,
47-
Path: fmt.Sprintf("/var/lib/vz/%s", name),
77+
Path: defaultBasePath + "/" + name,
4878
}
4979
return options
5080
}

cloud/services/compute/storage/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type Scope interface {
10-
cloud.Machine
10+
cloud.Cluster
1111
}
1212

1313
type Service struct {

config/samples/infrastructure_v1beta1_proxmoxcluster.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ spec:
1414
port: 0
1515
credentialsRef:
1616
name: proxmoxcluster-sample
17+
storage:
18+
name: proxmoxcluster-sample

0 commit comments

Comments
 (0)