ronantakizawa/codereview-bench huggingface dataset:
Repository: moby/moby
File: daemon/stats_unix_test.go (Go)

Code before:
//go:build !windows

package daemon

import (
	"embed"
	"strings"
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

//go:embed testdata/stat
var statData embed.FS

func TestGetSystemCPUUsageParsing(t *testing.T) {
	// Read the embedded stat testfile
	data, err := statData.ReadFile("testdata/stat")
	assert.NilError(t, err)

	input := strings.NewReader(string(data))
	cpuUsage, cpuNum, err := readSystemCPUUsage(input)
	assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
	assert.Check(t, is.Equal(cpuNum, uint32(128)))
}


Review comment (suggestion):
You can read it directly to a string:
```suggestion
//go:embed testdata/stat
var statData string
```


Code after:
//go:build !windows

package daemon

import (
	_ "embed"
	"strings"
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

//go:embed testdata/stat
var statData string

func TestGetSystemCPUUsageParsing(t *testing.T) {
	input := strings.NewReader(statData)
	cpuUsage, cpuNum, _ := readSystemCPUUsage(input)
	assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
	assert.Check(t, is.Equal(cpuNum, uint32(128)))
}
Repository: moby/moby
File: daemon/stats_unix_test.go (Go)

Code before:
//go:build !windows

package daemon

import (
	"embed"
	"strings"
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

//go:embed testdata/stat
var statData embed.FS

func TestGetSystemCPUUsageParsing(t *testing.T) {
	// Read the embedded stat testfile
	data, err := statData.ReadFile("testdata/stat")
	assert.NilError(t, err)

	input := strings.NewReader(string(data))
	cpuUsage, cpuNum, err := readSystemCPUUsage(input)
	assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
	assert.Check(t, is.Equal(cpuNum, uint32(128)))
}


Review comment (suggestion):
So all we need is:

```suggestion
	input := strings.NewReader(statData)
```

Code after:
//go:build !windows

package daemon

import (
	_ "embed"
	"strings"
	"testing"

	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

//go:embed testdata/stat
var statData string

func TestGetSystemCPUUsageParsing(t *testing.T) {
	input := strings.NewReader(statData)
	cpuUsage, cpuNum, _ := readSystemCPUUsage(input)
	assert.Check(t, is.Equal(cpuUsage, uint64(65647090000000)))
	assert.Check(t, is.Equal(cpuNum, uint32(128)))
}
Repository: moby/moby
File: daemon/logger/fluentd/fluentd_test.go (Go)

Code before:
	defer f.Close()

	// Ensure that the server is ready to accept connections since we have disabled async mode
	// in fluentd options.
	connectedWG.Wait()

	// Attempt writing 1MiB worth of log data (all 0's) repeatedly. We should see a failure
	// after the 1st or the 2nd attempt depending on when the connection's write buffer gets
	// filled up.
	// If we don't set a write timeout on the connection, this will hang forever. But, because
	// we have a write timeout, we expect the `Log` method to return an error.
	data := make([]byte, 1024*1024)
	for range 10 {
		err = f.Log(&logger.Message{
			Line:      data,
			Timestamp: time.Now(),
		})
		if err != nil {
			break
		}
	}

	assert.Check(t, err != nil)
	// Check if error string matches the pattern "fluent#write: failed to write after %d attempts"
	// where %d is a number.
	assert.Check(t, cmp.Regexp(`fluent#write: failed to write after \d+ attempts`, err.Error()))
}


Review comment (bug):
It's fine to simplify to just "error contains"; checking that prefix should be "good enough" to know we have the expected error;

```go
assert.ErrorContains(t, err, "fluent#write: failed to write after")
```

When doing so, you can also remove the `assert.Check(t, err != nil)` above (which is then redundant) and the comment (should be clear from the code what we're checking for)

Code after:
	assert.NilError(t, err)
	defer f.Close()

	// Ensure that the server is ready to accept connections since we have disabled async mode
	// in fluentd options.
	connectedWG.Wait()

	// Attempt writing 1MiB worth of log data (all 0's) repeatedly. We should see a failure
	// after the 1st or the 2nd attempt depending on when the connection's write buffer gets
	// filled up.
	// If we don't set a write timeout on the connection, this will hang forever. But, because
	// we have a write timeout, we expect the `Log` method to return an error.
	data := make([]byte, 1024*1024)
	for range 10 {
		err = f.Log(&logger.Message{
			Line:      data,
			Timestamp: time.Now(),
		})
		if err != nil {
			break
		}
	}

	// Checks if the error contains the expected message. The full message is of the format:
	// "fluent#write: failed to write after %d attempts".
	assert.ErrorContains(t, err, "fluent#write: failed to write after")
}
Repository: moby/moby
File: internal/metrics/metrics.go (Go)

Code before:
func newStateCounter(ns *gometrics.Namespace, desc *prometheus.Desc) *StateCounter {
	c := &StateCounter{
		states: make(map[string]string),
		desc:   desc,
	}
	ns.Add(c)
	return c
}

// Get returns the count of containers in running, paused, and stopped states
func (ctr *StateCounter) Get() (running, paused, stopped int) {
	ctr.mu.RLock()
	defer ctr.mu.RUnlock()

	running, paused, stopped = 0, 0, 0
	for _, state := range ctr.states {
		switch state {
		case "running":
			running++
		case "paused":
			paused++
		case "stopped":
			stopped++
		}
	}
	return
}

// Set updates the state for a container
func (ctr *StateCounter) Set(id, label string) {
	ctr.mu.Lock()
	defer ctr.mu.Unlock()

	ctr.states[id] = label
}

// Delete removes a container's state
func (ctr *StateCounter) Delete(id string) {
	ctr.mu.Lock()
	defer ctr.mu.Unlock()

	delete(ctr.states, id)
}

// Describe implements prometheus.Collector
func (ctr *StateCounter) Describe(ch chan<- *prometheus.Desc) {
	ch <- ctr.desc
}

// Collect implements prometheus.Collector
func (ctr *StateCounter) Collect(ch chan<- prometheus.Metric) {

Review comment (suggestion):
Can you change this to explicitly return the vars? (We try to avoid naked returns, and have a linter for that);

```
internal/metrics/metrics.go:105:2: naked return in func `Get` with 16 lines of code (nakedret)
	return
	^
```




Code after:
func newStateCounter(ns *gometrics.Namespace, desc *prometheus.Desc) *StateCounter {
	c := &StateCounter{
		states: make(map[string]string),
		desc:   desc,
	}
	ns.Add(c)
	return c
}

// Get returns the count of containers in running, paused, and stopped states
func (ctr *StateCounter) Get() (running int, paused int, stopped int) {
	ctr.mu.RLock()
	defer ctr.mu.RUnlock()

	for _, state := range ctr.states {
		switch state {
		case "running":
			running++
		case "paused":
			paused++
		case "stopped":
			stopped++
		}
	}
	return running, paused, stopped
}

// Set updates the state for a container
func (ctr *StateCounter) Set(id, label string) {
	ctr.mu.Lock()
	defer ctr.mu.Unlock()

	ctr.states[id] = label
}

// Delete removes a container's state
func (ctr *StateCounter) Delete(id string) {
	ctr.mu.Lock()
	defer ctr.mu.Unlock()

	delete(ctr.states, id)
}

// Describe implements prometheus.Collector
func (ctr *StateCounter) Describe(ch chan<- *prometheus.Desc) {
	ch <- ctr.desc
}

// Collect implements prometheus.Collector
func (ctr *StateCounter) Collect(ch chan<- prometheus.Metric) {
	running, paused, stopped := ctr.Get()
Repository: moby/moby
File: libnetwork/networkdb/networkdb_test.go (Go)

Code before:
		assert.NilError(t, err)
	}

	closeNetworkDBInstances(t, dbs)
}

func TestNetworkDBCRUDMediumCluster(t *testing.T) {
	n := 5

	dbs := createNetworkDBInstances(t, n, "node", DefaultConfig())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if i == j {
				continue
			}

			dbs[i].verifyNodeExistence(t, dbs[j].config.NodeID, true)
		}
	}

	for i := 0; i < n; i++ {
		err := dbs[i].JoinNetwork("network1")
		assert.NilError(t, err)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			dbs[i].verifyNetworkExistence(t, dbs[j].config.NodeID, "network1", true)
		}
	}

	err := dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
	assert.NilError(t, err)

	for i := 1; i < n; i++ {
		dbs[i].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_value", true)
	}

	err = dbs[0].UpdateEntry("test_table", "network1", "test_key", []byte("test_updated_value"))
	assert.NilError(t, err)

	for i := 1; i < n; i++ {
		dbs[i].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_updated_value", true)
	}

	err = dbs[0].DeleteEntry("test_table", "network1", "test_key")
	assert.NilError(t, err)

	for i := 1; i < n; i++ {
		dbs[i].verifyEntryExistence(t, "test_table", "network1", "test_key", "", false)

Review comment (bug):
Definitely non-blocking; guess we could make this use a context to make it slight more to the point

```go
// Shake out any data races.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, db := range dbs {
	go func(db *NetworkDB) {
		for ctx.Err() == nil {
			_ = db.GetTableByNetwork("test_table", "network1")
		}
	}(db)
}
```


Code after:
		assert.NilError(t, err)
	}

	closeNetworkDBInstances(t, dbs)
}

func TestNetworkDBCRUDMediumCluster(t *testing.T) {
	n := 5

	dbs := createNetworkDBInstances(t, n, "node", DefaultConfig())

	// Shake out any data races.
	done := make(chan struct{})
	defer close(done)
	for _, db := range dbs {
		go func(db *NetworkDB) {
			for {
				select {
				case <-done:
					return
				default:
				}
				_ = db.GetTableByNetwork("test_table", "network1")
			}
		}(db)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if i == j {
				continue
			}

			dbs[i].verifyNodeExistence(t, dbs[j].config.NodeID, true)
		}
	}

	for i := 0; i < n; i++ {
		err := dbs[i].JoinNetwork("network1")
		assert.NilError(t, err)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			dbs[i].verifyNetworkExistence(t, dbs[j].config.NodeID, "network1", true)
		}
	}

	err := dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
	assert.NilError(t, err)
Repository: moby/moby
File: builder/builder-next/worker/worker_test.go (Go)

Code before:
package worker

import (
	"testing"

	"github.com/containerd/platforms"
	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/stretchr/testify/require"
)

func TestWorker_Platforms_SetPlatforms(t *testing.T) {
	defaultPlatform := platforms.DefaultSpec()
	otherPlatform := ocispec.Platform{OS: "linux", Architecture: "arm64"}
	w := &Worker{Opt: Opt{Platforms: []ocispec.Platform{defaultPlatform, otherPlatform}}}
	got := w.Platforms(false)
	require.Equal(t, []ocispec.Platform{defaultPlatform, otherPlatform}, got)
}

func TestWorker_Platforms_NoCacheAddsSupported(t *testing.T) {
	defaultPlatform := platforms.DefaultSpec()
	otherPlatform := ocispec.Platform{OS: "linux", Architecture: "arm64"}
	thirdPlatform := ocispec.Platform{OS: "windows", Architecture: "amd64"}

	w := &Worker{
		Opt: Opt{Platforms: []ocispec.Platform{defaultPlatform}},
		supportedPlatforms: func(noCache bool) []ocispec.Platform {
			return []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform}
		},
	}
	got := w.Platforms(true)
	require.Contains(t, got, defaultPlatform)
	require.Contains(t, got, otherPlatform)
	require.Contains(t, got, thirdPlatform)

Review comment (suggestion):
Can you change this to use `gotest.tools` for asserting? We use testify only for a single package (functionality not provided by gotest.tools), but otherwise only use gotest.tools

Code after:
package worker

import (
	"testing"

	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestMergePlatforms(t *testing.T) {
	defaultPlatform := ocispec.Platform{OS: "linux", Architecture: "amd64"}
	otherPlatform := ocispec.Platform{OS: "windows", Architecture: "amd64"}
	thirdPlatform := ocispec.Platform{OS: "darwin", Architecture: "arm64"}

	tests := []struct {
		name      string
		defined   []ocispec.Platform
		supported []ocispec.Platform
		contains  []ocispec.Platform
		wantLen   int
	}{
		{
			name:      "AllUnique",
			defined:   []ocispec.Platform{defaultPlatform},
			supported: []ocispec.Platform{otherPlatform, thirdPlatform},
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
			wantLen:   3,
		},
		{
			name:      "SomeOverlap",
			defined:   []ocispec.Platform{defaultPlatform, otherPlatform},
			supported: []ocispec.Platform{otherPlatform, thirdPlatform},
Repository: moby/moby
File: builder/builder-next/worker/worker_test.go (Go)

Code before:
package worker

import (
	"testing"

	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func Test_Worker_Platforms(t *testing.T) {
	defaultPlatform := ocispec.Platform{OS: "some_linux", Architecture: "amd64"}
	otherPlatform := ocispec.Platform{OS: "some_windows", Architecture: "amd64"}
	thirdPlatform := ocispec.Platform{OS: "some_other", Architecture: "arm64"}

	tests := []struct {
		name               string
		platforms          []ocispec.Platform
		supportedPlatforms func(noCache bool) []ocispec.Platform
		noCache            bool
		contains           []ocispec.Platform
		wantLen            int
	}{
		{
			name:      "SetPlatforms",
			platforms: []ocispec.Platform{defaultPlatform, otherPlatform},
			noCache:   false,
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform},
			wantLen:   2,
		},
		{
			name:      "NoCacheAddsSupported",
			platforms: []ocispec.Platform{defaultPlatform},
			supportedPlatforms: func(noCache bool) []ocispec.Platform {
				return []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform}
			},

Review comment (suggestion):
Micro-nit; we usually don't use underscores in our test-names. I know it's a common convention elsewhere (and nothing bad on that), but perhaps align with what we do in this repo for consistency;

```suggestion
func TestWorkerPlatforms(t *testing.T) {
```


(Happy with the table test; indeed looks a lot better!)

Code after:
package worker

import (
	"testing"

	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestMergePlatforms(t *testing.T) {
	defaultPlatform := ocispec.Platform{OS: "linux", Architecture: "amd64"}
	otherPlatform := ocispec.Platform{OS: "windows", Architecture: "amd64"}
	thirdPlatform := ocispec.Platform{OS: "darwin", Architecture: "arm64"}

	tests := []struct {
		name      string
		defined   []ocispec.Platform
		supported []ocispec.Platform
		contains  []ocispec.Platform
		wantLen   int
	}{
		{
			name:      "AllUnique",
			defined:   []ocispec.Platform{defaultPlatform},
			supported: []ocispec.Platform{otherPlatform, thirdPlatform},
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
			wantLen:   3,
		},
		{
			name:      "SomeOverlap",
			defined:   []ocispec.Platform{defaultPlatform, otherPlatform},
			supported: []ocispec.Platform{otherPlatform, thirdPlatform},
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
			wantLen:   3,
		},
Repository: moby/moby
File: builder/builder-next/worker/worker_test.go (Go)

Code before:
package worker

import (
	"testing"

	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestWorkerPlatforms(t *testing.T) {
	defaultPlatform := ocispec.Platform{OS: "some_linux", Architecture: "amd64"}
	otherPlatform := ocispec.Platform{OS: "some_windows", Architecture: "amd64"}
	thirdPlatform := ocispec.Platform{OS: "some_other", Architecture: "arm64"}

	tests := []struct {
		name        string
		platforms   []ocispec.Platform
		platformsFn func(noCache bool) []ocispec.Platform
		noCache     bool
		contains    []ocispec.Platform
		wantLen     int
	}{
		{
			name:      "SetPlatforms",
			platforms: []ocispec.Platform{defaultPlatform, otherPlatform},
			noCache:   false,
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform},
			wantLen:   2,
		},
		{
			name:      "NoCacheAddsSupported",
			platforms: []ocispec.Platform{defaultPlatform},
			platformsFn: func(noCache bool) []ocispec.Platform {
				return []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform}
			},

Review comment (suggestion):
I'm not sure what this test is supposed to verify as all the output logic of the function that was updated comes from implementation of `platforms.Only` but these cases in here don't trigger any functionality from it. Atm it just seems to verify that items passed in `contains` are also in items passed in `platformsFn`.

Code after:
package worker

import (
	"testing"

	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestMergePlatforms(t *testing.T) {
	defaultPlatform := ocispec.Platform{OS: "linux", Architecture: "amd64"}
	otherPlatform := ocispec.Platform{OS: "windows", Architecture: "amd64"}
	thirdPlatform := ocispec.Platform{OS: "darwin", Architecture: "arm64"}

	tests := []struct {
		name      string
		defined   []ocispec.Platform
		supported []ocispec.Platform
		contains  []ocispec.Platform
		wantLen   int
	}{
		{
			name:      "AllUnique",
			defined:   []ocispec.Platform{defaultPlatform},
			supported: []ocispec.Platform{otherPlatform, thirdPlatform},
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
			wantLen:   3,
		},
		{
			name:      "SomeOverlap",
			defined:   []ocispec.Platform{defaultPlatform, otherPlatform},
			supported: []ocispec.Platform{otherPlatform, thirdPlatform},
			contains:  []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
			wantLen:   3,
		},
Repository: moby/moby
File: contrib/dockerd-rootless-setuptool.sh (Shell)

Code before:
		fi
	fi

	# instruction: RHEL/CentOS 7 requires setting max_user_namespaces
	if [ -f /proc/sys/user/max_user_namespaces ]; then
		if [ "0" = "$(cat /proc/sys/user/max_user_namespaces)" ]; then
			instructions=$(
				cat <<- EOI
					${instructions}
					# Set user.max_user_namespaces
					cat <<EOT > /etc/sysctl.d/51-rootless.conf
					user.max_user_namespaces = 28633
					EOT
					sysctl --system
				EOI
			)
		fi
	fi

	# instructions: validate subuid for current user
	if command -v "getsubids" > /dev/null 2>&1; then
		testcommand="getsubids '$USERNAME' > /dev/null 2>&1 || getsubids '$(id -u)' > /dev/null 2>&1"
	else
		testcommand="grep -q '^$USERNAME_ESCAPED:\|^$(id -u):' /etc/subuid 2> /dev/null"
	fi
	if ! eval "$testcommand"; then
		instructions=$(
			cat <<- EOI
				${instructions}
				# Add subuid entry for ${USERNAME}
				echo "${USERNAME}:100000:65536" >> /etc/subuid
			EOI
		)
	fi

	# instructions: validate subgid for current user
	if command -v "getsubids" > /dev/null 2>&1; then
		testcommand="getsubids -g '$USERNAME' > /dev/null 2>&1 || getsubids -g '$(id -u)' > /dev/null 2>&1"
	else
		testcommand="grep -q '^$USERNAME_ESCAPED:\|^$(id -u):' /etc/subgid 2> /dev/null"
	fi
	if ! eval "$testcommand"; then
		instructions=$(
			cat <<- EOI
				${instructions}
				# Add subgid entry for ${USERNAME}
				echo "${USERNAME}:100000:65536" >> /etc/subgid
			EOI
		)
	fi


Review comment (security):
`eval` can be a source of vulnerability.

Instead of this, a script could be like
```sh
error_subid=
if ...; then
  getsubids ... || error_subid=1
else
  grep ... || error_subid=1
fi

if [ "$error_subid" = "1" ] ; then
...
fi
```

Code after:
		fi
	fi

	# instruction: RHEL/CentOS 7 requires setting max_user_namespaces
	if [ -f /proc/sys/user/max_user_namespaces ]; then
		if [ "0" = "$(cat /proc/sys/user/max_user_namespaces)" ]; then
			instructions=$(
				cat <<- EOI
					${instructions}
					# Set user.max_user_namespaces
					cat <<EOT > /etc/sysctl.d/51-rootless.conf
					user.max_user_namespaces = 28633
					EOT
					sysctl --system
				EOI
			)
		fi
	fi

	# instructions: validate subuid for current user
	error_subid=
	if command -v "getsubids" > /dev/null 2>&1; then
		getsubids "$USERNAME" > /dev/null 2>&1 || getsubids "$(id -u)" > /dev/null 2>&1 || error_subid=1
	else
		grep -q "^$USERNAME_ESCAPED:\|^$(id -u):" /etc/subuid 2> /dev/null || error_subid=1
	fi
	if [ "$error_subid" = "1" ]; then
		instructions=$(
			cat <<- EOI
				${instructions}
				# Add subuid entry for ${USERNAME}
				echo "${USERNAME}:100000:65536" >> /etc/subuid
			EOI
		)
	fi

	# instructions: validate subgid for current user
	error_subid=
	if command -v "getsubids" > /dev/null 2>&1; then
		getsubids -g "$USERNAME" > /dev/null 2>&1 || getsubids -g "$(id -u)" > /dev/null 2>&1 || error_subid=1
	else
		grep -q "^$USERNAME_ESCAPED:\|^$(id -u):" /etc/subgid 2> /dev/null || error_subid=1
	fi
	if [ "$error_subid" = "1" ]; then
		instructions=$(
			cat <<- EOI
				${instructions}
				# Add subgid entry for ${USERNAME}
				echo "${USERNAME}:100000:65536" >> /etc/subgid
			EOI
		)
Repository: moby/moby
File: builder/builder-next/controller.go (Go)

Code before:

func getLabels(opt Opt, labels map[string]string) map[string]string {
	if labels == nil {
		labels = make(map[string]string)
	}
	if len(opt.DNSConfig.HostGatewayIPs) > 0 {
		// TODO(robmry) - buildx has its own version of toBuildkitExtraHosts(), which
		//   needs to be updated to understand >1 address. For now, take the IPv4 address
		//   if there is one, else IPv6.
		for _, gip := range opt.DNSConfig.HostGatewayIPs {
			labels[wlabel.HostGatewayIP] = gip.String()
			if gip.Is4() {
				break
			}
		}
	}
	return labels
}

func getCDIManager(opt Opt) (*cdidevices.Manager, error) {
	if opt.DisableCDI {
		return nil, nil
	}

	// Use the default CDI cache that was configured during daemon startup
	cdiCache := cdi.GetDefaultCache()

	// TODO: add support for auto-allowed devices from config
	return cdidevices.NewManager(cdiCache, nil), nil
}


Review comment (performance):
Does this mean that the builder and the daemon share the singleton CDI cache that is instantiated the first time that `GetDefaultCache` is called?

Code after:
func getLabels(opt Opt, labels map[string]string) map[string]string {
	if labels == nil {
		labels = make(map[string]string)
	}
	if len(opt.DNSConfig.HostGatewayIPs) > 0 {
		// TODO(robmry) - buildx has its own version of toBuildkitExtraHosts(), which
		//   needs to be updated to understand >1 address. For now, take the IPv4 address
		//   if there is one, else IPv6.
		for _, gip := range opt.DNSConfig.HostGatewayIPs {
			labels[wlabel.HostGatewayIP] = gip.String()
			if gip.Is4() {
				break
			}
		}
	}
	return labels
}

func getCDIManager(opt Opt) (*cdidevices.Manager, error) {
	if opt.CDICache == nil {
		return nil, nil
	}
	// TODO: add support for auto-allowed devices from config
	return cdidevices.NewManager(opt.CDICache, nil), nil
}