diff --git a/build/yamls/antrea-aks.yml b/build/yamls/antrea-aks.yml index d88b483202a..e045005401a 100644 --- a/build/yamls/antrea-aks.yml +++ b/build/yamls/antrea-aks.yml @@ -2918,6 +2918,7 @@ rules: - get - watch - list + - patch - apiGroups: - "" resources: diff --git a/build/yamls/antrea-eks.yml b/build/yamls/antrea-eks.yml index a4f3ad1c1e6..f752cfcabf6 100644 --- a/build/yamls/antrea-eks.yml +++ b/build/yamls/antrea-eks.yml @@ -2918,6 +2918,7 @@ rules: - get - watch - list + - patch - apiGroups: - "" resources: diff --git a/build/yamls/antrea-gke.yml b/build/yamls/antrea-gke.yml index c42c3256f45..8aa7fc48eec 100644 --- a/build/yamls/antrea-gke.yml +++ b/build/yamls/antrea-gke.yml @@ -2918,6 +2918,7 @@ rules: - get - watch - list + - patch - apiGroups: - "" resources: diff --git a/build/yamls/antrea-ipsec.yml b/build/yamls/antrea-ipsec.yml index b8ec34cd2c7..9ed0170847f 100644 --- a/build/yamls/antrea-ipsec.yml +++ b/build/yamls/antrea-ipsec.yml @@ -2918,6 +2918,7 @@ rules: - get - watch - list + - patch - apiGroups: - "" resources: diff --git a/build/yamls/antrea.yml b/build/yamls/antrea.yml index 5b6c682e4b3..2f3f4e62e64 100644 --- a/build/yamls/antrea.yml +++ b/build/yamls/antrea.yml @@ -2918,6 +2918,7 @@ rules: - get - watch - list + - patch - apiGroups: - "" resources: diff --git a/build/yamls/base/agent-rbac.yml b/build/yamls/base/agent-rbac.yml index d1b900d573e..13c94107750 100644 --- a/build/yamls/base/agent-rbac.yml +++ b/build/yamls/base/agent-rbac.yml @@ -18,6 +18,7 @@ rules: - get - watch - list + - patch - apiGroups: - "" resources: diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 440f77c9e4f..395127ed67c 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -16,6 +16,7 @@ package agent import ( "context" + "encoding/json" "fmt" "net" "os" @@ -25,8 +26,10 @@ import ( "github.com/containernetworking/plugins/pkg/ip" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" + "k8s.io/client-go/util/retry" "k8s.io/klog" "github.com/vmware-tanzu/antrea/pkg/agent/cniserver" @@ -637,11 +640,31 @@ func (i *Initializer) initNodeLocalConfig() error { ipAddr, err := noderoute.GetNodeAddr(node) if err != nil { - return fmt.Errorf("failed to obtain local IP address from k8s: %w", err) + return fmt.Errorf("failed to obtain local IP address from K8s: %w", err) } localAddr, localIntf, err := util.GetIPNetDeviceFromIP(ipAddr) if err != nil { - return fmt.Errorf("failed to get local IPNet: %v", err) + return fmt.Errorf("failed to get local IPNet device with IP %v: %v", ipAddr, err) + } + + // Update the Node's MAC address in the annotations of the Node. The MAC address will be used for direct routing by + // OVS in noencap case on Windows Nodes. As a mixture of Linux and Windows nodes is possible, Linux Nodes' MAC + // addresses should be reported too to make them discoverable for Windows Nodes. + if i.networkConfig.TrafficEncapMode.SupportsNoEncap() { + klog.Infof("Updating Node MAC annotation") + patch, _ := json.Marshal(map[string]interface{}{ + "metadata": map[string]interface{}{ + "annotations": map[string]string{ + types.NodeMACAddressAnnotationKey: localIntf.HardwareAddr.String(), + }, + }, + }) + if err := retry.RetryOnConflict(retry.DefaultRetry, func() error { + _, err := i.client.CoreV1().Nodes().Patch(context.TODO(), nodeName, apitypes.MergePatchType, patch, metav1.PatchOptions{}) + return err + }); err != nil { + return err + } } i.nodeConfig = &config.NodeConfig{ diff --git a/pkg/agent/agent_windows.go b/pkg/agent/agent_windows.go index 13a8ab745c5..9d66d816ae1 100644 --- a/pkg/agent/agent_windows.go +++ b/pkg/agent/agent_windows.go @@ -71,7 +71,7 @@ func (i *Initializer) prepareHostNetwork() error { // Create HNS network. subnetCIDR := i.nodeConfig.PodIPv4CIDR if subnetCIDR == nil { - return fmt.Errorf("Failed to find valid IPv4 PodCIDR") + return fmt.Errorf("failed to find valid IPv4 PodCIDR") } return util.PrepareHNSNetwork(subnetCIDR, i.nodeConfig.NodeIPAddr, adapter) } @@ -82,7 +82,7 @@ func (i *Initializer) prepareOVSBridge() error { hnsNetwork, err := hcsshim.GetHNSNetworkByName(util.LocalHNSNetwork) defer func() { // prepareOVSBridge only works on windows platform. The operation has a chance to fail on the first time agent - // starts up when OVS bridge uplink and local inteface have not been configured. If the operation fails, the + // starts up when OVS bridge uplink and local interface have not been configured. If the operation fails, the // host can not communicate with external network. To make sure the agent can connect to API server in // next retry, this step deletes OVS bridge and HNS network created previously which will restore the // host network. @@ -144,7 +144,7 @@ func (i *Initializer) prepareOVSBridge() error { // Move network configuration of uplink interface to OVS bridge local interface. // - The net configuration of uplink will be restored by OS if the attached HNS network is deleted. - // - When ovs-switchd is down, antrea-agent will disable OVS Extension. The OVS bridge local interface will work + // - When ovs-vswitchd is down, antrea-agent will disable OVS Extension. The OVS bridge local interface will work // like a normal interface on host and is responsible for forwarding host traffic. if err = util.EnableHostInterface(brName); err != nil { return err diff --git a/pkg/agent/types/annotations.go b/pkg/agent/types/annotations.go new file mode 100644 index 00000000000..c835cb34e10 --- /dev/null +++ b/pkg/agent/types/annotations.go @@ -0,0 +1,20 @@ +// 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 types + +const ( + // NodeMACAddressAnnotationKey represents the key of the Node's MAC address in the Annotations of the Node. + NodeMACAddressAnnotationKey string = "node.antrea.io/mac-address" +) diff --git a/pkg/agent/util/net_windows.go b/pkg/agent/util/net_windows.go index 2913c468192..78db0b9e591 100644 --- a/pkg/agent/util/net_windows.go +++ b/pkg/agent/util/net_windows.go @@ -400,7 +400,7 @@ func GetLocalBroadcastIP(ipNet *net.IPNet) net.IP { return lastAddr } -// GetDefaultGatewayByInterfaceIndex returns the default gateway configured on the speicified interface. +// GetDefaultGatewayByInterfaceIndex returns the default gateway configured on the specified interface. func GetDefaultGatewayByInterfaceIndex(ifIndex int) (string, error) { cmd := fmt.Sprintf("$(Get-NetRoute -InterfaceIndex %d -DestinationPrefix 0.0.0.0/0 ).NextHop", ifIndex) defaultGW, err := CallPSCommand(cmd)