cleanDanglingReferences removes any dangling references from the index. If any have a tag that points to a manifest not in the index, it will remove it.
(ii v1.ImageIndex)
| 162 | // cleanDanglingReferences removes any dangling references from the index. |
| 163 | // If any have a tag that points to a manifest not in the index, it will remove it. |
| 164 | func cleanDanglingReferences(ii v1.ImageIndex) (v1.ImageIndex, error) { |
| 165 | var ( |
| 166 | digests = make(map[v1.Hash]bool) |
| 167 | toRemove []v1.Hash |
| 168 | ) |
| 169 | // first, record all of the valid digests |
| 170 | existingManifests, err := ii.IndexManifest() |
| 171 | if err != nil { |
| 172 | return nil, fmt.Errorf("could not read index manifest: %v", err) |
| 173 | } |
| 174 | for _, m := range existingManifests.Manifests { |
| 175 | if m.Platform == nil || m.Platform.Architecture == "" || m.Platform.Architecture == unknown { |
| 176 | continue |
| 177 | } |
| 178 | digests[m.Digest] = true |
| 179 | } |
| 180 | // next make sure each item has a valid digest |
| 181 | for _, m := range existingManifests.Manifests { |
| 182 | if m.Platform == nil || m.Platform.Architecture != unknown || m.Platform.OS != unknown { |
| 183 | continue |
| 184 | } |
| 185 | // get the annotations |
| 186 | if m.Annotations == nil { |
| 187 | continue |
| 188 | } |
| 189 | |
| 190 | referenced, ok := m.Annotations[util.AnnotationDockerReferenceDigest] |
| 191 | if !ok || referenced == "" { |
| 192 | continue |
| 193 | } |
| 194 | // check if the referenced digest is in the index |
| 195 | digest, err := v1.NewHash(referenced) |
| 196 | if err != nil { |
| 197 | return nil, fmt.Errorf("could not parse hash %s: %v", referenced, err) |
| 198 | } |
| 199 | // if it is not in the index, we need to remove this manifest |
| 200 | if _, ok := digests[digest]; !ok { |
| 201 | toRemove = append(toRemove, m.Digest) |
| 202 | } |
| 203 | } |
| 204 | ii = mutate.RemoveManifests(ii, match.Digests(toRemove...)) |
| 205 | return ii, nil |
| 206 | } |
no outgoing calls