diff --git a/cmd/antrea-agent-simulator/simulator.go b/cmd/antrea-agent-simulator/simulator.go index 7f6db4da39f..ba12eec28b5 100644 --- a/cmd/antrea-agent-simulator/simulator.go +++ b/cmd/antrea-agent-simulator/simulator.go @@ -30,9 +30,9 @@ import ( "k8s.io/klog/v2" "antrea.io/antrea/pkg/agent" - "antrea.io/antrea/pkg/k8s" "antrea.io/antrea/pkg/signals" "antrea.io/antrea/pkg/util/env" + "antrea.io/antrea/pkg/util/k8s" "antrea.io/antrea/pkg/version" ) diff --git a/cmd/antrea-agent/agent.go b/cmd/antrea-agent/agent.go index 2f9562519a4..bc8ac2045b0 100644 --- a/cmd/antrea-agent/agent.go +++ b/cmd/antrea-agent/agent.go @@ -45,13 +45,13 @@ import ( "antrea.io/antrea/pkg/agent/types" crdinformers "antrea.io/antrea/pkg/client/informers/externalversions" "antrea.io/antrea/pkg/features" - "antrea.io/antrea/pkg/k8s" "antrea.io/antrea/pkg/log" "antrea.io/antrea/pkg/monitor" ofconfig "antrea.io/antrea/pkg/ovs/openflow" "antrea.io/antrea/pkg/ovs/ovsconfig" "antrea.io/antrea/pkg/signals" "antrea.io/antrea/pkg/util/cipher" + "antrea.io/antrea/pkg/util/k8s" "antrea.io/antrea/pkg/version" ) diff --git a/cmd/antrea-controller/controller.go b/cmd/antrea-controller/controller.go index 022ac55a8a7..8b8f18cded2 100644 --- a/cmd/antrea-controller/controller.go +++ b/cmd/antrea-controller/controller.go @@ -49,13 +49,13 @@ import ( "antrea.io/antrea/pkg/controller/stats" "antrea.io/antrea/pkg/controller/traceflow" "antrea.io/antrea/pkg/features" - "antrea.io/antrea/pkg/k8s" legacycrdinformers "antrea.io/antrea/pkg/legacyclient/informers/externalversions" "antrea.io/antrea/pkg/log" "antrea.io/antrea/pkg/monitor" "antrea.io/antrea/pkg/signals" "antrea.io/antrea/pkg/util/cipher" "antrea.io/antrea/pkg/util/env" + "antrea.io/antrea/pkg/util/k8s" "antrea.io/antrea/pkg/version" ) diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index b99a0c79040..d059c9b30f1 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -43,6 +43,7 @@ import ( "antrea.io/antrea/pkg/agent/util" "antrea.io/antrea/pkg/ovs/ovsconfig" "antrea.io/antrea/pkg/util/env" + "antrea.io/antrea/pkg/util/k8s" ) const ( @@ -641,7 +642,7 @@ func (i *Initializer) initNodeLocalConfig() error { return err } - ipAddr, err := noderoute.GetNodeAddr(node) + ipAddr, err := k8s.GetNodeAddr(node) if err != nil { return fmt.Errorf("failed to obtain local IP address from K8s: %w", err) } diff --git a/pkg/agent/cniserver/interface_configuration_windows.go b/pkg/agent/cniserver/interface_configuration_windows.go index fd4c0ab77cd..e0e2b71277b 100644 --- a/pkg/agent/cniserver/interface_configuration_windows.go +++ b/pkg/agent/cniserver/interface_configuration_windows.go @@ -34,8 +34,8 @@ import ( "antrea.io/antrea/pkg/agent/interfacestore" "antrea.io/antrea/pkg/agent/util" cnipb "antrea.io/antrea/pkg/apis/cni/v1beta1" - "antrea.io/antrea/pkg/k8s" "antrea.io/antrea/pkg/ovs/ovsconfig" + "antrea.io/antrea/pkg/util/k8s" ) const ( diff --git a/pkg/agent/cniserver/pod_configuration.go b/pkg/agent/cniserver/pod_configuration.go index 4cf604109dc..57417e89071 100644 --- a/pkg/agent/cniserver/pod_configuration.go +++ b/pkg/agent/cniserver/pod_configuration.go @@ -33,8 +33,8 @@ import ( "antrea.io/antrea/pkg/agent/types" "antrea.io/antrea/pkg/agent/util" "antrea.io/antrea/pkg/apis/controlplane/v1beta2" - "antrea.io/antrea/pkg/k8s" "antrea.io/antrea/pkg/ovs/ovsconfig" + "antrea.io/antrea/pkg/util/k8s" ) type vethPair struct { diff --git a/pkg/agent/controller/egress/egress_controller.go b/pkg/agent/controller/egress/egress_controller.go index 91e088a564a..f7576151743 100644 --- a/pkg/agent/controller/egress/egress_controller.go +++ b/pkg/agent/controller/egress/egress_controller.go @@ -41,7 +41,7 @@ import ( crdv1a2 "antrea.io/antrea/pkg/apis/crd/v1alpha2" crdinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1alpha2" crdlisters "antrea.io/antrea/pkg/client/listers/crd/v1alpha2" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" ) const ( diff --git a/pkg/agent/controller/egress/egress_controller_test.go b/pkg/agent/controller/egress/egress_controller_test.go index b41a0871711..832cd74e14e 100644 --- a/pkg/agent/controller/egress/egress_controller_test.go +++ b/pkg/agent/controller/egress/egress_controller_test.go @@ -40,7 +40,7 @@ import ( "antrea.io/antrea/pkg/client/clientset/versioned/fake" fakeversioned "antrea.io/antrea/pkg/client/clientset/versioned/fake" crdinformers "antrea.io/antrea/pkg/client/informers/externalversions" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" ) const ( diff --git a/pkg/agent/controller/noderoute/node_route_controller.go b/pkg/agent/controller/noderoute/node_route_controller.go index 26086a7e303..3b90b8c5db6 100644 --- a/pkg/agent/controller/noderoute/node_route_controller.go +++ b/pkg/agent/controller/noderoute/node_route_controller.go @@ -38,6 +38,7 @@ import ( "antrea.io/antrea/pkg/agent/util" "antrea.io/antrea/pkg/ovs/ovsconfig" utilip "antrea.io/antrea/pkg/util/ip" + "antrea.io/antrea/pkg/util/k8s" ) const ( @@ -212,7 +213,7 @@ func (c *Controller) removeStaleTunnelPorts() error { continue } - peerNodeIP, err := GetNodeAddr(node) + peerNodeIP, err := k8s.GetNodeAddr(node) if err != nil { klog.Errorf("Failed to retrieve IP address of Node %s: %v", node.Name, err) continue @@ -456,7 +457,7 @@ func (c *Controller) addNodeRoute(nodeName string, node *corev1.Node) error { podCIDRs = append(podCIDRs, peerPodCIDR) } - peerNodeIP, err := GetNodeAddr(node) + peerNodeIP, err := k8s.GetNodeAddr(node) if err != nil { klog.Errorf("Failed to retrieve IP address of Node %s: %v", nodeName, err) return nil @@ -593,31 +594,6 @@ func ParseTunnelInterfaceConfig( return interfaceConfig } -// GetNodeAddr gets the available IP address of a Node. GetNodeAddr will first try to get the -// NodeInternalIP, then try to get the NodeExternalIP. -// Note: Although K8s supports dual-stack, there is only a single Internal address per Node because of issue ( -// kubernetes/kubernetes#91940 ). The Node might have multiple addresses after the issue is fixed, and one per address -// family. And we should change the return type at that time. -func GetNodeAddr(node *corev1.Node) (net.IP, error) { - addresses := make(map[corev1.NodeAddressType]string) - for _, addr := range node.Status.Addresses { - addresses[addr.Type] = addr.Address - } - var ipAddrStr string - if internalIP, ok := addresses[corev1.NodeInternalIP]; ok { - ipAddrStr = internalIP - } else if externalIP, ok := addresses[corev1.NodeExternalIP]; ok { - ipAddrStr = externalIP - } else { - return nil, fmt.Errorf("node %s has neither external ip nor internal ip", node.Name) - } - ipAddr := net.ParseIP(ipAddrStr) - if ipAddr == nil { - return nil, fmt.Errorf("<%v> is not a valid ip address", ipAddrStr) - } - return ipAddr, nil -} - func (c *Controller) IPInPodSubnets(ip net.IP) bool { var ipCIDR *net.IPNet var curNodeCIDRStr string diff --git a/pkg/agent/interfacestore/interface_cache.go b/pkg/agent/interfacestore/interface_cache.go index cb4cb44684f..51deb3d9238 100644 --- a/pkg/agent/interfacestore/interface_cache.go +++ b/pkg/agent/interfacestore/interface_cache.go @@ -21,7 +21,7 @@ import ( "antrea.io/antrea/pkg/agent/metrics" "antrea.io/antrea/pkg/agent/util" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" ) const ( diff --git a/pkg/antctl/raw/proxy/command.go b/pkg/antctl/raw/proxy/command.go index 2ccfca84195..11c93b6b103 100644 --- a/pkg/antctl/raw/proxy/command.go +++ b/pkg/antctl/raw/proxy/command.go @@ -29,10 +29,10 @@ import ( "k8s.io/klog/v2" "k8s.io/kubectl/pkg/proxy" - "antrea.io/antrea/pkg/agent/controller/noderoute" "antrea.io/antrea/pkg/antctl/runtime" clusterinformationv1beta1 "antrea.io/antrea/pkg/apis/crd/v1beta1" antrea "antrea.io/antrea/pkg/client/clientset/versioned" + "antrea.io/antrea/pkg/util/k8s" ) const ( @@ -185,7 +185,7 @@ func createAgentClientCfg(k8sClientset kubernetes.Interface, antreaClientset ant if agentInfo == nil { return nil, fmt.Errorf("no Antrea Agent found for Node name %s", nodeName) } - nodeIP, err := noderoute.GetNodeAddr(node) + nodeIP, err := k8s.GetNodeAddr(node) if err != nil { return nil, fmt.Errorf("error when parsing IP of Node %s", nodeName) } @@ -205,7 +205,7 @@ func createControllerClientCfg(k8sClientset kubernetes.Interface, antreaClientse return nil, fmt.Errorf("error when searching the Node of the controller: %w", err) } var controllerNodeIP net.IP - controllerNodeIP, err = noderoute.GetNodeAddr(controllerNode) + controllerNodeIP, err = k8s.GetNodeAddr(controllerNode) if err != nil { return nil, fmt.Errorf("error when parsing controller IP: %w", err) } diff --git a/pkg/antctl/raw/supportbundle/command.go b/pkg/antctl/raw/supportbundle/command.go index efa92e5f695..96d9f7c674a 100644 --- a/pkg/antctl/raw/supportbundle/command.go +++ b/pkg/antctl/raw/supportbundle/command.go @@ -42,12 +42,12 @@ import ( "k8s.io/klog/v2" agentapiserver "antrea.io/antrea/pkg/agent/apiserver" - "antrea.io/antrea/pkg/agent/controller/noderoute" "antrea.io/antrea/pkg/antctl/runtime" "antrea.io/antrea/pkg/apis" systemv1beta1 "antrea.io/antrea/pkg/apis/system/v1beta1" controllerapiserver "antrea.io/antrea/pkg/apiserver" antrea "antrea.io/antrea/pkg/client/clientset/versioned" + "antrea.io/antrea/pkg/util/k8s" ) const ( @@ -326,7 +326,7 @@ func createAgentClients(k8sClientset kubernetes.Interface, antreaClientset antre if !ok { continue } - ip, err := noderoute.GetNodeAddr(&node) + ip, err := k8s.GetNodeAddr(&node) if err != nil { klog.Warningf("Error when parsing IP of Node %s", node.Name) continue @@ -354,7 +354,7 @@ func createControllerClient(k8sClientset kubernetes.Interface, antreaClientset a return nil, fmt.Errorf("error when searching the Node of the controller: %w", err) } var controllerNodeIP net.IP - controllerNodeIP, err = noderoute.GetNodeAddr(controllerNode) + controllerNodeIP, err = k8s.GetNodeAddr(controllerNode) if err != nil { return nil, fmt.Errorf("error when parsing controllre IP: %w", err) } diff --git a/pkg/apiserver/registry/networkpolicy/groupassociation/rest_test.go b/pkg/apiserver/registry/networkpolicy/groupassociation/rest_test.go index be8f4bfa97a..57879223c50 100644 --- a/pkg/apiserver/registry/networkpolicy/groupassociation/rest_test.go +++ b/pkg/apiserver/registry/networkpolicy/groupassociation/rest_test.go @@ -25,7 +25,7 @@ import ( "antrea.io/antrea/pkg/apis/controlplane" antreatypes "antrea.io/antrea/pkg/controller/types" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" ) type fakeQuerier struct { diff --git a/pkg/controller/networkpolicy/networkpolicy_controller.go b/pkg/controller/networkpolicy/networkpolicy_controller.go index 0bb29fc3c12..d7ff3c47bc6 100644 --- a/pkg/controller/networkpolicy/networkpolicy_controller.go +++ b/pkg/controller/networkpolicy/networkpolicy_controller.go @@ -60,7 +60,7 @@ import ( "antrea.io/antrea/pkg/controller/networkpolicy/store" antreatypes "antrea.io/antrea/pkg/controller/types" "antrea.io/antrea/pkg/features" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" utilsets "antrea.io/antrea/pkg/util/sets" ) diff --git a/pkg/controller/networkpolicy/store/group.go b/pkg/controller/networkpolicy/store/group.go index 58c4127f14f..d8316592660 100644 --- a/pkg/controller/networkpolicy/store/group.go +++ b/pkg/controller/networkpolicy/store/group.go @@ -23,7 +23,7 @@ import ( "antrea.io/antrea/pkg/apiserver/storage" "antrea.io/antrea/pkg/apiserver/storage/ram" antreatypes "antrea.io/antrea/pkg/controller/types" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" ) const ( diff --git a/pkg/controller/stats/aggregator.go b/pkg/controller/stats/aggregator.go index 503706c3f5f..e39bc11793d 100644 --- a/pkg/controller/stats/aggregator.go +++ b/pkg/controller/stats/aggregator.go @@ -30,7 +30,7 @@ import ( statsv1alpha1 "antrea.io/antrea/pkg/apis/stats/v1alpha1" crdvinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1alpha1" "antrea.io/antrea/pkg/features" - "antrea.io/antrea/pkg/k8s" + "antrea.io/antrea/pkg/util/k8s" ) const ( diff --git a/pkg/k8s/client.go b/pkg/util/k8s/client.go similarity index 100% rename from pkg/k8s/client.go rename to pkg/util/k8s/client.go diff --git a/pkg/k8s/doc.go b/pkg/util/k8s/doc.go similarity index 100% rename from pkg/k8s/doc.go rename to pkg/util/k8s/doc.go diff --git a/pkg/k8s/name.go b/pkg/util/k8s/name.go similarity index 100% rename from pkg/k8s/name.go rename to pkg/util/k8s/name.go diff --git a/pkg/util/k8s/node.go b/pkg/util/k8s/node.go new file mode 100644 index 00000000000..0f4b48796e2 --- /dev/null +++ b/pkg/util/k8s/node.go @@ -0,0 +1,47 @@ +// Copyright 2021 Antrea Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package k8s + +import ( + "fmt" + "net" + + v1 "k8s.io/api/core/v1" +) + +// GetNodeAddr gets the available IP address of a Node. GetNodeAddr will first try to get the NodeInternalIP, then try +// to get the NodeExternalIP. +// Note: Although K8s supports dual-stack, there is only a single Internal address per Node because of issue ( +// kubernetes/kubernetes#91940 ). The Node might have multiple addresses after the issue is fixed, and one per address +// family. And we should change the return type at that time. +func GetNodeAddr(node *v1.Node) (net.IP, error) { + addresses := make(map[v1.NodeAddressType]string) + for _, addr := range node.Status.Addresses { + addresses[addr.Type] = addr.Address + } + var ipAddrStr string + if internalIP, ok := addresses[v1.NodeInternalIP]; ok { + ipAddrStr = internalIP + } else if externalIP, ok := addresses[v1.NodeExternalIP]; ok { + ipAddrStr = externalIP + } else { + return nil, fmt.Errorf("Node %s has neither external ip nor internal ip", node.Name) + } + ipAddr := net.ParseIP(ipAddrStr) + if ipAddr == nil { + return nil, fmt.Errorf("<%v> is not a valid ip address", ipAddrStr) + } + return ipAddr, nil +} diff --git a/pkg/util/k8s/node_test.go b/pkg/util/k8s/node_test.go new file mode 100644 index 00000000000..07362337a30 --- /dev/null +++ b/pkg/util/k8s/node_test.go @@ -0,0 +1,108 @@ +// Copyright 2021 Antrea Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package k8s + +import ( + "fmt" + "net" + "testing" + + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestGetNodeAddr(t *testing.T) { + tests := []struct { + name string + node *corev1.Node + expectedAddr net.IP + expectedErr error + }{ + { + name: "internal address first", + node: &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + }, + Status: corev1.NodeStatus{ + Addresses: []corev1.NodeAddress{ + { + Type: corev1.NodeInternalIP, + Address: "192.168.10.10", + }, + { + Type: corev1.NodeExternalIP, + Address: "1.1.1.1", + }, + { + Type: corev1.NodeHostName, + Address: "foo", + }, + }, + }, + }, + expectedAddr: net.ParseIP("192.168.10.10"), + expectedErr: nil, + }, + { + name: "external address", + node: &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + }, + Status: corev1.NodeStatus{ + Addresses: []corev1.NodeAddress{ + { + Type: corev1.NodeExternalIP, + Address: "1.1.1.1", + }, + { + Type: corev1.NodeHostName, + Address: "foo", + }, + }, + }, + }, + expectedAddr: net.ParseIP("1.1.1.1"), + expectedErr: nil, + }, + { + name: "no valid address", + node: &corev1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + }, + Status: corev1.NodeStatus{ + Addresses: []corev1.NodeAddress{ + { + Type: corev1.NodeHostName, + Address: "foo", + }, + }, + }, + }, + expectedAddr: nil, + expectedErr: fmt.Errorf("Node foo has neither external ip nor internal ip"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + addr, err := GetNodeAddr(tt.node) + assert.Equal(t, tt.expectedErr, err) + assert.Equal(t, tt.expectedAddr, addr) + }) + } +}