Skip to content

Commit

Permalink
Merge pull request #702 from xuxiaoyong945/main
Browse files Browse the repository at this point in the history
feat: Specify to retrieve the node as an InternalIP type
  • Loading branch information
duanmengkk committed Sep 6, 2024
2 parents 3d46595 + 0abb035 commit 978e257
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions pkg/kubenest/controller/virtualcluster_init_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubenest/util/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions pkg/kubenest/util/address_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
3 changes: 1 addition & 2 deletions pkg/kubenest/util/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubenest/util/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions pkg/utils/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -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())
}
4 changes: 2 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
Expand Down

0 comments on commit 978e257

Please sign in to comment.