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

Auto start buildkit daemon on image build for containerd #12076

Merged
merged 1 commit into from
Aug 2, 2021
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
23 changes: 23 additions & 0 deletions pkg/minikube/cruntime/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ func downloadRemote(cr CommandRunner, src string) (string, error) {

// BuildImage builds an image into this runtime
func (r *Containerd) BuildImage(src string, file string, tag string, push bool, env []string, opts []string) error {
if err := r.initBuildkitDaemon(); err != nil {
return fmt.Errorf("failed to init buildkit daemon: %v", err)
}

// download url if not already present
dir, err := downloadRemote(r.Runner, src)
if err != nil {
Expand Down Expand Up @@ -393,6 +397,25 @@ func (r *Containerd) BuildImage(src string, file string, tag string, push bool,
return nil
}

func (r *Containerd) initBuildkitDaemon() error {
// if daemon is already running, do nothing
cmd := exec.Command("pgrep", "buildkitd")
if _, err := r.Runner.RunCmd(cmd); err == nil {
return nil
}

// otherwise, start daemon
cmd = exec.Command("/bin/bash", "-c", "sudo -b buildkitd --oci-worker false --containerd-worker true --containerd-worker-namespace k8s.io &> /dev/null")
if _, err := r.Runner.RunCmd(cmd); err != nil {
return fmt.Errorf("failed to start buildkit daemon: %v", err)
}

// give the daemon time to finish starting up or image build will fail
time.Sleep(1 * time.Second)

return nil
}

// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd")
func (r *Containerd) CGroupDriver() (string, error) {
info, err := getCRIInfo(r.Runner)
Expand Down
16 changes: 1 addition & 15 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,14 @@ func listImages(ctx context.Context, t *testing.T, profile string) (*RunResult,
// validateBuildImage makes sures that `minikube image build` works as expected
func validateBuildImage(ctx context.Context, t *testing.T, profile string) {
if NoneDriver() {
t.Skip("load image not available on none driver")
t.Skip("image build not available on none driver")
}
if GithubActionRunner() && runtime.GOOS == "darwin" {
t.Skip("skipping on github actions and darwin, as this test requires a running docker daemon")
}
defer PostMortemLogs(t, profile)

newImage := fmt.Sprintf("localhost/my-image:%s", profile)
if ContainerRuntime() == "containerd" {
startBuildkit(ctx, t, profile)
// unix:///run/buildkit/buildkitd.sock
}

// try to build the new image with minikube
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "image", "build", "-t", newImage, filepath.Join(*testdataDir, "build")))
Expand All @@ -379,16 +375,6 @@ func validateBuildImage(ctx context.Context, t *testing.T, profile string) {
}
}

func startBuildkit(ctx context.Context, t *testing.T, profile string) {
// sudo systemctl start buildkit.socket
cmd := exec.CommandContext(ctx, Target(), "ssh", "-p", profile, "--", "nohup",
"sudo", "-b", "buildkitd", "--oci-worker=false",
"--containerd-worker=true", "--containerd-worker-namespace=k8s.io")
if rr, err := Run(t, cmd); err != nil {
t.Fatalf("%s failed: %v", rr.Command(), err)
}
}

// validateListImages makes sures that `minikube image ls` works as expected
func validateListImages(ctx context.Context, t *testing.T, profile string) {
if NoneDriver() {
Expand Down