@@ -3,15 +3,22 @@ package proxmox
33import (
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+
1522type instance struct {
1623 compute * rest.RESTClient
1724}
@@ -32,8 +39,43 @@ func newInstances(config proxmoxConfig) (cloudprovider.InstancesV2, error) {
3239}
3340
3441func (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
3981func (i * instance ) InstanceShutdown (ctx context.Context , node * v1.Node ) (bool , error ) {
0 commit comments