Skip to content

Commit df11ce9

Browse files
committed
implement instanceExists()
1 parent b704002 commit df11ce9

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/pkg/errors v0.9.1
7-
github.com/sp-yduck/proxmox-go v0.0.0-20230717071731-9fdab94cfe8d
7+
github.com/sp-yduck/proxmox-go v0.0.0-alpha1
88
github.com/spf13/pflag v1.0.5
99
gopkg.in/yaml.v3 v3.0.1
1010
k8s.io/api v0.27.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
263263
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
264264
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
265265
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
266-
github.com/sp-yduck/proxmox-go v0.0.0-20230717071731-9fdab94cfe8d h1:qCiXtxzEl9h5YLd9yOhLULnt7dCcEP5DS7PCGhmX3J0=
267-
github.com/sp-yduck/proxmox-go v0.0.0-20230717071731-9fdab94cfe8d/go.mod h1:hrQc1bUUCN71yn+in+LkfdpJstaaMv2nrn3+GeKFbkA=
266+
github.com/sp-yduck/proxmox-go v0.0.0-alpha1 h1:o7L6ua8fdgE0zXPNRs5apMDsodwKaCPQGWYPN4ywNoQ=
267+
github.com/sp-yduck/proxmox-go v0.0.0-alpha1/go.mod h1:hrQc1bUUCN71yn+in+LkfdpJstaaMv2nrn3+GeKFbkA=
268268
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
269269
github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI=
270270
github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=

pkg/cloudprovider/instances.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ package proxmox
33
import (
44
"context"
55
"crypto/tls"
6+
"errors"
67
"fmt"
78
"net/http"
9+
"regexp"
10+
"strings"
811

912
"github.com/sp-yduck/proxmox-go/rest"
1013
v1 "k8s.io/api/core/v1"
1114
cloudprovider "k8s.io/cloud-provider"
1215
"k8s.io/klog/v2"
1316
)
1417

18+
const (
19+
UUIDFormat = `[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}`
20+
)
21+
1522
type instance struct {
1623
compute *rest.RESTClient
1724
}
@@ -32,8 +39,43 @@ func newInstances(config proxmoxConfig) (cloudprovider.InstancesV2, error) {
3239
}
3340

3441
func (i *instance) InstanceExists(ctc context.Context, node *v1.Node) (bool, error) {
35-
klog.Info("checking if instance exists")
36-
return true, nil
42+
klog.Info("checking if instance exists (node=%s)", node.Name)
43+
44+
nodes, err := i.compute.GetNodes()
45+
if err != nil {
46+
return true, err
47+
}
48+
for _, n := range nodes {
49+
vms, err := i.compute.GetVirtualMachines(n.Node)
50+
if err != nil {
51+
return true, err
52+
}
53+
for _, vm := range vms {
54+
config, err := i.compute.GetVirtualMachineConfig(n.Node, vm.VMID)
55+
if err != nil {
56+
return true, err
57+
}
58+
smbios := config.SMBios1
59+
uuid, err := convertSMBiosToUUID(smbios)
60+
if err != nil {
61+
return true, err
62+
}
63+
if uuid == node.Status.NodeInfo.SystemUUID {
64+
return true, nil
65+
}
66+
}
67+
}
68+
return false, nil
69+
}
70+
71+
func convertSMBiosToUUID(smbios string) (string, error) {
72+
re := regexp.MustCompile(fmt.Sprintf("uuid=%s", UUIDFormat))
73+
match := re.FindString(smbios)
74+
if match == "" {
75+
return "", errors.New("failed to fetch uuid form smbios")
76+
}
77+
// match: uuid=<uuid>
78+
return strings.Split(match, "=")[1], nil
3779
}
3880

3981
func (i *instance) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {

0 commit comments

Comments
 (0)