From 3520e7a2be0326761295c3a84db7b80eeee31b40 Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Mon, 16 Sep 2024 17:45:05 -0300 Subject: [PATCH] repository: change interface to only expose Ensure method --- pkg/build/buildkit/build.go | 13 +--------- pkg/build/buildkit/build_test.go | 2 +- pkg/build/grpc_build_v1/build_service.pb.go | 24 +++++++++---------- .../grpc_build_v1/build_service_grpc.pb.go | 15 ++++++------ pkg/repository/fake/repository.go | 23 ++++++++++-------- pkg/repository/oci/oci.go | 21 +++++++++++++--- pkg/repository/oci/oci_test.go | 24 ++++++------------- pkg/repository/repository.go | 4 +--- 8 files changed, 60 insertions(+), 66 deletions(-) diff --git a/pkg/build/buildkit/build.go b/pkg/build/buildkit/build.go index 49c8eec..0391557 100644 --- a/pkg/build/buildkit/build.go +++ b/pkg/build/buildkit/build.go @@ -274,18 +274,7 @@ func (b *BuildKit) extractTsuruConfigsFromContainerImage(ctx context.Context, c func (b *BuildKit) createRemoteRepository(ctx context.Context, r *pb.BuildRequest) error { for _, v := range r.DestinationImages { if provider, ok := b.opts.RemoteRepository[build.GetRegistry(v)]; ok { - err := provider.Auth(ctx) - if err != nil { - return err - } - exists, err := provider.Exists(ctx, v) - if err != nil { - return err - } - if exists { - continue - } - err = provider.Create(ctx, v) + err := provider.Ensure(ctx, v) if err != nil { return err } diff --git a/pkg/build/buildkit/build_test.go b/pkg/build/buildkit/build_test.go index c124e8b..6e1ae38 100644 --- a/pkg/build/buildkit/build_test.go +++ b/pkg/build/buildkit/build_test.go @@ -388,7 +388,7 @@ func TestBuildKit_Build_FromContainerImages(t *testing.T) { DestinationImages: []string{baseRegistry(t, "app-my-app", "v1")}, PushOptions: &pb.PushOptions{InsecureRegistry: registryHTTP}, } - opts := &BuildKitOptions{TempDir: t.TempDir(), RemoteRepository: map[string]repository.Repository{registryAddress: &fake.FakeRepository{AuthSuccess: true}}} + opts := &BuildKitOptions{TempDir: t.TempDir(), RemoteRepository: map[string]repository.Repository{registryAddress: &fake.FakeRepository{}}} assert.Equal(t, opts.RemoteRepository[registryAddress].(*fake.FakeRepository).RepoExists, map[string]bool(nil)) _, err := NewBuildKit(bc, *opts). Build(context.TODO(), req, os.Stdout) diff --git a/pkg/build/grpc_build_v1/build_service.pb.go b/pkg/build/grpc_build_v1/build_service.pb.go index b1db89b..ef4fe15 100644 --- a/pkg/build/grpc_build_v1/build_service.pb.go +++ b/pkg/build/grpc_build_v1/build_service.pb.go @@ -4,8 +4,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.34.2 -// protoc v5.27.1 +// protoc-gen-go v1.31.0 +// protoc v5.27.3 // source: pkg/build/grpc_build_v1/build_service.proto package grpc_build_v1 @@ -820,7 +820,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_rawDescGZIP() []byte { var file_pkg_build_grpc_build_v1_build_service_proto_enumTypes = make([]protoimpl.EnumInfo, 1) var file_pkg_build_grpc_build_v1_build_service_proto_msgTypes = make([]protoimpl.MessageInfo, 10) -var file_pkg_build_grpc_build_v1_build_service_proto_goTypes = []any{ +var file_pkg_build_grpc_build_v1_build_service_proto_goTypes = []interface{}{ (BuildKind)(0), // 0: grpc_build_v1.BuildKind (*BuildRequest)(nil), // 1: grpc_build_v1.BuildRequest (*BuildResponse)(nil), // 2: grpc_build_v1.BuildResponse @@ -858,7 +858,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return } if !protoimpl.UnsafeEnabled { - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[0].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*BuildRequest); i { case 0: return &v.state @@ -870,7 +870,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[1].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*BuildResponse); i { case 0: return &v.state @@ -882,7 +882,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[2].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TsuruApp); i { case 0: return &v.state @@ -894,7 +894,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[3].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TsuruJob); i { case 0: return &v.state @@ -906,7 +906,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[4].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TsuruPlatform); i { case 0: return &v.state @@ -918,7 +918,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[5].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*PushOptions); i { case 0: return &v.state @@ -930,7 +930,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[6].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*ContainerImageConfig); i { case 0: return &v.state @@ -942,7 +942,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { return nil } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[7].Exporter = func(v any, i int) any { + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*TsuruConfig); i { case 0: return &v.state @@ -955,7 +955,7 @@ func file_pkg_build_grpc_build_v1_build_service_proto_init() { } } } - file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[1].OneofWrappers = []any{ + file_pkg_build_grpc_build_v1_build_service_proto_msgTypes[1].OneofWrappers = []interface{}{ (*BuildResponse_Output)(nil), (*BuildResponse_TsuruConfig)(nil), } diff --git a/pkg/build/grpc_build_v1/build_service_grpc.pb.go b/pkg/build/grpc_build_v1/build_service_grpc.pb.go index d790d7c..ceaeb50 100644 --- a/pkg/build/grpc_build_v1/build_service_grpc.pb.go +++ b/pkg/build/grpc_build_v1/build_service_grpc.pb.go @@ -4,8 +4,8 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: -// - protoc-gen-go-grpc v1.4.0 -// - protoc v5.27.1 +// - protoc-gen-go-grpc v1.3.0 +// - protoc v5.27.3 // source: pkg/build/grpc_build_v1/build_service.proto package grpc_build_v1 @@ -19,8 +19,8 @@ import ( // This is a compile-time assertion to ensure that this generated file // is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.62.0 or later. -const _ = grpc.SupportPackageIsVersion8 +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 const ( Build_Build_FullMethodName = "/grpc_build_v1.Build/Build" @@ -43,12 +43,11 @@ func NewBuildClient(cc grpc.ClientConnInterface) BuildClient { } func (c *buildClient) Build(ctx context.Context, in *BuildRequest, opts ...grpc.CallOption) (Build_BuildClient, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - stream, err := c.cc.NewStream(ctx, &Build_ServiceDesc.Streams[0], Build_Build_FullMethodName, cOpts...) + stream, err := c.cc.NewStream(ctx, &Build_ServiceDesc.Streams[0], Build_Build_FullMethodName, opts...) if err != nil { return nil, err } - x := &buildBuildClient{ClientStream: stream} + x := &buildBuildClient{stream} if err := x.ClientStream.SendMsg(in); err != nil { return nil, err } @@ -109,7 +108,7 @@ func _Build_Build_Handler(srv interface{}, stream grpc.ServerStream) error { if err := stream.RecvMsg(m); err != nil { return err } - return srv.(BuildServer).Build(m, &buildBuildServer{ServerStream: stream}) + return srv.(BuildServer).Build(m, &buildBuildServer{stream}) } type Build_BuildServer interface { diff --git a/pkg/repository/fake/repository.go b/pkg/repository/fake/repository.go index 4abecb6..fad1bd7 100644 --- a/pkg/repository/fake/repository.go +++ b/pkg/repository/fake/repository.go @@ -12,17 +12,9 @@ import ( type FakeRepository struct { CreatedRepos map[string]bool RepoExists map[string]bool - AuthSuccess bool } -func (f *FakeRepository) Auth(ctx context.Context) error { - if f.AuthSuccess { - return nil - } - return errors.New("auth repository failed") -} - -func (f *FakeRepository) Create(ctx context.Context, name string) error { +func (f *FakeRepository) create(name string) error { if _, exists := f.RepoExists[name]; exists { return errors.New("repository already exists") } @@ -37,10 +29,21 @@ func (f *FakeRepository) Create(ctx context.Context, name string) error { return nil } -func (f *FakeRepository) Exists(ctx context.Context, name string) (bool, error) { +func (f *FakeRepository) exists(name string) (bool, error) { if f.RepoExists == nil { f.RepoExists = make(map[string]bool) } exists := f.RepoExists[name] return exists, nil } + +func (f *FakeRepository) Ensure(ctx context.Context, name string) error { + exists, err := f.exists(name) + if err != nil { + return err + } + if !exists { + return f.create(name) + } + return nil +} diff --git a/pkg/repository/oci/oci.go b/pkg/repository/oci/oci.go index e34ec21..2bdde3a 100644 --- a/pkg/repository/oci/oci.go +++ b/pkg/repository/oci/oci.go @@ -34,7 +34,22 @@ func NewOCI(data map[string]string) *OCI { } } -func (r *OCI) Auth(ctx context.Context) error { +func (r *OCI) Ensure(ctx context.Context, name string) error { + err := r.auth(ctx) + if err != nil { + return err + } + exists, err := r.exists(ctx, name) + if err != nil { + return err + } + if !exists { + return r.create(ctx, name) + } + return nil +} + +func (r *OCI) auth(ctx context.Context) error { if r.client != nil { return nil } @@ -47,7 +62,7 @@ func (r *OCI) Auth(ctx context.Context) error { return err } -func (r *OCI) Create(ctx context.Context, name string) error { +func (r *OCI) create(ctx context.Context, name string) error { name, err := parserRegistryRepository(name) if err != nil { return err @@ -65,7 +80,7 @@ func (r *OCI) Create(ctx context.Context, name string) error { return nil } -func (r *OCI) Exists(ctx context.Context, name string) (bool, error) { +func (r *OCI) exists(ctx context.Context, name string) (bool, error) { name, err := parserRegistryRepository(name) if err != nil { return false, err diff --git a/pkg/repository/oci/oci_test.go b/pkg/repository/oci/oci_test.go index 063a865..6ac6548 100644 --- a/pkg/repository/oci/oci_test.go +++ b/pkg/repository/oci/oci_test.go @@ -43,35 +43,25 @@ func (m *FakeArtifactsClient) ListContainerRepositories(ctx context.Context, req }, nil } -func TestOCI_Create(t *testing.T) { +func TestOCI_Ensure(t *testing.T) { fakeClient := new(FakeArtifactsClient) oci := &OCI{ client: fakeClient, } ctx := context.TODO() name := "registry/namespace/test-repo" - err := oci.Create(ctx, name) - assert.NoError(t, err) - err = oci.Create(ctx, name) - assert.Error(t, err, "repository already exists") -} - -func TestOCI_Exists(t *testing.T) { - fakeClient := new(FakeArtifactsClient) - oci := &OCI{ - client: fakeClient, - } - ctx := context.TODO() - name := "registry/namespace/test-repo" - exists, err := oci.Exists(ctx, name) + exists, err := oci.exists(ctx, name) assert.NoError(t, err) assert.False(t, exists) - err = oci.Create(ctx, name) + err = oci.Ensure(ctx, name) assert.NoError(t, err) - exists, err = oci.Exists(ctx, name) + exists, err = oci.exists(ctx, name) assert.NoError(t, err) assert.True(t, exists) + err = oci.create(ctx, name) + assert.Error(t, err, "repository already exists") } + func TestParserRegistryRepository(t *testing.T) { tests := []struct { name string diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go index be8ad4d..8acaa68 100644 --- a/pkg/repository/repository.go +++ b/pkg/repository/repository.go @@ -14,9 +14,7 @@ import ( ) type Repository interface { - Auth(ctx context.Context) error - Create(ctx context.Context, name string) error - Exists(ctx context.Context, name string) (bool, error) + Ensure(ctx context.Context, name string) error } type RemoteRepositoryProvider map[string]map[string]string