Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the order of obtaining subnet-id #234

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions pkg/cloudprovider/huaweicloud/huaweicloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
}
Expand Down
59 changes: 10 additions & 49 deletions pkg/cloudprovider/huaweicloud/sharedloadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down