Skip to content

Commit 5a20c4e

Browse files
committed
use websocket instead of ssh
1 parent 1d70f39 commit 5a20c4e

File tree

15 files changed

+45
-192
lines changed

15 files changed

+45
-192
lines changed

api/v1beta1/proxmoxcluster_types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ type ProxmoxClusterSpec struct {
3838
// ServerRef is used for configuring Proxmox client
3939
ServerRef ServerRef `json:"serverRef"`
4040

41-
// NodesRef contains reference of nodes used for ProxmoxCluster
42-
NodeRefs []NodeRef `json:"nodeRefs,omitempty"`
43-
4441
// storage is for proxmox storage used by vm instances
4542
// +optional
4643
Storage Storage `json:"storage"`

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/interfaces.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
88

99
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
10-
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
1110
)
1211

1312
type Reconciler interface {
@@ -17,7 +16,6 @@ type Reconciler interface {
1716

1817
type Client interface {
1918
CloudClient() *proxmox.Service
20-
RemoteClient() *scope.SSHClient
2119
}
2220

2321
type Cluster interface {

cloud/scope/clients.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929

3030
type ProxmoxServices struct {
3131
Compute *proxmox.Service
32-
Remote *SSHClient
3332
}
3433

3534
func newComputeService(ctx context.Context, serverRef infrav1.ServerRef, crClient client.Client) (*proxmox.Service, error) {
@@ -53,30 +52,3 @@ func newComputeService(ctx context.Context, serverRef infrav1.ServerRef, crClien
5352

5453
return proxmox.NewService(serverRef.Endpoint, authConfig, true)
5554
}
56-
57-
func newRemoteClient(ctx context.Context, secretRef *infrav1.ObjectReference, crClient client.Client) (*SSHClient, error) {
58-
if secretRef == nil {
59-
return nil, errors.New("failed to get proxmox client form nil secretRef")
60-
}
61-
62-
var secret corev1.Secret
63-
key := client.ObjectKey{Namespace: secretRef.Namespace, Name: secretRef.Name}
64-
if err := crClient.Get(ctx, key, &secret); err != nil {
65-
return nil, err
66-
}
67-
68-
nodeurl, ok := secret.Data["NODE_URL"]
69-
if !ok {
70-
return nil, errors.Errorf("failed to fetch NODE_URL from Secret : %v", key)
71-
}
72-
nodeuser, ok := secret.Data["NODE_USER"]
73-
if !ok {
74-
return nil, errors.Errorf("failed to fetch PROXMOX_USER from Secret : %v", key)
75-
}
76-
nodepassword, ok := secret.Data["NODE_PASSWORD"]
77-
if !ok {
78-
return nil, errors.Errorf("failed to fetch PROXMOX_PASSWORD from Secret : %v", key)
79-
}
80-
81-
return NewSSHClient(string(nodeurl), string(nodeuser), string(nodepassword))
82-
}

cloud/scope/cluster.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterSc
5252
params.ProxmoxServices.Compute = computeSvc
5353
}
5454

55-
if params.ProxmoxServices.Remote == nil {
56-
// current CAPPX is compatible with only single node proxmox cluster
57-
remote, err := newRemoteClient(ctx, params.ProxmoxCluster.Spec.NodeRefs[0].SecretRef, params.Client)
58-
if err != nil {
59-
return nil, errors.Errorf("failed to create remote client: %v", err)
60-
}
61-
params.ProxmoxServices.Remote = remote
62-
}
63-
6455
helper, err := patch.NewHelper(params.ProxmoxCluster, params.Client)
6556
if err != nil {
6657
return nil, errors.Wrap(err, "failed to init patch helper")
@@ -79,11 +70,6 @@ func populateNamespace(proxmoxCluster *infrav1.ProxmoxCluster) {
7970
if proxmoxCluster.Spec.ServerRef.SecretRef.Namespace == "" {
8071
proxmoxCluster.Spec.ServerRef.SecretRef.Namespace = proxmoxCluster.Namespace
8172
}
82-
for i, nodeRef := range proxmoxCluster.Spec.NodeRefs {
83-
if nodeRef.SecretRef.Namespace == "" {
84-
proxmoxCluster.Spec.NodeRefs[i].SecretRef.Namespace = proxmoxCluster.Namespace
85-
}
86-
}
8773
}
8874

8975
type ClusterScope struct {
@@ -114,10 +100,6 @@ func (s *ClusterScope) CloudClient() *proxmox.Service {
114100
return s.ProxmoxServices.Compute
115101
}
116102

117-
func (s *ClusterScope) RemoteClient() *SSHClient {
118-
return s.ProxmoxServices.Remote
119-
}
120-
121103
func (s *ClusterScope) Close() error {
122104
return s.PatchObject()
123105
}

cloud/scope/machine.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ func (m *MachineScope) CloudClient() *proxmox.Service {
8282
return m.ClusterGetter.CloudClient()
8383
}
8484

85-
func (m *MachineScope) RemoteClient() *SSHClient {
86-
return m.ClusterGetter.Remote
87-
}
88-
8985
func (m *MachineScope) GetStorage() infrav1.Storage {
9086
return m.ClusterGetter.ProxmoxCluster.Spec.Storage
9187
}

cloud/scope/remote.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

cloud/services/compute/instance/cloudinit.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const (
1616
)
1717

1818
// reconcileCloudInit
19-
func (s *Service) reconcileCloudInit(bootstrap string) error {
19+
func (s *Service) reconcileCloudInit(ctx context.Context, bootstrap string) error {
2020
// user
21-
if err := s.reconcileCloudInitUser(bootstrap); err != nil {
21+
if err := s.reconcileCloudInitUser(ctx, bootstrap); err != nil {
2222
return err
2323
}
2424
return nil
@@ -42,9 +42,8 @@ func (s *Service) deleteCloudConfig(ctx context.Context) error {
4242
return storage.DeleteVolume(ctx, volumeID)
4343
}
4444

45-
func (s *Service) reconcileCloudInitUser(bootstrap string) error {
45+
func (s *Service) reconcileCloudInitUser(ctx context.Context, bootstrap string) error {
4646
vmName := s.scope.Name()
47-
storagePath := s.scope.GetStorage().Path
4847
config := s.scope.GetCloudInit().User
4948

5049
bootstrapConfig, err := cloudinit.ParseUser(bootstrap)
@@ -66,13 +65,17 @@ func (s *Service) reconcileCloudInitUser(bootstrap string) error {
6665
if err != nil {
6766
return err
6867
}
69-
7068
klog.Info(configYaml)
7169

7270
// to do: should be set via API
73-
out, err := s.remote.RunWithStdin(fmt.Sprintf("tee %s/%s", storagePath, userSnippetPath(vmName)), configYaml)
71+
vnc, err := s.vncClient(s.scope.NodeName())
7472
if err != nil {
75-
return errors.Errorf("ssh command error : %s : %v", out, err)
73+
return err
74+
}
75+
defer vnc.Close()
76+
filePath := fmt.Sprintf("%s/%s", s.scope.GetStorage().Path, userSnippetPath(vmName))
77+
if err := vnc.WriteFile(context.TODO(), configYaml, filePath); err != nil {
78+
return errors.Errorf("failed to write file error : %v", err)
7679
}
7780

7881
return nil

cloud/services/compute/instance/image.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,20 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/sp-yduck/proxmox-go/proxmox"
1111
"sigs.k8s.io/controller-runtime/pkg/log"
12-
13-
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
14-
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
1512
)
1613

1714
// reconcileBootDevice
1815
func (s *Service) reconcileBootDevice(ctx context.Context, vm *proxmox.VirtualMachine) error {
19-
vmid := s.scope.GetVMID()
20-
storage := s.scope.GetStorage()
21-
image := s.scope.GetImage()
22-
hardware := s.scope.GetHardware()
2316
log := log.FromContext(ctx)
24-
log.Info(fmt.Sprintf("%v", hardware))
17+
log.Info("reconcile boot device")
2518

2619
// os image
27-
if err := setCloudImage(ctx, *vmid, storage, image, s.remote); err != nil {
20+
if err := s.setCloudImage(ctx); err != nil {
2821
return err
2922
}
3023

3124
// volume
32-
if err := vm.ResizeVolume(ctx, "scsi0", hardware.Disk); err != nil {
25+
if err := vm.ResizeVolume(ctx, bootDvice, s.scope.GetHardware().Disk); err != nil {
3326
return err
3427
}
3528

@@ -38,18 +31,21 @@ func (s *Service) reconcileBootDevice(ctx context.Context, vm *proxmox.VirtualMa
3831

3932
// setCloudImage downloads OS image into Proxmox node
4033
// and then sets it to specified storage
41-
func setCloudImage(ctx context.Context, vmid int, storage infrav1.Storage, image infrav1.Image, ssh scope.SSHClient) error {
34+
func (s *Service) setCloudImage(ctx context.Context) error {
4235
log := log.FromContext(ctx)
4336
log.Info("setting cloud image")
4437

38+
image := s.scope.GetImage()
4539
url := image.URL
4640
fileName := path.Base(url)
4741
rawImageDirPath := fmt.Sprintf("%s/images", etcCAPPX)
4842
rawImageFilePath := fmt.Sprintf("%s/%s", rawImageDirPath, fileName)
4943

5044
// workaround
5145
// API does not support something equivalent of "qm importdisk"
52-
out, err := ssh.RunCommand(fmt.Sprintf("wget %s --directory-prefix %s -nc", url, rawImageDirPath))
46+
vnc, err := s.vncClient(s.scope.NodeName())
47+
defer vnc.Close()
48+
out, _, err := vnc.Exec(ctx, fmt.Sprintf("wget %s --directory-prefix %s -nc", url, rawImageDirPath))
5349
if err != nil {
5450
return errors.Errorf("failed to download image: %s : %v", out, err)
5551
}
@@ -60,16 +56,16 @@ func setCloudImage(ctx context.Context, vmid int, storage infrav1.Storage, image
6056
if err != nil {
6157
return err
6258
}
63-
cmd := fmt.Sprintf("%s --check -", cscmd)
64-
stdin := fmt.Sprintf("%s %s", image.Checksum, rawImageFilePath)
65-
out, err = ssh.RunWithStdin(cmd, stdin)
59+
cmd := fmt.Sprintf("echo '%s %s' | %s --check -", image.Checksum, rawImageFilePath, cscmd)
60+
out, _, err = vnc.Exec(context.TODO(), cmd)
6661
if err != nil {
6762
return errors.Errorf("failed to confirm checksum: %s : %v", out, err)
6863
}
6964
}
7065

71-
destPath := fmt.Sprintf("%s/images/%d/vm-%d-disk-0.raw", storage.Path, vmid, vmid)
72-
out, err = ssh.RunCommand(fmt.Sprintf("/usr/bin/qemu-img convert -O raw %s %s", rawImageFilePath, destPath))
66+
vmid := s.scope.GetVMID()
67+
destPath := fmt.Sprintf("%s/images/%d/vm-%d-disk-0.raw", s.scope.GetStorage().Path, *vmid, *vmid)
68+
out, _, err = vnc.Exec(context.TODO(), fmt.Sprintf("/usr/bin/qemu-img convert -O raw %s %s", rawImageFilePath, destPath))
7369
if err != nil {
7470
return errors.Errorf("failed to convert iamge : %s : %v", out, err)
7571
}

cloud/services/compute/instance/qemu.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
infrav1 "github.com/sp-yduck/cluster-api-provider-proxmox/api/v1beta1"
1616
)
1717

18+
const (
19+
bootDvice = "scsi0"
20+
)
21+
1822
func (s *Service) reconcileQEMU(ctx context.Context) (*proxmox.VirtualMachine, error) {
1923
log := log.FromContext(ctx)
2024
log.Info("Reconciling QEMU")
@@ -109,7 +113,7 @@ func generateVMOptions(vmName, storageName string, network infrav1.Network, hard
109113
Memory: hardware.Memory,
110114
Name: vmName,
111115
NameServer: network.NameServer,
112-
Boot: "order=scsi0",
116+
Boot: fmt.Sprintf("order=%s", bootDvice),
113117
Ide: api.Ide{Ide2: fmt.Sprintf("file=%s:cloudinit,media=cdrom", storageName)},
114118
CiCustom: fmt.Sprintf("user=%s:%s", storageName, userSnippetPath(vmName)),
115119
IPConfig: api.IPConfig{IPConfig0: network.IPConfig.String()},

0 commit comments

Comments
 (0)