Skip to content

Commit

Permalink
Merge pull request #762 from volume-ji/jiyuan
Browse files Browse the repository at this point in the history
Integrate repeated code and modify confused fdErr and err using defer.
  • Loading branch information
ivan4th authored Sep 12, 2018
2 parents 5b53a91 + 78d007f commit cfb5e53
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions pkg/manager/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (v *VirtletRuntimeService) Version(ctx context.Context, in *kubeapi.Version
//

// RunPodSandbox implements RunPodSandbox method of CRI.
func (v *VirtletRuntimeService) RunPodSandbox(ctx context.Context, in *kubeapi.RunPodSandboxRequest) (*kubeapi.RunPodSandboxResponse, error) {
func (v *VirtletRuntimeService) RunPodSandbox(ctx context.Context, in *kubeapi.RunPodSandboxRequest) (response *kubeapi.RunPodSandboxResponse, retErr error) {
config := in.GetConfig()
if config == nil {
return nil, errors.New("no pod sandbox config passed to RunPodSandbox")
Expand Down Expand Up @@ -146,39 +146,38 @@ func (v *VirtletRuntimeService) RunPodSandbox(ctx context.Context, in *kubeapi.R
Options: config.DnsConfig.Options,
}
}

fdPayload := &tapmanager.GetFDPayload{Description: pnd}
csnBytes, err := v.fdManager.AddFDs(podID, fdPayload)
if err != nil {
// Try to clean up CNI netns (this may be necessary e.g. in case of multiple CNI plugins with CNI Genie)
if fdErr := v.fdManager.ReleaseFDs(podID); err != nil {
glog.Errorf("Error removing pod %s (%s) from CNI network: %v", podName, podID, fdErr)
// The reason for defer here is that it is also necessary to ReleaseFDs if AddFDs fail
// Try to clean up CNI netns (this may be necessary e.g. in case of multiple CNI plugins with CNI Genie)
defer func() {
if retErr != nil {
// Try to clean up CNI netns if we couldn't add the pod to the metadata store or if AddFDs call wasn't
// successful to avoid leaking resources
if fdErr := v.fdManager.ReleaseFDs(podID); fdErr != nil {
glog.Errorf("Error removing pod %s (%s) from CNI network: %v", podName, podID, fdErr)
}
}

}()
if err != nil {
return nil, fmt.Errorf("Error adding pod %s (%s) to CNI network: %v", podName, podID, err)
}

psi, err := metadata.NewPodSandboxInfo(
CRIPodSandboxConfigToPodSandboxConfig(config),
csnBytes, types.PodSandboxState(state), v.clock)
if err != nil {
// cleanup cni if we could not add pod to metadata store to prevent resource leaking
if fdErr := v.fdManager.ReleaseFDs(podID); err != nil {
glog.Errorf("Error removing pod %s (%s) from CNI network: %v", podName, podID, fdErr)
}
return nil, err
}

sandbox = v.metadataStore.PodSandbox(config.Metadata.Uid)
if storeErr := sandbox.Save(
if err := sandbox.Save(
func(c *types.PodSandboxInfo) (*types.PodSandboxInfo, error) {
return psi, nil
},
); storeErr != nil {
// cleanup cni if we could not add pod to metadata store to prevent resource leaking
if err := v.fdManager.ReleaseFDs(podID); err != nil {
glog.Errorf("Error removing pod %s (%s) from CNI network: %v", podName, podID, err)
}
return nil, storeErr
); err != nil {
return nil, err
}

return &kubeapi.RunPodSandboxResponse{
Expand Down

0 comments on commit cfb5e53

Please sign in to comment.