Skip to content

Commit

Permalink
Merge pull request #226 from chengxiangdong/feat_node
Browse files Browse the repository at this point in the history
Supports cluster node name domain name or private IP
  • Loading branch information
k8s-ci-robot authored Oct 7, 2023
2 parents 413cb0b + 30de4fd commit f694c0c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/cloudprovider/huaweicloud/huaweicloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (b Basic) getNodeSubnetID(node *v1.Node) (string, error) {
return "", err
}

instance, err := b.ecsClient.GetByName(node.Name)
instance, err := b.ecsClient.GetByNodeName(node.Name)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func (b Basic) allowHealthCheckRule(node *v1.Node) error {
healthCheckCidrOptLock.Unlock()
}()

instance, err := b.ecsClient.GetByName(node.Name)
instance, err := b.ecsClient.GetByNodeName(node.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func (b Basic) allowHealthCheckRule(node *v1.Node) error {
}

func (b Basic) removeHealthCheckRules(node *v1.Node) error {
instance, err := b.ecsClient.GetByName(node.Name)
instance, err := b.ecsClient.GetByNodeName(node.Name)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cloudprovider/huaweicloud/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Instances struct {
// NodeAddresses returns the addresses of the specified instance.
func (i *Instances) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.NodeAddress, error) {
klog.Infof("NodeAddresses is called with name %s", name)
instance, err := i.ecsClient.GetByName(string(name))
instance, err := i.ecsClient.GetByNodeName(string(name))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (i *Instances) NodeAddressesByProviderID(_ context.Context, providerID stri
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
func (i *Instances) InstanceID(_ context.Context, name types.NodeName) (string, error) {
klog.Infof("InstanceID is called with name %s", name)
server, err := i.ecsClient.GetByName(string(name))
server, err := i.ecsClient.GetByNodeName(string(name))
if err != nil {
return "", err
}
Expand All @@ -91,7 +91,7 @@ func (i *Instances) InstanceID(_ context.Context, name types.NodeName) (string,
// InstanceType returns the type of the specified instance.
func (i *Instances) InstanceType(_ context.Context, name types.NodeName) (string, error) {
klog.Infof("InstanceType is called with name %s", name)
instance, err := i.ecsClient.GetByName(string(name))
instance, err := i.ecsClient.GetByNodeName(string(name))
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/huaweicloud/sharedloadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func (l *SharedLoadBalancer) getNodeSubnetID(node *corev1.Node) (string, error)
return "", err
}

instance, err := l.ecsClient.GetByName(node.Name)
instance, err := l.ecsClient.GetByNodeName(node.Name)
if err != nil {
return "", err
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/cloudprovider/huaweicloud/wrapper/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,51 @@ func (e *EcsClient) Get(id string) (*model.ServerDetail, error) {
return rst, err
}

func (e *EcsClient) GetByNodeName(name string) (*model.ServerDetail, error) {
privateIP := ""
if net.ParseIP(name).To4() != nil {
privateIP = name
} else if ips := utils.LookupHost(name); len(ips) > 0 {
for _, ip := range ips {
if ip != "127.0.0.1" {
privateIP = ip
break
}
}
}

if privateIP == "" {
klog.V(6).Infof("query ECS detail by name: %s", name)
return e.GetByName(name)
}

klog.V(6).Infof("query ECS detail by private IP: %s, NodeName: %s", privateIP, name)

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

notFound := fmt.Errorf("not found any ECS, node: %s, PrivateIP: %s", name, 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
31 changes: 24 additions & 7 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package utils
import (
"encoding/json"
"fmt"
"net"
"strings"

"k8s.io/klog"
)

// IsStrSliceContains searches if a string list contains the given string or not.
Expand All @@ -39,18 +43,31 @@ func CutString(original string, length int) string {
return rst
}

func ToString(a any) string {
if v, ok := a.(string); ok {
func ToString(val any) string {
switch v := val.(type) {
case string:
return v
}
if v, ok := a.(*string); ok {
case *string:
if v == nil {
return ""
}
return *v
default:
b, err := json.Marshal(val)
if err != nil {
return fmt.Sprintf("%#v", val)
}
return string(b)
}
}

b, err := json.Marshal(a)
func LookupHost(domain string) []string {
ns, err := net.LookupHost(domain)
if err != nil {
return fmt.Sprintf("%#v", a)
klog.Errorf("failed to looks up the given host using the local resolver: %s", err)
return nil
}

return string(b)
klog.Infof("lookup host %s: %s", domain, strings.Join(ns, ", "))
return ns
}
26 changes: 26 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package utils

import (
"net/http"
"testing"

"k8s.io/utils/pointer"
Expand Down Expand Up @@ -111,6 +112,11 @@ func TestCutString(t *testing.T) {
}

func TestToJsonStr(t *testing.T) {
var str *string
var obj *http.Server
var mp *map[string]any
var ap *[]any

tests := []struct {
name string
object any
Expand Down Expand Up @@ -165,6 +171,26 @@ func TestToJsonStr(t *testing.T) {
object: pointer.Bool(true),
expected: "true",
},
{
name: "test8",
object: str,
expected: "",
},
{
name: "test9",
object: obj,
expected: "null",
},
{
name: "test10",
object: mp,
expected: "null",
},
{
name: "test11",
object: ap,
expected: "null",
},
}

for _, te := range tests {
Expand Down

0 comments on commit f694c0c

Please sign in to comment.