Skip to content

Commit

Permalink
Address comment.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <lantaol@google.com>
  • Loading branch information
Random-Liu committed Sep 19, 2019
1 parent 35eb96d commit c1ece0c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
27 changes: 16 additions & 11 deletions pkg/server/sandbox_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
// In this case however caching the IP will add a subtle performance enhancement by avoiding
// calls to network namespace of the pod to query the IP of the veth interface on every
// SandboxStatus request.
sandbox.IP, sandbox.AdditionalIPs, sandbox.CNIResult, err = c.setupPodNetwork(ctx, id, sandbox.NetNSPath, config)
if err != nil {
if err := c.setupPodNetwork(ctx, &sandbox); err != nil {
return nil, errors.Wrapf(err, "failed to setup network for sandbox %q", id)
}
defer func() {
if retErr != nil {
// Teardown network if an error is returned.
if err := c.teardownPodNetwork(ctx, id, sandbox.NetNSPath, config); err != nil {
if err := c.teardownPodNetwork(ctx, sandbox); err != nil {
log.G(ctx).WithError(err).Errorf("Failed to destroy network for sandbox %q", id)
}
}
Expand Down Expand Up @@ -303,31 +302,37 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
}

// setupPodNetwork setups up the network for a pod
func (c *criService) setupPodNetwork(ctx context.Context, id string, path string, config *runtime.PodSandboxConfig) (string, []string, *cni.CNIResult, error) {
func (c *criService) setupPodNetwork(ctx context.Context, sandbox *sandboxstore.Sandbox) error {
var (
id = sandbox.ID
config = sandbox.Config
path = sandbox.NetNSPath
)
if c.netPlugin == nil {
return "", nil, nil, errors.New("cni config not initialized")
return errors.New("cni config not initialized")
}

opts, err := cniNamespaceOpts(id, config)
if err != nil {
return "", nil, nil, errors.Wrap(err, "get cni namespace options")
return errors.Wrap(err, "get cni namespace options")
}

result, err := c.netPlugin.Setup(ctx, id, path, opts...)
if err != nil {
return "", nil, nil, err
return err
}
logDebugCNIResult(ctx, id, result)
// Check if the default interface has IP config
if configs, ok := result.Interfaces[defaultIfName]; ok && len(configs.IPConfigs) > 0 {
ip, additionalIPs := selectPodIPs(configs.IPConfigs)
return ip, additionalIPs, result, nil
sandbox.IP, sandbox.AdditionalIPs = selectPodIPs(configs.IPConfigs)
sandbox.CNIResult = result
return nil
}
// If it comes here then the result was invalid so destroy the pod network and return error
if err := c.teardownPodNetwork(ctx, id, path, config); err != nil {
if err := c.teardownPodNetwork(ctx, *sandbox); err != nil {
log.G(ctx).WithError(err).Errorf("Failed to destroy network for sandbox %q", id)
}
return "", nil, result, errors.Errorf("failed to find network info for sandbox %q", id)
return errors.Errorf("failed to find network info for sandbox %q", id)
}

// cniNamespaceOpts get CNI namespace options from sandbox config.
Expand Down
12 changes: 8 additions & 4 deletions pkg/server/sandbox_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ func (c *criService) StopPodSandbox(ctx context.Context, r *runtime.StopPodSandb

// Teardown network for sandbox.
if sandbox.NetNS != nil {
netNSPath := sandbox.NetNSPath
// Use empty netns path if netns is not available. This is defined in:
// https://github.com/containernetworking/cni/blob/v0.7.0-alpha1/SPEC.md
if closed, err := sandbox.NetNS.Closed(); err != nil {
return nil, errors.Wrap(err, "failed to check network namespace closed")
} else if closed {
netNSPath = ""
sandbox.NetNSPath = ""
}
if err := c.teardownPodNetwork(ctx, id, netNSPath, sandbox.Config); err != nil {
if err := c.teardownPodNetwork(ctx, sandbox); err != nil {
return nil, errors.Wrapf(err, "failed to destroy network for sandbox %q", id)
}
if err = sandbox.NetNS.Remove(); err != nil {
Expand Down Expand Up @@ -156,11 +155,16 @@ func (c *criService) waitSandboxStop(ctx context.Context, sandbox sandboxstore.S
}

// teardownPodNetwork removes the network from the pod
func (c *criService) teardownPodNetwork(ctx context.Context, id string, path string, config *runtime.PodSandboxConfig) error {
func (c *criService) teardownPodNetwork(ctx context.Context, sandbox sandboxstore.Sandbox) error {
if c.netPlugin == nil {
return errors.New("cni config not initialized")
}

var (
id = sandbox.ID
path = sandbox.NetNSPath
config = sandbox.Config
)
opts, err := cniNamespaceOpts(id, config)
if err != nil {
return errors.Wrap(err, "get cni namespace options")
Expand Down

0 comments on commit c1ece0c

Please sign in to comment.