-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[image-builder] Backport mkIII from gitpod-com #4251
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
# Licensed under the MIT License. See License-MIT.txt in the project root for license information. | ||
|
||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: image-builder-authkey | ||
labels: | ||
app: {{ template "gitpod.fullname" . }} | ||
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" | ||
release: "{{ .Release.Name }}" | ||
heritage: "{{ .Release.Service }}" | ||
type: Opaque | ||
data: | ||
keyfile: {{ randAlphaNum 32 | b64enc }} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -49,10 +49,14 @@ data: | |||||
"probe.yaml": | | ||||||
{{ dict | merge ($comp.templates.probe | default dict) (include "coreWorkspaceAffinity" (dict "comp" $comp "tpe" "probe") | fromYaml) | toJson | indent 4 }} | ||||||
{{- end }} | ||||||
{{ if (or $comp.templates.regular $comp.affinity) }} | ||||||
{{ if (or $comp.templates.ghost $comp.affinity) }} | ||||||
"ghost.yaml": | | ||||||
{{ dict | merge ($comp.templates.ghost | default dict) (include "coreWorkspaceAffinity" (dict "comp" $comp "tpe" "ghost") | fromYaml) | toJson | indent 4 }} | ||||||
{{- end }} | ||||||
{{ if (or $comp.templates.imagebuild $comp.affinity) }} | ||||||
"imagebuild.yaml": | | ||||||
{{ dict | merge ($comp.templates.ghost | default dict) (include "coreWorkspaceAffinity" (dict "comp" $comp "tpe" "imagebuild") | fromYaml) | toJson | indent 4 }} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason that it is
Suggested change
|
||||||
{{- end }} | ||||||
{{ if (or $comp.templates.regular $comp.affinity) }} | ||||||
"regular.yaml": | | ||||||
{{ dict | merge ($comp.templates.regular | default dict) (include "coreWorkspaceAffinity" (dict "comp" $comp "tpe" "regular") | fromYaml) | toJson | indent 4 }} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bob |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
packages: | ||
- name: app | ||
type: go | ||
srcs: | ||
- "**/*.go" | ||
- "go.mod" | ||
- "go.sum" | ||
env: | ||
- CGO_ENABLED=0 | ||
- GOOS=linux | ||
- GOPROXY= | ||
deps: | ||
- components/common-go:lib | ||
config: | ||
packaging: app | ||
- name: docker | ||
type: docker | ||
srcs: | ||
- supervisor-ide-config.json | ||
- ide-startup.sh | ||
deps: | ||
- :app | ||
- components/image-builder/workspace-image-layer:pack | ||
config: | ||
dockerfile: leeway.Dockerfile | ||
argdeps: | ||
- imageRepoBase | ||
image: | ||
- ${imageRepoBase}/image-builder/bob:${version} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/gitpod-io/gitpod/image-builder/bob/pkg/builder" | ||
|
||
log "github.com/gitpod-io/gitpod/common-go/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// buildCmd represents the build command | ||
var buildCmd = &cobra.Command{ | ||
Use: "build", | ||
Short: "Runs the image build and is configured using environment variables (see pkg/builder/config.go for details)", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if os.Geteuid() != 0 { | ||
log.Fatal("must run as root") | ||
} | ||
|
||
// give the headless listener some time to attach | ||
time.Sleep(1 * time.Second) | ||
|
||
cfg, err := builder.GetConfigFromEnv() | ||
if err != nil { | ||
log.WithError(err).Fatal("cannot get config") | ||
return | ||
} | ||
|
||
b := &builder.Builder{ | ||
Config: cfg, | ||
} | ||
err = b.Build() | ||
if err != nil { | ||
log.WithError(err).Fatal("build failed") | ||
return | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(buildCmd) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,102 @@ | ||||||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||||||
// Licensed under the GNU Affero General Public License (AGPL). | ||||||
// See License-AGPL.txt in the project root for license information. | ||||||
|
||||||
package cmd | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"os" | ||||||
"os/signal" | ||||||
"syscall" | ||||||
|
||||||
log "github.com/gitpod-io/gitpod/common-go/log" | ||||||
"github.com/gitpod-io/gitpod/image-builder/bob/pkg/builder" | ||||||
|
||||||
"github.com/containerd/console" | ||||||
"github.com/moby/buildkit/client" | ||||||
"github.com/moby/buildkit/client/llb" | ||||||
"github.com/moby/buildkit/util/progress/progressui" | ||||||
"github.com/spf13/cobra" | ||||||
"golang.org/x/sync/errgroup" | ||||||
) | ||||||
|
||||||
// daemonCmd represents the build command | ||||||
var daemonCmd = &cobra.Command{ | ||||||
Use: "daemon <socket-path>", | ||||||
Short: "Starts a buildkitd and pre-caches images", | ||||||
Args: cobra.ExactArgs(1), | ||||||
Run: func(cmd *cobra.Command, args []string) { | ||||||
if os.Geteuid() != 0 { | ||||||
log.Fatal("must run as root") | ||||||
} | ||||||
|
||||||
skt := args[0] | ||||||
cl, teardown, err := builder.StartBuildkit(skt) | ||||||
if err != nil { | ||||||
log.WithError(err).Fatal("cannot start daemon") | ||||||
} | ||||||
defer teardown() | ||||||
|
||||||
rawimgs := os.Getenv("BOB_CACHE_IMAGES") | ||||||
if rawimgs != "" { | ||||||
var images []string | ||||||
err = json.Unmarshal([]byte(rawimgs), &images) | ||||||
if err != nil { | ||||||
log.WithError(err).Error("cannot unmarshal BOB_CACHE_IMAGES") | ||||||
} | ||||||
|
||||||
if len(images) > 0 { | ||||||
err = prewarmCache(cl, images) | ||||||
if err != nil { | ||||||
log.WithError(err).Error("cannot prewarm cache") | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// run until we're told to stop | ||||||
sigChan := make(chan os.Signal, 1) | ||||||
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) | ||||||
log.Info("👷 image-builder daemon is up and running. Stop with SIGINT or CTRL+C") | ||||||
<-sigChan | ||||||
log.Info("Received SIGINT - shutting down") | ||||||
}, | ||||||
} | ||||||
|
||||||
func prewarmCache(cl *client.Client, images []string) error { | ||||||
bld := llb.Image("alpine:latest").Run(llb.Shlex("echo")) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would love if we could make the |
||||||
for idx, img := range images { | ||||||
bld = bld.AddMount(fmt.Sprintf("/mnt/%03d", idx), llb.Image(img)).Run(llb.Shlex("echo")) | ||||||
} | ||||||
pulllb, err := bld.Marshal(context.Background()) | ||||||
if err != nil { | ||||||
log.WithError(err).Fatal("cannot produce iamge pull LLB") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo:
Suggested change
|
||||||
} | ||||||
|
||||||
log.Info("pulling images") | ||||||
var ( | ||||||
ch = make(chan *client.SolveStatus) | ||||||
eg, ctx = errgroup.WithContext(context.Background()) | ||||||
) | ||||||
eg.Go(func() error { | ||||||
_, err := cl.Solve(ctx, pulllb, client.SolveOpt{}, ch) | ||||||
return err | ||||||
}) | ||||||
eg.Go(func() error { | ||||||
var c console.Console | ||||||
// not using shared context to not disrupt display but let is finish reporting errors | ||||||
return progressui.DisplaySolveStatus(context.TODO(), "", c, os.Stderr, ch) | ||||||
}) | ||||||
err = eg.Wait() | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
log.Info("done pulling images") | ||||||
return nil | ||||||
} | ||||||
|
||||||
func init() { | ||||||
rootCmd.AddCommand(daemonCmd) | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
log "github.com/gitpod-io/gitpod/common-go/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "bob", | ||
Short: "Bob is the in-workspace component of the image builder. You should never have to interact with it directly.", | ||
} | ||
|
||
// Execute runs the root command | ||
func Execute() { | ||
log.Init("bob", "", true, true) | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
echo '{"message":{"type": "workspaceTaskDone"}}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that the selection of the ws-manager is future work but what do you think of at least having something like this:
(However, that's not a blocker, though. Feel free to ignore.)