MCPcopy Create free account
hub / github.com/containers/image / SearchRegistry

Function SearchRegistry

docker/docker_client.go:322–451  ·  view source on GitHub ↗

SearchRegistry queries a registry for images that contain "image" in their name The limit is the max number of results desired Note: The limit value doesn't work with all registries for example registry.access.redhat.com returns all the results without limiting it to the limit value

(ctx context.Context, sys *types.SystemContext, registry, image string, limit int)

Source from the content-addressed store, hash-verified

320// Note: The limit value doesn't work with all registries
321// for example registry.access.redhat.com returns all the results without limiting it to the limit value
322func SearchRegistry(ctx context.Context, sys *types.SystemContext, registry, image string, limit int) ([]SearchResult, error) {
323 type V2Results struct {
324 // Repositories holds the results returned by the /v2/_catalog endpoint
325 Repositories []string `json:"repositories"`
326 }
327 type V1Results struct {
328 // Results holds the results returned by the /v1/search endpoint
329 Results []SearchResult `json:"results"`
330 }
331 v1Res := &V1Results{}
332
333 // Get credentials from authfile for the underlying hostname
334 // We can't use GetCredentialsForRef here because we want to search the whole registry.
335 auth, err := config.GetCredentials(sys, registry)
336 if err != nil {
337 return nil, fmt.Errorf("getting username and password: %w", err)
338 }
339
340 // The /v2/_catalog endpoint has been disabled for docker.io therefore
341 // the call made to that endpoint will fail. So using the v1 hostname
342 // for docker.io for simplicity of implementation and the fact that it
343 // returns search results.
344 hostname := registry
345 if registry == dockerHostname {
346 hostname = dockerV1Hostname
347 // A search term of library/foo does not find the library/foo image on the docker.io servers,
348 // which is surprising - and that Docker is modifying the search term client-side this same way,
349 // and it seems convenient to do the same thing.
350 // Read more here: https://github.com/containers/image/pull/2133#issue-1928524334
351 image = strings.TrimPrefix(image, "library/")
352 }
353
354 client, err := newDockerClient(sys, hostname, registry)
355 if err != nil {
356 return nil, fmt.Errorf("creating new docker client: %w", err)
357 }
358 defer client.Close()
359 client.auth = auth
360 if sys != nil {
361 client.registryToken = sys.DockerBearerRegistryToken
362 }
363
364 // Only try the v1 search endpoint if the search query is not empty. If it is
365 // empty skip to the v2 endpoint.
366 if image != "" {
367 // set up the query values for the v1 endpoint
368 u := url.URL{
369 Path: "/v1/search",
370 }
371 q := u.Query()
372 q.Set("q", image)
373 q.Set("n", strconv.Itoa(limit))
374 u.RawQuery = q.Encode()
375
376 logrus.Debugf("trying to talk to v1 search endpoint")
377 resp, err := client.makeRequest(ctx, http.MethodGet, u.String(), nil, nil, noAuth, nil)
378 if err != nil {
379 logrus.Debugf("error getting search results from v1 endpoint %q: %v", registry, err)

Callers

nothing calls this directly

Calls 9

GetCredentialsFunction · 0.92
httpResponseToErrorFunction · 0.85
ContainsMethod · 0.80
GetMethod · 0.80
newDockerClientFunction · 0.70
CloseMethod · 0.65
StringMethod · 0.65
makeRequestMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…