From 85339976645063b4010ea87080397613e0777372 Mon Sep 17 00:00:00 2001 From: Alexey Igrychev Date: Mon, 14 Mar 2022 18:41:32 +0000 Subject: [PATCH] refactor: docker registry package - move push image options - move and rename docker registry interface - rename docker registry api interface Signed-off-by: Alexey Igrychev --- integration/suites/cleanup/suite_test.go | 2 +- .../cleanup_after_converge/suite_test.go | 2 +- pkg/build/stage/base.go | 2 +- pkg/build/stage/dockerfile.go | 2 +- pkg/build/stage/dockerfile_test.go | 2 +- pkg/build/stage/interface.go | 2 +- pkg/build/stage/stubs_test.go | 10 ++++---- pkg/docker_registry/api.go | 4 +++ pkg/docker_registry/docker_registry.go | 25 +------------------ pkg/docker_registry/interface.go | 19 +++++++++++++- pkg/storage/repo_stages_storage.go | 2 +- 11 files changed, 35 insertions(+), 37 deletions(-) diff --git a/integration/suites/cleanup/suite_test.go b/integration/suites/cleanup/suite_test.go index b8253e52f0..1d2bed4499 100644 --- a/integration/suites/cleanup/suite_test.go +++ b/integration/suites/cleanup/suite_test.go @@ -33,7 +33,7 @@ var SuiteData struct { suite_init.SuiteData TestImplementation string StagesStorage storage.StagesStorage - ContainerRegistry docker_registry.DockerRegistry + ContainerRegistry docker_registry.Interface } var ( diff --git a/integration/suites/cleanup_after_converge/suite_test.go b/integration/suites/cleanup_after_converge/suite_test.go index 9f15781d0c..74af733091 100644 --- a/integration/suites/cleanup_after_converge/suite_test.go +++ b/integration/suites/cleanup_after_converge/suite_test.go @@ -32,7 +32,7 @@ func TestSuite(t *testing.T) { var SuiteData = struct { suite_init.SuiteData StagesStorage storage.StagesStorage - ContainerRegistry docker_registry.DockerRegistry + ContainerRegistry docker_registry.Interface }{} var ( diff --git a/pkg/build/stage/base.go b/pkg/build/stage/base.go index a0d742d04a..e91d0fc1aa 100644 --- a/pkg/build/stage/base.go +++ b/pkg/build/stage/base.go @@ -122,7 +122,7 @@ func (s *BaseStage) Name() StageName { panic("name must be defined!") } -func (s *BaseStage) FetchDependencies(_ context.Context, _ Conveyor, _ container_runtime.ContainerRuntime, _ docker_registry.DockerRegistryInterface) error { +func (s *BaseStage) FetchDependencies(_ context.Context, _ Conveyor, _ container_runtime.ContainerRuntime, _ docker_registry.ApiInterface) error { return nil } diff --git a/pkg/build/stage/dockerfile.go b/pkg/build/stage/dockerfile.go index b5bc58b46a..249858fd8b 100644 --- a/pkg/build/stage/dockerfile.go +++ b/pkg/build/stage/dockerfile.go @@ -318,7 +318,7 @@ type dockerfileInstructionInterface interface { Name() string } -func (s *DockerfileStage) FetchDependencies(ctx context.Context, c Conveyor, containerRuntime container_runtime.ContainerRuntime, dockerRegistry docker_registry.DockerRegistryInterface) error { +func (s *DockerfileStage) FetchDependencies(ctx context.Context, c Conveyor, containerRuntime container_runtime.ContainerRuntime, dockerRegistry docker_registry.ApiInterface) error { resolvedDependenciesArgsHash := resolveDependenciesArgsHash(s.dependencies, c) resolvedDockerMetaArgsHash, err := s.resolveDockerMetaArgs(resolvedDependenciesArgsHash) diff --git a/pkg/build/stage/dockerfile_test.go b/pkg/build/stage/dockerfile_test.go index 1f88a31951..d4bc396c68 100644 --- a/pkg/build/stage/dockerfile_test.go +++ b/pkg/build/stage/dockerfile_test.go @@ -290,7 +290,7 @@ RUN echo hello containerRuntime := NewContainerRuntimeMock() - dockerRegistry := NewDockerRegistryStub() + dockerRegistry := NewDockerRegistryApiStub() err := stage.FetchDependencies(ctx, conveyor, containerRuntime, dockerRegistry) Expect(IsErrInvalidBaseImage(err)).To(BeTrue()) diff --git a/pkg/build/stage/interface.go b/pkg/build/stage/interface.go index e899934e6c..3a01427f75 100644 --- a/pkg/build/stage/interface.go +++ b/pkg/build/stage/interface.go @@ -14,7 +14,7 @@ type Interface interface { IsEmpty(ctx context.Context, c Conveyor, prevBuiltImage container_runtime.LegacyImageInterface) (bool, error) - FetchDependencies(ctx context.Context, c Conveyor, cr container_runtime.ContainerRuntime, dockerRegistry docker_registry.DockerRegistryInterface) error + FetchDependencies(ctx context.Context, c Conveyor, cr container_runtime.ContainerRuntime, dockerRegistry docker_registry.ApiInterface) error GetDependencies(ctx context.Context, c Conveyor, prevImage container_runtime.LegacyImageInterface, prevBuiltImage container_runtime.LegacyImageInterface) (string, error) GetNextStageDependencies(ctx context.Context, c Conveyor) (string, error) diff --git a/pkg/build/stage/stubs_test.go b/pkg/build/stage/stubs_test.go index 36f141428d..bc6fe004b9 100644 --- a/pkg/build/stage/stubs_test.go +++ b/pkg/build/stage/stubs_test.go @@ -192,15 +192,15 @@ func (containerRuntime *ContainerRuntimeMock) Pull(ctx context.Context, ref stri return nil } -type DockerRegistryStub struct { - docker_registry.DockerRegistryInterface +type DockerRegistryApiStub struct { + docker_registry.ApiInterface } -func NewDockerRegistryStub() *DockerRegistryStub { - return &DockerRegistryStub{} +func NewDockerRegistryApiStub() *DockerRegistryApiStub { + return &DockerRegistryApiStub{} } -func (dockerRegistry *DockerRegistryStub) GetRepoImageConfigFile(ctx context.Context, reference string) (*v1.ConfigFile, error) { +func (dockerRegistry *DockerRegistryApiStub) GetRepoImageConfigFile(ctx context.Context, reference string) (*v1.ConfigFile, error) { return &v1.ConfigFile{ Config: v1.Config{}, }, nil diff --git a/pkg/docker_registry/api.go b/pkg/docker_registry/api.go index 22b49afdb1..84e99a2692 100644 --- a/pkg/docker_registry/api.go +++ b/pkg/docker_registry/api.go @@ -219,6 +219,10 @@ func (api *api) MutateAndPushImage(_ context.Context, sourceReference, destinati return nil } +type PushImageOptions struct { + Labels map[string]string +} + func (api *api) PushImage(ctx context.Context, reference string, opts *PushImageOptions) error { retriesLimit := 5 diff --git a/pkg/docker_registry/docker_registry.go b/pkg/docker_registry/docker_registry.go index 5702a7ad3e..d6a03104c4 100644 --- a/pkg/docker_registry/docker_registry.go +++ b/pkg/docker_registry/docker_registry.go @@ -1,37 +1,14 @@ package docker_registry import ( - "context" "fmt" "regexp" "strings" "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/name" - v1 "github.com/google/go-containerregistry/pkg/v1" - - "github.com/werf/werf/pkg/image" ) -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) - 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) - IsRepoImageExists(ctx context.Context, reference string) (bool, error) - DeleteRepoImage(ctx context.Context, repoImage *image.Info) error - PushImage(ctx context.Context, reference string, opts *PushImageOptions) error - MutateAndPushImage(ctx context.Context, sourceReference, destinationReference string, mutateConfigFunc func(v1.Config) (v1.Config, error)) error - - String() string -} - -type PushImageOptions struct { - Labels map[string]string -} - type DockerRegistryOptions struct { InsecureRegistry bool SkipTlsVerifyRegistry bool @@ -114,7 +91,7 @@ func (o *DockerRegistryOptions) defaultOptions() defaultImplementationOptions { }} } -func NewDockerRegistry(repositoryAddress string, implementation string, options DockerRegistryOptions) (DockerRegistry, error) { +func NewDockerRegistry(repositoryAddress string, implementation string, options DockerRegistryOptions) (Interface, error) { switch implementation { case AwsEcrImplementationName: return newAwsEcr(options.awsEcrOptions()) diff --git a/pkg/docker_registry/interface.go b/pkg/docker_registry/interface.go index 6293839fdc..515ca71339 100644 --- a/pkg/docker_registry/interface.go +++ b/pkg/docker_registry/interface.go @@ -4,8 +4,25 @@ import ( "context" v1 "github.com/google/go-containerregistry/pkg/v1" + + "github.com/werf/werf/pkg/image" ) -type DockerRegistryInterface interface { +type Interface interface { + CreateRepo(ctx context.Context, reference string) error + DeleteRepo(ctx context.Context, reference string) error + Tags(ctx context.Context, reference string) ([]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) + IsRepoImageExists(ctx context.Context, reference string) (bool, error) + DeleteRepoImage(ctx context.Context, repoImage *image.Info) error + PushImage(ctx context.Context, reference string, opts *PushImageOptions) error + MutateAndPushImage(ctx context.Context, sourceReference, destinationReference string, mutateConfigFunc func(v1.Config) (v1.Config, error)) error + + String() string +} + +type ApiInterface interface { GetRepoImageConfigFile(ctx context.Context, reference string) (*v1.ConfigFile, error) } diff --git a/pkg/storage/repo_stages_storage.go b/pkg/storage/repo_stages_storage.go index 2a601aaf72..0c3bf43229 100644 --- a/pkg/storage/repo_stages_storage.go +++ b/pkg/storage/repo_stages_storage.go @@ -60,7 +60,7 @@ func isUnexpectedTagFormatError(err error) bool { type RepoStagesStorage struct { RepoAddress string - DockerRegistry docker_registry.DockerRegistry + DockerRegistry docker_registry.Interface ContainerRuntime container_runtime.ContainerRuntime }