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

new connhelper: kube-pod #951

Merged
merged 1 commit into from
Apr 22, 2019
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ buildctl build --help
The images can be also built locally using `./hack/dockerfiles/test.Dockerfile` (or `./hack/dockerfiles/test.buildkit.Dockerfile` if you already have BuildKit).
Run `make images` to build the images as `moby/buildkit:local` and `moby/buildkit:local-rootless`.

#### Connection helpers

If you are running `moby/buildkit:master` or `moby/buildkit:master-rootless` as a Docker/Kubernetes container, you can use special `BUILDKIT_HOST` URL for connecting to the BuildKit daemon in the container:

```
export BUILDKIT_HOST=docker://<container>
```

```
export BUILDKIT_HOST=kube-pod://<pod>
```

### Opentracing support

BuildKit supports opentracing for buildkitd gRPC API and buildctl commands. To capture the trace to [Jaeger](https://github.com/jaegertracing/jaeger), set `JAEGER_TRACE` environment variable to the collection address.
Expand Down
77 changes: 77 additions & 0 deletions client/connhelper/kubepod/kubepod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Package kubepod provides connhelper for kube-pod://<pod>
package kubepod

import (
"context"
"net"
"net/url"
"regexp"

"github.com/docker/cli/cli/connhelper/commandconn"
"github.com/moby/buildkit/client/connhelper"
"github.com/pkg/errors"
)

func init() {
connhelper.Register("kube-pod", Helper)
}

// Helper returns helper for connecting to a Kubernetes pod.
// Requires BuildKit v0.5.0 or later in the pod.
func Helper(u *url.URL) (*connhelper.ConnectionHelper, error) {
sp, err := SpecFromURL(u)
if err != nil {
return nil, err
}
return &connhelper.ConnectionHelper{
ContextDialer: func(ctx context.Context, addr string) (net.Conn, error) {
return commandconn.New(ctx, "kubectl", "--context="+sp.Context, "--namespace="+sp.Namespace,
"exec", "--container="+sp.Container, "-i", sp.Pod, "--", "buildctl", "dial-stdio")
},
}, nil
}

// Spec
type Spec struct {
Context string
Namespace string
Pod string
Container string
}

// SpecFromURL creates Spec from URL.
// URL is like kube-pod://<pod>?context=<context>&namespace=<namespace>&container=<container> .
// Only <pod> part is mandatory.
func SpecFromURL(u *url.URL) (*Spec, error) {
q := u.Query()
sp := Spec{
Context: q.Get("context"),
Namespace: q.Get("namespace"),
Pod: u.Hostname(),
Container: q.Get("container"),
}
if sp.Context != "" && !validKubeIdentifier(sp.Context) {
return nil, errors.Errorf("unsupported context name: %q", sp.Context)
}
if sp.Namespace != "" && !validKubeIdentifier(sp.Namespace) {
return nil, errors.Errorf("unsupported namespace name: %q", sp.Namespace)
}
if sp.Pod == "" {
return nil, errors.New("url lacks pod name")
}
if !validKubeIdentifier(sp.Pod) {
return nil, errors.Errorf("unsupported pod name: %q", sp.Pod)
}
if sp.Container != "" && !validKubeIdentifier(sp.Container) {
return nil, errors.Errorf("unsupported container name: %q", sp.Container)
}
return &sp, nil
}

var kubeIdentifierRegexp = regexp.MustCompile(`^[-a-z0-9.]+$`)

// validKubeIdentifier: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
// The length is not checked because future version of Kube may support longer identifiers.
func validKubeIdentifier(s string) bool {
return kubeIdentifierRegexp.MatchString(s)
}
34 changes: 34 additions & 0 deletions client/connhelper/kubepod/kubepod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kubepod

import (
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

func TestSpecFromURL(t *testing.T) {
cases := map[string]*Spec{
"kube-pod://podname": {
Pod: "podname",
},
"kube-pod://podname?container=containername&namespace=nsname&context=ctxname": {
Context: "ctxname", Namespace: "nsname", Pod: "podname", Container: "containername",
},
"kube-pod://": nil,
"kube-pod://unsupported_pod_name": nil,
}
for s, expected := range cases {
u, err := url.Parse(s)
if err != nil {
t.Fatal(err)
}
got, err := SpecFromURL(u)
if expected != nil {
require.NoError(t, err)
require.EqualValues(t, expected, got, s)
} else {
require.Error(t, err, s)
}
}
}
1 change: 1 addition & 0 deletions cmd/buildctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

_ "github.com/moby/buildkit/client/connhelper/dockercontainer"
_ "github.com/moby/buildkit/client/connhelper/kubepod"
bccommon "github.com/moby/buildkit/cmd/buildctl/common"
"github.com/moby/buildkit/util/apicaps"
"github.com/moby/buildkit/util/appdefaults"
Expand Down