-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 842adependabot/go_modules/build/tools/sigs…
….k8s.io/controller-tools-0.16.4
- Loading branch information
Showing
22 changed files
with
499 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "./") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.