Skip to content

Commit

Permalink
Merge pull request #236 from chengxiangdong/fix_sid
Browse files Browse the repository at this point in the history
fix: query ECS based on Pod HostIP to obtain subnet ID instead of HostName
  • Loading branch information
k8s-ci-robot authored Dec 26, 2023
2 parents 02185c4 + b7c4225 commit 798f900
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pkg/cloudprovider/huaweicloud/dedicatedloadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
"context"
"encoding/json"
"fmt"
cloudprovider "k8s.io/cloud-provider"
"strings"

elbmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/errors"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"

elbmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model"

"sigs.k8s.io/cloud-provider-huaweicloud/pkg/common"
"sigs.k8s.io/cloud-provider-huaweicloud/pkg/config"
"sigs.k8s.io/cloud-provider-huaweicloud/pkg/utils"
Expand Down Expand Up @@ -589,7 +590,7 @@ func (d *DedicatedLoadBalancer) addMember(service *v1.Service, loadbalancer *elb
Address: address,
}
if !loadbalancer.IpTargetEnable {
subnetID, err := d.getSubnetID(service, node)
subnetID, err := d.getNodeSubnetIDByHostIP(address)
if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/cloudprovider/huaweicloud/huaweicloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ func (b Basic) getSubnetID(service *v1.Service, node *v1.Node) (string, error) {
return subnetID, nil
}

func (b Basic) getNodeSubnetIDByHostIP(privateIP string) (string, error) {
instance, err := b.ecsClient.GetByNodeIP(privateIP)
if err != nil {
return "", err
}

interfaces, err := b.ecsClient.ListInterfaces(&ecsmodel.ListServerInterfacesRequest{ServerId: instance.Id})
if err != nil {
return "", err
}

for _, inter := range interfaces {
for _, fixedIP := range *inter.FixedIps {
if fixedIP.IpAddress != nil && *fixedIP.IpAddress == privateIP {
return *fixedIP.SubnetId, nil
}
}
}

return "", fmt.Errorf("failed to get node subnet ID with private IP: %s", privateIP)
}

func (b Basic) getNodeSubnetID(node *v1.Node) (string, error) {
ipAddress, err := getNodeAddress(node)
if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions pkg/cloudprovider/huaweicloud/sharedloadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (l *SharedLoadBalancer) addOrRemoveMembers(loadbalancer *elbmodel.Loadbalan
pod.Namespace, pod.Spec.NodeName)
}

//address, err := getNodeAddress(node)
// address, err := getNodeAddress(node)
address, portNum, err := getMemberIP(service, node, pod, svcPort)
if err != nil {
if common.IsNotFound(err) {
Expand Down Expand Up @@ -472,6 +472,10 @@ func getMemberIP(service *v1.Service, node *v1.Node, pod v1.Pod, svcPort v1.Serv
if service.Spec.AllocateLoadBalancerNodePorts != nil && *service.Spec.AllocateLoadBalancerNodePorts {
klog.V(6).Infof("add member using the Node's IP and port, service: %s/%s, port: %s ", service.Namespace, service.Name, svcPort.Name)

if pod.Status.HostIP != "" {
return pod.Status.HostIP, svcPort.NodePort, nil
}

address, err := getNodeAddress(node)
if err != nil {
return "", 0, err
Expand Down Expand Up @@ -506,18 +510,20 @@ func (l *SharedLoadBalancer) addMember(service *v1.Service, elbID, poolID string
return err
}

subnetID, err := l.getSubnetID(service, node)
subnetID, err := l.getNodeSubnetIDByHostIP(address)
if err != nil {
return err
}

_, err = l.sharedELBClient.AddMember(poolID, &elbmodel.CreateMemberReq{
req := elbmodel.CreateMemberReq{
ProtocolPort: port,
SubnetId: subnetID,
Address: address,
})
}
_, err = l.sharedELBClient.AddMember(poolID, &req)
if err != nil {
return fmt.Errorf("error creating SharedLoadBalancer pool member for node: %s, %v", node.Name, err)
return fmt.Errorf("error creating SharedLoadBalancer pool member for node: %s, %v, options: %s",
node.Name, err, utils.ToString(req))
}

loadbalancer, err := l.sharedELBClient.WaitStatusActive(elbID)
Expand Down
37 changes: 34 additions & 3 deletions pkg/cloudprovider/huaweicloud/wrapper/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"sort"
"strings"

"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/sdkerr"
ecs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"

"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/sdkerr"
ecs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2/model"

"sigs.k8s.io/cloud-provider-huaweicloud/pkg/config"
"sigs.k8s.io/cloud-provider-huaweicloud/pkg/utils"
)
Expand Down Expand Up @@ -94,6 +95,36 @@ func (e *EcsClient) GetByNodeName(name string) (*model.ServerDetail, error) {
return nil, notFound
}

func (e *EcsClient) GetByNodeIP(privateIP string) (*model.ServerDetail, error) {
if privateIP == "" {
return nil, fmt.Errorf("privateIP can be empty")
}

rsp, err := e.List(&model.ListServersDetailsRequest{
IpEq: &privateIP,
})
if err != nil {
return nil, err
}

notFound := fmt.Errorf("not found any ECS, PrivateIP: %s", privateIP)
if rsp.Servers == nil || len(*rsp.Servers) == 0 {
return nil, notFound
}

for _, sv := range *rsp.Servers {
for _, addresses := range sv.Addresses {
for _, addr := range addresses {
if addr.Addr == privateIP {
return &sv, nil
}
}
}
}

return nil, notFound
}

func (e *EcsClient) GetByName(name string) (*model.ServerDetail, error) {
name = fmt.Sprintf("^%s$", name)

Expand Down

0 comments on commit 798f900

Please sign in to comment.