From db59e7ae5db8d69bea23a52cd2def82801ec4a1e Mon Sep 17 00:00:00 2001 From: Alfred Krohmer Date: Mon, 12 Aug 2024 16:32:46 +0200 Subject: [PATCH] fix: implement HasInstance() for OCI providers This implements HasInstance() for the OCI instance pool and node pool providers. --- .../oci/instancepools/oci_cloud_provider.go | 24 ++++++++++++++++--- .../oci/nodepools/oci_cloud_provider.go | 21 ++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/cluster-autoscaler/cloudprovider/oci/instancepools/oci_cloud_provider.go b/cluster-autoscaler/cloudprovider/oci/instancepools/oci_cloud_provider.go index bb8ec2f2cab4..dfcc1728f7f2 100644 --- a/cluster-autoscaler/cloudprovider/oci/instancepools/oci_cloud_provider.go +++ b/cluster-autoscaler/cloudprovider/oci/instancepools/oci_cloud_provider.go @@ -5,6 +5,8 @@ Copyright 2021-2023 Oracle and/or its affiliates. package instancepools import ( + "strings" + "github.com/pkg/errors" apiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -19,7 +21,6 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog/v2" - "strings" ) // OciCloudProvider implements the CloudProvider interface for OCI. It contains an @@ -66,8 +67,25 @@ func (ocp *OciCloudProvider) NodeGroupForNode(n *apiv1.Node) (cloudprovider.Node } // HasInstance returns whether a given node has a corresponding instance in this cloud provider -func (ocp *OciCloudProvider) HasInstance(n *apiv1.Node) (bool, error) { - return true, cloudprovider.ErrNotImplemented +func (ocp *OciCloudProvider) HasInstance(node *apiv1.Node) (bool, error) { + instance, err := ocicommon.NodeToOciRef(node) + if err != nil { + return false, err + } + instancePool, err := ocp.poolManager.GetInstancePoolForInstance(instance) + if err != nil { + return false, err + } + instances, err := ocp.poolManager.GetInstancePoolNodes(*instancePool) + if err != nil { + return false, err + } + for _, i := range instances { + if i.Id == instance.InstanceID { + return true, nil + } + } + return false, nil } // Pricing returns pricing model for this cloud provider or error if not available. diff --git a/cluster-autoscaler/cloudprovider/oci/nodepools/oci_cloud_provider.go b/cluster-autoscaler/cloudprovider/oci/nodepools/oci_cloud_provider.go index abe69e432b49..b97f98fc6179 100644 --- a/cluster-autoscaler/cloudprovider/oci/nodepools/oci_cloud_provider.go +++ b/cluster-autoscaler/cloudprovider/oci/nodepools/oci_cloud_provider.go @@ -70,8 +70,25 @@ func (ocp *OciCloudProvider) GetNodeGpuConfig(node *apiv1.Node) *cloudprovider.G } // HasInstance returns whether a given node has a corresponding instance in this cloud provider -func (ocp *OciCloudProvider) HasInstance(n *apiv1.Node) (bool, error) { - return true, cloudprovider.ErrNotImplemented +func (ocp *OciCloudProvider) HasInstance(node *apiv1.Node) (bool, error) { + instance, err := ocicommon.NodeToOciRef(node) + if err != nil { + return true, err + } + np, err := ocp.manager.GetNodePoolForInstance(instance) + if err != nil { + return true, err + } + nodes, err := ocp.manager.GetNodePoolNodes(np) + if err != nil { + return true, err + } + for _, n := range nodes { + if n.Id == instance.InstanceID { + return true, nil + } + } + return false, nil } // Pricing returns pricing model for this cloud provider or error if not available.