From 87b213c3ae85fbf63f61a7fba25f47edc4b471d4 Mon Sep 17 00:00:00 2001 From: hmwenchen Date: Tue, 26 Apr 2016 20:06:21 +0800 Subject: [PATCH] Use registry 2.4 toget tag info from events --- models/notification.go | 1 + models/repo.go | 13 ------ service/notification.go | 81 ++------------------------------------ utils/registry/registry.go | 3 ++ 4 files changed, 7 insertions(+), 91 deletions(-) diff --git a/models/notification.go b/models/notification.go index 0f608e2c6..3f30ae7e4 100644 --- a/models/notification.go +++ b/models/notification.go @@ -40,6 +40,7 @@ type Target struct { Digest string Repository string URL string `json:"Url"` + Tag string } // Actor holds information about actor. diff --git a/models/repo.go b/models/repo.go index 4ace8d1b6..f224c902e 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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"` -} diff --git a/service/notification.go b/service/notification.go index f47ddd627..d307cb487 100644 --- a/service/notification.go +++ b/service/notification.go @@ -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() { diff --git a/utils/registry/registry.go b/utils/registry/registry.go index e390c4c0d..b4f41131b 100644 --- a/utils/registry/registry.go +++ b/utils/registry/registry.go @@ -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)