Browse code

daemon/builder/dockerfile: copyRunConfig: use slices/maps.Clone

Simplify the code by replacing our DYI code to clone using
the slices and maps packages.

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

Sebastiaan van Stijn authored on 2025/08/09 21:58:06
Showing 1 changed files
... ...
@@ -8,6 +8,8 @@ import (
8 8
 	"crypto/sha256"
9 9
 	"encoding/hex"
10 10
 	"fmt"
11
+	"maps"
12
+	"slices"
11 13
 	"strings"
12 14
 
13 15
 	"github.com/containerd/log"
... ...
@@ -279,32 +281,14 @@ func withoutHealthcheck() runConfigModifier {
279 279
 
280 280
 func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
281 281
 	cfgCopy := *runConfig
282
-	cfgCopy.Cmd = copyStringSlice(runConfig.Cmd)
283
-	cfgCopy.Env = copyStringSlice(runConfig.Env)
284
-	cfgCopy.Entrypoint = copyStringSlice(runConfig.Entrypoint)
285
-	cfgCopy.OnBuild = copyStringSlice(runConfig.OnBuild)
286
-	cfgCopy.Shell = copyStringSlice(runConfig.Shell)
287
-
288
-	if cfgCopy.Volumes != nil {
289
-		cfgCopy.Volumes = make(map[string]struct{}, len(runConfig.Volumes))
290
-		for k, v := range runConfig.Volumes {
291
-			cfgCopy.Volumes[k] = v
292
-		}
293
-	}
294
-
295
-	if cfgCopy.ExposedPorts != nil {
296
-		cfgCopy.ExposedPorts = make(container.PortSet, len(runConfig.ExposedPorts))
297
-		for k, v := range runConfig.ExposedPorts {
298
-			cfgCopy.ExposedPorts[k] = v
299
-		}
300
-	}
301
-
302
-	if cfgCopy.Labels != nil {
303
-		cfgCopy.Labels = make(map[string]string, len(runConfig.Labels))
304
-		for k, v := range runConfig.Labels {
305
-			cfgCopy.Labels[k] = v
306
-		}
307
-	}
282
+	cfgCopy.Cmd = slices.Clone(runConfig.Cmd)
283
+	cfgCopy.Env = slices.Clone(runConfig.Env)
284
+	cfgCopy.Entrypoint = slices.Clone(runConfig.Entrypoint)
285
+	cfgCopy.OnBuild = slices.Clone(runConfig.OnBuild)
286
+	cfgCopy.Shell = slices.Clone(runConfig.Shell)
287
+	cfgCopy.Volumes = maps.Clone(runConfig.Volumes)
288
+	cfgCopy.ExposedPorts = maps.Clone(runConfig.ExposedPorts)
289
+	cfgCopy.Labels = maps.Clone(runConfig.Labels)
308 290
 
309 291
 	for _, modifier := range modifiers {
310 292
 		modifier(&cfgCopy)
... ...
@@ -312,13 +296,6 @@ func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier)
312 312
 	return &cfgCopy
313 313
 }
314 314
 
315
-func copyStringSlice(orig []string) []string {
316
-	if orig == nil {
317
-		return nil
318
-	}
319
-	return append([]string{}, orig...)
320
-}
321
-
322 315
 // getShell is a helper function which gets the right shell for prefixing the
323 316
 // shell-form of RUN, ENTRYPOINT and CMD instructions
324 317
 func getShell(c *container.Config, os string) []string {