Skip to content

Commit

Permalink
Merge branch 'master' into 842adependabot/go_modules/build/tools/sigs…
Browse files Browse the repository at this point in the history
….k8s.io/controller-tools-0.16.4
  • Loading branch information
jc2543 authored Oct 25, 2024
2 parents 727a4bf + 571afbc commit 816eabe
Show file tree
Hide file tree
Showing 22 changed files with 499 additions and 191 deletions.
43 changes: 21 additions & 22 deletions azure-ipam/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.2
require (
github.com/Azure/azure-container-networking v1.5.21
github.com/containernetworking/cni v1.2.3
github.com/containernetworking/plugins v1.5.1
github.com/containernetworking/plugins v1.6.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
Expand All @@ -24,26 +24,26 @@ require (
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Microsoft/hcsshim v0.12.3 // indirect
github.com/Microsoft/hcsshim v0.12.7 // indirect
github.com/avast/retry-go/v3 v3.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/billgraziano/dpapi v0.5.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups/v3 v3.0.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/coreos/go-iptables v0.7.0 // indirect
github.com/coreos/go-iptables v0.8.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand All @@ -59,28 +59,27 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_golang v1.20.2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.66.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
124 changes: 52 additions & 72 deletions azure-ipam/go.sum

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ const (
Managed = "Managed"
CRD = "CRD"
MultiTenantCRD = "MultiTenantCRD"
AzureHost = "AzureHost"
)

var ErrInvalidNCID = errors.New("invalid NetworkContainerID")
var ErrInvalidIP = errors.New("invalid IP")
var (
ErrInvalidNCID = errors.New("invalid NetworkContainerID")
ErrInvalidIP = errors.New("invalid IP")
)

// CreateNetworkContainerRequest specifies request to create a network container or network isolation boundary.
type CreateNetworkContainerRequest struct {
Expand Down
20 changes: 18 additions & 2 deletions cns/hnsclient/hnsclient_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
)

// TODO redesign hnsclient on windows
const (
// Name of the external hns network
ExtHnsNetworkName = "ext"
Expand Down Expand Up @@ -53,6 +54,9 @@ const (
// Name of the loopback adapter needed to create Host NC apipa network
hostNCLoopbackAdapterName = "LoopbackAdapterHostNCConnectivity"

// HNS rehydration issue requires this GW to be different than the loopback adapter ip, so we set it to .2
defaultHnsGwIPAddress = "169.254.128.2"
hnsLoopbackAdapterIPAddress = "169.254.128.1"
// protocolTCP indicates the TCP protocol identifier in HCN
protocolTCP = "6"

Expand Down Expand Up @@ -301,7 +305,7 @@ func createHostNCApipaNetwork(
if interfaceExists, _ := networkcontainers.InterfaceExists(hostNCLoopbackAdapterName); !interfaceExists {
ipconfig := cns.IPConfiguration{
IPSubnet: cns.IPSubnet{
IPAddress: localIPConfiguration.GatewayIPAddress,
IPAddress: hnsLoopbackAdapterIPAddress,
PrefixLength: localIPConfiguration.IPSubnet.PrefixLength,
},
GatewayIPAddress: localIPConfiguration.GatewayIPAddress,
Expand Down Expand Up @@ -510,7 +514,7 @@ func configureHostNCApipaEndpoint(
endpointPolicies, err := configureAclSettingHostNCApipaEndpoint(
protocolList,
networkContainerApipaIP,
hostApipaIP,
hnsLoopbackAdapterIPAddress,
allowNCToHostCommunication,
allowHostToNCCommunication,
ncPolicies)
Expand Down Expand Up @@ -573,6 +577,7 @@ func CreateHostNCApipaEndpoint(
return endpoint.Id, nil
}

updateGwForLocalIPConfiguration(&localIPConfiguration)
if network, err = createHostNCApipaNetwork(localIPConfiguration); err != nil {
logger.Errorf("[Azure CNS] Failed to create HostNCApipaNetwork. Error: %v", err)
return "", err
Expand Down Expand Up @@ -604,6 +609,17 @@ func CreateHostNCApipaEndpoint(
return endpoint.Id, nil
}

// updateGwForLocalIPConfiguration applies change on gw IP address for apipa NW and endpoint.
// Currently, cns using the same ip address "169.254.128.1" for both apipa gw and loopback adapter. This cause conflict issue when hns get restarted and not able to rehydrate the apipa endpoints.
// This func is to overwrite the address to 169.254.128.2 when the gateway address is 169.254.128.1
func updateGwForLocalIPConfiguration(localIPConfiguration *cns.IPConfiguration) {
// When gw address is 169.254.128.1, should use .2 instead. If gw address is not .1, that mean this value is
// configured from dnc, we should keep it
if localIPConfiguration.GatewayIPAddress == "169.254.128.1" {
localIPConfiguration.GatewayIPAddress = defaultHnsGwIPAddress
}
}

func getHostNCApipaEndpointName(
networkContainerID string) string {
return hostNCApipaEndpointNamePrefix + "-" + networkContainerID
Expand Down
35 changes: 35 additions & 0 deletions cns/hnsclient/hnsclient_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hnsclient

import (
"testing"

"github.com/Azure/azure-container-networking/cns"
"github.com/stretchr/testify/assert"
)

func TestAdhocAdjustIPConfig(t *testing.T) {
tests := []struct {
name string
ipConfig cns.IPConfiguration
expected cns.IPConfiguration
}{
{
name: "expect no change when gw address is not 169.254.128.1",
ipConfig: cns.IPConfiguration{GatewayIPAddress: "169.254.128.3"},
expected: cns.IPConfiguration{GatewayIPAddress: "169.254.128.3"},
},
{
name: "expect default gw address is set when gw address is 169.254.128.1",
ipConfig: cns.IPConfiguration{GatewayIPAddress: "169.254.128.1"},
expected: cns.IPConfiguration{GatewayIPAddress: "169.254.128.2"},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
updateGwForLocalIPConfiguration(&tt.ipConfig)
assert.Equal(t, tt.expected.GatewayIPAddress, tt.ipConfig.GatewayIPAddress)
})
}
}
36 changes: 36 additions & 0 deletions cns/nodesubnet/initialization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nodesubnet

import (
"context"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
cnstypes "github.com/Azure/azure-container-networking/cns/types"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
)

type ipamReconciler interface {
ReconcileIPAMStateForNodeSubnet(ncRequests []*cns.CreateNetworkContainerRequest, podInfoByIP map[string]cns.PodInfo) cnstypes.ResponseCode
}

func ReconcileInitialCNSState(_ context.Context, ipamReconciler ipamReconciler, podInfoByIPProvider cns.PodInfoByIPProvider) (int, error) {
// Get previous PodInfo state from podInfoByIPProvider
podInfoByIP, err := podInfoByIPProvider.PodInfoByIP()
if err != nil {
return 0, errors.Wrap(err, "provider failed to provide PodInfoByIP")
}

logger.Printf("Reconciling initial CNS state with %d IPs", len(podInfoByIP))

// Create a network container request that holds all the IPs from PodInfoByIP
secondaryIPs := maps.Keys(podInfoByIP)
ncRequest := CreateNodeSubnetNCRequest(secondaryIPs)
responseCode := ipamReconciler.ReconcileIPAMStateForNodeSubnet([]*cns.CreateNetworkContainerRequest{ncRequest}, podInfoByIP)

if responseCode != cnstypes.Success {
return 0, errors.Errorf("failed to reconcile initial CNS state: %d", responseCode)
}

return len(secondaryIPs), nil
}
114 changes: 114 additions & 0 deletions cns/nodesubnet/initialization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package nodesubnet_test

import (
"context"
"net"
"testing"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/cnireconciler"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/cns/nodesubnet"
"github.com/Azure/azure-container-networking/cns/restserver"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Azure/azure-container-networking/store"
)

func getMockStore() store.KeyValueStore {
mockStore := store.NewMockStore("")
endpointState := map[string]*restserver.EndpointInfo{
"12e65d89e58cb23c784e97840cf76866bfc9902089bdc8e87e9f64032e312b0b": {
PodName: "coredns-54b69f46b8-ldmwr",
PodNamespace: "kube-system",
IfnameToIPMap: map[string]*restserver.IPInfo{
"eth0": {
IPv4: []net.IPNet{
{
IP: net.IPv4(10, 10, 0, 52),
Mask: net.CIDRMask(24, 32),
},
},
},
},
},
"1fc5176913a3a1a7facfb823dde3b4ded404041134fef4f4a0c8bba140fc0413": {
PodName: "load-test-7f7d49687d-wxc9p",
PodNamespace: "load-test",
IfnameToIPMap: map[string]*restserver.IPInfo{
"eth0": {
IPv4: []net.IPNet{
{
IP: net.IPv4(10, 10, 0, 63),
Mask: net.CIDRMask(24, 32),
},
},
},
},
},
}

err := mockStore.Write(restserver.EndpointStoreKey, endpointState)
if err != nil {
return nil
}
return mockStore
}

type MockIpamStateReconciler struct{}

func (m *MockIpamStateReconciler) ReconcileIPAMStateForNodeSubnet(ncRequests []*cns.CreateNetworkContainerRequest, podInfoByIP map[string]cns.PodInfo) types.ResponseCode {
if len(ncRequests) == 1 && len(ncRequests[0].SecondaryIPConfigs) == len(podInfoByIP) {
return types.Success
}

return types.UnexpectedError
}

func TestNewCNSPodInfoProvider(t *testing.T) {
tests := []struct {
name string
store store.KeyValueStore
wantErr bool
reconciler *MockIpamStateReconciler
exp int
}{
{
name: "happy_path",
store: getMockStore(),
wantErr: false,
reconciler: &MockIpamStateReconciler{},
exp: 2,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
ctx, cancel := testContext(t)
defer cancel()

podInfoByIPProvider, err := cnireconciler.NewCNSPodInfoProvider(tt.store)
checkErr(t, err, false)

got, err := nodesubnet.ReconcileInitialCNSState(ctx, tt.reconciler, podInfoByIPProvider)
checkErr(t, err, tt.wantErr)
if got != tt.exp {
t.Errorf("got %d IPs reconciled, expected %d", got, tt.exp)
}
})
}
}

// testContext creates a context from the provided testing.T that will be
// canceled if the test suite is terminated.
func testContext(t *testing.T) (context.Context, context.CancelFunc) {
if deadline, ok := t.Deadline(); ok {
return context.WithDeadline(context.Background(), deadline)
}
return context.WithCancel(context.Background())
}

func init() {
logger.InitLogger("testlogs", 0, 0, "./")
}
40 changes: 40 additions & 0 deletions cns/nodesubnet/nodesubnet_nc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package nodesubnet

import (
"strconv"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
)

const (
// ID for fake NC that we create to store NodeSubnet IPS
NodeSubnetNCID = "55022629-3854-499b-7133-5e6887959f4ea" // md5sum of "NodeSubnetNC_IPv4"
NodeSubnetNCVersion = 0
NodeSubnetHostVersion = "0"
NodeSubnetNCStatus = v1alpha.NCUpdateSuccess
NodeSubnetHostPrimaryIP = ""
)

// CreateNodeSubnetNCRequest generates a CreateNetworkContainerRequest that simply stores the static secondary IPs.
func CreateNodeSubnetNCRequest(secondaryIPs []string) *cns.CreateNetworkContainerRequest {
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{}

for _, secondaryIP := range secondaryIPs {
// iterate through all secondary IP addresses add them to the request as secondary IPConfigs.
secondaryIPConfigs[secondaryIP] = cns.SecondaryIPConfig{
IPAddress: secondaryIP,
NCVersion: NodeSubnetNCVersion,
}
}

return &cns.CreateNetworkContainerRequest{
HostPrimaryIP: NodeSubnetHostPrimaryIP,
SecondaryIPConfigs: secondaryIPConfigs,
NetworkContainerid: NodeSubnetNCID,
NetworkContainerType: cns.Docker, // Using docker as the NC type for NodeSubnet to match Swift. (The NC is not real)
Version: strconv.FormatInt(NodeSubnetNCVersion, 10), //nolint:gomnd // it's decimal
IPConfiguration: cns.IPConfiguration{},
NCStatus: NodeSubnetNCStatus,
}
}
Loading

0 comments on commit 816eabe

Please sign in to comment.