Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support '--ssh' option in 'docker build' #4660

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/content/en/schemas/v2beta10.json
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,11 @@
"description": "contains information about a local secret passed to `docker build`, along with optional destination information.",
"x-intellij-html-description": "contains information about a local secret passed to <code>docker build</code>, along with optional destination information."
},
"ssh": {
"type": "string",
"description": "used to pass in --ssh to docker build to use SSH agent. Format is \"default|<id>[=<socket>|<key>[,<key>]]\".",
"x-intellij-html-description": "used to pass in --ssh to docker build to use SSH agent. Format is &quot;default|<id>[=<socket>|<key>[,<key>]]&quot;."
},
"target": {
"type": "string",
"description": "Dockerfile target name to build.",
Expand All @@ -1024,7 +1029,8 @@
"network",
"cacheFrom",
"noCache",
"secret"
"secret",
"ssh"
],
"additionalProperties": false,
"description": "describes an artifact built from a Dockerfile, usually using `docker build`.",
Expand Down
6 changes: 6 additions & 0 deletions integration/testdata/build/ssh/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# syntax=docker/dockerfile:1.0.0-experimental

FROM alpine:3.10

# https://github.com/tonistiigi/buildkit/blob/1604b1b9ed70bcbc002033d8dd0e65ab13f13554/client/llb/exec.go#L138
RUN --mount=type=ssh ls /run/buildkit/ssh_agent.*
10 changes: 10 additions & 0 deletions integration/testdata/build/ssh/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: skaffold/v2beta10
kind: Config
build:
local:
useBuildkit: true
push: false
artifacts:
- image: ssh
docker:
ssh: "default"
4 changes: 2 additions & 2 deletions pkg/skaffold/build/gcb/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (b *Builder) cacheFromSteps(artifact *latest.DockerArtifact) []*cloudbuild.
func (b *Builder) dockerBuildArgs(a *latest.Artifact, tag string, deps []*latest.ArtifactDependency) ([]string, error) {
d := a.DockerArtifact
// TODO(nkubala): remove when buildkit is supported in GCB (#4773)
if d.Secret != nil {
return nil, errors.New("docker build secrets not currently supported in GCB builds")
if d.Secret != nil || d.SSH != "" {
return nil, errors.New("docker build options, secrets and ssh, are not currently supported in GCB builds")
}
requiredImages := docker.ResolveDependencyImages(deps, b.artifactStore, true)
buildArgs, err := docker.EvalBuildArgs(b.cfg.Mode(), a.Workspace, d.DockerfilePath, d.BuildArgs, requiredImages)
Expand Down
14 changes: 13 additions & 1 deletion pkg/skaffold/build/gcb/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestDockerBuildSpec(t *testing.T) {
},
},
{
description: "buildkit features not supported in GCB",
description: "buildkit `secret` option not supported in GCB",
artifact: &latest.Artifact{
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
Expand All @@ -117,6 +117,18 @@ func TestDockerBuildSpec(t *testing.T) {
},
shouldErr: true,
},
{
description: "buildkit `ssh` option not supported in GCB",
artifact: &latest.Artifact{
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: "Dockerfile",
SSH: "default",
},
},
},
shouldErr: true,
},
}

for _, test := range tests {
Expand Down
8 changes: 6 additions & 2 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (l *localDaemon) ConfigFile(ctx context.Context, image string) (*v1.ConfigF
}

func (l *localDaemon) CheckCompatible(a *latest.DockerArtifact) error {
if a.Secret != nil {
return fmt.Errorf("docker build secrets require BuildKit - set `useBuildkit: true` in your config, or run with `DOCKER_BUILDKIT=1`")
if a.Secret != nil || a.SSH != "" {
return fmt.Errorf("docker build options, secrets and ssh, require BuildKit - set `useBuildkit: true` in your config, or run with `DOCKER_BUILDKIT=1`")
Comment on lines +170 to +171
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is ad hoc and not very good, but I think it's out of the scope of this PR to refactor these and the only options that BuildKit needs, for now, are "secret" and "ssh" so I'll leave that for this PR I'll leave it.

}
return nil
}
Expand Down Expand Up @@ -510,6 +510,10 @@ func ToCLIBuildArgs(a *latest.DockerArtifact, evaluatedArgs map[string]*string)
args = append(args, "--secret", secretString)
}

if a.SSH != "" {
args = append(args, "--ssh", a.SSH)
}

return args, nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ func TestGetBuildArgs(t *testing.T) {
},
want: []string{"--secret", "id=mysecret,src=foo.src,dst=foo.dst"},
},
{
description: "ssh with no source",
artifact: &latest.DockerArtifact{
SSH: "default",
},
want: []string{"--ssh", "default"},
},
{
description: "all",
artifact: &latest.DockerArtifact{
Expand Down
3 changes: 3 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,9 @@ type DockerArtifact struct {
// Secret contains information about a local secret passed to `docker build`,
// along with optional destination information.
Secret *DockerSecret `yaml:"secret,omitempty"`

// SSH is used to pass in --ssh to docker build to use SSH agent. Format is "default|<id>[=<socket>|<key>[,<key>]]".
SSH string `yaml:"ssh,omitempty"`
Copy link
Contributor Author

@h-michael h-michael Nov 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering what to do here. "--ssh" option can have only one argument "default", "<id>[=<socket>]" or "<key>[,<key>]".
It would be nice if we could used a type representation like a union type, but the JSON schema(and Golang) doesn't support such a type, so I decided to just make it string type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM — I think we could use a yaml oneOf annotation, but this will do for now.

}

// DockerSecret contains information about a local secret passed to `docker build`,
Expand Down