chore: bump deps

Updated dependencies

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
This commit is contained in:
Serge Logvinov
2025-01-20 14:35:20 +02:00
committed by Serge
parent bb868bcbd7
commit ca452ad040
6 changed files with 76 additions and 59 deletions

View File

@@ -18,6 +18,7 @@ limitations under the License.
package cluster
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
@@ -27,6 +28,8 @@ import (
"strings"
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
"k8s.io/klog/v2"
)
// Cluster is a Proxmox client.
@@ -53,7 +56,7 @@ func NewCluster(config *ClustersConfig, hclient *http.Client) (*Cluster, error)
}
if cfg.Username != "" && cfg.Password != "" {
if err := client.Login(cfg.Username, cfg.Password, ""); err != nil {
if err := client.Login(context.Background(), cfg.Username, cfg.Password, ""); err != nil {
return nil, err
}
} else {
@@ -73,11 +76,22 @@ func NewCluster(config *ClustersConfig, hclient *http.Client) (*Cluster, error)
}
// CheckClusters checks if the Proxmox connection is working.
func (c *Cluster) CheckClusters() error {
func (c *Cluster) CheckClusters(ctx context.Context) error {
for region, client := range c.proxmox {
if _, err := client.GetVersion(); err != nil {
if _, err := client.GetVersion(ctx); err != nil {
return fmt.Errorf("failed to initialized proxmox client in region %s, error: %v", region, err)
}
vms, err := client.GetVmList(ctx)
if err != nil {
return fmt.Errorf("failed to get list of VMs in region %s, error: %v", region, err)
}
if len(vms) > 0 {
klog.V(4).InfoS("Proxmox cluster has VMs", "region", region, "count", len(vms))
} else {
klog.InfoS("Proxmox cluster has no VMs, or check the account permission", "region", region)
}
}
return nil
@@ -93,9 +107,9 @@ func (c *Cluster) GetProxmoxCluster(region string) (*pxapi.Client, error) {
}
// FindVMByName find a VM by name in all Proxmox clusters.
func (c *Cluster) FindVMByName(name string) (*pxapi.VmRef, string, error) {
func (c *Cluster) FindVMByName(ctx context.Context, name string) (*pxapi.VmRef, string, error) {
for region, px := range c.proxmox {
vmr, err := px.GetVmRefByName(name)
vmr, err := px.GetVmRefByName(ctx, name)
if err != nil {
if strings.Contains(err.Error(), "not found") {
continue
@@ -111,9 +125,9 @@ func (c *Cluster) FindVMByName(name string) (*pxapi.VmRef, string, error) {
}
// FindVMByUUID find a VM by uuid in all Proxmox clusters.
func (c *Cluster) FindVMByUUID(uuid string) (*pxapi.VmRef, string, error) {
func (c *Cluster) FindVMByUUID(ctx context.Context, uuid string) (*pxapi.VmRef, string, error) {
for region, px := range c.proxmox {
vms, err := px.GetResourceList("vm")
vms, err := px.GetResourceList(ctx, "vm")
if err != nil {
return nil, "", fmt.Errorf("error get resources %v", err)
}
@@ -132,7 +146,7 @@ func (c *Cluster) FindVMByUUID(uuid string) (*pxapi.VmRef, string, error) {
vmr.SetNode(vm["node"].(string)) //nolint:errcheck
vmr.SetVmType("qemu")
config, err := px.GetVmConfig(vmr)
config, err := px.GetVmConfig(ctx, vmr)
if err != nil {
return nil, "", err
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package cluster_test
import (
"context"
"fmt"
"net/http"
"strings"
@@ -78,7 +79,7 @@ func TestCheckClusters(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, pxapi)
err = client.CheckClusters()
err = client.CheckClusters(context.Background())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to initialized proxmox client in region")
}
@@ -125,7 +126,7 @@ func TestFindVMByNameNonExist(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, client)
vmr, cluster, err := client.FindVMByName("non-existing-vm")
vmr, cluster, err := client.FindVMByName(context.Background(), "non-existing-vm")
assert.NotNil(t, err)
assert.Equal(t, "", cluster)
assert.Nil(t, vmr)
@@ -202,7 +203,7 @@ func TestFindVMByNameExist(t *testing.T) {
testCase := testCase
t.Run(fmt.Sprint(testCase.msg), func(t *testing.T) {
vmr, cluster, err := client.FindVMByName(testCase.vmName)
vmr, cluster, err := client.FindVMByName(context.Background(), testCase.vmName)
if testCase.expectedError == nil {
assert.Nil(t, err)

View File

@@ -85,7 +85,7 @@ func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder,
c.ctx = ctx
c.stop = cancel
err := c.client.CheckClusters()
err := c.client.CheckClusters(ctx)
if err != nil {
klog.ErrorS(err, "failed to check proxmox cluster")
}

View File

@@ -47,7 +47,7 @@ func newInstances(client *cluster.Cluster, provider cluster.Provider) *instances
// InstanceExists returns true if the instance for the given node exists according to the cloud provider.
// Use the node.name or node.spec.providerID field to find the node in the cloud provider.
func (i *instances) InstanceExists(_ context.Context, node *v1.Node) (bool, error) {
func (i *instances) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
klog.V(4).InfoS("instances.InstanceExists() called", "node", klog.KRef("", node.Name))
if node.Spec.ProviderID == "" {
@@ -63,7 +63,7 @@ func (i *instances) InstanceExists(_ context.Context, node *v1.Node) (bool, erro
}
mc := metrics.NewMetricContext("getVmInfo")
if _, _, err := i.getInstance(node); mc.ObserveRequest(err) != nil {
if _, _, err := i.getInstance(ctx, node); mc.ObserveRequest(err) != nil {
if err == cloudprovider.InstanceNotFound {
klog.V(4).InfoS("instances.InstanceExists() instance not found", "node", klog.KObj(node), "providerID", node.Spec.ProviderID)
@@ -78,7 +78,7 @@ func (i *instances) InstanceExists(_ context.Context, node *v1.Node) (bool, erro
// InstanceShutdown returns true if the instance is shutdown according to the cloud provider.
// Use the node.name or node.spec.providerID field to find the node in the cloud provider.
func (i *instances) InstanceShutdown(_ context.Context, node *v1.Node) (bool, error) {
func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
klog.V(4).InfoS("instances.InstanceShutdown() called", "node", klog.KRef("", node.Name))
if node.Spec.ProviderID == "" {
@@ -109,7 +109,7 @@ func (i *instances) InstanceShutdown(_ context.Context, node *v1.Node) (bool, er
mc := metrics.NewMetricContext("getVmState")
vmState, err := px.GetVmState(vmr)
vmState, err := px.GetVmState(ctx, vmr)
if mc.ObserveRequest(err) != nil {
return false, err
}
@@ -124,7 +124,7 @@ func (i *instances) InstanceShutdown(_ context.Context, node *v1.Node) (bool, er
// InstanceMetadata returns the instance's metadata. The values returned in InstanceMetadata are
// translated into specific fields in the Node object on registration.
// Use the node.name or node.spec.providerID field to find the node in the cloud provider.
func (i *instances) InstanceMetadata(_ context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
klog.V(4).InfoS("instances.InstanceMetadata() called", "node", klog.KRef("", node.Name))
if providedIP, ok := node.ObjectMeta.Annotations[cloudproviderapi.AnnotationAlphaProvidedIPAddr]; ok {
@@ -142,11 +142,11 @@ func (i *instances) InstanceMetadata(_ context.Context, node *v1.Node) (*cloudpr
mc := metrics.NewMetricContext("findVmByName")
vmRef, region, err = i.c.FindVMByName(node.Name)
vmRef, region, err = i.c.FindVMByName(ctx, node.Name)
if mc.ObserveRequest(err) != nil {
mc := metrics.NewMetricContext("findVmByUUID")
vmRef, region, err = i.c.FindVMByUUID(uuid)
vmRef, region, err = i.c.FindVMByUUID(ctx, uuid)
if mc.ObserveRequest(err) != nil {
return nil, fmt.Errorf("instances.InstanceMetadata() - failed to find instance by name/uuid %s: %v, skipped", node.Name, err)
}
@@ -166,7 +166,7 @@ func (i *instances) InstanceMetadata(_ context.Context, node *v1.Node) (*cloudpr
if vmRef == nil {
mc := metrics.NewMetricContext("getVmInfo")
vmRef, region, err = i.getInstance(node)
vmRef, region, err = i.getInstance(ctx, node)
if mc.ObserveRequest(err) != nil {
return nil, err
}
@@ -180,7 +180,7 @@ func (i *instances) InstanceMetadata(_ context.Context, node *v1.Node) (*cloudpr
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeHostName, Address: node.Name})
instanceType, err := i.getInstanceType(vmRef, region)
instanceType, err := i.getInstanceType(ctx, vmRef, region)
if err != nil {
instanceType = vmRef.GetVmType()
}
@@ -189,7 +189,7 @@ func (i *instances) InstanceMetadata(_ context.Context, node *v1.Node) (*cloudpr
ProviderID: providerID,
NodeAddresses: addresses,
InstanceType: instanceType,
Zone: vmRef.Node(),
Zone: vmRef.Node().String(),
Region: region,
}, nil
}
@@ -199,11 +199,13 @@ func (i *instances) InstanceMetadata(_ context.Context, node *v1.Node) (*cloudpr
return &cloudprovider.InstanceMetadata{}, nil
}
func (i *instances) getInstance(node *v1.Node) (*pxapi.VmRef, string, error) {
func (i *instances) getInstance(ctx context.Context, node *v1.Node) (*pxapi.VmRef, string, error) {
klog.V(4).InfoS("instances.getInstance() called", "node", klog.KRef("", node.Name), "provider", i.provider)
if i.provider == cluster.ProviderCapmox {
uuid := node.Status.NodeInfo.SystemUUID
vmRef, region, err := i.c.FindVMByUUID(uuid)
vmRef, region, err := i.c.FindVMByUUID(ctx, uuid)
if err != nil {
return nil, "", fmt.Errorf("instances.getInstance() error: %v", err)
}
@@ -211,7 +213,7 @@ func (i *instances) getInstance(node *v1.Node) (*pxapi.VmRef, string, error) {
return vmRef, region, nil
}
vm, region, err := provider.ParseProviderID(node.Spec.ProviderID)
vmRef, region, err := provider.ParseProviderID(node.Spec.ProviderID)
if err != nil {
return nil, "", fmt.Errorf("instances.getInstance() error: %v", err)
}
@@ -223,7 +225,7 @@ func (i *instances) getInstance(node *v1.Node) (*pxapi.VmRef, string, error) {
mc := metrics.NewMetricContext("getVmInfo")
vmInfo, err := px.GetVmInfo(vm)
vmInfo, err := px.GetVmInfo(ctx, vmRef)
if mc.ObserveRequest(err) != nil {
if strings.Contains(err.Error(), "not found") {
return nil, "", cloudprovider.InstanceNotFound
@@ -238,10 +240,10 @@ func (i *instances) getInstance(node *v1.Node) (*pxapi.VmRef, string, error) {
klog.V(5).Infof("instances.getInstance() vmInfo %+v", vmInfo)
return vm, region, nil
return vmRef, region, nil
}
func (i *instances) getInstanceType(vmRef *pxapi.VmRef, region string) (string, error) {
func (i *instances) getInstanceType(ctx context.Context, vmRef *pxapi.VmRef, region string) (string, error) {
px, err := i.c.GetProxmoxCluster(region)
if err != nil {
return "", err
@@ -249,7 +251,7 @@ func (i *instances) getInstanceType(vmRef *pxapi.VmRef, region string) (string,
mc := metrics.NewMetricContext("getVmInfo")
vmInfo, err := px.GetVmInfo(vmRef)
vmInfo, err := px.GetVmInfo(ctx, vmRef)
if mc.ObserveRequest(err) != nil {
return "", err
}