Browse code

daemon: Daemon.getInspectData: inline struct-literals

Also rename the "container" argument, which shadowed an import.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/08/26 22:19:26
Showing 3 changed files
... ...
@@ -9,6 +9,7 @@ import (
9 9
 
10 10
 	containertypes "github.com/moby/moby/api/types/container"
11 11
 	networktypes "github.com/moby/moby/api/types/network"
12
+	"github.com/moby/moby/api/types/storage"
12 13
 	"github.com/moby/moby/v2/daemon/config"
13 14
 	"github.com/moby/moby/v2/daemon/container"
14 15
 	"github.com/moby/moby/v2/daemon/network"
... ...
@@ -94,12 +95,12 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options
94 94
 	}, nil
95 95
 }
96 96
 
97
-func (daemon *Daemon) getInspectData(daemonCfg *config.Config, container *container.Container) (*containertypes.ContainerJSONBase, error) {
97
+func (daemon *Daemon) getInspectData(daemonCfg *config.Config, ctr *container.Container) (*containertypes.ContainerJSONBase, error) {
98 98
 	// make a copy to play with
99
-	hostConfig := *container.HostConfig
99
+	hostConfig := *ctr.HostConfig
100 100
 
101 101
 	// Add information for legacy links
102
-	children := daemon.linkIndex.children(container)
102
+	children := daemon.linkIndex.children(ctr)
103 103
 	hostConfig.Links = nil // do not expose the internal structure
104 104
 	for linkAlias, child := range children {
105 105
 		hostConfig.Links = append(hostConfig.Links, fmt.Sprintf("%s:%s", child.Name, linkAlias))
... ...
@@ -112,85 +113,84 @@ func (daemon *Daemon) getInspectData(daemonCfg *config.Config, container *contai
112 112
 	// Config.MacAddress field for older API versions (< 1.44). We set it here
113 113
 	// unconditionally, to keep backward compatibility with clients that use
114 114
 	// unversioned API endpoints.
115
-	if container.Config != nil && container.Config.MacAddress == "" { //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
115
+	if ctr.Config != nil && ctr.Config.MacAddress == "" { //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
116 116
 		if nwm := hostConfig.NetworkMode; nwm.IsBridge() || nwm.IsUserDefined() {
117
-			if epConf, ok := container.NetworkSettings.Networks[nwm.NetworkName()]; ok {
118
-				container.Config.MacAddress = epConf.DesiredMacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
117
+			if epConf, ok := ctr.NetworkSettings.Networks[nwm.NetworkName()]; ok {
118
+				ctr.Config.MacAddress = epConf.DesiredMacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
119 119
 			}
120 120
 		}
121 121
 	}
122 122
 
123 123
 	var containerHealth *containertypes.Health
124
-	if container.State.Health != nil {
124
+	if ctr.State.Health != nil {
125 125
 		containerHealth = &containertypes.Health{
126
-			Status:        container.State.Health.Status(),
127
-			FailingStreak: container.State.Health.FailingStreak,
128
-			Log:           append([]*containertypes.HealthcheckResult{}, container.State.Health.Log...),
126
+			Status:        ctr.State.Health.Status(),
127
+			FailingStreak: ctr.State.Health.FailingStreak,
128
+			Log:           append([]*containertypes.HealthcheckResult{}, ctr.State.Health.Log...),
129 129
 		}
130 130
 	}
131 131
 
132
-	containerState := &containertypes.State{
133
-		Status:     container.State.StateString(),
134
-		Running:    container.State.Running,
135
-		Paused:     container.State.Paused,
136
-		Restarting: container.State.Restarting,
137
-		OOMKilled:  container.State.OOMKilled,
138
-		Dead:       container.State.Dead,
139
-		Pid:        container.State.Pid,
140
-		ExitCode:   container.State.ExitCode(),
141
-		Error:      container.State.ErrorMsg,
142
-		StartedAt:  container.State.StartedAt.Format(time.RFC3339Nano),
143
-		FinishedAt: container.State.FinishedAt.Format(time.RFC3339Nano),
144
-		Health:     containerHealth,
145
-	}
146
-
147
-	contJSONBase := &containertypes.ContainerJSONBase{
148
-		ID:           container.ID,
149
-		Created:      container.Created.Format(time.RFC3339Nano),
150
-		Path:         container.Path,
151
-		Args:         container.Args,
152
-		State:        containerState,
153
-		Image:        container.ImageID.String(),
154
-		LogPath:      container.LogPath,
155
-		Name:         container.Name,
156
-		RestartCount: container.RestartCount,
157
-		Driver:       container.Driver,
158
-		Platform:     container.ImagePlatform.OS,
159
-		MountLabel:   container.MountLabel,
160
-		ProcessLabel: container.ProcessLabel,
161
-		ExecIDs:      container.GetExecIDs(),
132
+	inspectResponse := &containertypes.ContainerJSONBase{
133
+		ID:      ctr.ID,
134
+		Created: ctr.Created.Format(time.RFC3339Nano),
135
+		Path:    ctr.Path,
136
+		Args:    ctr.Args,
137
+		State: &containertypes.State{
138
+			Status:     ctr.State.StateString(),
139
+			Running:    ctr.State.Running,
140
+			Paused:     ctr.State.Paused,
141
+			Restarting: ctr.State.Restarting,
142
+			OOMKilled:  ctr.State.OOMKilled,
143
+			Dead:       ctr.State.Dead,
144
+			Pid:        ctr.State.Pid,
145
+			ExitCode:   ctr.State.ExitCode(),
146
+			Error:      ctr.State.ErrorMsg,
147
+			StartedAt:  ctr.State.StartedAt.Format(time.RFC3339Nano),
148
+			FinishedAt: ctr.State.FinishedAt.Format(time.RFC3339Nano),
149
+			Health:     containerHealth,
150
+		},
151
+		Image:        ctr.ImageID.String(),
152
+		LogPath:      ctr.LogPath,
153
+		Name:         ctr.Name,
154
+		RestartCount: ctr.RestartCount,
155
+		Driver:       ctr.Driver,
156
+		Platform:     ctr.ImagePlatform.OS,
157
+		MountLabel:   ctr.MountLabel,
158
+		ProcessLabel: ctr.ProcessLabel,
159
+		ExecIDs:      ctr.GetExecIDs(),
162 160
 		HostConfig:   &hostConfig,
161
+		GraphDriver: storage.DriverData{
162
+			Name: ctr.Driver,
163
+		},
163 164
 	}
164 165
 
165 166
 	// Now set any platform-specific fields
166
-	contJSONBase = setPlatformSpecificContainerFields(container, contJSONBase)
167
-
168
-	contJSONBase.GraphDriver.Name = container.Driver
167
+	inspectResponse = setPlatformSpecificContainerFields(ctr, inspectResponse)
169 168
 
170 169
 	if daemon.UsesSnapshotter() {
171 170
 		// Additional information only applies to graphDrivers, so we're done.
172
-		return contJSONBase, nil
171
+		return inspectResponse, nil
173 172
 	}
174 173
 
175
-	if container.RWLayer == nil {
176
-		if container.Dead {
177
-			return contJSONBase, nil
174
+	if ctr.RWLayer == nil {
175
+		if ctr.Dead {
176
+			return inspectResponse, nil
178 177
 		}
179
-		return nil, errdefs.System(errors.New("RWLayer of container " + container.ID + " is unexpectedly nil"))
178
+		return nil, errdefs.System(errors.New("RWLayer of container " + ctr.ID + " is unexpectedly nil"))
180 179
 	}
181 180
 
182
-	graphDriverData, err := container.RWLayer.Metadata()
181
+	graphDriverData, err := ctr.RWLayer.Metadata()
183 182
 	if err != nil {
184
-		if container.Dead {
183
+		if ctr.Dead {
185 184
 			// container is marked as Dead, and its graphDriver metadata may
186 185
 			// have been removed; we can ignore errors.
187
-			return contJSONBase, nil
186
+			return inspectResponse, nil
188 187
 		}
189 188
 		return nil, errdefs.System(err)
190 189
 	}
191 190
 
192
-	contJSONBase.GraphDriver.Data = graphDriverData
193
-	return contJSONBase, nil
191
+	inspectResponse.GraphDriver.Data = graphDriverData
192
+	return inspectResponse, nil
194 193
 }
195 194
 
196 195
 // ContainerExecInspect returns low-level information about the exec
... ...
@@ -6,11 +6,11 @@ import (
6 6
 )
7 7
 
8 8
 // This sets platform-specific fields
9
-func setPlatformSpecificContainerFields(container *containerpkg.Container, contJSONBase *container.ContainerJSONBase) *container.ContainerJSONBase {
10
-	contJSONBase.AppArmorProfile = container.AppArmorProfile
11
-	contJSONBase.ResolvConfPath = container.ResolvConfPath
12
-	contJSONBase.HostnamePath = container.HostnamePath
13
-	contJSONBase.HostsPath = container.HostsPath
9
+func setPlatformSpecificContainerFields(ctr *containerpkg.Container, resp *container.ContainerJSONBase) *container.ContainerJSONBase {
10
+	resp.AppArmorProfile = ctr.AppArmorProfile
11
+	resp.ResolvConfPath = ctr.ResolvConfPath
12
+	resp.HostnamePath = ctr.HostnamePath
13
+	resp.HostsPath = ctr.HostsPath
14 14
 
15
-	return contJSONBase
15
+	return resp
16 16
 }
... ...
@@ -6,6 +6,6 @@ import (
6 6
 )
7 7
 
8 8
 // This sets platform-specific fields
9
-func setPlatformSpecificContainerFields(container *containerpkg.Container, contJSONBase *container.ContainerJSONBase) *container.ContainerJSONBase {
10
-	return contJSONBase
9
+func setPlatformSpecificContainerFields(ctr *containerpkg.Container, resp *container.ContainerJSONBase) *container.ContainerJSONBase {
10
+	return resp
11 11
 }