Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Export adapter #45

Open
wants to merge 7 commits into
base: artifact-list-export-adapter
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions src/controller/replication/flow/stage.go
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stage.go should not change, it has an impact on other adapters, preventing them from working.

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func fetchResources(adapter adp.Adapter, policy *repctlmodel.Policy) ([]*model.R

// assemble the source resources by filling the registry information
func assembleSourceResources(resources []*model.Resource,
policy *repctlmodel.Policy) []*model.Resource {
policy *repctlmodel.Policy,
) []*model.Resource {
for _, resource := range resources {
resource.Registry = policy.SrcRegistry
}
Expand All @@ -83,32 +84,59 @@ func assembleSourceResources(resources []*model.Resource,

// assemble the destination resources by filling the metadata, registry and override properties
func assembleDestinationResources(resources []*model.Resource,
policy *repctlmodel.Policy, dstRepoComponentPathType string) ([]*model.Resource, error) {
policy *repctlmodel.Policy, dstRepoComponentPathType string,
) ([]*model.Resource, error) {
var result []*model.Resource
for _, resource := range resources {
name, err := replaceNamespace(resource.Metadata.Repository.Name, policy.DestNamespace, policy.DestNamespaceReplaceCount, dstRepoComponentPathType)
if err != nil {
return nil, err
var registry *model.Registry
var repositoryName string
var err error

log.Debugf("assembling dest resources...")

// Check condition to determine whether to assemble list or destination resources
if policy.DestRegistry.Type == "artifact-list-export" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some debug print statements left in the code. But the most important thing is that such PR won't be accepted by the community of Harbor. We can't place such a switcher inside of core logic.

// Assemble list resources
registry = policy.SrcRegistry
repositoryName = resource.Metadata.Repository.Name

if resource.ExtendedInfo == nil {
resource.ExtendedInfo = make(map[string]interface{})
}
resource.ExtendedInfo["destinationURL"] = policy.DestRegistry.URL
resource.ExtendedInfo["groupName"] = policy.DestNamespace
} else {
// Assemble destination resources
registry = policy.DestRegistry
repositoryName, err = replaceNamespace(resource.Metadata.Repository.Name, policy.DestNamespace, policy.DestNamespaceReplaceCount, dstRepoComponentPathType)
if err != nil {
return nil, err
}
}

// Create the resource
res := &model.Resource{
Type: resource.Type,
Registry: policy.DestRegistry,
Registry: registry,
ExtendedInfo: resource.ExtendedInfo,
Deleted: resource.Deleted,
IsDeleteTag: resource.IsDeleteTag,
Override: policy.Override,
Skip: resource.Skip,
}

// Fill the resource metadata
res.Metadata = &model.ResourceMetadata{
Repository: &model.Repository{
Name: name,
Name: repositoryName,
Metadata: resource.Metadata.Repository.Metadata,
},
Vtags: resource.Metadata.Vtags,
Artifacts: resource.Metadata.Artifacts,
}
result = append(result, res)
}

log.Debug("assemble the destination resources completed")
return result, nil
}
Expand Down
87 changes: 47 additions & 40 deletions src/pkg/reg/adapter/list-export/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"

"github.com/docker/distribution"
"github.com/goharbor/harbor/src/common/secret"
"github.com/goharbor/harbor/src/lib/config"
"github.com/goharbor/harbor/src/lib/errors"
regadapter "github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/model"
"github.com/google/go-containerregistry/pkg/crane"
"io"
"net/http"
"net/url"
"path"
"time"
)

var (
Expand All @@ -25,6 +23,7 @@ var (
var ErrNotImplemented = errors.New("not implemented")

type Result struct {
Group string `json:"group"`
Registry string `json:"registry"`
Artifacts []Artifact `json:"artifacts"`
}
Expand All @@ -45,8 +44,7 @@ func init() {
}
}

type factory struct {
}
type factory struct{}

// Create ...
func (f *factory) Create(r *model.Registry) (regadapter.Adapter, error) {
Expand All @@ -63,7 +61,6 @@ type adapter struct {
}

func (a adapter) RoundTrip(request *http.Request) (*http.Response, error) {

u, err := url.Parse(config.InternalCoreURL())
if err != nil {
return nil, fmt.Errorf("unable to parse internal core url: %v", err)
Expand All @@ -85,11 +82,11 @@ func (a adapter) Info() (*model.RegistryInfo, error) {
}

func (a adapter) PrepareForPush(resources []*model.Resource) error {

var (
artifacts []Artifact
registry *model.Registry
destinationRepo string
artifacts []Artifact
registry *model.Registry
destinationURL string
groupName string
)

for _, r := range resources {
Expand All @@ -102,15 +99,32 @@ func (a adapter) PrepareForPush(resources []*model.Resource) error {
if r.Registry == nil {
continue
}
if r.ExtendedInfo == nil {
return fmt.Errorf("extended_info map is nil")
}

if registry == nil {
registry = r.Registry
}
if destinationURL == "" {
destURL, ok := r.ExtendedInfo["destinationURL"].(string)
if ok {
destinationURL = destURL
} else {
return fmt.Errorf("destination_url not a string or missing")
}
}

for _, at := range r.Metadata.Artifacts {
if destinationRepo == "" {
destinationRepo = r.Metadata.Repository.Name
if groupName == "" {
grp, ok := r.ExtendedInfo["groupName"].(string)
if ok {
groupName = grp
} else {
return fmt.Errorf("groupName not a string or missing")
}
}

for _, at := range r.Metadata.Artifacts {
artifacts = append(artifacts, Artifact{
Repository: r.Metadata.Repository.Name,
Deleted: r.Deleted,
Expand All @@ -127,6 +141,7 @@ func (a adapter) PrepareForPush(resources []*model.Resource) error {
}

result := &Result{
Group: groupName,
Registry: registry.URL,
Artifacts: artifacts,
}
Expand All @@ -136,38 +151,31 @@ func (a adapter) PrepareForPush(resources []*model.Resource) error {
return errors.Wrap(err, "failed to marshal result")
}

img, err := crane.Image(map[string][]byte{
"artifacts.json": data,
})
if err != nil {
return fmt.Errorf("image create failed: %v", err)
}

destinationRepo = fmt.Sprintf("%s/%s", path.Dir(destinationRepo), "state")
//
fmt.Println("kumar is here \n \n\n kumar")
fmt.Printf("json %v: \n \n\n kumar", string(data))

err = crane.Push(img, destinationRepo, crane.WithTransport(a))
// Create a POST request
req, err := http.NewRequest("POST", destinationURL, bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("push image failed: %v", err)
fmt.Println("Error creating request:", err)
return nil
}

err = crane.Tag(destinationRepo, fmt.Sprintf("%d", time.Now().Unix()), crane.WithTransport(a))
if err != nil {
return fmt.Errorf("tag image failed: %v", err)
}

err = crane.Tag(destinationRepo, "latest", crane.WithTransport(a))
if err != nil {
return fmt.Errorf("tag image failed: %v", err)
}
// Set the content type header
req.Header.Set("Content-Type", "application/json")

responseBody := bytes.NewBuffer(data)
resp, err := http.Post(registry.URL, "application/json", responseBody)
// Send the request using http.Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Wrap(err, "failed to post result")
fmt.Println("Error sending request:", err)
return nil
}
defer resp.Body.Close()

// Print the response status
fmt.Println("Response Status:", resp.Status)

return nil
}

Expand Down Expand Up @@ -232,7 +240,6 @@ func (a adapter) ListTags(repository string) (tags []string, err error) {
}

func newAdapter(_ *model.Registry) (regadapter.Adapter, error) {

return &adapter{
httpClient: &http.Client{},
}, nil
Expand Down
Loading