Skip to content

Commit

Permalink
Move GetNodeAddr function to k8s util package (#2191)
Browse files Browse the repository at this point in the history
The function is consumed by antctl and several places in antrea-agent.
Importing it from "pkg/agent/controller/noderoute" is weird and
complicate the module dependency. This patch moves it to k8s util
package which contains K8s specific helper functions. With it, antctl
binary size is reduced about 8KB.

Besides, it moves pkg/k8s to pkg/util/k8s.

Signed-off-by: Quan Tian <qtian@vmware.com>
  • Loading branch information
tnqn authored May 24, 2021
1 parent ee50a52 commit c21592c
Show file tree
Hide file tree
Showing 21 changed files with 178 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cmd/antrea-agent-simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/antrea-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/antrea-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/cniserver/interface_configuration_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/cniserver/pod_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/controller/egress/egress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/controller/egress/egress_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
30 changes: 3 additions & 27 deletions pkg/agent/controller/noderoute/node_route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/interfacestore/interface_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions pkg/antctl/raw/proxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/antctl/raw/supportbundle/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/networkpolicy/networkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/networkpolicy/store/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/stats/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions pkg/util/k8s/node.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit c21592c

Please sign in to comment.