@@ -52,7 +52,6 @@ import (
5252 cnstypes "github.com/Azure/azure-container-networking/cns/types"
5353 "github.com/Azure/azure-container-networking/cns/wireserver"
5454 acn "github.com/Azure/azure-container-networking/common"
55- "github.com/Azure/azure-container-networking/crd"
5655 "github.com/Azure/azure-container-networking/crd/clustersubnetstate"
5756 cssv1alpha1 "github.com/Azure/azure-container-networking/crd/clustersubnetstate/api/v1alpha1"
5857 "github.com/Azure/azure-container-networking/crd/multitenancy"
@@ -74,7 +73,6 @@ import (
7473 "go.uber.org/zap"
7574 "golang.org/x/time/rate"
7675 corev1 "k8s.io/api/core/v1"
77- apierrors "k8s.io/apimachinery/pkg/api/errors"
7876 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7977 "k8s.io/apimachinery/pkg/fields"
8078 kuberuntime "k8s.io/apimachinery/pkg/runtime"
@@ -1307,37 +1305,17 @@ func InitializeMultiTenantController(ctx context.Context, httpRestService cns.HT
13071305 return nil
13081306}
13091307
1310- type nodeNetworkConfigGetter interface {
1311- Get (context.Context ) (* v1alpha.NodeNetworkConfig , error )
1312- }
1313-
13141308type ipamStateReconciler interface {
13151309 ReconcileIPAMStateForSwift (ncRequests []* cns.CreateNetworkContainerRequest , podInfoByIP map [string ]cns.PodInfo , nnc * v1alpha.NodeNetworkConfig ) cnstypes.ResponseCode
13161310}
13171311
13181312// TODO(rbtr) where should this live??
13191313// reconcileInitialCNSState initializes cns by passing pods and a CreateNetworkContainerRequest
1320- func reconcileInitialCNSState (ctx context.Context , cli nodeNetworkConfigGetter , ipamReconciler ipamStateReconciler , podInfoByIPProvider cns.PodInfoByIPProvider , isSwiftV2 bool ) error {
1321- // Get nnc using direct client
1322- nnc , err := cli .Get (ctx )
1323- if err != nil {
1324- if crd .IsNotDefined (err ) {
1325- return errors .Wrap (err , "failed to init CNS state: NNC CRD is not defined" )
1326- }
1327- if apierrors .IsNotFound (err ) {
1328- return errors .Wrap (err , "failed to init CNS state: NNC not found" )
1329- }
1330- return errors .Wrap (err , "failed to init CNS state: failed to get NNC CRD" )
1331- }
1332-
1333- logger .Printf ("Retrieved NNC: %+v" , nnc )
1334- if ! nnc .DeletionTimestamp .IsZero () {
1335- return errors .New ("failed to init CNS state: NNC is being deleted" )
1336- }
1337-
1338- // If there are no NCs, we can't initialize our state and we should fail out.
1339- if len (nnc .Status .NetworkContainers ) == 0 {
1340- return errors .New ("failed to init CNS state: no NCs found in NNC CRD" )
1314+ func reconcileInitialCNSState (nnc * v1alpha.NodeNetworkConfig , ipamReconciler ipamStateReconciler , podInfoByIPProvider cns.PodInfoByIPProvider , isSwiftV2 bool ) error {
1315+ // if no NCs, nothing to do
1316+ ncCount := len (nnc .Status .NetworkContainers )
1317+ if ncCount == 0 {
1318+ return errors .New ("no network containers found in NNC status" )
13411319 }
13421320
13431321 // Get previous PodInfo state from podInfoByIPProvider
@@ -1444,35 +1422,22 @@ func InitializeCRDState(ctx context.Context, z *zap.Logger, httpRestService cns.
14441422 return errors .Wrap (err , "failed to initialize ip state" )
14451423 }
14461424
1447- // create scoped kube clients.
1448- directcli , err := client .New (kubeConfig , client.Options {Scheme : nodenetworkconfig .Scheme })
1449- if err != nil {
1450- return errors .Wrap (err , "failed to create ctrl client" )
1451- }
1452- directnnccli := nodenetworkconfig .NewClient (directcli )
1453- if err != nil {
1454- return errors .Wrap (err , "failed to create NNC client" )
1455- }
1456- // TODO(rbtr): nodename and namespace should be in the cns config
1457- directscopedcli := nncctrl .NewScopedClient (directnnccli , types.NamespacedName {Namespace : "kube-system" , Name : nodeName })
1458-
1459- logger .Printf ("Reconciling initial CNS state" )
1460- // apiserver nnc might not be registered or api server might be down and crashloop backof puts us outside of 5-10 minutes we have for
1461- // aks addons to come up so retry a bit more aggresively here.
1462- // will retry 10 times maxing out at a minute taking about 8 minutes before it gives up.
1463- attempt := 0
1464- _ = retry .Do (func () error {
1465- attempt ++
1466- logger .Printf ("reconciling initial CNS state attempt: %d" , attempt )
1467- err = reconcileInitialCNSState (ctx , directscopedcli , httpRestServiceImplementation , podInfoByIPProvider , cnsconfig .EnableSwiftV2 )
1468- if err != nil {
1469- logger .Errorf ("failed to reconcile initial CNS state, attempt: %d err: %v" , attempt , err )
1470- nncInitFailure .Inc ()
1425+
1426+ hasInitialized := false
1427+ initFunc := func (nnc * v1alpha.NodeNetworkConfig ) error {
1428+ if hasInitialized {
1429+ return nil
14711430 }
1472- return errors .Wrap (err , "failed to initialize CNS state" )
1473- }, retry .Context (ctx ), retry .Delay (initCNSInitalDelay ), retry .MaxDelay (time .Minute ), retry .UntilSucceeded ())
1474- logger .Printf ("reconciled initial CNS state after %d attempts" , attempt )
1475- hasNNCInitialized .Set (1 )
1431+ logger .Printf ("Reconciling initial CNS state" )
1432+ if err := reconcileInitialCNSState (nnc , httpRestServiceImplementation , podInfoByIPProvider , cnsconfig .EnableSwiftV2 ); err != nil {
1433+ return err
1434+ }
1435+ hasNNCInitialized .Set (2 )
1436+ hasInitialized = true
1437+ return nil
1438+ }
1439+
1440+
14761441 scheme := kuberuntime .NewScheme ()
14771442 if err := corev1 .AddToScheme (scheme ); err != nil { //nolint:govet // intentional shadow
14781443 return errors .Wrap (err , "failed to add corev1 to scheme" )
@@ -1561,7 +1526,7 @@ func InitializeCRDState(ctx context.Context, z *zap.Logger, httpRestService cns.
15611526
15621527 // get CNS Node IP to compare NC Node IP with this Node IP to ensure NCs were created for this node
15631528 nodeIP := configuration .NodeIP ()
1564- nncReconciler := nncctrl .NewReconciler (httpRestServiceImplementation , poolMonitor , nodeIP , cnsconfig .EnableSwiftV2 )
1529+ nncReconciler := nncctrl .NewReconciler (httpRestServiceImplementation , initFunc , poolMonitor , nodeIP , cnsconfig .EnableSwiftV2 )
15651530 // pass Node to the Reconciler for Controller xref
15661531 // IPAMv1 - reconcile only status changes (where generation doesn't change).
15671532 // IPAMv2 - reconcile all updates.
0 commit comments