Skip to content

Commit

Permalink
feat: ability to choose container platform
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r committed Sep 23, 2024
1 parent 66403e5 commit 17eed30
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
17 changes: 9 additions & 8 deletions cmd/vela-worker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ func run(c *cli.Context) error {
},
// runtime configuration
Runtime: &runtime.Setup{
Driver: c.String("runtime.driver"),
ConfigFile: c.String("runtime.config"),
Namespace: c.String("runtime.namespace"),
PodsTemplateName: c.String("runtime.pods-template-name"),
PodsTemplateFile: c.Path("runtime.pods-template-file"),
HostVolumes: c.StringSlice("runtime.volumes"),
PrivilegedImages: c.StringSlice("runtime.privileged-images"),
DropCapabilities: c.StringSlice("runtime.drop-capabilities"),
Driver: c.String("runtime.driver"),
ConfigFile: c.String("runtime.config"),
Namespace: c.String("runtime.namespace"),
PodsTemplateName: c.String("runtime.pods-template-name"),
PodsTemplateFile: c.Path("runtime.pods-template-file"),
HostVolumes: c.StringSlice("runtime.volumes"),
PrivilegedImages: c.StringSlice("runtime.privileged-images"),
DropCapabilities: c.StringSlice("runtime.drop-capabilities"),
ContainerPlatform: c.String("runtime.container-platform"),
},
// queue configuration
Queue: &queue.Setup{
Expand Down
11 changes: 10 additions & 1 deletion runtime/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
dockerContainerTypes "github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
v1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/pipeline"
Expand Down Expand Up @@ -147,6 +148,11 @@ func (c *client) RunContainer(ctx context.Context, ctn *pipeline.Container, b *p
}
}

platform := strings.Split(c.config.ContainerPlatform, "/")
if len(platform) != 2 {
return fmt.Errorf("invalid container platform %s", c.config.ContainerPlatform)
}

// send API call to create the container
//
// https://pkg.go.dev/github.com/docker/docker/client#Client.ContainerCreate
Expand All @@ -155,7 +161,10 @@ func (c *client) RunContainer(ctx context.Context, ctn *pipeline.Container, b *p
containerConf,
hostConf,
networkConf,
nil,
&v1.Platform{
OS: platform[0],
Architecture: platform[1],
},
ctn.ID,
)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type config struct {
Volumes []string
// specifies a list of kernel capabilities to drop for each Docker container
DropCapabilities []string
//
ContainerPlatform string
}

type client struct {
Expand Down Expand Up @@ -90,6 +92,8 @@ func New(opts ...ClientOpt) (*client, error) {
// set the Docker client in the runtime client
c.Docker = _docker

c.Logger.Infof("Docker client initialized with API version %s", c.Docker.ClientVersion())

return c, nil
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func (c *client) CreateImage(ctx context.Context, ctn *pipeline.Container) error
// create options for pulling image
//
// https://pkg.go.dev/github.com/docker/docker/api/types/image#PullOptions
opts := dockerImageTypes.PullOptions{}
opts := dockerImageTypes.PullOptions{
Platform: c.config.ContainerPlatform,
}

// send API call to pull the image for the container
//
Expand Down
9 changes: 9 additions & 0 deletions runtime/docker/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ func WithDropCapabilities(caps []string) ClientOpt {
return nil
}
}

func WithContainerPlatform(platform string) ClientOpt {
return func(c *client) error {
c.Logger.Trace("configuring container platform in docker runtime client")
// set the runtime container platform in the docker client
c.config.ContainerPlatform = platform
return nil
}
}
9 changes: 9 additions & 0 deletions runtime/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package runtime

import (
"runtime"

"github.com/urfave/cli/v2"

"github.com/go-vela/types/constants"
Expand Down Expand Up @@ -64,4 +66,11 @@ var Flags = []cli.Flag{
Name: "runtime.drop-capabilities",
Usage: "list of kernel capabilities to drop from container privileges (only used by Docker)",
},
&cli.StringFlag{
EnvVars: []string{"VELA_RUNTIME_CONTAINER_PLATFORM", "RUNTIME_CONTAINER_PLATFORM"},
FilePath: "/vela/runtime/container_platform",
Name: "runtime.container-platform",
Usage: "container platform to be used for the runtime",
Value: "linux/" + runtime.GOARCH,
},
}
3 changes: 3 additions & 0 deletions runtime/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Setup struct {
PrivilegedImages []string
// specifies a list of kernel capabilities to drop from container (only used by Docker)
DropCapabilities []string
//
ContainerPlatform string
}

// Docker creates and returns a Vela engine capable of
Expand All @@ -55,6 +57,7 @@ func (s *Setup) Docker() (Engine, error) {
docker.WithPrivilegedImages(s.PrivilegedImages),
docker.WithLogger(s.Logger),
docker.WithDropCapabilities(s.DropCapabilities),
docker.WithContainerPlatform(s.ContainerPlatform),
}

if s.Mock {
Expand Down

0 comments on commit 17eed30

Please sign in to comment.