Skip to content

Commit b318a4a

Browse files
committed
fix image name generation and make sure manifest applying is effective.
Signed-off-by: Kitt Hsu <kitt.hsu@gmail.com>
1 parent 9a4d2b6 commit b318a4a

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ kubectl dev build -f hack/dev/Dockerfile --local _output/ --target mac-cli
4040

4141
#### Auto-generate image name for testing
4242
Build command will automatically generate image name if no `-t(--tag)` or `--local` provided.
43-
The default name is in the format of `build.local/x/%s:latest`.
43+
The default name is in the format of `build.local/x/%s:v%d`.
44+
The %s is going to be replaced by the build context directory.
45+
The %d will be replaced by an integer incremented by 1 in each build.
46+
4447
Users can change the default pattern by setting `--tag-pattern`.
4548

4649
#### Apply k8s manifests after build

pkg/cmd/build.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type BuildContext struct {
5959
PathToManifest string `yaml:"path_to_manifest,omitempty"`
6060
BuildContextDir string `yaml:"build_context_dir,omitempty"`
6161

62+
Count int `yaml:"count"`
63+
6264
solveOpt *buildkit.SolveOpt `yaml:"-"`
6365
}
6466

@@ -112,20 +114,21 @@ func (o *BuildOptions) buildSolveOpt(bc *BuildContext) (*buildkit.SolveOpt, erro
112114
solveOpt.FrontendAttrs["build-arg:"+kv[0]] = kv[1]
113115
}
114116

117+
tag := bc.Tag
115118
if len(bc.Tag) == 0 && len(bc.LocalDir) == 0 && len(bc.AutoTagPattern) > 0 {
116119
absCtx, err := filepath.Abs(bc.BuildContextDir)
117120
if err != nil {
118121
return nil, err
119122
}
120-
bc.Tag = fmt.Sprintf(bc.AutoTagPattern, filepath.Base(absCtx))
123+
tag = fmt.Sprintf(bc.AutoTagPattern, filepath.Base(absCtx), bc.Count)
121124
fmt.Printf("Neither image tag nor local binary is given. \nAssuming to build a local image for testing: %s\n", bc.Tag)
122125
}
123126

124-
if len(bc.Tag) > 0 {
127+
if len(tag) > 0 {
125128
export := buildkit.ExportEntry{
126129
Type: "image",
127130
Attrs: map[string]string{
128-
"name": bc.Tag,
131+
"name": tag,
129132
},
130133
}
131134

@@ -228,6 +231,8 @@ func (o *BuildOptions) Complete(cmd *cobra.Command, args []string) error {
228231
}
229232
o.config[k] = bc
230233
}
234+
} else {
235+
return errors.New("more arguments are required")
231236
}
232237
} else {
233238
o.BuildContextDir = "."
@@ -251,11 +256,14 @@ func solve(
251256
ctx context.Context, client *buildkit.Client, pw progresswriter.Writer,
252257
solveOpt *buildkit.SolveOpt, config *BuildContext,
253258
) error {
254-
if _, err := client.Solve(ctx, nil, *solveOpt, pw.Status()); err != nil {
259+
_, err := client.Solve(ctx, nil, *solveOpt, pw.Status())
260+
if err != nil {
255261
<-pw.Done()
256262
return fmt.Errorf("%s", err)
257263
}
258264

265+
config.Count++
266+
259267
if len(config.PathToManifest) > 0 {
260268
manifest, err := ioutil.ReadFile(config.PathToManifest)
261269
if err == os.ErrNotExist {
@@ -272,7 +280,13 @@ func solve(
272280
}
273281

274282
if len(match) == 1 {
275-
manifest = imagePattern.ReplaceAll(manifest, []byte(fmt.Sprintf("image: %s", config.Tag)))
283+
image := ""
284+
for _, export := range solveOpt.Exports {
285+
if export.Type == "image" {
286+
image = export.Attrs["name"]
287+
}
288+
}
289+
manifest = imagePattern.ReplaceAll(manifest, []byte(fmt.Sprintf("image: %s", image)))
276290
}
277291

278292
err = kubectl.ApplyManifestsFromStdin(strings.NewReader(string(manifest)))
@@ -331,21 +345,19 @@ func (o *BuildOptions) Run(ctx context.Context) (err error) {
331345

332346
<-pw.Done()
333347

334-
if o.config == nil {
335-
config := make(BuildConfig)
336-
workdir, err := os.Getwd()
337-
if err != nil {
338-
return err
339-
}
340-
if err = conf.Load(buildConfFile, &config); err == nil {
341-
return err
342-
}
343-
config[workdir] = DirBuildContext{
344-
o.Dockerfile + "/" + o.TargetStage: o.BuildContext,
345-
}
346-
if err = conf.Save(buildConfFile, config); err != nil {
347-
return err
348-
}
348+
config := make(BuildConfig)
349+
workdir, err := os.Getwd()
350+
if err != nil {
351+
return err
352+
}
353+
if err = conf.Load(buildConfFile, &config); err == nil {
354+
return err
355+
}
356+
config[workdir] = DirBuildContext{
357+
o.Dockerfile + "/" + o.TargetStage: o.BuildContext,
358+
}
359+
if err = conf.Save(buildConfFile, config); err != nil {
360+
return err
349361
}
350362

351363
return nil
@@ -389,7 +401,7 @@ kubectl dev build -t foo:latest -f Dockerfile --manifest foo/bar/manifest.yaml
389401
"Name of the Dockerfile (Default is 'PATH/Dockerfile')")
390402
cmd.Flags().StringVarP(&o.Tag, "tag", "t", defaultTag,
391403
"Image name and optionally a tag in the 'name:tag' format")
392-
cmd.Flags().StringVar(&o.AutoTagPattern, "tag-pattern", "build.local/x/%s:latest",
404+
cmd.Flags().StringVar(&o.AutoTagPattern, "tag-pattern", "build.local/x/%s:v%d",
393405
"Pattern to generate image name if no tag is given")
394406
cmd.Flags().StringVar(&o.LocalDir, "local", defaultLocalDir,
395407
"Build binaries instead an image and copy them to the specified path.")

0 commit comments

Comments
 (0)