@@ -3,6 +3,7 @@ package container
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
67 "fmt"
78 "os"
89 "path"
@@ -19,7 +20,6 @@ import (
1920 "github.com/moby/moby/api/types/container"
2021 "github.com/moby/moby/api/types/mount"
2122 "github.com/moby/moby/api/types/network"
22- "github.com/pkg/errors"
2323 "github.com/spf13/pflag"
2424 cdi "tags.cncf.io/container-device-interface/pkg/parser"
2525)
@@ -351,7 +351,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
351351 // Validate the input mac address
352352 if copts .macAddress != "" {
353353 if _ , err := opts .ValidateMACAddress (copts .macAddress ); err != nil {
354- return nil , errors .Errorf ("%s is not a valid mac address" , copts .macAddress )
354+ return nil , fmt .Errorf ("%s is not a valid mac address" , copts .macAddress )
355355 }
356356 }
357357 if copts .stdin {
@@ -367,7 +367,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
367367
368368 swappiness := copts .swappiness
369369 if swappiness != - 1 && (swappiness < 0 || swappiness > 100 ) {
370- return nil , errors .Errorf ("invalid value: %d. Valid memory swappiness range is 0-100" , swappiness )
370+ return nil , fmt .Errorf ("invalid value: %d. Valid memory swappiness range is 0-100" , swappiness )
371371 }
372372
373373 var binds []string
@@ -442,7 +442,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
442442 // Merge in exposed ports to the map of published ports
443443 for _ , e := range copts .expose .GetSlice () {
444444 if strings .Contains (e , ":" ) {
445- return nil , errors .Errorf ("invalid port format for --expose: %s" , e )
445+ return nil , fmt .Errorf ("invalid port format for --expose: %s" , e )
446446 }
447447 // support two formats for expose, original format <portnum>/[<proto>]
448448 // or <startport-endport>/[<proto>]
@@ -451,7 +451,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
451451 // if expose a port, the start and end port are the same
452452 start , end , err := nat .ParsePortRange (port )
453453 if err != nil {
454- return nil , errors .Errorf ("invalid range format for --expose: %s, error: %s " , e , err )
454+ return nil , fmt .Errorf ("invalid range format for --expose: %s, error: %w " , e , err )
455455 }
456456 for i := start ; i <= end ; i ++ {
457457 p , err := nat .NewPort (proto , strconv .FormatUint (i , 10 ))
@@ -505,22 +505,22 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
505505
506506 pidMode := container .PidMode (copts .pidMode )
507507 if ! pidMode .Valid () {
508- return nil , errors .Errorf ("--pid: invalid PID mode" )
508+ return nil , errors .New ("--pid: invalid PID mode" )
509509 }
510510
511511 utsMode := container .UTSMode (copts .utsMode )
512512 if ! utsMode .Valid () {
513- return nil , errors .Errorf ("--uts: invalid UTS mode" )
513+ return nil , errors .New ("--uts: invalid UTS mode" )
514514 }
515515
516516 usernsMode := container .UsernsMode (copts .usernsMode )
517517 if ! usernsMode .Valid () {
518- return nil , errors .Errorf ("--userns: invalid USER mode" )
518+ return nil , errors .New ("--userns: invalid USER mode" )
519519 }
520520
521521 cgroupnsMode := container .CgroupnsMode (copts .cgroupnsMode )
522522 if ! cgroupnsMode .Valid () {
523- return nil , errors .Errorf ("--cgroupns: invalid CGROUP mode" )
523+ return nil , errors .New ("--cgroupns: invalid CGROUP mode" )
524524 }
525525
526526 restartPolicy , err := opts .ParseRestartPolicy (copts .restartPolicy )
@@ -555,7 +555,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
555555 copts .healthStartInterval != 0
556556 if copts .noHealthcheck {
557557 if haveHealthSettings {
558- return nil , errors .Errorf ("--no-healthcheck conflicts with --health-* options" )
558+ return nil , errors .New ("--no-healthcheck conflicts with --health-* options" )
559559 }
560560 healthConfig = & container.HealthConfig {Test : []string {"NONE" }}
561561 } else if haveHealthSettings {
@@ -564,13 +564,13 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
564564 probe = []string {"CMD-SHELL" , copts .healthCmd }
565565 }
566566 if copts .healthInterval < 0 {
567- return nil , errors .Errorf ("--health-interval cannot be negative" )
567+ return nil , errors .New ("--health-interval cannot be negative" )
568568 }
569569 if copts .healthTimeout < 0 {
570- return nil , errors .Errorf ("--health-timeout cannot be negative" )
570+ return nil , errors .New ("--health-timeout cannot be negative" )
571571 }
572572 if copts .healthRetries < 0 {
573- return nil , errors .Errorf ("--health-retries cannot be negative" )
573+ return nil , errors .New ("--health-retries cannot be negative" )
574574 }
575575 if copts .healthStartPeriod < 0 {
576576 return nil , errors .New ("--health-start-period cannot be negative" )
@@ -703,7 +703,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
703703 }
704704
705705 if copts .autoRemove && ! hostConfig .RestartPolicy .IsNone () {
706- return nil , errors .Errorf ("conflicting options: cannot specify both --restart and --rm" )
706+ return nil , errors .New ("conflicting options: cannot specify both --restart and --rm" )
707707 }
708708
709709 // only set this value if the user provided the flag, else it should default to nil
@@ -786,7 +786,7 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*network.EndpointSett
786786 return nil , err
787787 }
788788 if _ , ok := endpoints [n .Target ]; ok {
789- return nil , invalidParameter (errors .Errorf ("network %q is specified multiple times" , n .Target ))
789+ return nil , invalidParameter (fmt .Errorf ("network %q is specified multiple times" , n .Target ))
790790 }
791791
792792 // For backward compatibility: if no custom options are provided for the network,
@@ -884,7 +884,7 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*network.Endpoint
884884 }
885885 if ep .MacAddress != "" {
886886 if _ , err := opts .ValidateMACAddress (ep .MacAddress ); err != nil {
887- return nil , errors .Errorf ("%s is not a valid mac address" , ep .MacAddress )
887+ return nil , fmt .Errorf ("%s is not a valid mac address" , ep .MacAddress )
888888 }
889889 epConfig .MacAddress = ep .MacAddress
890890 }
@@ -899,7 +899,7 @@ func convertToStandardNotation(ports []string) ([]string, error) {
899899 for _ , param := range strings .Split (publish , "," ) {
900900 k , v , ok := strings .Cut (param , "=" )
901901 if ! ok || k == "" {
902- return optsList , errors .Errorf ("invalid publish opts format (should be name=value but got '%s')" , param )
902+ return optsList , fmt .Errorf ("invalid publish opts format (should be name=value but got '%s')" , param )
903903 }
904904 params [k ] = v
905905 }
@@ -914,7 +914,7 @@ func convertToStandardNotation(ports []string) ([]string, error) {
914914func parseLoggingOpts (loggingDriver string , loggingOpts []string ) (map [string ]string , error ) {
915915 loggingOptsMap := opts .ConvertKVStringsToMap (loggingOpts )
916916 if loggingDriver == "none" && len (loggingOpts ) > 0 {
917- return map [string ]string {}, errors .Errorf ("invalid logging opts for driver %s" , loggingDriver )
917+ return map [string ]string {}, fmt .Errorf ("invalid logging opts for driver %s" , loggingDriver )
918918 }
919919 return loggingOptsMap , nil
920920}
@@ -928,7 +928,7 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) {
928928 }
929929 if (! ok || v == "" ) && k != "no-new-privileges" {
930930 // "no-new-privileges" is the only option that does not require a value.
931- return securityOpts , errors .Errorf ("Invalid --security-opt: %q" , opt )
931+ return securityOpts , fmt .Errorf ("invalid --security-opt: %q" , opt )
932932 }
933933 if k == "seccomp" {
934934 switch v {
@@ -939,11 +939,11 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) {
939939 // content if it's valid JSON.
940940 f , err := os .ReadFile (v )
941941 if err != nil {
942- return securityOpts , errors .Errorf ("opening seccomp profile (%s) failed: %v " , v , err )
942+ return securityOpts , fmt .Errorf ("opening seccomp profile (%s) failed: %w " , v , err )
943943 }
944944 b := bytes .NewBuffer (nil )
945945 if err := json .Compact (b , f ); err != nil {
946- return securityOpts , errors .Errorf ("compacting json for seccomp profile (%s) failed: %v " , v , err )
946+ return securityOpts , fmt .Errorf ("compacting json for seccomp profile (%s) failed: %w " , v , err )
947947 }
948948 securityOpts [key ] = fmt .Sprintf ("seccomp=%s" , b .Bytes ())
949949 }
@@ -978,7 +978,7 @@ func parseStorageOpts(storageOpts []string) (map[string]string, error) {
978978 for _ , option := range storageOpts {
979979 k , v , ok := strings .Cut (option , "=" )
980980 if ! ok {
981- return nil , errors .Errorf ("invalid storage option" )
981+ return nil , errors .New ("invalid storage option" )
982982 }
983983 m [k ] = v
984984 }
@@ -993,7 +993,7 @@ func parseDevice(device, serverOS string) (container.DeviceMapping, error) {
993993 case "windows" :
994994 return parseWindowsDevice (device )
995995 }
996- return container.DeviceMapping {}, errors .Errorf ("unknown server OS: %s" , serverOS )
996+ return container.DeviceMapping {}, fmt .Errorf ("unknown server OS: %s" , serverOS )
997997}
998998
999999// parseLinuxDevice parses a device mapping string to a container.DeviceMapping struct
@@ -1017,7 +1017,7 @@ func parseLinuxDevice(device string) (container.DeviceMapping, error) {
10171017 case 1 :
10181018 src = arr [0 ]
10191019 default :
1020- return container.DeviceMapping {}, errors .Errorf ("invalid device specification: %s" , device )
1020+ return container.DeviceMapping {}, fmt .Errorf ("invalid device specification: %s" , device )
10211021 }
10221022
10231023 if dst == "" {
@@ -1047,7 +1047,7 @@ func validateDeviceCgroupRule(val string) (string, error) {
10471047 return val , nil
10481048 }
10491049
1050- return val , errors .Errorf ("invalid device cgroup format '%s'" , val )
1050+ return val , fmt .Errorf ("invalid device cgroup format '%s'" , val )
10511051}
10521052
10531053// validDeviceMode checks if the mode for device is valid or not.
@@ -1079,7 +1079,7 @@ func validateDevice(val string, serverOS string) (string, error) {
10791079 // Windows does validation entirely server-side
10801080 return val , nil
10811081 }
1082- return "" , errors .Errorf ("unknown server OS: %s" , serverOS )
1082+ return "" , fmt .Errorf ("unknown server OS: %s" , serverOS )
10831083}
10841084
10851085// validateLinuxPath is the implementation of validateDevice knowing that the
@@ -1094,12 +1094,12 @@ func validateLinuxPath(val string, validator func(string) bool) (string, error)
10941094 var mode string
10951095
10961096 if strings .Count (val , ":" ) > 2 {
1097- return val , errors .Errorf ("bad format for path: %s" , val )
1097+ return val , fmt .Errorf ("bad format for path: %s" , val )
10981098 }
10991099
11001100 split := strings .SplitN (val , ":" , 3 )
11011101 if split [0 ] == "" {
1102- return val , errors .Errorf ("bad format for path: %s" , val )
1102+ return val , fmt .Errorf ("bad format for path: %s" , val )
11031103 }
11041104 switch len (split ) {
11051105 case 1 :
@@ -1118,13 +1118,13 @@ func validateLinuxPath(val string, validator func(string) bool) (string, error)
11181118 containerPath = split [1 ]
11191119 mode = split [2 ]
11201120 if isValid := validator (split [2 ]); ! isValid {
1121- return val , errors .Errorf ("bad mode specified: %s" , mode )
1121+ return val , fmt .Errorf ("bad mode specified: %s" , mode )
11221122 }
11231123 val = fmt .Sprintf ("%s:%s:%s" , split [0 ], containerPath , mode )
11241124 }
11251125
11261126 if ! path .IsAbs (containerPath ) {
1127- return val , errors .Errorf ("%s is not an absolute path" , containerPath )
1127+ return val , fmt .Errorf ("%s is not an absolute path" , containerPath )
11281128 }
11291129 return val , nil
11301130}
@@ -1137,5 +1137,5 @@ func validateAttach(val string) (string, error) {
11371137 return s , nil
11381138 }
11391139 }
1140- return val , errors .Errorf ("valid streams are STDIN, STDOUT and STDERR" )
1140+ return val , errors .New ("valid streams are STDIN, STDOUT and STDERR" )
11411141}
0 commit comments