Skip to content

Commit

Permalink
feature: Complete the details of StartPodSandbox
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Sep 18, 2018
1 parent e24c034 commit 758c818
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 19 deletions.
92 changes: 75 additions & 17 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,25 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
hostNet := securityContext.GetNamespaceOptions().GetNetwork() == runtime.NamespaceMode_NODE
// If it is in host network, no need to configure the network of sandbox.
if !hostNet {
container, err := c.ContainerMgr.Get(ctx, id)
if err != nil {
return nil, err
}
netnsPath = containerNetns(container)
if netnsPath == "" {
return nil, fmt.Errorf("failed to find network namespace path for sandbox %q", id)
}

err = c.CniMgr.SetUpPodNetwork(&ocicni.PodNetwork{
Name: config.GetMetadata().GetName(),
Namespace: config.GetMetadata().GetNamespace(),
ID: id,
NetNS: netnsPath,
PortMappings: toCNIPortMappings(config.GetPortMappings()),
})
netnsPath, err := c.setupPodNetwork(ctx, id, config)
if err != nil {
return nil, err
}
defer func() {
// Teardown network if an error is returned.
if err != nil {
teardownNetErr := c.CniMgr.TearDownPodNetwork(&ocicni.PodNetwork{
Name: config.GetMetadata().GetName(),
Namespace: config.GetMetadata().GetNamespace(),
ID: id,
NetNS: netnsPath,
PortMappings: toCNIPortMappings(config.GetPortMappings()),
})
if teardownNetErr != nil {
logrus.Errorf("failed to destroy network for sandbox %q: %v", id, teardownNetErr)
}
}
}()
}

sandboxMeta := &SandboxMeta{
Expand Down Expand Up @@ -303,7 +303,65 @@ func (c *CriManager) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
// and we should reconfigure it with network plugin which will make sure it reacquire its original network configuration,
// like IP address.
func (c *CriManager) StartPodSandbox(ctx context.Context, r *runtime.StartPodSandboxRequest) (*runtime.StartPodSandboxResponse, error) {
// TODO Complete the details of function.
podSandboxID := r.GetPodSandboxId()

// start PodSandbox.
startErr := c.ContainerMgr.Start(ctx, podSandboxID, &apitypes.ContainerStartOptions{})
if startErr != nil {
return nil, fmt.Errorf("failed to start podSandbox %q: %v", podSandboxID, startErr)
}

var err error
defer func() {
if err != nil {
stopErr := c.ContainerMgr.Stop(ctx, podSandboxID, defaultStopTimeout)
if stopErr != nil {
logrus.Errorf("failed to stop sandbox %q: %v", podSandboxID, stopErr)
}
}
}()

// get the sandbox's meta data.
res, err := c.SandboxStore.Get(podSandboxID)
if err != nil {
return nil, fmt.Errorf("failed to get metadata of %q from SandboxStore: %v", podSandboxID, err)
}
sandboxMeta := res.(*SandboxMeta)

// setup networking for the sandbox.
var netnsPath string
securityContext := sandboxMeta.Config.GetLinux().GetSecurityContext()
hostNet := securityContext.GetNamespaceOptions().GetNetwork() == runtime.NamespaceMode_NODE
// If it is in host network, no need to configure the network of sandbox.
if !hostNet {
netnsPath, err := c.setupPodNetwork(ctx, podSandboxID, sandboxMeta.Config)
if err != nil {
return nil, err
}
defer func() {
// Teardown network if an error is returned.
if err != nil {
teardownNetErr := c.CniMgr.TearDownPodNetwork(&ocicni.PodNetwork{
Name: sandboxMeta.Config.GetMetadata().GetName(),
Namespace: sandboxMeta.Config.GetMetadata().GetNamespace(),
ID: podSandboxID,
NetNS: netnsPath,
PortMappings: toCNIPortMappings(sandboxMeta.Config.GetPortMappings()),
})
if teardownNetErr != nil {
logrus.Errorf("failed to destroy network for sandbox %q: %v", podSandboxID, teardownNetErr)
}
}
}()
}

// update sandboxMeta
sandboxMeta.NetNSPath = netnsPath
err = c.SandboxStore.Put(sandboxMeta)
if err != nil {
return nil, err
}

return &runtime.StartPodSandboxResponse{}, nil
}

Expand Down
26 changes: 26 additions & 0 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,32 @@ func setupSandboxFiles(sandboxRootDir string, config *runtime.PodSandboxConfig)
return nil
}

// setupPodNetwork sets up the network of PodSandbox and return the netnsPath of PodSandbox
// and do nothing when networkNamespaceMode equals runtime.NamespaceMode_NODE.
func (c *CriManager) setupPodNetwork(ctx context.Context, id string, config *runtime.PodSandboxConfig) (string, error) {
container, err := c.ContainerMgr.Get(ctx, id)
if err != nil {
return "", err
}
netnsPath := containerNetns(container)
if netnsPath == "" {
return "", fmt.Errorf("failed to find network namespace path for sandbox %q", id)
}

err = c.CniMgr.SetUpPodNetwork(&ocicni.PodNetwork{
Name: config.GetMetadata().GetName(),
Namespace: config.GetMetadata().GetNamespace(),
ID: id,
NetNS: netnsPath,
PortMappings: toCNIPortMappings(config.GetPortMappings()),
})
if err != nil {
return "", err
}

return netnsPath, nil
}

// Container related tool functions.

func makeContainerName(s *runtime.PodSandboxConfig, c *runtime.ContainerConfig) string {
Expand Down
37 changes: 35 additions & 2 deletions docs/kubernetes/pouch_cri_api_changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ Kubernetes Version: V1.10.0+
4. Support to the ability to acquire the envs of the Container.
- Scenario:
- In the Upgrade process, the env of the new container needs to remain consistent with that of the old container, so the envs of the old container needs to be read.

5. Support to the ability to start the PodSandbox specified and setup the network.
- Scenario:
- It will fail to get IP after PodSandbox restarts because of the external factors such as shutdown the host.

# The Changes Of CRI API

## UpdateContainerResources
Expand Down Expand Up @@ -278,7 +281,8 @@ message ContainerConfig {

+ Provides an interface for removing volume.
+ The containerstatus interface supports querying volume by name.
+ The changes need to be made in the proto file are as follows:

The changes need to be made in the proto file are as follows:

```
service VolumeService {
Expand Down Expand Up @@ -306,6 +310,35 @@ message Mount {
```

## StartPodSandbox

### What To Solve?

+ StartPodSandbox restarts a sandbox pod which was stopped by accident and setup the network with network plugin.

### Modification

+ Provides an interface for starting PodSandbox specified.

The changes need to be made in the proto file are as follows:

```
service RuntimeService {
...
// Start a sandbox pod which was forced to stop by external factors.
// Network plugin returns same IPs when input same pod names and namespaces
rpc StartPodSandbox(StartPodSandboxRequest) returns (StartPodSandboxResponse) {}
...
}
message StartPodSandboxRequest {
// ID of the PodSandbox to start.
string pod_sandbox_id = 1;
}
message StartPodSandboxResponse {}
```

## Pull Request

+ feature: extend cri apis for special needs [#1617](https://github.com/alibaba/pouch/pull/1617)
Expand Down

0 comments on commit 758c818

Please sign in to comment.