FindDescriptor get the first descriptor pointed to by the image reference, whether tagged or digested
(ref *reference.Spec)
| 83 | |
| 84 | // FindDescriptor get the first descriptor pointed to by the image reference, whether tagged or digested |
| 85 | func (p *Provider) FindDescriptor(ref *reference.Spec) (*v1.Descriptor, error) { |
| 86 | index, err := p.Index() |
| 87 | // if there is no root index, we are broken |
| 88 | if err != nil { |
| 89 | return nil, fmt.Errorf("invalid image cache: %v", err) |
| 90 | } |
| 91 | |
| 92 | // parse the ref.Object to determine what it is, then search by that |
| 93 | dig := ref.Digest() |
| 94 | hash, err := v1.NewHash(dig.String()) |
| 95 | // if we had a valid hash, search by that |
| 96 | if err == nil { |
| 97 | // we had a valid hash, so we should search by it |
| 98 | descs, err := partial.FindManifests(index, match.Digests(hash)) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | if len(descs) > 0 { |
| 103 | return &descs[0], nil |
| 104 | } |
| 105 | // we had a valid hash, but didn't find any descriptors |
| 106 | return nil, nil |
| 107 | } |
| 108 | // no valid hash, try the tag |
| 109 | tag := ref.Object |
| 110 | // remove anything after an '@' |
| 111 | n := strings.LastIndex(tag, "@") |
| 112 | if n == 0 { |
| 113 | return nil, fmt.Errorf("invalid tag, was not digest, yet began with '@': %s", tag) |
| 114 | } |
| 115 | if n >= 0 { |
| 116 | tag = tag[:n] |
| 117 | } |
| 118 | descs, err := partial.FindManifests(index, match.Name(fmt.Sprintf("%s:%s", ref.Locator, tag))) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | if len(descs) > 0 { |
| 123 | return &descs[0], nil |
| 124 | } |
| 125 | return nil, nil |
| 126 | } |
no test coverage detected