Use registry 2.4 toget tag info from events

This commit is contained in:
hmwenchen 2016-04-26 20:06:21 +08:00
parent 957df50958
commit 87b213c3ae
4 changed files with 7 additions and 91 deletions

View File

@ -40,6 +40,7 @@ type Target struct {
Digest string
Repository string
URL string `json:"Url"`
Tag string
}
// Actor holds information about actor.

View File

@ -60,16 +60,3 @@ type histroyItem struct {
type blobSumItem struct {
BlobSum string `json:"blobSum"`
}
// ManifestDigest ...
type ManifestDigest struct {
MediaType string `json:"mediaType"`
SchemaVersion int `json:"schemaVersion"`
Layers []layerItem `json:"layers"`
}
type layerItem struct {
MediaType string `json:"mediaType"`
Size int `json:"size"`
Digest string `json:"digest"`
}

View File

@ -52,7 +52,7 @@ func (n *NotificationHandler) Post() {
log.Errorf("error while decoding json: %v", err)
return
}
var username, action, repo, project, repoTag, tagURL, digest string
var username, action, repo, project, repoTag string
var matched bool
var client *http.Client
for _, e := range notification.Events {
@ -65,82 +65,8 @@ func (n *NotificationHandler) Post() {
username = e.Actor.Name
action = e.Action
repo = e.Target.Repository
tagURL = e.Target.URL
digest = e.Target.Digest
client = registry.NewClientUsernameAuthHandlerEmbeded(username)
log.Debug("initializing username auth handler: %s", username)
endpoint := os.Getenv("REGISTRY_URL")
r, err1 := registry.New(endpoint, client)
if err1 != nil {
log.Fatalf("error occurred while initializing auth handler for repository API: %v", err1)
}
n.registry = r
_, _, payload, err2 := n.registry.PullManifest(repo, digest, registry.ManifestVersion1)
if err2 != nil {
log.Errorf("Failed to get manifests for repo, repo name: %s, tag: %s, error: %v", repo, tagURL, err2)
return
}
maniDig := models.ManifestDigest{}
err = json.Unmarshal(payload, &maniDig)
if err != nil {
log.Errorf("Failed to decode json from response for manifests, repo name: %s, tag: %s, error: %v", repo, tagURL, err)
return
}
var digestLayers []string
var tagLayers []string
for _, diglayer := range maniDig.Layers {
digestLayers = append(digestLayers, diglayer.Digest)
}
tags, err := n.registry.ListTag(repo)
if err != nil {
e, ok := errors.ParseError(err)
if ok {
log.Info(e)
} else {
log.Error(err)
}
return
}
log.Infof("tags : %v ", tags)
for _, tag := range tags {
_, _, payload, err := n.registry.PullManifest(repo, tag, registry.ManifestVersion1)
if err != nil {
e, ok := errors.ParseError(err)
if ok {
log.Info(e)
} else {
log.Error(err)
}
continue
}
taginfo := models.Manifest{}
err = json.Unmarshal(payload, &taginfo)
if err != nil {
log.Errorf("Failed to decode json from response for manifests, repo name: %s, tag: %s, error: %v", repo, tag, err)
continue
}
for _, fslayer := range taginfo.FsLayers {
tagLayers = append(tagLayers, fslayer.BlobSum)
}
sort.Strings(digestLayers)
sort.Strings(tagLayers)
eq := compStringArray(digestLayers, tagLayers)
if eq {
repoTag = tag
break
}
}
repoTag = e.Target.Tag
log.Debugf("repo tag is : %v ", repoTag)
if strings.Contains(repo, "/") {
project = repo[0:strings.LastIndex(repo, "/")]
@ -148,7 +74,6 @@ func (n *NotificationHandler) Post() {
if username == "" {
username = "anonymous"
}
log.Debugf("repo tag is : %v ", repoTag)
go dao.AccessLog(username, project, repo, repoTag, action)
if action == "push" {
go func() {

View File

@ -25,6 +25,7 @@ import (
"github.com/docker/distribution/manifest"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/vmware/harbor/utils/log"
"github.com/vmware/harbor/utils/registry/errors"
)
@ -197,6 +198,8 @@ func (r *Registry) PullManifest(name, reference string, version manifest.Version
}
// if the registry does not support schema 2, schema 1 manifest will be returned
log.Debugf("--- Pull request headers: %v ---", req.Header)
log.Debugf("--- Pull request Accept header: %v ---", http.CanonicalHeaderKey("Accept"))
req.Header.Set(http.CanonicalHeaderKey("Accept"), version.MediaType)
resp, err := r.client.Do(req)