@@ -10,23 +10,17 @@ import (
1010 "strings"
1111 "sync"
1212
13+ "github.com/Microsoft/go-winio/pkg/guid"
1314 "github.com/Microsoft/hcsshim/hcn"
1415 "github.com/Microsoft/hcsshim/internal/log"
1516 "github.com/Microsoft/hcsshim/internal/logfields"
16- "github.com/Microsoft/hcsshim/internal/vm/guestmanager"
17- "github.com/Microsoft/hcsshim/internal/vm/vmmanager"
18-
19- "github.com/Microsoft/go-winio/pkg/guid"
2017 "github.com/sirupsen/logrus"
2118)
2219
2320// Manager is the concrete implementation of [Controller].
2421type Manager struct {
2522 mu sync.Mutex
2623
27- // podID is the identifier of the pod whose network this Controller manages.
28- podID string
29-
3024 // namespaceID is the HCN namespace ID in use after a successful Setup.
3125 namespaceID string
3226
@@ -40,13 +34,13 @@ type Manager struct {
4034 isNamespaceSupportedByGuest bool
4135
4236 // vmNetManager performs host-side NIC hot-add/remove on the UVM.
43- vmNetManager vmmanager. NetworkManager
37+ vmNetManager vmNetworkManager
4438
4539 // linuxGuestMgr performs guest-side NIC inject/remove for LCOW.
46- linuxGuestMgr guestmanager. LCOWNetworkManager
40+ linuxGuestMgr linuxGuestNetworkManager
4741
4842 // winGuestMgr performs guest-side NIC/namespace operations for WCOW.
49- winGuestMgr guestmanager. WCOWNetworkManager
43+ winGuestMgr windowsGuestNetworkManager
5044
5145 // capsProvider exposes the guest's declared capabilities.
5246 // Used to check IsNamespaceAddRequestSupported.
@@ -61,9 +55,9 @@ var _ Controller = (*Manager)(nil)
6155// This method is called from [VMController.CreateNetworkController()]
6256// which injects the necessary dependencies.
6357func New (
64- vmNetManager vmmanager. NetworkManager ,
65- linuxGuestMgr guestmanager. LCOWNetworkManager ,
66- windowsGuestMgr guestmanager. WCOWNetworkManager ,
58+ vmNetManager vmNetworkManager ,
59+ linuxGuestMgr linuxGuestNetworkManager ,
60+ windowsGuestMgr windowsGuestNetworkManager ,
6761 capsProvider capabilitiesProvider ,
6862) * Manager {
6963 m := & Manager {
@@ -87,15 +81,12 @@ func New(
8781// and hot-adds all endpoints found in that namespace.
8882// It must be called only once; subsequent calls return an error.
8983func (m * Manager ) Setup (ctx context.Context , opts * SetupOptions ) (err error ) {
90- ctx , _ = log .WithContext (ctx , logrus .WithField (logfields .Operation , "Network Setup" ))
84+ ctx , _ = log .WithContext (ctx , logrus .WithField (logfields .Namespace , opts . NetworkNamespace ))
9185
9286 m .mu .Lock ()
9387 defer m .mu .Unlock ()
9488
95- log .G (ctx ).WithFields (logrus.Fields {
96- logfields .PodID : opts .PodID ,
97- logfields .Namespace : opts .NetworkNamespace ,
98- }).Debug ("starting network setup" )
89+ log .G (ctx ).Debug ("starting network setup" )
9990
10091 // If Setup has already been called, then error out.
10192 if m .netState != StateNotConfigured {
@@ -138,19 +129,18 @@ func (m *Manager) Setup(ctx context.Context, opts *SetupOptions) (err error) {
138129 if err != nil {
139130 return fmt .Errorf ("generate NIC GUID: %w" , err )
140131 }
141- if err = m .addEndpointToGuestNamespace (ctx , nicGUID .String (), endpoint , opts .PolicyBasedRouting ); err != nil {
132+ // add the nicID and endpointID to the context for trace.
133+ nicCtx , _ := log .WithContext (ctx , logrus .WithFields (logrus.Fields {"vm_nic_id" : nicGUID .String (), "hns_endpoint_id" : endpoint .Id }))
134+
135+ if err = m .addEndpointToGuestNamespace (nicCtx , nicGUID .String (), endpoint , opts .PolicyBasedRouting ); err != nil {
142136 return fmt .Errorf ("add endpoint %s to guest: %w" , endpoint .Name , err )
143137 }
144138 }
145139
146- m .podID = opts .PodID
147140 m .namespaceID = hcnNamespace .Id
148141 m .netState = StateConfigured
149142
150- log .G (ctx ).WithFields (logrus.Fields {
151- logfields .PodID : opts .PodID ,
152- logfields .Namespace : hcnNamespace .Id ,
153- }).Info ("network setup completed successfully" )
143+ log .G (ctx ).Info ("network setup completed successfully" )
154144
155145 return nil
156146}
@@ -160,16 +150,12 @@ func (m *Manager) Setup(ctx context.Context, opts *SetupOptions) (err error) {
160150// It is idempotent: calling it when the network is already torn down or not yet
161151// configured is a no-op.
162152func (m * Manager ) Teardown (ctx context.Context ) error {
163- ctx , _ = log .WithContext (ctx , logrus .WithField (logfields .Operation , "Network Teardown" ))
153+ ctx , _ = log .WithContext (ctx , logrus .WithField (logfields .Namespace , m . namespaceID ))
164154
165155 m .mu .Lock ()
166156 defer m .mu .Unlock ()
167157
168- log .G (ctx ).WithFields (logrus.Fields {
169- logfields .PodID : m .podID ,
170- logfields .Namespace : m .namespaceID ,
171- "State" : m .netState ,
172- }).Debug ("starting network teardown" )
158+ log .G (ctx ).WithField ("State" , m .netState ).Debug ("starting network teardown" )
173159
174160 if m .netState == StateTornDown {
175161 // Teardown is idempotent, so return nil if already torn down.
@@ -188,32 +174,34 @@ func (m *Manager) Teardown(ctx context.Context) error {
188174 // failures, then collect all errors.
189175 var teardownErrs []error
190176 for nicID , endpoint := range m .vmEndpoints {
191- if err := m .removeEndpointFromGuestNamespace (ctx , nicID , endpoint ); err != nil {
177+ // add the nicID and endpointID to the context for trace.
178+ nicCtx , _ := log .WithContext (ctx , logrus .WithFields (logrus.Fields {"vm_nic_id" : nicID , "hns_endpoint_id" : endpoint .Id }))
179+
180+ if err := m .removeEndpointFromGuestNamespace (nicCtx , nicID , endpoint ); err != nil {
192181 teardownErrs = append (teardownErrs , fmt .Errorf ("remove endpoint %s from guest: %w" , endpoint .Name , err ))
193182 continue // continue attempting to remove other endpoints
194183 }
195184
196185 delete (m .vmEndpoints , nicID )
197186 }
198187
199- if err := m .removeNetNSInsideGuest (ctx , m .namespaceID ); err != nil {
200- teardownErrs = append (teardownErrs , fmt .Errorf ("remove network namespace from guest: %w" , err ))
201- }
202-
203188 if len (teardownErrs ) > 0 {
204189 // If any errors were encountered during teardown, mark the state as invalid.
205190 m .netState = StateInvalid
206191 return errors .Join (teardownErrs ... )
207192 }
208193
194+ if err := m .removeNetNSInsideGuest (ctx , m .namespaceID ); err != nil {
195+ // Mark the state as invalid so that we can retry teardown.
196+ m .netState = StateInvalid
197+ return fmt .Errorf ("remove network namespace from guest: %w" , err )
198+ }
199+
209200 // Mark as torn down if we do not encounter any errors.
210201 // No further Setup or Teardown calls are allowed.
211202 m .netState = StateTornDown
212203
213- log .G (ctx ).WithFields (logrus.Fields {
214- logfields .PodID : m .podID ,
215- "networkNamespace" : m .namespaceID ,
216- }).Info ("network teardown completed successfully" )
204+ log .G (ctx ).Info ("network teardown completed successfully" )
217205
218206 return nil
219207}
@@ -222,7 +210,6 @@ func (m *Manager) Teardown(ctx context.Context) error {
222210// the given namespace.
223211// Endpoints are sorted so that those with names ending in "eth0" appear first.
224212func (m * Manager ) fetchEndpointsInNamespace (ctx context.Context , ns * hcn.HostComputeNamespace ) ([]* hcn.HostComputeEndpoint , error ) {
225- ctx , _ = log .WithContext (ctx , logrus .WithField (logfields .Namespace , ns .Id ))
226213 log .G (ctx ).Info ("fetching endpoints from the network namespace" )
227214
228215 ids , err := hcn .GetNamespaceEndpointIds (ns .Id )
@@ -239,8 +226,8 @@ func (m *Manager) fetchEndpointsInNamespace(ctx context.Context, ns *hcn.HostCom
239226 }
240227
241228 // Ensure the endpoint named "eth0" is added first when multiple endpoints are present,
242- // so it maps to eth0 inside the guest. CNI results aren't available here, so we rely
243- // on the endpoint name suffix as a heuristic.
229+ // so it maps to eth0 inside the pod network namespace within guest.
230+ // CNI results aren't available here, so we rely on the endpoint name suffix as a heuristic.
244231 cmp := func (a , b * hcn.HostComputeEndpoint ) int {
245232 if strings .HasSuffix (a .Name , "eth0" ) {
246233 return - 1
0 commit comments