mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-25 19:56:09 +01:00
refactor: initialize the remote helper using ctx from http request
Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
parent
93a078d225
commit
d00024ab36
@ -17,8 +17,6 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/goharbor/harbor/src/controller/tag"
|
|
||||||
proModels "github.com/goharbor/harbor/src/pkg/project/models"
|
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -29,11 +27,13 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/controller/artifact"
|
"github.com/goharbor/harbor/src/controller/artifact"
|
||||||
"github.com/goharbor/harbor/src/controller/blob"
|
"github.com/goharbor/harbor/src/controller/blob"
|
||||||
"github.com/goharbor/harbor/src/controller/event/operator"
|
"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"
|
||||||
"github.com/goharbor/harbor/src/lib/cache"
|
"github.com/goharbor/harbor/src/lib/cache"
|
||||||
"github.com/goharbor/harbor/src/lib/errors"
|
"github.com/goharbor/harbor/src/lib/errors"
|
||||||
"github.com/goharbor/harbor/src/lib/log"
|
"github.com/goharbor/harbor/src/lib/log"
|
||||||
"github.com/goharbor/harbor/src/lib/orm"
|
"github.com/goharbor/harbor/src/lib/orm"
|
||||||
|
proModels "github.com/goharbor/harbor/src/pkg/project/models"
|
||||||
"github.com/opencontainers/go-digest"
|
"github.com/opencontainers/go-digest"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,6 +69,7 @@ type Controller interface {
|
|||||||
// EnsureTag ensure tag for digest
|
// EnsureTag ensure tag for digest
|
||||||
EnsureTag(ctx context.Context, art lib.ArtifactInfo, tagName string) error
|
EnsureTag(ctx context.Context, art lib.ArtifactInfo, tagName string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type controller struct {
|
type controller struct {
|
||||||
blobCtl blob.Controller
|
blobCtl blob.Controller
|
||||||
artifactCtl artifact.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
|
// actual redis key format is cache:manifestlist:<repo name>:sha256:xxxx
|
||||||
return "manifestlist:" + repo + ":" + dig
|
return "manifestlist:" + repo + ":" + dig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *controller) ProxyManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (distribution.Manifest, error) {
|
func (c *controller) ProxyManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (distribution.Manifest, error) {
|
||||||
var man distribution.Manifest
|
var man distribution.Manifest
|
||||||
remoteRepo := getRemoteRepo(art)
|
remoteRepo := getRemoteRepo(art)
|
||||||
@ -227,15 +229,17 @@ func (c *controller) ProxyManifest(ctx context.Context, art lib.ArtifactInfo, re
|
|||||||
|
|
||||||
return man, nil
|
return man, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *controller) HeadManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (bool, *distribution.Descriptor, error) {
|
func (c *controller) HeadManifest(ctx context.Context, art lib.ArtifactInfo, remote RemoteInterface) (bool, *distribution.Descriptor, error) {
|
||||||
remoteRepo := getRemoteRepo(art)
|
remoteRepo := getRemoteRepo(art)
|
||||||
ref := getReference(art)
|
ref := getReference(art)
|
||||||
return remote.ManifestExist(remoteRepo, ref)
|
return remote.ManifestExist(remoteRepo, ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *controller) ProxyBlob(ctx context.Context, p *proModels.Project, art lib.ArtifactInfo) (int64, io.ReadCloser, error) {
|
func (c *controller) ProxyBlob(ctx context.Context, p *proModels.Project, art lib.ArtifactInfo) (int64, io.ReadCloser, error) {
|
||||||
remoteRepo := getRemoteRepo(art)
|
remoteRepo := getRemoteRepo(art)
|
||||||
log.Debugf("The blob doesn't exist, proxy the request to the target server, url:%v", remoteRepo)
|
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 {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,14 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/docker/distribution"
|
"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"
|
||||||
"github.com/goharbor/harbor/src/pkg/reg/adapter"
|
"github.com/goharbor/harbor/src/pkg/reg/adapter"
|
||||||
"github.com/goharbor/harbor/src/pkg/reg/model"
|
"github.com/goharbor/harbor/src/pkg/reg/model"
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RemoteInterface defines operations related to remote repository under proxy
|
// RemoteInterface defines operations related to remote repository under proxy
|
||||||
@ -42,22 +43,22 @@ type remoteHelper struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRemoteHelper create a remote interface
|
// NewRemoteHelper create a remote interface
|
||||||
func NewRemoteHelper(regID int64) (RemoteInterface, error) {
|
func NewRemoteHelper(ctx context.Context, regID int64) (RemoteInterface, error) {
|
||||||
r := &remoteHelper{
|
r := &remoteHelper{
|
||||||
regID: regID,
|
regID: regID,
|
||||||
registryMgr: reg.Mgr}
|
registryMgr: reg.Mgr}
|
||||||
if err := r.init(); err != nil {
|
if err := r.init(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *remoteHelper) init() error {
|
func (r *remoteHelper) init(ctx context.Context) error {
|
||||||
|
|
||||||
if r.registry != nil {
|
if r.registry != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
reg, err := r.registryMgr.Get(orm.Context(), r.regID)
|
reg, err := r.registryMgr.Get(ctx, r.regID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ func handleManifest(w http.ResponseWriter, r *http.Request, next http.Handler) e
|
|||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
remote, err := proxy.NewRemoteHelper(p.RegistryID)
|
remote, err := proxy.NewRemoteHelper(r.Context(), p.RegistryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user