Skip to content

Commit

Permalink
Add OCI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmabadr committed Nov 27, 2023
1 parent 44a6148 commit 0a3afb3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
47 changes: 47 additions & 0 deletions pkg/module/oci_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package module

import (
"fmt"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer/transferhandler/standard"
)

//go:generate mockery --name OciRepo
type OciRepoImpl interface {
ComponentVersionExists(archive *comparch.ComponentArchive, repo cpi.Repository) (bool, error)
GetComponentVersion(archive *comparch.ComponentArchive, repo cpi.Repository) (ocm.ComponentVersionAccess, error)
PushComponentVersion(archive *comparch.ComponentArchive, repository cpi.Repository, overwrite bool) error
}

type OciRepo struct{}

func (r *OciRepo) ComponentVersionExists(archive *comparch.ComponentArchive, repo cpi.Repository) (bool, error) {
return repo.ExistsComponentVersion(archive.ComponentVersionAccess.GetName(),
archive.ComponentVersionAccess.GetVersion())
}

func (r *OciRepo) GetComponentVersion(archive *comparch.ComponentArchive,
repo cpi.Repository) (ocm.ComponentVersionAccess, error) {
return repo.LookupComponentVersion(archive.ComponentVersionAccess.GetName(),
archive.ComponentVersionAccess.GetVersion())
}

func (r *OciRepo) PushComponentVersion(archive *comparch.ComponentArchive, repo cpi.Repository, overwrite bool) error {
transferHandler, err := standard.New(standard.Overwrite(overwrite))
if err != nil {
return fmt.Errorf("could not setup archive transfer: %w", err)
}

if err = transfer.TransferVersion(
common.NewLoggingPrinter(archive.GetContext().Logger()), nil, archive.ComponentVersionAccess, repo,
&customTransferHandler{transferHandler},
); err != nil {
return fmt.Errorf("could not finish component transfer: %w", err)
}
return nil
}
27 changes: 6 additions & 21 deletions pkg/module/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"regexp"
"strings"

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/dockerconfig"
oci "github.com/open-component-model/ocm/pkg/contexts/oci/repositories/ocireg"
Expand All @@ -18,9 +17,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/genericocireg"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/ocireg"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer/transferhandler"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer/transferhandler/standard"
"github.com/open-component-model/ocm/pkg/runtime"
)

Expand All @@ -39,6 +36,7 @@ type Remote struct {
Credentials string
Token string
Insecure bool
OciRepo
}

func (r *Remote) GetRepository(ctx cpi.Context) (cpi.Repository, error) {
Expand Down Expand Up @@ -137,13 +135,10 @@ func (r *Remote) Push(archive *comparch.ComponentArchive, overwrite bool) (ocm.C
}

if !overwrite {
versionExists, _ := repo.ExistsComponentVersion(archive.ComponentVersionAccess.GetName(),
archive.ComponentVersionAccess.GetVersion())
versionExists, _ := r.ComponentVersionExists(archive, repo)

if versionExists {
versionAccess, err := repo.LookupComponentVersion(
archive.ComponentVersionAccess.GetName(), archive.ComponentVersionAccess.GetVersion(),
)
versionAccess, err := r.GetComponentVersion(archive, repo)
if err != nil {
return nil, false, fmt.Errorf("could not lookup component version: %w", err)
}
Expand All @@ -158,21 +153,11 @@ func (r *Remote) Push(archive *comparch.ComponentArchive, overwrite bool) (ocm.C
}
}

transferHandler, err := standard.New(standard.Overwrite(overwrite))
if err != nil {
return nil, false, fmt.Errorf("could not setup archive transfer: %w", err)
}

if err = transfer.TransferVersion(
common.NewLoggingPrinter(archive.GetContext().Logger()), nil, archive.ComponentVersionAccess, repo,
&customTransferHandler{transferHandler},
); err != nil {
return nil, false, fmt.Errorf("could not finish component transfer: %w", err)
if err = r.PushComponentVersion(archive, repo, overwrite); err != nil {
return nil, false, err
}

componentVersion, err := repo.LookupComponentVersion(
archive.ComponentVersionAccess.GetName(), archive.ComponentVersionAccess.GetVersion(),
)
componentVersion, err := r.GetComponentVersion(archive, repo)

return componentVersion, err == nil, err
}
Expand Down

0 comments on commit 0a3afb3

Please sign in to comment.