This repository was archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgcr.go
74 lines (67 loc) · 2.2 KB
/
gcr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
"regexp"
)
var (
gcrSHA256Pattern = regexp.MustCompile(`^gcr.io\/(.*)@(sha256:[0-9a-f]{64})$`)
)
func isGCRHash(image string) bool { return gcrSHA256Pattern.MatchString(image) }
// resolveGCRHashToTag returns the image with IMAGE:TAG format if it can be
// resolved. If no tags are available, an empty string is returned. If multiple
// tags are available, first one that's not "latest" is returned.
func resolveGCRHashToTag(image string) (string, error) {
groups := gcrSHA256Pattern.FindStringSubmatch(image)
if len(groups) != 3 {
return "", errors.Errorf("image %s cannot be parsed into repo/sha (got %d groups)", image, len(groups))
}
repo, hash := groups[1], groups[2]
resp, err := http.Get(fmt.Sprintf("https://gcr.io/v2/%s/tags/list", repo))
if err != nil {
return "", errors.Wrapf(err, "failed to query tags from GCR for image %s", image)
}
defer resp.Body.Close()
var v struct {
Manifest map[string]struct {
Tags []string `json:"tag"`
} `json:"manifest"`
}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return "", errors.Wrap(err, "failed to read and decode response body")
}
man, ok := v.Manifest[hash]
if !ok {
return "", errors.Wrapf(err, "hash %q not found in response manifest", hash)
}
if len(man.Tags) == 0 {
return "", errors.Errorf("no tags found for gcr image %s", image)
}
// return the first tag that's not "latest"
var tag string
for _, t := range man.Tags {
if t != "latest" {
tag = t
break
}
}
if tag == "" {
tag = man.Tags[0]
}
return fmt.Sprintf("gcr.io/%s:%s", repo, tag), nil
}