zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/extensions/scrub/scrub.go (about)

     1  //go:build scrub
     2  // +build scrub
     3  
     4  package scrub
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"path"
    10  
    11  	"zotregistry.dev/zot/pkg/log"
    12  	"zotregistry.dev/zot/pkg/storage"
    13  	storageTypes "zotregistry.dev/zot/pkg/storage/types"
    14  )
    15  
    16  // Scrub Extension for repo...
    17  func RunScrubRepo(ctx context.Context, imgStore storageTypes.ImageStore, repo string, log log.Logger) error {
    18  	execMsg := fmt.Sprintf("executing scrub to check manifest/blob integrity for %s", path.Join(imgStore.RootDir(), repo))
    19  	log.Info().Msg(execMsg)
    20  
    21  	results, err := storage.CheckRepo(ctx, repo, imgStore)
    22  	if err != nil {
    23  		errMessage := fmt.Sprintf("failed to run scrub for %s", path.Join(imgStore.RootDir(), repo))
    24  		log.Error().Err(err).Msg(errMessage)
    25  		log.Info().Msg(fmt.Sprintf("scrub unsuccessfully completed for %s", path.Join(imgStore.RootDir(), repo)))
    26  
    27  		return err
    28  	}
    29  
    30  	for _, result := range results {
    31  		if result.Status == "ok" {
    32  			log.Info().
    33  				Str("image", result.ImageName).
    34  				Str("tag", result.Tag).
    35  				Str("status", result.Status).
    36  				Str("component", "scrub").
    37  				Msg("blobs/manifest ok")
    38  		} else {
    39  			log.Warn().
    40  				Str("image", result.ImageName).
    41  				Str("tag", result.Tag).
    42  				Str("status", result.Status).
    43  				Str("affected blob", result.AffectedBlob).
    44  				Str("error", result.Error).
    45  				Str("component", "scrub").
    46  				Msg("blobs/manifest affected")
    47  		}
    48  	}
    49  
    50  	log.Info().Msg(fmt.Sprintf("scrub successfully completed for %s", path.Join(imgStore.RootDir(), repo)))
    51  
    52  	return nil
    53  }
    54  
    55  type Task struct {
    56  	imgStore storageTypes.ImageStore
    57  	repo     string
    58  	log      log.Logger
    59  }
    60  
    61  func NewTask(imgStore storageTypes.ImageStore, repo string, log log.Logger) *Task {
    62  	return &Task{imgStore, repo, log}
    63  }
    64  
    65  func (scrubT *Task) DoWork(ctx context.Context) error {
    66  	return RunScrubRepo(ctx, scrubT.imgStore, scrubT.repo, scrubT.log) //nolint: contextcheck
    67  }
    68  
    69  func (scrubT *Task) String() string {
    70  	return fmt.Sprintf("{taskGenerator: \"%s\", repo: \"%s\"}",
    71  		"image scrub", // description of generator's task purpose
    72  		scrubT.repo)
    73  }
    74  
    75  func (scrubT *Task) Name() string {
    76  	return "ScrubTask"
    77  }