Skip to content

Commit e52bd3c

Browse files
authored
Merge pull request #19 from sp-yduck/disk-size
fix #12
2 parents 7bb55d1 + 6ebb117 commit e52bd3c

File tree

6 files changed

+82
-35
lines changed

6 files changed

+82
-35
lines changed

api/v1beta1/type.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ type Hardware struct {
6363
// +kubebuilder:validation:Minimum:=16
6464
// +kubebuilder:default:=4096
6565
Memory int `json:"memory,omitempty"`
66+
67+
// hard disk size
68+
// +kubebuilder:default:="50G"
69+
Disk string `json:"disk,omitempty"`
6670
}
6771

6872
// Network

cloud/scope/machine.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ func (m *MachineScope) GetHardware() infrav1.Hardware {
181181
if m.ProxmoxMachine.Spec.Hardware.Memory == 0 {
182182
m.ProxmoxMachine.Spec.Hardware.Memory = 4096
183183
}
184+
// to do: validation
185+
if m.ProxmoxMachine.Spec.Hardware.Disk == "" {
186+
m.ProxmoxMachine.Spec.Hardware.Disk = "50G"
187+
}
184188
return m.ProxmoxMachine.Spec.Hardware
185189
}
186190

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"path"
7+
8+
"github.com/pkg/errors"
9+
"github.com/sp-yduck/proxmox/pkg/service/node/vm"
10+
"sigs.k8s.io/controller-runtime/pkg/log"
11+
12+
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
13+
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
14+
)
15+
16+
// reconcileBootDevice
17+
func (s *Service) reconcileBootDevice(ctx context.Context, vm *vm.VirtualMachine) error {
18+
vmid := s.scope.GetVMID()
19+
storage := s.scope.GetStorage()
20+
image := s.scope.GetImage()
21+
hardware := s.scope.GetHardware()
22+
log := log.FromContext(ctx)
23+
log.Info(fmt.Sprintf("%v", hardware))
24+
25+
// os image
26+
if err := SetCloudImage(ctx, *vmid, storage, image, s.remote); err != nil {
27+
return err
28+
}
29+
30+
// volume
31+
if err := vm.ResizeVolume("scsi0", hardware.Disk); err != nil {
32+
return err
33+
}
34+
35+
return nil
36+
}
37+
38+
// setCloudImage downloads OS image into Proxmox node
39+
// and then sets it to specified storage
40+
func SetCloudImage(ctx context.Context, vmid int, storage infrav1.Storage, image infrav1.Image, ssh scope.SSHClient) error {
41+
log := log.FromContext(ctx)
42+
log.Info("setting cloud image")
43+
44+
url := image.URL
45+
fileName := path.Base(url)
46+
rawImageDirPath := fmt.Sprintf("%s/images", etcCAPP)
47+
rawImageFilePath := fmt.Sprintf("%s/%s", rawImageDirPath, fileName)
48+
49+
// workaround
50+
// API does not support something equivalent of "qm importdisk"
51+
out, err := ssh.RunCommand(fmt.Sprintf("wget %s --directory-prefix %s -nc", url, rawImageDirPath))
52+
if err != nil {
53+
return errors.Errorf("failed to download image: %s : %v", out, err)
54+
}
55+
56+
// to do: should confirm if the checksum matchies
57+
58+
destPath := fmt.Sprintf("%s/images/%d/vm-%d-disk-0.raw", storage.Path, vmid, vmid)
59+
out, err = ssh.RunCommand(fmt.Sprintf("/usr/bin/qemu-img convert -O raw %s %s", rawImageFilePath, destPath))
60+
if err != nil {
61+
return errors.Errorf("failed to convert iamge : %s : %v", out, err)
62+
}
63+
return nil
64+
}

cloud/services/compute/instance/reconcile.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package instance
33
import (
44
"context"
55
"fmt"
6-
"path"
76
"regexp"
87
"strconv"
98
"strings"
@@ -16,7 +15,6 @@ import (
1615

1716
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
1817
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/providerid"
19-
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
2018
)
2119

2220
const (
@@ -206,14 +204,8 @@ func (s *Service) CreateInstance(ctx context.Context, bootstrap string) (*vm.Vir
206204
return nil, err
207205
}
208206

209-
// os image
210-
if err := SetCloudImage(ctx, vmid, s.scope.GetStorage(), s.scope.GetImage(), s.remote); err != nil {
211-
return nil, err
212-
}
213-
214-
// volume
215-
// to do: size option
216-
if err := vm.ResizeVolume("scsi0", "+30G"); err != nil {
207+
// set cloud image to hard disk and then resize
208+
if err := s.reconcileBootDevice(ctx, vm); err != nil {
217209
return nil, err
218210
}
219211

@@ -228,31 +220,6 @@ func IsNotFound(err error) bool {
228220
return api.IsNotFound(err)
229221
}
230222

231-
// setCloudImage set OS image to specified storage
232-
func SetCloudImage(ctx context.Context, vmid int, storage infrav1.Storage, image infrav1.Image, ssh scope.SSHClient) error {
233-
log := log.FromContext(ctx)
234-
log.Info("setting cloud image")
235-
236-
url := image.URL
237-
fileName := path.Base(url)
238-
rawImageDirPath := fmt.Sprintf("%s/images", etcCAPP)
239-
rawImageFilePath := fmt.Sprintf("%s/%s", rawImageDirPath, fileName)
240-
241-
// workaround
242-
// API does not support something equivalent of "qm importdisk"
243-
out, err := ssh.RunCommand(fmt.Sprintf("wget %s --directory-prefix %s -nc", url, rawImageDirPath))
244-
if err != nil {
245-
return errors.Errorf("failed to download image: %s : %v", out, err)
246-
}
247-
248-
destPath := fmt.Sprintf("%s/images/%d/vm-%d-disk-0.raw", storage.Path, vmid, vmid)
249-
out, err = ssh.RunCommand(fmt.Sprintf("/usr/bin/qemu-img convert -O raw %s %s", rawImageFilePath, destPath))
250-
if err != nil {
251-
return errors.Errorf("failed to convert iamge : %s : %v", out, err)
252-
}
253-
return nil
254-
}
255-
256223
func EnsureRunning(instance vm.VirtualMachine) error {
257224
// ensure instance is running
258225
switch instance.Status {

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachines.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ spec:
8585
default: 2
8686
description: 'number of CPU cores : 1 ~'
8787
type: integer
88+
disk:
89+
default: 50G
90+
description: hard disk size
91+
type: string
8892
memory:
8993
default: 4096
9094
description: 'amount of RAM for the VM in MiB : 16 ~'

config/crd/bases/infrastructure.cluster.x-k8s.io_proxmoxmachinetemplates.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ spec:
109109
default: 2
110110
description: 'number of CPU cores : 1 ~'
111111
type: integer
112+
disk:
113+
default: 50G
114+
description: hard disk size
115+
type: string
112116
memory:
113117
default: 4096
114118
description: 'amount of RAM for the VM in MiB : 16 ~'

0 commit comments

Comments
 (0)