newImageSource creates a new ImageSource for the specified image reference. The caller must call .Close() on the returned ImageSource. The caller must ensure !ref.isUnknownDigest.
(ctx context.Context, sys *types.SystemContext, ref dockerReference)
| 54 | // The caller must call .Close() on the returned ImageSource. |
| 55 | // The caller must ensure !ref.isUnknownDigest. |
| 56 | func newImageSource(ctx context.Context, sys *types.SystemContext, ref dockerReference) (*dockerImageSource, error) { |
| 57 | if ref.isUnknownDigest { |
| 58 | return nil, fmt.Errorf("reading images from docker: reference %q without a tag or digest is not supported", ref.StringWithinTransport()) |
| 59 | } |
| 60 | |
| 61 | registryConfig, err := loadRegistryConfiguration(sys) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | registry, err := sysregistriesv2.FindRegistry(sys, ref.ref.Name()) |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("loading registries configuration: %w", err) |
| 68 | } |
| 69 | if registry == nil { |
| 70 | // No configuration was found for the provided reference, so use the |
| 71 | // equivalent of a default configuration. |
| 72 | registry = &sysregistriesv2.Registry{ |
| 73 | Endpoint: sysregistriesv2.Endpoint{ |
| 74 | Location: ref.ref.String(), |
| 75 | }, |
| 76 | Prefix: ref.ref.String(), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Check all endpoints for the manifest availability. If we find one that does |
| 81 | // contain the image, it will be used for all future pull actions. Always try the |
| 82 | // non-mirror original location last; this both transparently handles the case |
| 83 | // of no mirrors configured, and ensures we return the error encountered when |
| 84 | // accessing the upstream location if all endpoints fail. |
| 85 | pullSources, err := registry.PullSourcesFromReference(ref.ref) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | type attempt struct { |
| 90 | ref reference.Named |
| 91 | err error |
| 92 | } |
| 93 | attempts := []attempt{} |
| 94 | for _, pullSource := range pullSources { |
| 95 | if sys != nil && sys.DockerLogMirrorChoice { |
| 96 | logrus.Infof("Trying to access %q", pullSource.Reference) |
| 97 | } else { |
| 98 | logrus.Debugf("Trying to access %q", pullSource.Reference) |
| 99 | } |
| 100 | s, err := newImageSourceAttempt(ctx, sys, ref, pullSource, registryConfig) |
| 101 | if err == nil { |
| 102 | return s, nil |
| 103 | } |
| 104 | logrus.Debugf("Accessing %q failed: %v", pullSource.Reference, err) |
| 105 | attempts = append(attempts, attempt{ |
| 106 | ref: pullSource.Reference, |
| 107 | err: err, |
| 108 | }) |
| 109 | } |
| 110 | switch len(attempts) { |
| 111 | case 0: |
| 112 | return nil, errors.New("Internal error: newImageSource returned without trying any endpoint") |
| 113 | case 1: |
nothing calls this directly
no test coverage detected
searching dependent graphs…