Repository: containerd/containerd
File: internal/cri/server/sandbox_run.go (Go)

Code before:

	// Send CONTAINER_STARTED event with ContainerId equal to SandboxId.
	c.generateAndSendContainerEvent(ctx, id, id, runtime.ContainerEventType_CONTAINER_STARTED_EVENT)

	sandboxRuntimeCreateTimer.WithValues(labels["oci_runtime_type"]).UpdateSince(runtimeStart)

	return &runtime.RunPodSandboxResponse{PodSandboxId: id}, nil
}

// getNetworkPlugin returns the network plugin to be used by the runtime class
// defaults to the global CNI options in the CRI config
func (c *criService) getNetworkPlugin(runtimeClass string) cni.CNI {
	if c.netPlugin == nil {
		return nil
	}
	i, ok := c.netPlugin[runtimeClass]
	if !ok {
		if i, ok = c.netPlugin[defaultNetworkPlugin]; !ok {
			return nil
		}
	}
	return i
}

// setupPodNetwork setups up the network for a pod
func (c *criService) setupPodNetwork(ctx context.Context, sandbox *sandboxstore.Sandbox) error {
	ctx, span := tracing.StartSpan(ctx, tracing.Name("cni", "setup_pod_network"),
		tracing.WithNamespace(ctx),
	)
	defer span.End()

	var (
		id        = sandbox.ID
		config    = sandbox.Config
		path      = sandbox.NetNSPath
		netPlugin = c.getNetworkPlugin(sandbox.RuntimeHandler)
		err       error
		result    *cni.Result
	)

	// Add tracing attributes
	span.SetAttributes(
		tracing.Attribute("sandbox.id", id),
		tracing.Attribute("netns.path", path),
		tracing.Attribute("runtime.handler", sandbox.RuntimeHandler),
	)

	if netPlugin == nil {
		err := errors.New("cni config not initialized")
		span.RecordError(err)
		return err

Review comment (suggestion):
for this function, you can use defer error.

```go
func (c *criService) setupPodNetwork(ctx context.Context, sandbox *sandboxstore.Sandbox) (retErr error) {

ctx, span := tracing.StartSpan(ctx, tracing.Name("cni", "setup_pod_network"),
		tracing.WithNamespace(ctx),
	)
	defer span.End()

     defer func() {
               if retErr != nil {
                     span.RecordError(err)
               }
      }()
```

so you don't need to change every single return branch.

Code after:
	c.generateAndSendContainerEvent(ctx, id, id, runtime.ContainerEventType_CONTAINER_STARTED_EVENT)

	sandboxRuntimeCreateTimer.WithValues(labels["oci_runtime_type"]).UpdateSince(runtimeStart)

	return &runtime.RunPodSandboxResponse{PodSandboxId: id}, nil
}

// getNetworkPlugin returns the network plugin to be used by the runtime class
// defaults to the global CNI options in the CRI config
func (c *criService) getNetworkPlugin(runtimeClass string) cni.CNI {
	if c.netPlugin == nil {
		return nil
	}
	i, ok := c.netPlugin[runtimeClass]
	if !ok {
		if i, ok = c.netPlugin[defaultNetworkPlugin]; !ok {
			return nil
		}
	}
	return i
}

// setupPodNetwork setups up the network for a pod
func (c *criService) setupPodNetwork(ctx context.Context, sandbox *sandboxstore.Sandbox) (retErr error) {
	ctx, span := tracing.StartSpan(ctx, tracing.Name("cni", "setup_pod_network"),
		tracing.WithNamespace(ctx),
	)
	defer span.End()
	defer func() {
		if retErr != nil {
			span.RecordError(retErr)
		}
	}()

	var (
		id        = sandbox.ID
		config    = sandbox.Config
		path      = sandbox.NetNSPath
		netPlugin = c.getNetworkPlugin(sandbox.RuntimeHandler)
		err       error
		result    *cni.Result
	)

	// Add tracing attributes
	span.SetAttributes(
		tracing.Attribute("sandbox.id", id),
		tracing.Attribute("netns.path", path),
		tracing.Attribute("runtime.handler", sandbox.RuntimeHandler),
	)

	if netPlugin == nil {