Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions image-mapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ docker run -it --rm image-mapper ghcr.io/stakater/reloader:v1.4.1
# Or, pass a list of images from a text file
docker run -i --rm image-mapper -- - < images.txt
```

## Development

You can run integration tests against the actual catalog endpoint by setting
`IMAGE_MAPPER_RUN_INTEGRATION_TESTS=1`:

```
IMAGE_MAPPER_RUN_INTEGRATION_TESTS=1 go test ./...
```

This identifies regressions in the mapping logic or the catalog data by
recording known matches.
44 changes: 1 addition & 43 deletions image-mapper/internal/mapper/mapper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package mapper

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"slices"
"strings"

Expand Down Expand Up @@ -44,46 +41,6 @@ func NewMapper(ctx context.Context, opts ...Option) (*Mapper, error) {
return m, nil
}

// Repo describes a repo in the catalog
type Repo struct {
Name string `json:"name"`
CatalogTier string `json:"catalogTier"`
Aliases []string `json:"aliases"`
}

func listRepos(ctx context.Context) ([]Repo, error) {
c := &http.Client{}

buf := bytes.NewReader([]byte(`{"query":"query OrganizationImageCatalog($organization: ID!) {\n repos(filter: {uidp: {childrenOf: $organization}}) {\n name\n aliases\n catalogTier\n }\n}","variables":{"excludeDates":true,"excludeEpochs":true,"organization":"ce2d1984a010471142503340d670612d63ffb9f6"}}`))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://data.chainguard.dev/query?id=PrivateImageCatalog", buf)
if err != nil {
return nil, fmt.Errorf("constructing request: %w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "image-mapper")

resp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("making request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var data struct {
Data struct {
Repos []Repo `json:"repos"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, fmt.Errorf("unmarshaling body: %w", err)
}

return data.Data.Repos, nil
}

// MapAll returns mappings for all the images returned by the iterator
func (m *Mapper) MapAll(it Iterator) ([]*Mapping, error) {
mapped := make(map[string]struct{})
Expand Down Expand Up @@ -145,6 +102,7 @@ func (m *Mapper) Map(image string) (*Mapping, error) {
for match := range matches {
results = append(results, match)
}
slices.Sort(results)

return &Mapping{
Image: image,
Expand Down
Loading
Loading