Repository: containerd/containerd
File: core/runtime/v2/shim_manager.go (Go)

Code before:
	if opts.SandboxID != "" {
		var params shimbinary.BootstrapParams
		if opts.Address != "" {
			// The address returned from sandbox controller should be in the form like ttrpc+unix://<uds-path>
			// or grpc+vsock://<cid>:<port>, we should get the protocol from the url first.
			protocol, address, ok := strings.Cut(opts.Address, "+")
			if !ok {
				return nil, fmt.Errorf("the scheme of sandbox address should be in " +
					" the form of <protocol>+<unix|vsock|tcp>, i.e. ttrpc+unix or grpc+vsock")
			}
			params = shimbinary.BootstrapParams{
				Version:  int(opts.Version),
				Protocol: protocol,
				Address:  address,
			}
		} else {
			// For those sandbox we can not get endpoint,
			// fallback to legacy implementation
			process, err := m.Get(ctx, opts.SandboxID)
			if err != nil {
				return nil, fmt.Errorf("can't find sandbox %s", opts.SandboxID)
			}
			p, restoreErr := restoreBootstrapParams(process.Bundle())
			if restoreErr != nil {
				return nil, fmt.Errorf("failed to get bootstrap "+
					"params of sandbox %s, legacy restore error %v", opts.SandboxID, restoreErr)
			}
			params = p
		}

		// Write sandbox ID this task belongs to.
		if err := os.WriteFile(filepath.Join(bundle.Path, "sandbox"), []byte(opts.SandboxID), 0600); err != nil {
			return nil, err
		}

		if err := writeBootstrapParams(filepath.Join(bundle.Path, "bootstrap.json"), params); err != nil {
			return nil, fmt.Errorf("failed to write bootstrap.json for bundle %s: %w", bundle.Path, err)
		}

		shim, err := loadShim(ctx, bundle, func() {})
		if err != nil {
			return nil, fmt.Errorf("failed to load sandbox task %q: %w", opts.SandboxID, err)
		}

		if err := m.shims.Add(ctx, shim); err != nil {
			return nil, err
		}

		return shim, nil
	}


Review comment (suggestion):
We could probably update this one as well (`p, err := ...`)

Code after:
	if opts.SandboxID != "" {
		var params shimbinary.BootstrapParams
		if opts.Address != "" {
			// The address returned from sandbox controller should be in the form like ttrpc+unix://<uds-path>
			// or grpc+vsock://<cid>:<port>, we should get the protocol from the url first.
			protocol, address, ok := strings.Cut(opts.Address, "+")
			if !ok {
				return nil, fmt.Errorf("the scheme of sandbox address should be in " +
					" the form of <protocol>+<unix|vsock|tcp>, i.e. ttrpc+unix or grpc+vsock")
			}
			params = shimbinary.BootstrapParams{
				Version:  int(opts.Version),
				Protocol: protocol,
				Address:  address,
			}
		} else {
			// For those sandbox we can not get endpoint,
			// fallback to legacy implementation
			process, err := m.Get(ctx, opts.SandboxID)
			if err != nil {
				return nil, fmt.Errorf("can't find sandbox %s", opts.SandboxID)
			}
			p, err := restoreBootstrapParams(process.Bundle())
			if err != nil {
				return nil, fmt.Errorf("failed to get bootstrap "+
					"params of sandbox %s, legacy restore error %v", opts.SandboxID, err)
			}
			params = p
		}

		// Write sandbox ID this task belongs to.
		if err := os.WriteFile(filepath.Join(bundle.Path, "sandbox"), []byte(opts.SandboxID), 0600); err != nil {
			return nil, err
		}

		if err := writeBootstrapParams(filepath.Join(bundle.Path, "bootstrap.json"), params); err != nil {
			return nil, fmt.Errorf("failed to write bootstrap.json for bundle %s: %w", bundle.Path, err)
		}

		shim, err := loadShim(ctx, bundle, func() {})
		if err != nil {
			return nil, fmt.Errorf("failed to load sandbox task %q: %w", opts.SandboxID, err)
		}

		if err := m.shims.Add(ctx, shim); err != nil {
			return nil, err
		}

		return shim, nil
	}