Skip to content

Commit

Permalink
Remove workaround with RepoAuth option which tracks auth per repo
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Oct 21, 2023
1 parent 4ec4fc1 commit fb31e1d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
9 changes: 4 additions & 5 deletions pkg/domain/builder/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ type ImageConfig struct {
}

// NewRegistry generates a new regclient instance.
//
// NOTE: should generate a new instance for each image repository access,
// because it internally stores JWT scopes for each repository it accesses.
// Accessing large number of repositories with a single regclient instance
// will result in a bloating "Authorization" header.
func (c *ImageConfig) NewRegistry() *regclient.RegClient {
var opts []regclient.Opt

host := config.HostNewName(c.Registry.Scheme + "://" + c.Registry.Addr)
// RepoAuth should be set to true, because by default regclient internally merges scopes for all repositories
// it accesses, resulting in a bloating "Authorization" header when accessing large number of repositories at once.
// also see: https://distribution.github.io/distribution/spec/auth/jwt/
host.RepoAuth = true
if c.Registry.Username != "" {
host.User = c.Registry.Username
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/usecase/cleaner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ func NewService(
storage: storage,
}

r := c.image.NewRegistry()

ctx, cancel := context.WithCancel(context.Background())
c.start = func() {
go loop.Loop(ctx, func(ctx context.Context) {
start := time.Now()
err := c.pruneImages(ctx)
err := c.pruneImages(ctx, r)
if err != nil {
log.Errorf("failed to prune images: %+v", err)
return
Expand Down Expand Up @@ -89,14 +91,13 @@ func (c *cleanerService) Shutdown(_ context.Context) error {
return nil
}

func (c *cleanerService) pruneImages(ctx context.Context) error {
func (c *cleanerService) pruneImages(ctx context.Context, r *regclient.RegClient) error {
applications, err := c.appRepo.GetApplications(ctx, domain.GetApplicationCondition{DeployType: optional.From(domain.DeployTypeRuntime)})
if err != nil {
return err
}

for _, app := range applications {
r := c.image.NewRegistry()
err = c.pruneImage(ctx, r, app)
if err != nil {
log.Errorf("pruning image %v: %+v", c.image.NamePrefix+app.ID, err)
Expand Down

0 comments on commit fb31e1d

Please sign in to comment.