-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ jobs: | |
worker: | ||
- docker-container | ||
- remote | ||
- kubernetes | ||
pkg: | ||
- ./tests | ||
mode: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package helpers | ||
|
||
import ( | ||
"bytes" | ||
"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 | ||
} | ||
|
||
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() { | ||
if logbuf.Len() > 0 { | ||
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")) { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
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) NetNSDetached() 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 | ||
} |