Skip to content

Commit ceaa19e

Browse files
Christian Weichelcsweichel
Christian Weichel
authored andcommitted
Total rewrite using buildkit
1 parent f00b5d7 commit ceaa19e

29 files changed

+908
-1915
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ example/dazzle__*.Dockerfile
2020
# dive log
2121
dive.logdazzle
2222
dazzle
23+
!pkg/dazzle

.goreleaser.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ builds:
33
env:
44
- CGO_ENABLED=0
55
ldflags:
6-
- -s -w -X github.com/32leaves/dazzle/cmd/core.version={{.Version}}-{{.ShortCommit}}
6+
- -s -w -X github.com/csweichel/dazzle/cmd/core.version={{.Version}}-{{.ShortCommit}}
77
- id: dazzle-util
88
env:
99
- CGO_ENABLED=0
@@ -12,7 +12,7 @@ builds:
1212
flags:
1313
- -tags=util
1414
ldflags:
15-
- -s -w -X github.com/32leaves/dazzle/cmd/util.version={{.Version}}-{{.ShortCommit}}
15+
- -s -w -X github.com/csweichel/dazzle/cmd/util.version={{.Version}}-{{.ShortCommit}}
1616
archives:
1717
- id: dazzle
1818
replacements:

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<img src="logo.png" width="100">
22

3-
[![Setup Automated](https://img.shields.io/badge/setup-automated-blue?logo=gitpod)](https://gitpod.io/#https://github.com/32leaves/dazzle)
4-
[![Go Repord Cart](https://goreportcard.com/badge/github.com/32leaves/dazzle)](https://goreportcard.com/report/github.com/32leaves/dazzle)
3+
[![Setup Automated](https://img.shields.io/badge/setup-automated-blue?logo=gitpod)](https://gitpod.io/#https://github.com/csweichel/dazzle)
4+
[![Go Repord Cart](https://goreportcard.com/badge/github.com/csweichel/dazzle)](https://goreportcard.com/report/github.com/csweichel/dazzle)
55
[![Stability: Experimental](https://masterminds.github.io/stability/experimental.svg)](https://masterminds.github.io/stability/experimental.html)
66

77
dazzle is a rather experimental Docker image builder. Its goal is to build independent layers where a change to one layer does *not* invalidate the ones sitting "above" it. To this end, dazzle uses black magic.

cmd/core/build.go

+16-110
Original file line numberDiff line numberDiff line change
@@ -21,138 +21,44 @@
2121
package core
2222

2323
import (
24-
"encoding/xml"
25-
"fmt"
26-
"io/ioutil"
27-
"os"
24+
"context"
2825

29-
"github.com/32leaves/dazzle/pkg/dazzle"
30-
"github.com/32leaves/dazzle/pkg/fancylog"
31-
"github.com/32leaves/dazzle/pkg/test"
26+
"github.com/csweichel/dazzle/pkg/dazzle"
27+
"github.com/csweichel/dazzle/pkg/fancylog"
28+
"github.com/moby/buildkit/client"
3229
log "github.com/sirupsen/logrus"
3330
"github.com/spf13/cobra"
3431
)
3532

3633
// buildCmd represents the build command
3734
var buildCmd = &cobra.Command{
38-
Use: "build [context]",
35+
Use: "build <target-ref> <context>",
3936
Short: "Builds a Docker image with independent layers",
40-
Args: cobra.MaximumNArgs(1),
37+
Args: cobra.MinimumNArgs(2),
4138
RunE: func(cmd *cobra.Command, args []string) error {
4239
formatter := &fancylog.Formatter{}
4340
log.SetFormatter(formatter)
4441

45-
var wd string
46-
if len(args) > 0 {
47-
wd = args[0]
48-
49-
if stat, err := os.Stat(wd); os.IsNotExist(err) || !stat.IsDir() {
50-
return fmt.Errorf("context %s must be a directory", wd)
51-
}
52-
} else {
53-
var err error
54-
wd, err = os.Getwd()
55-
if err != nil {
56-
return err
57-
}
58-
}
59-
60-
dfn, err := cmd.Flags().GetString("file")
61-
if err != nil {
62-
return err
63-
}
64-
tag, err := cmd.Flags().GetString("tag")
65-
if err != nil {
66-
return err
67-
}
68-
repo, err := cmd.Flags().GetString("repository")
42+
var (
43+
targetref = args[0]
44+
wd = args[1]
45+
)
46+
prj, err := dazzle.LoadFromDir(wd)
6947
if err != nil {
7048
return err
7149
}
72-
repoChanged := cmd.Flags().Changed("repository")
73-
if !repoChanged {
74-
log.Warn("Using dazzle without --repository will likely produce incorrect results!")
75-
}
7650

77-
env, err := dazzle.NewEnvironment()
51+
sckt, _ := cmd.Flags().GetString("addr")
52+
cl, err := client.New(context.Background(), sckt, client.WithFailFast())
7853
if err != nil {
79-
log.Fatal(err)
80-
}
81-
env.Formatter = formatter
82-
83-
log.WithField("version", version).Debug("this is dazzle")
84-
85-
cfg := dazzle.BuildConfig{
86-
Env: env,
87-
BuildImageRepo: repo,
88-
}
89-
90-
res, err := dazzle.Build(cfg, wd, dfn, tag)
91-
logBuildResult(res)
92-
testXMLOutput, _ := cmd.Flags().GetString("output-test-xml")
93-
if testXMLOutput != "" {
94-
serr := saveTestXMLOutput(res, testXMLOutput)
95-
if serr != nil {
96-
log.WithError(serr).Error("cannot save test result")
97-
}
98-
}
99-
if err != nil {
100-
log.Fatal(err)
54+
return err
10155
}
102-
103-
return nil
56+
return prj.Build(context.Background(), cl, targetref)
10457
},
10558
}
10659

10760
func init() {
10861
rootCmd.AddCommand(buildCmd)
10962

110-
buildCmd.Flags().StringP("file", "f", "Dockerfile", "name of the Dockerfile")
111-
buildCmd.Flags().StringP("tag", "t", "dazzle-built:latest", "tag of the resulting image")
112-
buildCmd.Flags().StringP("repository", "r", "dazzle-work", "name of the Docker repository to work in (e.g. eu.gcr.io/someprj/dazzle-work)")
113-
buildCmd.Flags().String("output-test-xml", "", "save the test results as JUnit XML file")
114-
}
115-
116-
func logBuildResult(res *dazzle.BuildResult) {
117-
if res == nil {
118-
return
119-
}
120-
121-
log.Info("build done")
122-
log.WithField("size", res.BaseImage.Size).Debugf("base layer: %s", res.BaseImage.Ref)
123-
for _, l := range res.Layers {
124-
log.WithField("size", l.Size).WithField("ref", l.Ref).Debugf(" layer %s", l.LayerName)
125-
}
126-
}
127-
128-
func saveTestXMLOutput(res *dazzle.BuildResult, fn string) error {
129-
if res == nil {
130-
return nil
131-
}
132-
133-
var r test.Results
134-
for _, l := range res.Layers {
135-
ltr := l.TestResult
136-
if ltr == nil {
137-
continue
138-
}
139-
140-
for _, tr := range ltr.Result {
141-
ttr := *tr
142-
ttr.Desc = fmt.Sprintf("%s: %s", l.LayerName, tr.Desc)
143-
r.Result = append(r.Result, &ttr)
144-
}
145-
}
146-
147-
fc, err := xml.Marshal(r)
148-
if err != nil {
149-
return err
150-
}
151-
152-
err = ioutil.WriteFile(fn, fc, 0644)
153-
if err != nil {
154-
return err
155-
}
156-
157-
return nil
63+
buildCmd.Flags().String("addr", "unix:///run/buildkit/buildkitd.sock", "address of buildkitd")
15864
}

cmd/core/merge.go

-52
This file was deleted.

0 commit comments

Comments
 (0)