Skip to content

Commit

Permalink
repository: change interface to only expose Ensure method
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheu committed Sep 16, 2024
1 parent 97c4442 commit 3520e7a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 66 deletions.
13 changes: 1 addition & 12 deletions pkg/build/buildkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/buildkit/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions pkg/build/grpc_build_v1/build_service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions pkg/build/grpc_build_v1/build_service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions pkg/repository/fake/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
}
21 changes: 18 additions & 3 deletions pkg/repository/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 7 additions & 17 deletions pkg/repository/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3520e7a

Please sign in to comment.