refactor: initialize the remote helper using ctx from http request

Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
He Weiwei 2021-09-09 11:13:53 +00:00
parent 93a078d225
commit d00024ab36
3 changed files with 15 additions and 10 deletions

View File

@ -17,8 +17,6 @@ package proxy
import (
"context"
"fmt"
"github.com/goharbor/harbor/src/controller/tag"
proModels "github.com/goharbor/harbor/src/pkg/project/models"
"io"
"strings"
"sync"
@ -29,11 +27,13 @@ import (
"github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/controller/blob"
"github.com/goharbor/harbor/src/controller/event/operator"
"github.com/goharbor/harbor/src/controller/tag"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/cache"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
proModels "github.com/goharbor/harbor/src/pkg/project/models"
"github.com/opencontainers/go-digest"
)
@ -69,6 +69,7 @@ type Controller interface {
// EnsureTag ensure tag for digest
EnsureTag(ctx context.Context, art lib.ArtifactInfo, tagName string) error
}
type controller struct {
blobCtl blob.Controller
artifactCtl artifact.Controller
@ -179,6 +180,7 @@ func getManifestListKey(repo, dig string) string {
// actual redis key format is cache:manifestlist:<repo name>:sha256:xxxx
return "manifestlist:" + repo + ":" + dig
}
func (c *controller) ProxyManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (distribution.Manifest, error) {
var man distribution.Manifest
remoteRepo := getRemoteRepo(art)
@ -227,15 +229,17 @@ func (c *controller) ProxyManifest(ctx context.Context, art lib.ArtifactInfo, re
return man, nil
}
func (c *controller) HeadManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (bool, *distribution.Descriptor, error) {
remoteRepo := getRemoteRepo(art)
ref := getReference(art)
return remote.ManifestExist(remoteRepo, ref)
}
func (c *controller) ProxyBlob(ctx context.Context, p *proModels.Project, art lib.ArtifactInfo) (int64, io.ReadCloser, error) {
remoteRepo := getRemoteRepo(art)
log.Debugf("The blob doesn't exist, proxy the request to the target server, url:%v", remoteRepo)
rHelper, err := NewRemoteHelper(p.RegistryID)
rHelper, err := NewRemoteHelper(ctx, p.RegistryID)
if err != nil {
return 0, nil, err
}

View File

@ -15,13 +15,14 @@
package proxy
import (
"context"
"fmt"
"io"
"github.com/docker/distribution"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/pkg/reg"
"github.com/goharbor/harbor/src/pkg/reg/adapter"
"github.com/goharbor/harbor/src/pkg/reg/model"
"io"
)
// RemoteInterface defines operations related to remote repository under proxy
@ -42,22 +43,22 @@ type remoteHelper struct {
}
// NewRemoteHelper create a remote interface
func NewRemoteHelper(regID int64) (RemoteInterface, error) {
func NewRemoteHelper(ctx context.Context, regID int64) (RemoteInterface, error) {
r := &remoteHelper{
regID: regID,
registryMgr: reg.Mgr}
if err := r.init(); err != nil {
if err := r.init(ctx); err != nil {
return nil, err
}
return r, nil
}
func (r *remoteHelper) init() error {
func (r *remoteHelper) init(ctx context.Context) error {
if r.registry != nil {
return nil
}
reg, err := r.registryMgr.Get(orm.Context(), r.regID)
reg, err := r.registryMgr.Get(ctx, r.regID)
if err != nil {
return err
}

View File

@ -116,7 +116,7 @@ func handleManifest(w http.ResponseWriter, r *http.Request, next http.Handler) e
next.ServeHTTP(w, r)
return nil
}
remote, err := proxy.NewRemoteHelper(p.RegistryID)
remote, err := proxy.NewRemoteHelper(r.Context(), p.RegistryID)
if err != nil {
return err
}