From 0abb03593cecefb6fcf01e4aaffba48b3d2a7ea2 Mon Sep 17 00:00:00 2001 From: xuxiaoyong945 <282794439@qq.com> Date: Thu, 5 Sep 2024 15:30:26 +0800 Subject: [PATCH] feat: Specify to retrieve the node as an InternalIP type Signed-off-by: xuxiaoyong945 <282794439@qq.com> --- .../global_node_controller.go | 9 ++-- .../virtualcluster_init_controller.go | 11 +---- pkg/kubenest/util/address.go | 5 ++- pkg/kubenest/util/address_test.go | 45 +++++++++++++++++++ pkg/kubenest/util/helper_test.go | 3 +- pkg/kubenest/util/image.go | 2 +- pkg/utils/node.go | 21 +++++++++ pkg/utils/utils.go | 4 +- 8 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 pkg/kubenest/util/address_test.go diff --git a/pkg/kubenest/controller/global.node.controller/global_node_controller.go b/pkg/kubenest/controller/global.node.controller/global_node_controller.go index 5932ac752..829640812 100644 --- a/pkg/kubenest/controller/global.node.controller/global_node_controller.go +++ b/pkg/kubenest/controller/global.node.controller/global_node_controller.go @@ -231,12 +231,11 @@ func (r *GlobalNodeController) Reconcile(ctx context.Context, request reconcile. } globalNode.Name = request.Name globalNode.Spec.State = v1alpha1.NodeReserved - for _, a := range rootNode.Status.Addresses { - if a.Type == v1.NodeInternalIP { - globalNode.Spec.NodeIP = a.Address - break - } + firstNodeIP, err := utils.FindFirstNodeIPAddress(*rootNode, v1.NodeInternalIP) + if err != nil { + klog.Errorf("get first node ip address err: %s %s", v1.NodeInternalIP, err.Error()) } + globalNode.Spec.NodeIP = firstNodeIP if _, err = r.KosmosClient.KosmosV1alpha1().GlobalNodes().Create(ctx, &globalNode, metav1.CreateOptions{}); err != nil { klog.Errorf("global-node-controller: can not create global node: %s", globalNode.Name) return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil diff --git a/pkg/kubenest/controller/virtualcluster_init_controller.go b/pkg/kubenest/controller/virtualcluster_init_controller.go index e61361645..ff90f2003 100644 --- a/pkg/kubenest/controller/virtualcluster_init_controller.go +++ b/pkg/kubenest/controller/virtualcluster_init_controller.go @@ -746,15 +746,6 @@ func checkPortOnHostWithAddresses(port int32, hostAddress []string) (bool, error return false, nil } -func findAddress(node corev1.Node) (string, error) { - for _, addr := range node.Status.Addresses { - if addr.Type == corev1.NodeInternalIP { - return addr.Address, nil - } - } - return "", fmt.Errorf("cannot find internal IP address in node addresses, node name: %s", node.GetName()) -} - // Return false to indicate that the port is not occupied func CheckPortOnHost(addr string, port int32) (bool, error) { hostExectorHelper := exector.NewExectorHelper(addr, "") @@ -794,7 +785,7 @@ func (c *VirtualClusterInitController) findHostAddresses() ([]string, error) { ret := []string{} for _, node := range nodes.Items { - addr, err := findAddress(node) + addr, err := utils.FindFirstNodeIPAddress(node, corev1.NodeExternalIP) if err != nil { return nil, err } diff --git a/pkg/kubenest/util/address.go b/pkg/kubenest/util/address.go index 7e8a7175b..e477bad4e 100644 --- a/pkg/kubenest/util/address.go +++ b/pkg/kubenest/util/address.go @@ -10,6 +10,7 @@ import ( netutils "k8s.io/utils/net" "github.com/kosmos.io/kosmos/pkg/kubenest/constants" + "github.com/kosmos.io/kosmos/pkg/utils" ) func GetAPIServiceIP(clientset clientset.Interface) (string, error) { @@ -19,8 +20,8 @@ func GetAPIServiceIP(clientset clientset.Interface) (string, error) { } var ( - masterLabel = labels.Set{"node-role.kubernetes.io/master": ""} - controlplaneLabel = labels.Set{"node-role.kubernetes.io/control-plane": ""} + masterLabel = labels.Set{utils.LabelNodeRoleOldControlPlane: ""} + controlplaneLabel = labels.Set{utils.LabelNodeRoleControlPlane: ""} ) // first, select the master node as the IP of APIServer. if there is // no master nodes, randomly select a worker node. diff --git a/pkg/kubenest/util/address_test.go b/pkg/kubenest/util/address_test.go new file mode 100644 index 000000000..c08b10180 --- /dev/null +++ b/pkg/kubenest/util/address_test.go @@ -0,0 +1,45 @@ +package util + +import ( + "testing" + + netutils "k8s.io/utils/net" +) + +func TestGetAPIServiceIP(t *testing.T) { + client, err := prepare() + if err != nil { + t.Logf("failed to prepare client: %v", err) + return + } + + str, err := GetAPIServiceIP(client) + if err != nil { + t.Logf("failed to get api service ip: %v", err) + } + if len(str) == 0 { + t.Logf("api service ip is empty") + } else { + t.Logf("api service ip is %s", str) + } +} + +func TestParseIP(t *testing.T) { + tests := []struct { + name string + input string + want string + }{ + {"ipv4", "10.237.6.0", "10.237.6.0"}, + {"ipv6", "2409:8c2f:3800:0011::0a18:0000", "2409:8c2f:3800:11::a18:0"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ip := netutils.ParseIPSloppy(tt.input) + if ip.String() != tt.want { + t.Fatalf("%s, %s, %s, %s", tt.name, tt.input, ip.String(), tt.want) + } + }) + } +} diff --git a/pkg/kubenest/util/helper_test.go b/pkg/kubenest/util/helper_test.go index 8d520afe7..c1d58d13c 100644 --- a/pkg/kubenest/util/helper_test.go +++ b/pkg/kubenest/util/helper_test.go @@ -9,7 +9,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/kubernetes" - clientset "k8s.io/client-go/kubernetes" restclient "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) @@ -30,7 +29,7 @@ func createKubeConfig() (*restclient.Config, error) { return kubeConfig, nil } -func prepare() (clientset.Interface, error) { +func prepare() (kubernetes.Interface, error) { // Prepare kube config. kubeConfig, err := createKubeConfig() if err != nil { diff --git a/pkg/kubenest/util/image.go b/pkg/kubenest/util/image.go index 016e04dcf..cbf1c16d0 100644 --- a/pkg/kubenest/util/image.go +++ b/pkg/kubenest/util/image.go @@ -30,7 +30,7 @@ func GetCoreDnsImageTag() string { func GetVirtualControllerLabel() string { lb := os.Getenv(constants.DefaultVirtualControllerLabelEnv) if len(lb) == 0 { - return "node-role.kubernetes.io/control-plane" + return utils.LabelNodeRoleControlPlane } return lb } diff --git a/pkg/utils/node.go b/pkg/utils/node.go index f7cc169c4..482fc1937 100644 --- a/pkg/utils/node.go +++ b/pkg/utils/node.go @@ -1,6 +1,8 @@ package utils import ( + "fmt" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -102,3 +104,22 @@ func NodeReady(node *corev1.Node) bool { // n.Status.Conditions[i].LastHeartbeatTime = now // } //} + +func FindFirstNodeIPAddress(node corev1.Node, nodeAddressType corev1.NodeAddressType) (string, error) { + for _, addr := range node.Status.Addresses { + if addr.Type == nodeAddressType { + return addr.Address, nil + } + } + return "", fmt.Errorf("cannot find internal IP address in node addresses, node name: %s", node.GetName()) +} + +func FindNodeIPsAddress(node corev1.Node, nodeAddressType corev1.NodeAddressType) ([]string, error) { + ips := []string{} + for _, addr := range node.Status.Addresses { + if addr.Type == nodeAddressType { + ips = append(ips, addr.Address) + } + } + return ips, fmt.Errorf("cannot find internal IP address in node addresses, node name: %s", node.GetName()) +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index a0d91409b..8a5d47b25 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -2,11 +2,11 @@ package utils import ( "fmt" - "net" "os" "strings" corev1 "k8s.io/api/core/v1" + netutils "k8s.io/utils/net" ) func ContainsString(arr []string, s string) bool { @@ -60,7 +60,7 @@ func IPFamilyGenerator(apiServerServiceSubnet string) []corev1.IPFamily { } func FormatCIDR(cidr string) (string, error) { - _, ipNet, err := net.ParseCIDR(cidr) + _, ipNet, err := netutils.ParseCIDRSloppy(cidr) if err != nil { return "", fmt.Errorf("failed to parse cidr %s, err: %s", cidr, err.Error()) }