diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7c0f59227ff6..bc18b31b18a4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -69,6 +69,7 @@ jobs: - docker\+containerd # same as docker, but with containerd snapshotter - docker-container - remote + - kubernetes pkg: - ./tests include: diff --git a/Dockerfile b/Dockerfile index 835b1f280088..9edff5ec915d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,7 @@ ARG DOCKER_VERSION=24.0.6 ARG GOTESTSUM_VERSION=v1.9.0 ARG REGISTRY_VERSION=2.8.0 ARG BUILDKIT_VERSION=v0.11.6 +ARG K3S_VERSION=v1.21.2-k3s1 # xx is a helper for cross-compilation FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx @@ -21,7 +22,7 @@ ENV CGO_ENABLED=0 WORKDIR /src FROM registry:$REGISTRY_VERSION AS registry - +FROM rancher/k3s:${K3S_VERSION} AS k3s FROM moby/buildkit:$BUILDKIT_VERSION AS buildkit FROM gobase AS docker @@ -103,9 +104,22 @@ RUN apk add --no-cache \ shadow-uidmap \ xfsprogs \ xz +# k3s deps +RUN apk add --no-cache \ + busybox-binsh \ + cni-plugins \ + cni-plugin-flannel \ + conntrack-tools \ + coreutils \ + dbus \ + findutils \ + ipset +ENV PATH="/usr/libexec/cni:${PATH}" COPY --link --from=gotestsum /out/gotestsum /usr/bin/ COPY --link --from=registry /bin/registry /usr/bin/ COPY --link --from=docker /opt/docker/* /usr/bin/ +COPY --link --from=k3s /bin/k3s /usr/bin/ +COPY --link --from=k3s /bin/kubectl /usr/bin/ COPY --link --from=buildkit /usr/bin/buildkitd /usr/bin/ COPY --link --from=buildkit /usr/bin/buildctl /usr/bin/ COPY --link --from=binaries /buildx /usr/bin/ diff --git a/tests/helpers/k3s.go b/tests/helpers/k3s.go new file mode 100644 index 000000000000..5c5ff0a5132a --- /dev/null +++ b/tests/helpers/k3s.go @@ -0,0 +1,113 @@ +package helpers + +import ( + "bytes" + "fmt" + "net" + "os" + "os/exec" + "strconv" + "time" + + "github.com/cenkalti/backoff/v4" + "github.com/moby/buildkit/util/testutil/integration" + "github.com/pkg/errors" +) + +const ( + k3sBin = "k3s" + kubeCtlBin = "kubectl" +) + +func NewK3sServer(cfg *integration.BackendConfig) (kubeConfig string, cl func() error, err error) { + if _, err := exec.LookPath(k3sBin); err != nil { + return "", nil, errors.Wrapf(err, "failed to lookup %s binary", k3sBin) + } + if _, err := exec.LookPath(kubeCtlBin); err != nil { + return "", nil, errors.Wrapf(err, "failed to lookup %s binary", kubeCtlBin) + } + + deferF := &integration.MultiCloser{} + cl = deferF.F() + + defer func() { + if err != nil { + deferF.F()() + cl = nil + } + }() + + cfgfile, err := os.CreateTemp("", "kubeconfig*.yml") + if err != nil { + return "", nil, err + } + kubeConfig = cfgfile.Name() + deferF.Append(func() error { + return os.Remove(cfgfile.Name()) + }) + + k3sDataDir, err := os.MkdirTemp("", "kubedata") + if err != nil { + return "", nil, err + } + deferF.Append(func() error { + return os.RemoveAll(k3sDataDir) + }) + + l, err := net.Listen("tcp", "localhost:0") + if err != nil { + return "", nil, err + } + _ = l.Close() + + lport := strconv.Itoa(l.Addr().(*net.TCPAddr).Port) + nodeName := "integrationk3s" + + stop, err := integration.StartCmd(exec.Command(k3sBin, "server", + "--bind-address", "127.0.0.1", + "--https-listen-port", lport, + "--data-dir", k3sDataDir, // write to /tmp for overlayfs support + "--write-kubeconfig", cfgfile.Name(), + "--write-kubeconfig-mode", "666", + "--node-name", nodeName, + ), cfg.Logs) + if err != nil { + return "", nil, err + } + + time.Sleep(10 * time.Second) + if err = waitK3s(cfg, kubeConfig, nodeName); err != nil { + stop() + return "", nil, errors.Wrapf(err, "k3s did not start up: %s", integration.FormatLogs(cfg.Logs)) + } + + deferF.Append(stop) + return +} + +func waitK3s(cfg *integration.BackendConfig, kubeConfig string, nodeName string) error { + logbuf := new(bytes.Buffer) + defer func() { + cfg.Logs["waitk3s: "] = logbuf + }() + + boff := backoff.NewExponentialBackOff() + boff.InitialInterval = 3 * time.Second + boff.MaxInterval = 5 * time.Second + boff.MaxElapsedTime = 2 * time.Minute + + if err := backoff.Retry(func() error { + cmd := exec.Command(kubeCtlBin, "--kubeconfig", kubeConfig, "wait", "--for=condition=Ready", "node/"+nodeName) + out, err := cmd.CombinedOutput() + if err == nil && bytes.Contains(out, []byte("condition met")) { + logbuf.WriteString(fmt.Sprintf("%s %s", cmd.String(), string(out))) + return nil + } + return errors.Wrapf(err, "node is not ready: %s %s", cmd.String(), string(out)) + }, boff); err != nil { + logbuf.WriteString(errors.Unwrap(err).Error()) + return err + } + + return nil +} diff --git a/tests/integration_test.go b/tests/integration_test.go index e38e46cab5d3..3c201c56b90e 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -16,6 +16,7 @@ func init() { workers.InitDockerContainerWorker() } else { workers.InitRemoteWorker() + workers.InitKubernetesWorker() } } diff --git a/tests/workers/kubernetes.go b/tests/workers/kubernetes.go new file mode 100644 index 000000000000..2476a59f52e2 --- /dev/null +++ b/tests/workers/kubernetes.go @@ -0,0 +1,96 @@ +package workers + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "sync" + + "github.com/docker/buildx/tests/helpers" + "github.com/moby/buildkit/identity" + "github.com/moby/buildkit/util/testutil/integration" + "github.com/pkg/errors" +) + +func InitKubernetesWorker() { + integration.Register(&kubernetesWorker{ + id: "kubernetes", + }) +} + +type kubernetesWorker struct { + id string + + unsupported []string + + k3sConfig string + k3sClose func() error + k3sErr error + k3sOnce sync.Once +} + +func (w *kubernetesWorker) Name() string { + return w.id +} + +func (w *kubernetesWorker) Rootless() bool { + return false +} + +func (w *kubernetesWorker) New(ctx context.Context, cfg *integration.BackendConfig) (integration.Backend, func() error, error) { + var err error + + w.k3sOnce.Do(func() { + w.k3sConfig, w.k3sClose, w.k3sErr = helpers.NewK3sServer(cfg) + }) + if w.k3sErr != nil { + return nil, w.k3sClose, w.k3sErr + } + + cfgfile, err := integration.WriteConfig(cfg.DaemonConfig) + if err != nil { + return nil, nil, err + } + defer os.RemoveAll(filepath.Dir(cfgfile)) + + name := "integration-kubernetes-" + identity.NewID() + cmd := exec.Command("buildx", "create", + "--bootstrap", + "--name="+name, + "--config="+cfgfile, + "--driver=kubernetes", + ) + cmd.Env = append( + os.Environ(), + "BUILDX_CONFIG=/tmp/buildx-"+name, + "KUBECONFIG="+w.k3sConfig, + ) + if err := cmd.Run(); err != nil { + return nil, nil, errors.Wrapf(err, "failed to create buildx instance %s", name) + } + + cl := func() error { + cmd := exec.Command("buildx", "rm", "-f", name) + return cmd.Run() + } + + return &backend{ + builder: name, + unsupportedFeatures: w.unsupported, + }, cl, nil +} + +func (w *kubernetesWorker) Close() error { + if c := w.k3sClose; c != nil { + return c() + } + + // reset the worker to be ready to go again + w.k3sConfig = "" + w.k3sClose = nil + w.k3sErr = nil + w.k3sOnce = sync.Once{} + + return nil +}