diff --git a/pkg/cloudprovider/huaweicloud/huaweicloud.go b/pkg/cloudprovider/huaweicloud/huaweicloud.go index 570a2b4fc..bfe78c4bc 100644 --- a/pkg/cloudprovider/huaweicloud/huaweicloud.go +++ b/pkg/cloudprovider/huaweicloud/huaweicloud.go @@ -47,14 +47,14 @@ import ( servicehelper "k8s.io/cloud-provider/service/helpers" "k8s.io/klog/v2" "k8s.io/utils/pointer" - "sigs.k8s.io/cloud-provider-huaweicloud/pkg/common" - "sigs.k8s.io/cloud-provider-huaweicloud/pkg/utils" ecsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model" vpcmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v2/model" "sigs.k8s.io/cloud-provider-huaweicloud/pkg/cloudprovider/huaweicloud/wrapper" + "sigs.k8s.io/cloud-provider-huaweicloud/pkg/common" "sigs.k8s.io/cloud-provider-huaweicloud/pkg/config" + "sigs.k8s.io/cloud-provider-huaweicloud/pkg/utils" "sigs.k8s.io/cloud-provider-huaweicloud/pkg/utils/mutexkv" ) @@ -151,16 +151,20 @@ func (b Basic) sendEvent(reason, msg string, service *v1.Service) { } func (b Basic) getSubnetID(service *v1.Service, node *v1.Node) (string, error) { - subnetID := getStringFromSvsAnnotation(service, ElbSubnetID, b.cloudConfig.VpcOpts.SubnetID) + subnetID, err := b.getNodeSubnetID(node) + if err != nil { + klog.Warningf("unable to read subnet-id from the node, try reading from service or cloud-config, error: %s", err) + } if subnetID != "" { return subnetID, nil } - subnetID, err := b.getNodeSubnetID(node) - if err != nil { + subnetID = getStringFromSvsAnnotation(service, ElbSubnetID, b.cloudConfig.VpcOpts.SubnetID) + if subnetID == "" { return "", status.Errorf(codes.InvalidArgument, "missing subnet-id, "+ - "can not to read subnet-id from the node also, error: %s", err) + "can not to read subnet-id from service or cloud-config") } + return subnetID, nil } @@ -180,9 +184,9 @@ func (b Basic) getNodeSubnetID(node *v1.Node) (string, error) { return "", err } - for _, intfs := range interfaces { - for _, fixedIP := range *intfs.FixedIps { - if *fixedIP.IpAddress == ipAddress { + for _, inter := range interfaces { + for _, fixedIP := range *inter.FixedIps { + if fixedIP.IpAddress != nil && *fixedIP.IpAddress == ipAddress { return *fixedIP.SubnetId, nil } } diff --git a/pkg/cloudprovider/huaweicloud/sharedloadbalancer.go b/pkg/cloudprovider/huaweicloud/sharedloadbalancer.go index 957302ae4..15081390c 100644 --- a/pkg/cloudprovider/huaweicloud/sharedloadbalancer.go +++ b/pkg/cloudprovider/huaweicloud/sharedloadbalancer.go @@ -20,10 +20,8 @@ import ( "context" "encoding/json" "fmt" - ecsmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model" - eipmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2/model" - elbmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v2/model" - elbmodelv3 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model" + "strconv" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" corev1 "k8s.io/api/core/v1" @@ -33,7 +31,10 @@ import ( cloudprovider "k8s.io/cloud-provider" "k8s.io/klog/v2" "k8s.io/utils/pointer" - "strconv" + + eipmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2/model" + elbmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v2/model" + elbmodelv3 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model" "sigs.k8s.io/cloud-provider-huaweicloud/pkg/cloudprovider/huaweicloud/wrapper" "sigs.k8s.io/cloud-provider-huaweicloud/pkg/common" @@ -155,9 +156,10 @@ func (l *SharedLoadBalancer) EnsureLoadBalancer(ctx context.Context, clusterName return nil, err } if err != nil && common.IsNotFound(err) { - subnetID, e := l.getSubnetID(service, nodes[0]) - if e != nil { - return nil, e + subnetID := getStringFromSvsAnnotation(service, ElbSubnetID, l.cloudConfig.VpcOpts.SubnetID) + if subnetID == "" { + return nil, status.Errorf(codes.InvalidArgument, "missing subnet-id, "+ + "can not to read subnet-id from service or cloud-config") } loadbalancer, err = l.createLoadbalancer(clusterName, subnetID, service) } @@ -989,47 +991,6 @@ func unbindEIP(eipClient *wrapper.EIpClient, vipPortID, eipID string, keepEIP bo return nil } -func (l *SharedLoadBalancer) getSubnetID(service *v1.Service, node *v1.Node) (string, error) { - subnetID := getStringFromSvsAnnotation(service, ElbSubnetID, l.cloudConfig.VpcOpts.SubnetID) - if subnetID != "" { - return subnetID, nil - } - - subnetID, err := l.getNodeSubnetID(node) - if err != nil { - return "", status.Errorf(codes.InvalidArgument, "missing subnet-id, "+ - "can not to read subnet-id from the node also, error: %s", err) - } - return subnetID, nil -} - -func (l *SharedLoadBalancer) getNodeSubnetID(node *corev1.Node) (string, error) { - ipAddress, err := getNodeAddress(node) - if err != nil { - return "", err - } - - instance, err := l.ecsClient.GetByNodeName(node.Name) - if err != nil { - return "", err - } - - interfaces, err := l.ecsClient.ListInterfaces(&ecsmodel.ListServerInterfacesRequest{ServerId: instance.Id}) - if err != nil { - return "", err - } - - for _, ia := range interfaces { - for _, fixedIP := range *ia.FixedIps { - if *fixedIP.IpAddress == ipAddress { - return *fixedIP.SubnetId, nil - } - } - } - - return "", fmt.Errorf("failed to get node subnet ID") -} - func getNodeAddress(node *corev1.Node) (string, error) { addresses := node.Status.Addresses if len(addresses) == 0 {