Skip to content

Commit 8a813b5

Browse files
committed
feat(staged-dockerfile): support ONBUILD instructions (part 1, preparations)
refs #2215 Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
1 parent 8f6b562 commit 8a813b5

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

pkg/build/build_phase.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,11 @@ func (phase *BuildPhase) ImageProcessingShouldBeStopped(_ context.Context, _ *im
242242
func (phase *BuildPhase) BeforeImageStages(ctx context.Context, img *image.Image) (deferFn func(), err error) {
243243
phase.StagesIterator = NewStagesIterator(phase.Conveyor)
244244

245-
if err := img.SetupBaseImage(); err != nil {
246-
return nil, err
245+
if err := img.SetupBaseImage(ctx, phase.Conveyor.StorageManager, manager.StorageOptions{
246+
ContainerBackend: phase.Conveyor.ContainerBackend,
247+
DockerRegistry: docker_registry.API(),
248+
}); err != nil {
249+
return nil, fmt.Errorf("unable to setup base image: %w", err)
247250
}
248251

249252
if img.UsesBuildContext() {

pkg/build/image/image.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (i *Image) GetRebuilt() bool {
201201
return i.rebuilt
202202
}
203203

204-
func (i *Image) SetupBaseImage() error {
204+
func (i *Image) SetupBaseImage(ctx context.Context, storageManager manager.StorageManagerInterface, storageOpts manager.StorageOptions) error {
205205
switch i.baseImageType {
206206
case StageAsBaseImage:
207207
i.stageAsBaseImage = i.Conveyor.GetImage(i.baseImageName).GetLastNonEmptyStage()
@@ -222,6 +222,24 @@ func (i *Image) SetupBaseImage() error {
222222
panic(fmt.Sprintf("unknown base image type %q", i.baseImageType))
223223
}
224224

225+
if i.IsDockerfileImage && i.DockerfileImageConfig.Staged {
226+
switch i.baseImageType {
227+
case StageAsBaseImage, ImageFromRegistryAsBaseImage:
228+
229+
fmt.Printf("-- %s SetupBaseImage %q\n", i.Name, i.baseImageReference)
230+
231+
info, err := storageManager.GetImageInfo(ctx, i.baseImageReference, storageOpts)
232+
if err != nil {
233+
return fmt.Errorf("unable to get base image %q manifest: %w", i.baseImageReference, err)
234+
}
235+
236+
fmt.Printf("-- %s SetupBaseImage %q -> %#v\n", i.Name, i.baseImageReference, info)
237+
for _, expression := range info.OnBuild {
238+
fmt.Printf(">> %q\n", expression)
239+
}
240+
}
241+
}
242+
225243
return nil
226244
}
227245

pkg/image/info.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/docker/docker/api/types"
9+
v1 "github.com/google/go-containerregistry/pkg/v1"
910

1011
"github.com/werf/werf/pkg/util"
1112
)
@@ -99,3 +100,19 @@ func ParseRepositoryAndTag(ref string) (string, string) {
99100
repository := util.Reverse(parts[1])
100101
return repository, tag
101102
}
103+
104+
func NewImageInfoFromRegistryConfig(ref string, cfg *v1.ConfigFile) *Info {
105+
repository, tag := ParseRepositoryAndTag(ref)
106+
return &Info{
107+
Name: ref,
108+
Repository: repository,
109+
Tag: tag,
110+
Labels: cfg.Config.Labels,
111+
OnBuild: cfg.Config.OnBuild,
112+
CreatedAtUnixNano: cfg.Created.UnixNano(),
113+
RepoDigest: "", // TODO
114+
ID: "", // TODO
115+
ParentID: "", // TODO
116+
Size: 0, // TODO
117+
}
118+
}

pkg/storage/manager/storage_manager.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/werf/logboek/pkg/types"
1717
"github.com/werf/werf/pkg/build/stage"
1818
"github.com/werf/werf/pkg/container_backend"
19+
"github.com/werf/werf/pkg/docker_registry"
1920
"github.com/werf/werf/pkg/image"
2021
"github.com/werf/werf/pkg/storage"
2122
"github.com/werf/werf/pkg/storage/lrumeta"
@@ -47,6 +48,11 @@ type ForEachDeleteStageOptions struct {
4748
storage.FilterStagesAndProcessRelatedDataOptions
4849
}
4950

51+
type StorageOptions struct {
52+
ContainerBackend container_backend.ContainerBackend
53+
DockerRegistry docker_registry.ApiInterface
54+
}
55+
5056
type StorageManagerInterface interface {
5157
InitCache(ctx context.Context) error
5258

@@ -59,6 +65,8 @@ type StorageManagerInterface interface {
5965
MaxNumberOfWorkers() int
6066
GenerateStageUniqueID(digest string, stages []*image.StageDescription) (string, int64)
6167

68+
GetImageInfo(ctx context.Context, ref string, opts StorageOptions) (*image.Info, error)
69+
6270
LockStageImage(ctx context.Context, imageName string) error
6371
GetStagesByDigest(ctx context.Context, stageName, stageDigest string) ([]*image.StageDescription, error)
6472
GetStagesByDigestWithCache(ctx context.Context, stageName, stageDigest string) ([]*image.StageDescription, error)
@@ -342,6 +350,29 @@ func (m *StorageManager) ForEachDeleteStage(ctx context.Context, options ForEach
342350
})
343351
}
344352

353+
func (m *StorageManager) GetImageInfo(ctx context.Context, ref string, opts StorageOptions) (*image.Info, error) {
354+
info, err := m.getImageInfoFromContainerBackend(ctx, ref, opts.ContainerBackend)
355+
if err != nil {
356+
return nil, err
357+
}
358+
if info != nil {
359+
return info, err
360+
}
361+
return m.getImageInfoFromRegistry(ctx, ref, opts.DockerRegistry)
362+
}
363+
364+
func (m *StorageManager) getImageInfoFromContainerBackend(ctx context.Context, ref string, containerBackend container_backend.ContainerBackend) (*image.Info, error) {
365+
return containerBackend.GetImageInfo(ctx, ref, container_backend.GetImageInfoOpts{})
366+
}
367+
368+
func (m *StorageManager) getImageInfoFromRegistry(ctx context.Context, ref string, dockerRegistry docker_registry.ApiInterface) (*image.Info, error) {
369+
cfg, err := dockerRegistry.GetRepoImageConfigFile(ctx, ref)
370+
if err != nil {
371+
return nil, err
372+
}
373+
return image.NewImageInfoFromRegistryConfig(ref, cfg), nil
374+
}
375+
345376
func (m *StorageManager) LockStageImage(ctx context.Context, imageName string) error {
346377
imageLockName := container_backend.ImageLockName(imageName)
347378

0 commit comments

Comments
 (0)