Skip to content

Commit

Permalink
feat: add Docker repository (repo) parameter (#30)
Browse files Browse the repository at this point in the history
* refactor(build): simplify adding flags from slices

* feat: add Docker repository parameter

* chore: fix output and secret parameters

* chore: fix typos in comments
  • Loading branch information
jbrockopp authored Apr 5, 2021
1 parent 1438791 commit bbf86cd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 92 deletions.
7 changes: 4 additions & 3 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ The following parameters are used to configure the image:
| `cpu` | enables setting the cpu parameter, see [cpu](#cpu) settings below | `false` | N/A | `PARAMETER_CPU`<br>`DOCKER_CPU` |
| `daemon` | enables setting the daemon parameter, see [daemon](#daemon) settings below | `false` | N/A | `PARAMETER_DAEMON`<br>`DOCKER_DAEMON` |
| `disable_content_trust` | enables skipping image verification | `false` | N/A | `PARAMETER_DISABLE_CONTENT_TRUST`<br>`DOCKER_DISABLE_CONTENT_TRUST` |
| `dry_run` | enables building the image without publishing | `false` | `false` | `PARAMETER_DRY_RUN`<br>`DOCKER_DRY_RUN` |
| `dry_run` | enables building the image without publishing | `false` | `false` | `PARAMETER_DRY_RUN`<br>`DOCKER_DRY_RUN` |
| `file` | enables setting the name of the Dockerfile | `false` | N/A | `PARAMETER_FILE`<br>`DOCKER_FILE` |
| `force_rm` | enables setting always remove on intermediate containers | `false` | N/A | `PARAMETER_FORCE_RM`<br>`DOCKER_FORCE_RM` |
| `image_id_file` | enables setting writing the image ID to the file | `false` | N/A | `PARAMETER_IMAGE_ID_FILE`<br>`DOCKER_IMAGE_ID_FILE` |
Expand All @@ -178,13 +178,14 @@ The following parameters are used to configure the image:
| `network` | enables setting the networking mode for the RUN instructions during build | `false` | N/A | `PARAMETER_NETWORK`<br>`DOCKER_NETWORK` |
| `no_cache` | enables setting not use cache when building the image | `false` | N/A | `PARAMETER_NO_CACHE`<br>`DOCKER_NO_CACHE` |
| `outputs` | enables setting an output destination - format (type=local,dest=path) | `false` | N/A | `PARAMETER_OUTPUTS`<br>`DOCKER_OUTPUTS` |
| `password` | password for communication with the registry | `true` | N/A | `PARAMETER_PASSWORD`<br>`DOCKER_PASSWORD` |
| `password` | password for communication with the registry | `true` | N/A | `PARAMETER_PASSWORD`<br>`DOCKER_PASSWORD` |
| `platform` | enables setting a platform if server is multi-platform capable | `false` | N/A | `PARAMETER_PLATFORM`<br>`DOCKER_PLATFORM` |
| `progress` | enables setting type of progress output - options (auto\|plain\|tty) | `false` | N/A | `PARAMETER_PROGRESS`<br>`DOCKER_PROGRESS` |
| `pull` | enables always attempting to pull a newer version of the image | `false` | N/A | `PARAMETER_PULL`<br>`DOCKER_PULL` |
| `quiet` | enables suppressing the build output and print image ID on success | `false` | N/A | `PARAMETER_QUIET`<br>`DOCKER_QUIET` |
| `registry` | Docker registry address to communicate with | `true` | `index.docker.io` | `PARAMETER_REGISTRY`<br>`DOCKER_REGISTRY` |
| `registry` | Docker registry address to communicate with | `true` | `index.docker.io` | `PARAMETER_REGISTRY`<br>`DOCKER_REGISTRY` |
| `remove` | enables removing the intermediate containers after a successful build | `false` | N/A | `PARAMETER_REMOVE`<br>`DOCKER_REMOVE` |
| `repo` | Docker repository for the image | `false` | N/A | `PARAMETER_REPO`<br>`DOCKER_REPO` |
| `secrets` | enables setting a secret file to expose to the build - format (id=mysecret,src=/local/secret) | `false` | N/A | `PARAMETER_SECRETS`<br>`DOCKER_SECRETS` |
| `security_opts` | enables setting security options | `false` | N/A | `PARAMETER_SECURITY_OPTS`<br>`DOCKER_SECURITY_OPTS` |
| `shm_sizes` | enables setting the size of /dev/shm | `false` | N/A | `PARAMETER_SHM_SIZES`<br>`DOCKER_SHM_SIZES` |
Expand Down
158 changes: 75 additions & 83 deletions cmd/vela-docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type (
// enables setting not use cache when building the image
NoCache bool
// enables setting an output destination (format: type=local,dest=path)
Outputs []string
Output string
// enables setting a platform if server is multi-platform capable
Platform string
// enables setting type of progress output - options (auto|plain|tty)
Expand All @@ -72,8 +72,10 @@ type (
Quiet bool
// enables removing the intermediate containers after a successful build (default true)
Remove bool
// enables setting the Docker repository name for the image
Repo string
// enables setting a secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret
Secrets []string
Secret string
// enables setting security options
SecurityOpts []string
// enables setting the size of /dev/shm
Expand Down Expand Up @@ -229,11 +231,11 @@ var buildFlags = []cli.Flag{
Name: "build.no-cache",
Usage: "enables setting the networking mode for the RUN instructions during build (default \"default\")",
},
&cli.StringSliceFlag{
EnvVars: []string{"PARAMETER_OUTPUTS", "DOCKER_OUTPUTS"},
FilePath: "/vela/parameters/docker/outputs,/vela/secrets/docker/outputs",
Name: "build.outputs",
Usage: "enables setting an output destination (format: type=local,dest=path)",
&cli.StringFlag{
EnvVars: []string{"PARAMETER_OUTPUT", "DOCKER_OUTPUT"},
FilePath: "/vela/parameters/docker/output,/vela/secrets/docker/output",
Name: "build.output",
Usage: "set an output destination (format: type=local,dest=path)",
},
&cli.StringFlag{
EnvVars: []string{"PARAMETER_PLATFORM", "DOCKER_PLATFORM"},
Expand Down Expand Up @@ -266,11 +268,17 @@ var buildFlags = []cli.Flag{
Usage: "enables removing the intermediate containers after a successful build (default true)",
Value: true,
},
&cli.StringSliceFlag{
EnvVars: []string{"PARAMETER_SECRETS", "DOCKER_SECRETS"},
FilePath: "/vela/parameters/docker/secrets,/vela/secrets/docker/secrets",
Name: "build.secrets",
Usage: "enables setting a secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret",
&cli.StringFlag{
EnvVars: []string{"PARAMETER_REPO", "DOCKER_REPO"},
FilePath: "/vela/parameters/docker/repo,/vela/secrets/docker/repo",
Name: "build.repo",
Usage: "Docker repository name for the image",
},
&cli.StringFlag{
EnvVars: []string{"PARAMETER_SECRET", "DOCKER_SECRET"},
FilePath: "/vela/parameters/docker/secret,/vela/secrets/docker/secret",
Name: "build.secret",
Usage: "set a secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret",
},
&cli.StringSliceFlag{
EnvVars: []string{"PARAMETER_SECURITY_OPTS", "DOCKER_SECURITY_OPTS"},
Expand Down Expand Up @@ -358,20 +366,16 @@ func (b *Build) Command() (*exec.Cmd, error) {
// variable to store flags for command
var flags []string

// check if AddHosts is provided
if len(b.AddHosts) > 0 {
for _, a := range b.AddHosts {
// add flag for AddHosts from provided build command
flags = append(flags, "--add-host", a)
}
// iterate through the additional hosts provided
for _, a := range b.AddHosts {
// add flag for AddHosts from provided build command
flags = append(flags, "--add-host", a)
}

// check if BuildArgs is provided
if len(b.BuildArgs) > 0 {
for _, b := range b.BuildArgs {
// add flag for BuildArgs from provided build command
flags = append(flags, "--build-arg", b)
}
// iterate through the build arguments provided
for _, b := range b.BuildArgs {
// add flag for BuildArgs from provided build command
flags = append(flags, "--build-arg", b)
}

// check if CacheFrom is provided
Expand Down Expand Up @@ -425,28 +429,22 @@ func (b *Build) Command() (*exec.Cmd, error) {
flags = append(flags, "--isolation", b.Isolation)
}

// check if Labels is provided
if len(b.Labels) > 0 {
for _, l := range b.Labels {
// add flag for Labels from provided build command
flags = append(flags, "--label", l)
}
// iterate through the labels provided
for _, l := range b.Labels {
// add flag for Labels from provided build command
flags = append(flags, "--label", l)
}

// check if Memory is provided
if len(b.Memory) > 0 {
for _, m := range b.Memory {
// add flag for Memories from provided build command
flags = append(flags, "--memory", m)
}
// iterate through the memory arguments provided
for _, m := range b.Memory {
// add flag for Memory from provided build command
flags = append(flags, "--memory", m)
}

// check if MemorySwaps is provided
if len(b.MemorySwaps) > 0 {
for _, m := range b.MemorySwaps {
// add flag for Memories from provided build command
flags = append(flags, "--memory-swap", m)
}
// iterate through the memory swap arguments provided
for _, m := range b.MemorySwaps {
// add flag for Memory Swaps from provided build command
flags = append(flags, "--memory-swap", m)
}

// check if Network is provided
Expand All @@ -461,12 +459,10 @@ func (b *Build) Command() (*exec.Cmd, error) {
flags = append(flags, "--no-cache")
}

// check if Outputs is provided
if len(b.Outputs) > 0 {
for _, o := range b.Outputs {
// add flag for Outputs from provided build command
flags = append(flags, "--output", o)
}
// check if Output is provided
if len(b.Output) > 0 {
// add flag for output from provided build command
flags = append(flags, "--output", b.Output)
}

// check if Platform is provided
Expand Down Expand Up @@ -499,28 +495,22 @@ func (b *Build) Command() (*exec.Cmd, error) {
flags = append(flags, "--rm")
}

// check if Secrets is provided
if len(b.Secrets) > 0 {
for _, s := range b.Secrets {
// add flag for Secrets from provided build command
flags = append(flags, "--secret", s)
}
// check if Secret is provided
if len(b.Secret) > 0 {
// add flag for secret from provided build command
flags = append(flags, "--secret", b.Secret)
}

// check if SecurityOpts is provided
if len(b.SecurityOpts) > 0 {
for _, s := range b.SecurityOpts {
// add flag for SecurityOpts from provided build command
flags = append(flags, "--security-opt", s)
}
// iterate through the security options provided
for _, s := range b.SecurityOpts {
// add flag for SecurityOpts from provided build command
flags = append(flags, "--security-opt", s)
}

// check if ShmSizes is provided
if len(b.ShmSizes) > 0 {
for _, s := range b.ShmSizes {
// add flag for ShmSizes from provided build command
flags = append(flags, "--shm-size", s)
}
// iterate through the SHM sizes provided
for _, s := range b.ShmSizes {
// add flag for ShmSizes from provided build command
flags = append(flags, "--shm-size", s)
}

// check if Squash is provided
Expand All @@ -529,12 +519,10 @@ func (b *Build) Command() (*exec.Cmd, error) {
flags = append(flags, "--squash")
}

// check if SshComponents is provided
if len(b.SshComponents) > 0 {
for _, s := range b.SshComponents {
// add flag for SshComponents from provided build command
flags = append(flags, "--ssh", s)
}
// iterate through the SSH components provided
for _, s := range b.SshComponents {
// add flag for SshComponents from provided build command
flags = append(flags, "--ssh", s)
}

// check if Stream is provided
Expand All @@ -543,12 +531,18 @@ func (b *Build) Command() (*exec.Cmd, error) {
flags = append(flags, "--stream")
}

// check if Tags is provided
if len(b.Tags) > 0 {
for _, t := range b.Tags {
// add flag for Tags from provided build command
flags = append(flags, "--tag", t)
// iterate through the tags provided
for _, t := range b.Tags {
// check if a Docker repository was provided
if len(b.Repo) > 0 {
// check if the tag already has the repo in it
if !strings.Contains(t, b.Repo) {
t = fmt.Sprintf("%s:%s", b.Repo, t)
}
}

// add flag for Tags from provided build command
flags = append(flags, "--tag", t)
}

// check if Target is provided
Expand All @@ -557,12 +551,10 @@ func (b *Build) Command() (*exec.Cmd, error) {
flags = append(flags, "--target", b.Target)
}

// check if Ulimits is provided
if len(b.Ulimits) > 0 {
for _, u := range b.Ulimits {
// add flag for Ulimits from provided build command
flags = append(flags, "--ulimit", u)
}
// iterate through the ulimits provided
for _, u := range b.Ulimits {
// add flag for Ulimits from provided build command
flags = append(flags, "--ulimit", u)
}

// add the required directory param
Expand Down
8 changes: 4 additions & 4 deletions cmd/vela-docker/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func TestDocker_Build_Command(t *testing.T) {
MemorySwaps: []string{"1"},
Network: "default",
NoCache: true,
Outputs: []string{"foo"},
Output: "foo",
Platform: "linux",
Progress: "plain",
Pull: true,
Quiet: true,
Remove: true,
Secrets: []string{"foo"},
Secret: "foo",
SecurityOpts: []string{"seccomp"},
ShmSizes: []string{"1"},
Squash: true,
Expand Down Expand Up @@ -81,13 +81,13 @@ func TestDocker_Build_Command(t *testing.T) {
fmt.Sprintf("--memory-swap %s", b.MemorySwaps[0]),
fmt.Sprintf("--network %s", b.Network),
"--no-cache",
fmt.Sprintf("--output %s", b.Outputs[0]),
fmt.Sprintf("--output %s", b.Output),
fmt.Sprintf("--platform %s", b.Platform),
fmt.Sprintf("--progress %s", b.Progress),
"--pull",
"--quiet",
"--rm",
fmt.Sprintf("--secret %s", b.Secrets[0]),
fmt.Sprintf("--secret %s", b.Secret),
fmt.Sprintf("--security-opt %s", b.SecurityOpts[0]),
fmt.Sprintf("--shm-size %s", b.ShmSizes[0]),
"--squash",
Expand Down
5 changes: 3 additions & 2 deletions cmd/vela-docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ func run(c *cli.Context) error {
MemorySwaps: c.StringSlice("build.memory-swaps"),
Network: c.String("build.network"),
NoCache: c.Bool("build.no-cache"),
Outputs: c.StringSlice("build.outputs"),
Output: c.String("build.output"),
Platform: c.String("build.platform"),
Progress: c.String("build.progress"),
Pull: c.Bool("build.pull"),
Quiet: c.Bool("build.quiet"),
Remove: c.Bool("build.remove"),
Secrets: c.StringSlice("build.secrets"),
Repo: c.String("build.repo"),
Secret: c.String("build.secret"),
SecurityOpts: c.StringSlice("build.security-opts"),
ShmSizes: c.StringSlice("build.shm-sizes"),
Squash: c.Bool("build.squash"),
Expand Down

0 comments on commit bbf86cd

Please sign in to comment.