@@ -13,11 +13,13 @@ import (
1313 "github.com/alibaba/pouch/cri/stream"
1414 "github.com/alibaba/pouch/ctrd"
1515 "github.com/alibaba/pouch/daemon/config"
16+ "github.com/alibaba/pouch/pkg/collect"
1617 "github.com/alibaba/pouch/pkg/reference"
1718 "github.com/alibaba/pouch/version"
1819
1920 // NOTE: "golang.org/x/net/context" is compatible with standard "context" in golang1.7+.
2021 "github.com/cri-o/ocicni/pkg/ocicni"
22+ "github.com/sirupsen/logrus"
2123 "golang.org/x/net/context"
2224 "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
2325)
@@ -88,6 +90,9 @@ type CriManager struct {
8890
8991 // SandboxBaseDir is the directory used to store sandbox files like /etc/hosts, /etc/resolv.conf, etc.
9092 SandboxBaseDir string
93+
94+ // SandboxStore stores the configuration of sandboxes.
95+ SandboxStore * collect.SafeMap
9196}
9297
9398// NewCriManager creates a brand new cri manager.
@@ -103,6 +108,7 @@ func NewCriManager(config *config.Config, ctrMgr ContainerMgr, imgMgr ImageMgr)
103108 CniMgr : NewCniManager (& config .CriConfig ),
104109 StreamServer : streamServer ,
105110 SandboxBaseDir : path .Join (config .HomeDir , "sandboxes" ),
111+ SandboxStore : collect .NewSafeMap (),
106112 }
107113
108114 return NewCriWrapper (c ), nil
@@ -172,33 +178,38 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
172178 return nil , fmt .Errorf ("failed to setup sandbox files: %v" , err )
173179 }
174180
181+ // Step 4: Setup networking for the sandbox.
182+ var netnsPath string
175183 securityContext := config .GetLinux ().GetSecurityContext ()
176184 hostNet := securityContext .GetNamespaceOptions ().GetHostNetwork ()
177185 // If it is in host network, no need to configure the network of sandbox.
178- if hostNet {
179- return & runtime.RunPodSandboxResponse {PodSandboxId : id }, nil
180- }
186+ if ! hostNet {
187+ container , err := c .ContainerMgr .Get (ctx , id )
188+ if err != nil {
189+ return nil , err
190+ }
191+ netnsPath = containerNetns (container )
192+ if netnsPath == "" {
193+ return nil , fmt .Errorf ("failed to find network namespace path for sandbox %q" , id )
194+ }
181195
182- // Step 4: Setup networking for the sandbox.
183- container , err := c .ContainerMgr .Get (ctx , id )
184- if err != nil {
185- return nil , err
186- }
187- netnsPath := containerNetns (container )
188- if netnsPath == "" {
189- return nil , fmt .Errorf ("failed to find network namespace path for sandbox %q" , id )
196+ err = c .CniMgr .SetUpPodNetwork (& ocicni.PodNetwork {
197+ Name : config .GetMetadata ().GetName (),
198+ Namespace : config .GetMetadata ().GetNamespace (),
199+ ID : id ,
200+ NetNS : netnsPath ,
201+ PortMappings : toCNIPortMappings (config .GetPortMappings ()),
202+ })
203+ if err != nil {
204+ return nil , err
205+ }
190206 }
191207
192- err = c .CniMgr .SetUpPodNetwork (& ocicni.PodNetwork {
193- Name : config .GetMetadata ().GetName (),
194- Namespace : config .GetMetadata ().GetNamespace (),
195- ID : id ,
196- NetNS : netnsPath ,
197- PortMappings : toCNIPortMappings (config .GetPortMappings ()),
198- })
199- if err != nil {
200- return nil , err
208+ sandboxMeta := & SandboxMeta {
209+ Config : config ,
210+ NetNSPath : netnsPath ,
201211 }
212+ c .SandboxStore .Put (id , sandboxMeta )
202213
203214 return & runtime.RunPodSandboxResponse {PodSandboxId : id }, nil
204215}
@@ -207,6 +218,11 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
207218// sandbox, they should be forcibly terminated.
208219func (c * CriManager ) StopPodSandbox (ctx context.Context , r * runtime.StopPodSandboxRequest ) (* runtime.StopPodSandboxResponse , error ) {
209220 podSandboxID := r .GetPodSandboxId ()
221+ res , ok := c .SandboxStore .Get (podSandboxID ).Result ()
222+ if ! ok {
223+ return nil , fmt .Errorf ("failed to get metadata of %q from SandboxStore" , podSandboxID )
224+ }
225+ sandboxMeta := res .(* SandboxMeta )
210226
211227 opts := & ContainerListOption {All : true }
212228 filter := func (c * ContainerMeta ) bool {
@@ -226,32 +242,30 @@ func (c *CriManager) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandb
226242 }
227243 }
228244
229- // Tear down sandbox's network.
230245 container , err := c .ContainerMgr .Get (ctx , podSandboxID )
231246 if err != nil {
232247 return nil , err
233248 }
234- netnsPath := containerNetns (container )
235- if netnsPath == "" {
236- return nil , fmt .Errorf ("failed to find network namespace path for sandbox %q" , podSandboxID )
237- }
238-
239249 metadata , err := parseSandboxName (container .Name )
240250 if err != nil {
241251 return nil , fmt .Errorf ("failed to parse metadata of sandbox %q from container name: %v" , podSandboxID , err )
242252 }
243253
244- // TODO: how to figure out if the network is in host mode?
245- // Maybe we need to store some configuration of sandbox.
246- err = c .CniMgr .TearDownPodNetwork (& ocicni.PodNetwork {
247- Name : metadata .GetName (),
248- Namespace : metadata .GetNamespace (),
249- ID : podSandboxID ,
250- NetNS : netnsPath ,
251- // TODO: get portmapping configuration.
252- })
253- if err != nil {
254- return nil , err
254+ securityContext := sandboxMeta .Config .GetLinux ().GetSecurityContext ()
255+ hostNet := securityContext .GetNamespaceOptions ().GetHostNetwork ()
256+
257+ // Teardown network of the pod, if it is not in host network mode.
258+ if ! hostNet {
259+ err = c .CniMgr .TearDownPodNetwork (& ocicni.PodNetwork {
260+ Name : metadata .GetName (),
261+ Namespace : metadata .GetNamespace (),
262+ ID : podSandboxID ,
263+ NetNS : sandboxMeta .NetNSPath ,
264+ PortMappings : toCNIPortMappings (sandboxMeta .Config .GetPortMappings ()),
265+ })
266+ if err != nil {
267+ return nil , err
268+ }
255269 }
256270
257271 // Stop the sandbox container.
@@ -299,12 +313,21 @@ func (c *CriManager) RemovePodSandbox(ctx context.Context, r *runtime.RemovePodS
299313 return nil , fmt .Errorf ("failed to remove root directory %q: %v" , sandboxRootDir , err )
300314 }
301315
316+ c .SandboxStore .Remove (podSandboxID )
317+
302318 return & runtime.RemovePodSandboxResponse {}, nil
303319}
304320
305321// PodSandboxStatus returns the status of the PodSandbox.
306322func (c * CriManager ) PodSandboxStatus (ctx context.Context , r * runtime.PodSandboxStatusRequest ) (* runtime.PodSandboxStatusResponse , error ) {
307323 podSandboxID := r .GetPodSandboxId ()
324+
325+ res , ok := c .SandboxStore .Get (podSandboxID ).Result ()
326+ if ! ok {
327+ return nil , fmt .Errorf ("failed to get metadata of %q from SandboxStore" , podSandboxID )
328+ }
329+ sandboxMeta := res .(* SandboxMeta )
330+
308331 sandbox , err := c .ContainerMgr .Get (ctx , podSandboxID )
309332 if err != nil {
310333 return nil , fmt .Errorf ("failed to get status of sandbox %q: %v" , podSandboxID , err )
@@ -328,13 +351,16 @@ func (c *CriManager) PodSandboxStatus(ctx context.Context, r *runtime.PodSandbox
328351 }
329352 labels , annotations := extractLabels (sandbox .Config .Labels )
330353
331- // TODO: check if the sandbox's network is in host mode.
354+ securityContext := sandboxMeta .Config .GetLinux ().GetSecurityContext ()
355+ hostNet := securityContext .GetNamespaceOptions ().GetHostNetwork ()
356+
332357 var ip string
333- netnsPath := containerNetns ( sandbox )
334- if netnsPath != "" {
335- ip , err = c .CniMgr .GetPodNetworkStatus (netnsPath )
358+ // No need to get ip for host network mode.
359+ if ! hostNet {
360+ ip , err = c .CniMgr .GetPodNetworkStatus (sandboxMeta . NetNSPath )
336361 if err != nil {
337- return nil , err
362+ // Maybe the pod has been stopped.
363+ logrus .Warnf ("failed to get ip of sandbox %q: %v" , podSandboxID , err )
338364 }
339365 }
340366
0 commit comments