Skip to content

Commit

Permalink
refactor: custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-igrychev committed Oct 11, 2021
1 parent 6f4be9c commit 20358de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
20 changes: 12 additions & 8 deletions pkg/build/build_phase.go
Expand Up @@ -237,8 +237,8 @@ func (phase *BuildPhase) AfterImageStages(ctx context.Context, img *Image) error
}

if !phase.ShouldBeBuiltMode {
if err := phase.addCustomImageTags(ctx, img); err != nil {
return err
if err := phase.addCustomImageTagsToStagesStorage(ctx, img); err != nil {
return fmt.Errorf("unable to add custom image tags to stages storage: %s", err)
}
} else {
if err := phase.checkCustomImageTagsExistence(ctx, img); err != nil {
Expand Down Expand Up @@ -295,8 +295,12 @@ func (phase *BuildPhase) publishImageMetadata(ctx context.Context, img *Image) e
})
}

func (phase *BuildPhase) addCustomImageTags(ctx context.Context, img *Image) error {
if len(phase.CustomTagFuncList) == 0 {
func (phase *BuildPhase) addCustomImageTagsToStagesStorage(ctx context.Context, img *Image) error {
return addCustomImageTags(ctx, phase.Conveyor.StorageManager.GetStagesStorage(), img, phase.CustomTagFuncList)
}

func addCustomImageTags(ctx context.Context, stagesStorage storage.StagesStorage, img *Image, customTagFuncList []func(string) string) error {
if len(customTagFuncList) == 0 {
return nil
}

Expand All @@ -305,9 +309,9 @@ func (phase *BuildPhase) addCustomImageTags(ctx context.Context, img *Image) err
options.Style(style.Highlight())
}).
DoError(func() error {
for _, tagFunc := range phase.CustomTagFuncList {
for _, tagFunc := range customTagFuncList {
tag := tagFunc(img.GetName())
if err := phase.addCustomImageTag(ctx, img, tag); err != nil {
if err := addCustomImageTag(ctx, stagesStorage, img, tag); err != nil {
return err
}
}
Expand All @@ -316,11 +320,11 @@ func (phase *BuildPhase) addCustomImageTags(ctx context.Context, img *Image) err
})
}

func (phase *BuildPhase) addCustomImageTag(ctx context.Context, img *Image, tag string) error {
func addCustomImageTag(ctx context.Context, stagesStorage storage.StagesStorage, img *Image, tag string) error {
return logboek.Context(ctx).Default().LogProcess("tag %s", tag).
DoError(func() error {
stageDesc := img.GetLastNonEmptyStage().GetImage().GetStageDescription()
if err := phase.Conveyor.StorageManager.GetStagesStorage().AddStageCustomTag(ctx, stageDesc, tag); err != nil {
if err := stagesStorage.AddStageCustomTag(ctx, stageDesc, tag); err != nil {
return err
}

Expand Down
18 changes: 0 additions & 18 deletions pkg/docker_registry/default.go
Expand Up @@ -32,24 +32,6 @@ func (r *defaultImplementation) DeleteRepo(_ context.Context, _ string) error {
return fmt.Errorf("method is not implemented")
}

func (r *defaultImplementation) CheckRepoImageCustomTag(ctx context.Context, repoImage *image.Info, tag string) error {
tagReference := strings.Join([]string{repoImage.Repository, tag}, ":")
tagRepoImage, err := r.api.TryGetRepoImage(ctx, tagReference)
if err != nil {
return err
}

if tagRepoImage == nil {
return fmt.Errorf("the custom tag %q not found", tag)
}

if repoImage.ID != tagRepoImage.ID {
return fmt.Errorf("the custom tag %q image must be the same as related content-based tag %q image", tag, repoImage.Tag)
}

return nil
}

func (r *defaultImplementation) TagRepoImage(_ context.Context, repoImage *image.Info, tag string) error {
return r.api.tagImage(strings.Join([]string{repoImage.Repository, repoImage.Tag}, ":"), tag)
}
Expand Down
1 change: 0 additions & 1 deletion pkg/docker_registry/docker_registry.go
Expand Up @@ -17,7 +17,6 @@ type DockerRegistry interface {
CreateRepo(ctx context.Context, reference string) error
DeleteRepo(ctx context.Context, reference string) error
Tags(ctx context.Context, reference string) ([]string, error)
CheckRepoImageCustomTag(ctx context.Context, repoImage *image.Info, tag string) error
TagRepoImage(ctx context.Context, repoImage *image.Info, tag string) error
GetRepoImage(ctx context.Context, reference string) (*image.Info, error)
TryGetRepoImage(ctx context.Context, reference string) (*image.Info, error)
Expand Down
17 changes: 15 additions & 2 deletions pkg/storage/repo_stages_storage.go
Expand Up @@ -299,8 +299,21 @@ func (storage *RepoStagesStorage) GetStageDescription(ctx context.Context, proje
}

func (storage *RepoStagesStorage) CheckStageCustomTag(ctx context.Context, stageDescription *image.StageDescription, tag string) error {
logboek.Context(ctx).Debug().LogF("-- RepoStagesStorage CheckStageCustomTag %s %s\n", stageDescription.Info.Name, tag)
return storage.DockerRegistry.CheckRepoImageCustomTag(ctx, stageDescription.Info, tag)
fullImageName := strings.Join([]string{storage.RepoAddress, tag}, ":")
customTagImgInfo, err := storage.DockerRegistry.TryGetRepoImage(ctx, fullImageName)
if err != nil {
return err
}

if customTagImgInfo == nil {
return fmt.Errorf("custom tag %q not found", tag)
}

if customTagImgInfo.ID != stageDescription.Info.ID {
return fmt.Errorf("custom tag %q image must be the same as associated content-based tag %q image", tag, stageDescription.StageID.String())
}

return nil
}

func (storage *RepoStagesStorage) AddStageCustomTag(ctx context.Context, stageDescription *image.StageDescription, tag string) error {
Expand Down

0 comments on commit 20358de

Please sign in to comment.