From a34a65796b34f5baa3759d20bb5c72465352caaf Mon Sep 17 00:00:00 2001 From: Thomas Cooper <57812123+coopernetes@users.noreply.github.com> Date: Fri, 6 Oct 2023 09:00:03 -0400 Subject: [PATCH] feat: --platform flag - for images under test, pass the new --platform flag to pull the corresponding image if it is multi-platform capable - test execution via CreateContainerOptions passes in platform. If unset, will default to linux/amd64 --- cmd/container-structure-test/app/cmd/test.go | 4 +++- go.mod | 10 ++++++---- go.sum | 17 +++++++++-------- pkg/config/options.go | 1 + pkg/drivers/docker_driver.go | 6 ++++++ pkg/drivers/driver.go | 1 + 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cmd/container-structure-test/app/cmd/test.go b/cmd/container-structure-test/app/cmd/test.go index 6a0e3d3b..bd5f7b4b 100644 --- a/cmd/container-structure-test/app/cmd/test.go +++ b/cmd/container-structure-test/app/cmd/test.go @@ -101,6 +101,7 @@ func run(out io.Writer) error { Save: opts.Save, Metadata: opts.Metadata, Runtime: opts.Runtime, + Platform: opts.Platform, } var err error @@ -172,6 +173,7 @@ func run(out io.Writer) error { logrus.Fatalf("error connecting to daemon: %v", err) } if err = client.PullImage(docker.PullImageOptions{ + Platform: opts.Platform, Repository: ref.Context().RepositoryStr(), Tag: ref.Identifier(), Registry: ref.Context().RegistryStr(), @@ -225,7 +227,7 @@ func AddTestFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&opts.Driver, "driver", "d", "docker", "driver to use when running tests") cmd.Flags().StringVar(&opts.Metadata, "metadata", "", "path to image metadata file") cmd.Flags().StringVar(&opts.Runtime, "runtime", "", "runtime to use with docker driver") - + cmd.Flags().StringVar(&opts.Platform, "platform", "linux/amd64", "Set platform if host is multi-platform capable") cmd.Flags().BoolVar(&opts.Pull, "pull", false, "force a pull of the image before running tests") cmd.MarkFlagsMutuallyExclusive("image-from-oci-layout", "pull") cmd.Flags().BoolVar(&opts.Save, "save", false, "preserve created containers after test run") diff --git a/go.mod b/go.mod index 8e3f0255..ee6ec131 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/GoogleContainerTools/container-diff v0.17.1-0.20230727210151-35d9770aeea3 - github.com/fsouza/go-dockerclient v1.9.7 + github.com/fsouza/go-dockerclient v1.10.0 github.com/google/go-cmp v0.5.9 github.com/google/go-containerregistry v0.15.2 github.com/joho/godotenv v1.5.1 @@ -16,6 +16,8 @@ require ( gopkg.in/yaml.v2 v2.4.0 ) +exclude github.com/docker/docker v24.0.6+incompatible // indirect + require ( github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect @@ -32,7 +34,7 @@ require ( github.com/klauspost/compress v1.16.5 // indirect github.com/kr/text v0.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/moby/patternmatcher v0.5.0 // indirect + github.com/moby/patternmatcher v0.6.0 // indirect github.com/moby/sys/sequential v0.5.0 // indirect github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect github.com/morikuni/aec v1.0.0 // indirect @@ -43,7 +45,7 @@ require ( golang.org/x/mod v0.10.0 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.10.0 // indirect - golang.org/x/term v0.10.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect golang.org/x/tools v0.8.0 // indirect ) diff --git a/go.sum b/go.sum index d561814b..b3924eba 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsouza/go-dockerclient v1.3.6/go.mod h1:ptN6nXBwrXuiHAz2TYGOFCBB1aKGr371sGjMFdJEr1A= -github.com/fsouza/go-dockerclient v1.9.7 h1:FlIrT71E62zwKgRvCvWGdxRD+a/pIy+miY/n3MXgfuw= -github.com/fsouza/go-dockerclient v1.9.7/go.mod h1:vx9C32kE2D15yDSOMCDaAEIARZpDQDFBHeqL3MgQy/U= +github.com/fsouza/go-dockerclient v1.10.0 h1:ppSBsbR60I1DFbV4Ag7LlHlHakHFRNLk9XakATW1yVQ= +github.com/fsouza/go-dockerclient v1.10.0/go.mod h1:+iNzAW78AzClIBTZ6WFjkaMvOgz68GyCJ236b1opLTs= github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= github.com/go-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -148,8 +148,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A= -github.com/moby/patternmatcher v0.5.0 h1:YCZgJOeULcxLw1Q+sVR636pmS7sPEn1Qo2iAN6M7DBo= -github.com/moby/patternmatcher v0.5.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= +github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk= +github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc= github.com/moby/sys/sequential v0.5.0/go.mod h1:tH2cOOs5V9MlPiXcQzRC+eEyab644PWKGRYaaV5ZZlo= @@ -207,6 +207,7 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -304,11 +305,11 @@ golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c= -golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/pkg/config/options.go b/pkg/config/options.go index 86df8c05..576af6a2 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -22,6 +22,7 @@ type StructureTestOptions struct { DefaultImageTag string Driver string Runtime string + Platform string Metadata string TestReport string ConfigFiles []string diff --git a/pkg/drivers/docker_driver.go b/pkg/drivers/docker_driver.go index c71bcb46..2dea126e 100644 --- a/pkg/drivers/docker_driver.go +++ b/pkg/drivers/docker_driver.go @@ -42,6 +42,7 @@ type DockerDriver struct { env map[string]string save bool runtime string + platform string runOpts unversioned.ContainerRunOptions } @@ -57,6 +58,7 @@ func NewDockerDriver(args DriverConfig) (Driver, error) { env: nil, save: args.Save, runtime: args.Runtime, + platform: args.Platform, runOpts: args.RunOpts, }, nil } @@ -98,6 +100,7 @@ func (d *DockerDriver) Destroy() { func (d *DockerDriver) SetEnv(envVars []unversioned.EnvVar) error { env := d.processEnvVars(envVars) container, err := d.cli.CreateContainer(docker.CreateContainerOptions{ + Platform: d.platform, Config: &docker.Config{ Image: d.currentImage, Env: env, @@ -205,6 +208,7 @@ func (d *DockerDriver) retrieveTar(path string) (*tar.Reader, error) { // this contains a placeholder command which does not get run, since // the client doesn't allow creating a container without a command. container, err := d.cli.CreateContainer(docker.CreateContainerOptions{ + Platform: d.platform, Config: &docker.Config{ Image: d.currentImage, Cmd: []string{utils.NoopCommand}, @@ -316,6 +320,7 @@ func (d *DockerDriver) ReadDir(target string) ([]os.FileInfo, error) { // and sets that image as the new "current image" func (d *DockerDriver) runAndCommit(env []string, command []string) (string, error) { createOpts := docker.CreateContainerOptions{ + Platform: d.platform, Config: &docker.Config{ Image: d.currentImage, Env: env, @@ -365,6 +370,7 @@ func (d *DockerDriver) runAndCommit(env []string, command []string) (string, err func (d *DockerDriver) exec(env []string, command []string) (string, string, int, error) { createOpts := docker.CreateContainerOptions{ + Platform: d.platform, Config: &docker.Config{ Image: d.currentImage, Env: env, diff --git a/pkg/drivers/driver.go b/pkg/drivers/driver.go index d6726c43..d743a85f 100644 --- a/pkg/drivers/driver.go +++ b/pkg/drivers/driver.go @@ -33,6 +33,7 @@ type DriverConfig struct { Save bool // used by Docker/Tar drivers Metadata string // used by Host driver Runtime string // used by Docker driver + Platform string // used by Docker driver RunOpts unversioned.ContainerRunOptions // used by Docker driver }