Browse code

daemon: remove/rename err-returns and remove naked returns

Prevent accidentally shadowing these errors, which are used in defers, and
remove naked returns.

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

Sebastiaan van Stijn authored on 2023/12/28 21:22:37
Showing 9 changed files
... ...
@@ -226,10 +226,10 @@ func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *
226 226
 
227 227
 // verifyContainerSettings performs validation of the hostconfig and config
228 228
 // structures.
229
-func (daemon *Daemon) verifyContainerSettings(daemonCfg *configStore, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, err error) {
229
+func (daemon *Daemon) verifyContainerSettings(daemonCfg *configStore, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) (warnings []string, _ error) {
230 230
 	// First perform verification of settings common across all platforms.
231
-	if err = validateContainerConfig(config); err != nil {
232
-		return warnings, err
231
+	if err := validateContainerConfig(config); err != nil {
232
+		return nil, err
233 233
 	}
234 234
 
235 235
 	warns, err := validateHostConfig(hostConfig)
... ...
@@ -761,12 +761,12 @@ func (daemon *Daemon) connectToNetwork(ctx context.Context, cfg *config.Config,
761 761
 	if !ctr.Managed {
762 762
 		// add container name/alias to DNS
763 763
 		if err := daemon.ActivateContainerServiceBinding(ctr.Name); err != nil {
764
-			return fmt.Errorf("Activate container service binding for %s failed: %v", ctr.Name, err)
764
+			return fmt.Errorf("activate container service binding for %s failed: %v", ctr.Name, err)
765 765
 		}
766 766
 	}
767 767
 
768 768
 	if err := updateJoinInfo(ctr.NetworkSettings, n, ep); err != nil {
769
-		return fmt.Errorf("Updating join info failed: %v", err)
769
+		return fmt.Errorf("updating join info failed: %v", err)
770 770
 	}
771 771
 
772 772
 	ctr.NetworkSettings.Ports = getPortMapInfo(sb)
... ...
@@ -413,7 +413,7 @@ func adaptSharedNamespaceContainer(daemon containerGetter, hostConfig *container
413 413
 }
414 414
 
415 415
 // verifyPlatformContainerResources performs platform-specific validation of the container's resource-configuration
416
-func verifyPlatformContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo, update bool) (warnings []string, err error) {
416
+func verifyPlatformContainerResources(resources *containertypes.Resources, sysInfo *sysinfo.SysInfo, update bool) (warnings []string, _ error) {
417 417
 	fixMemorySwappiness(resources)
418 418
 
419 419
 	// memory subsystem checks and adjustments
... ...
@@ -650,7 +650,7 @@ func isRunningSystemd() bool {
650 650
 
651 651
 // verifyPlatformContainerSettings performs platform-specific validation of the
652 652
 // hostconfig and config structures.
653
-func verifyPlatformContainerSettings(daemon *Daemon, daemonCfg *configStore, hostConfig *containertypes.HostConfig, update bool) (warnings []string, err error) {
653
+func verifyPlatformContainerSettings(daemon *Daemon, daemonCfg *configStore, hostConfig *containertypes.HostConfig, update bool) (warnings []string, _ error) {
654 654
 	if hostConfig == nil {
655 655
 		return nil, nil
656 656
 	}
... ...
@@ -67,7 +67,7 @@ func (daemon *Daemon) adaptContainerSettings(daemonCfg *config.Config, hostConfi
67 67
 }
68 68
 
69 69
 // verifyPlatformContainerResources performs platform-specific validation of the container's resource-configuration
70
-func verifyPlatformContainerResources(resources *containertypes.Resources, isHyperv bool) (warnings []string, err error) {
70
+func verifyPlatformContainerResources(resources *containertypes.Resources, isHyperv bool) (warnings []string, _ error) {
71 71
 	fixMemorySwappiness(resources)
72 72
 	if !isHyperv {
73 73
 		// The processor resource controls are mutually exclusive on
... ...
@@ -171,7 +171,7 @@ func verifyPlatformContainerResources(resources *containertypes.Resources, isHyp
171 171
 
172 172
 // verifyPlatformContainerSettings performs platform-specific validation of the
173 173
 // hostconfig and config structures.
174
-func verifyPlatformContainerSettings(daemon *Daemon, daemonCfg *configStore, hostConfig *containertypes.HostConfig, update bool) (warnings []string, err error) {
174
+func verifyPlatformContainerSettings(daemon *Daemon, daemonCfg *configStore, hostConfig *containertypes.HostConfig, update bool) (warnings []string, _ error) {
175 175
 	if hostConfig == nil {
176 176
 		return nil, nil
177 177
 	}
... ...
@@ -589,14 +589,14 @@ func (daemon *Daemon) deleteNetwork(nw *libnetwork.Network, dynamic bool) error
589 589
 }
590 590
 
591 591
 // GetNetworks returns a list of all networks
592
-func (daemon *Daemon) GetNetworks(filter filters.Args, config backend.NetworkListConfig) (networks []networktypes.Inspect, err error) {
592
+func (daemon *Daemon) GetNetworks(filter filters.Args, config backend.NetworkListConfig) ([]networktypes.Inspect, error) {
593 593
 	var idx map[string]*libnetwork.Network
594 594
 	if config.Detailed {
595 595
 		idx = make(map[string]*libnetwork.Network)
596 596
 	}
597 597
 
598 598
 	allNetworks := daemon.getAllNetworks()
599
-	networks = make([]networktypes.Inspect, 0, len(allNetworks))
599
+	networks := make([]networktypes.Inspect, 0, len(allNetworks))
600 600
 	for _, n := range allNetworks {
601 601
 		nr := buildNetworkResource(n)
602 602
 		networks = append(networks, nr)
... ...
@@ -605,6 +605,7 @@ func (daemon *Daemon) GetNetworks(filter filters.Args, config backend.NetworkLis
605 605
 		}
606 606
 	}
607 607
 
608
+	var err error
608 609
 	networks, err = network.FilterNetworks(networks, filter)
609 610
 	if err != nil {
610 611
 		return nil, err
... ...
@@ -476,7 +476,7 @@ func inSlice(slice []string, s string) bool {
476 476
 
477 477
 // withMounts sets the container's mounts
478 478
 func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, mounts []container.Mount) coci.SpecOpts {
479
-	return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) (err error) {
479
+	return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
480 480
 		sortMounts(mounts)
481 481
 
482 482
 		userMounts := make(map[string]struct{})
... ...
@@ -1003,7 +1003,7 @@ func WithUser(c *container.Container) coci.SpecOpts {
1003 1003
 	}
1004 1004
 }
1005 1005
 
1006
-func (daemon *Daemon) createSpec(ctx context.Context, daemonCfg *configStore, c *container.Container, mounts []container.Mount) (retSpec *specs.Spec, err error) {
1006
+func (daemon *Daemon) createSpec(ctx context.Context, daemonCfg *configStore, c *container.Container, mounts []container.Mount) (retSpec *specs.Spec, _ error) {
1007 1007
 	var (
1008 1008
 		opts []coci.SpecOpts
1009 1009
 		s    = oci.DefaultSpec()
... ...
@@ -478,7 +478,7 @@ func (daemon *Daemon) mergeUlimits(c *containertypes.HostConfig, daemonCfg *conf
478 478
 // listing only the methods we care about here.
479 479
 // It's mainly useful to easily allow mocking the registry in tests.
480 480
 type registryKey interface {
481
-	GetStringValue(name string) (val string, valtype uint32, err error)
481
+	GetStringValue(name string) (val string, valType uint32, err error)
482 482
 	Close() error
483 483
 }
484 484
 
... ...
@@ -526,7 +526,8 @@ func readCredentialSpecFile(id, root, location string) (string, error) {
526 526
 	return string(bcontents[:]), nil
527 527
 }
528 528
 
529
-func setupWindowsDevices(devices []containertypes.DeviceMapping) (specDevices []specs.WindowsDevice, err error) {
529
+func setupWindowsDevices(devices []containertypes.DeviceMapping) ([]specs.WindowsDevice, error) {
530
+	var specDevices []specs.WindowsDevice
530 531
 	for _, deviceMapping := range devices {
531 532
 		if strings.HasPrefix(deviceMapping.PathOnHost, "class/") {
532 533
 			specDevices = append(specDevices, specs.WindowsDevice{
... ...
@@ -111,7 +111,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
111 111
 	t.Run("happy path with a 'registry://' option", func(t *testing.T) {
112 112
 		valueName := "my-cred-spec"
113 113
 		key := &dummyRegistryKey{
114
-			getStringValueFunc: func(name string) (val string, valtype uint32, err error) {
114
+			getStringValueFunc: func(name string) (val string, valType uint32, _ error) {
115 115
 				assert.Equal(t, valueName, name)
116 116
 				return dummyCredFileContents, 0, nil
117 117
 			},
... ...
@@ -141,7 +141,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
141 141
 	t.Run("when using a 'registry://' option pointing to a value that doesn't exist, it fails gracefully", func(t *testing.T) {
142 142
 		valueName := "my-cred-spec"
143 143
 		key := &dummyRegistryKey{
144
-			getStringValueFunc: func(name string) (val string, valtype uint32, err error) {
144
+			getStringValueFunc: func(name string) (val string, valType uint32, _ error) {
145 145
 				assert.Equal(t, valueName, name)
146 146
 				return "", 0, registry.ErrNotExist
147 147
 			},
... ...
@@ -159,7 +159,7 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
159 159
 		dummyError := fmt.Errorf("dummy error")
160 160
 		valueName := "my-cred-spec"
161 161
 		key := &dummyRegistryKey{
162
-			getStringValueFunc: func(name string) (val string, valtype uint32, err error) {
162
+			getStringValueFunc: func(name string) (val string, valType uint32, _ error) {
163 163
 				assert.Equal(t, valueName, name)
164 164
 				return "", 0, dummyError
165 165
 			},
... ...
@@ -274,11 +274,11 @@ func TestSetWindowsCredentialSpecInSpec(t *testing.T) {
274 274
 /* Helpers below */
275 275
 
276 276
 type dummyRegistryKey struct {
277
-	getStringValueFunc func(name string) (val string, valtype uint32, err error)
277
+	getStringValueFunc func(name string) (val string, valType uint32, err error)
278 278
 	closed             bool
279 279
 }
280 280
 
281
-func (k *dummyRegistryKey) GetStringValue(name string) (val string, valtype uint32, err error) {
281
+func (k *dummyRegistryKey) GetStringValue(name string) (val string, valType uint32, _ error) {
282 282
 	return k.getStringValueFunc(name)
283 283
 }
284 284
 
... ...
@@ -358,8 +358,7 @@ func getSystemCPUUsage() (cpuUsage uint64, cpuNum uint32, _ error) {
358 358
 				}
359 359
 				totalClockTicks += v
360 360
 			}
361
-			cpuUsage = (totalClockTicks * nanoSecondsPerSecond) /
362
-				clockTicksPerSecond
361
+			cpuUsage = (totalClockTicks * nanoSecondsPerSecond) / clockTicksPerSecond
363 362
 		}
364 363
 		if '0' <= line[3] && line[3] <= '9' {
365 364
 			cpuNum++