github.com/quay/claircore@v1.5.28/indexer/controller/checkmanifest.go (about)

     1  package controller
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/prometheus/client_golang/prometheus/promauto"
    10  	"github.com/quay/zlog"
    11  
    12  	"github.com/quay/claircore/indexer"
    13  )
    14  
    15  var scannedManifestCounter = promauto.NewCounterVec(
    16  	prometheus.CounterOpts{
    17  		Namespace: "claircore",
    18  		Subsystem: "indexer",
    19  		Name:      "scanned_manifests",
    20  		Help:      "Total number of scanned manifests.",
    21  	},
    22  	[]string{"scanned_before"},
    23  )
    24  
    25  func checkManifest(ctx context.Context, s *Controller) (State, error) {
    26  	// determine if we've seen this manifest and if we've
    27  	// scanned it with the desired scanners
    28  	ok, err := s.Store.ManifestScanned(ctx, s.manifest.Hash, s.Vscnrs)
    29  	if err != nil {
    30  		return Terminal, err
    31  	}
    32  
    33  	scannedManifestCounter.WithLabelValues(strconv.FormatBool(ok)).Add(1)
    34  
    35  	// if we haven't seen this manifest, determine which scanners to use, persist it
    36  	// and transition to FetchLayer state.
    37  	if !ok {
    38  		zlog.Info(ctx).Msg("manifest to be scanned")
    39  
    40  		// if a manifest was analyzed by a particular scanner we can
    41  		// omit it from this index, as all its comprising layers were analyzed
    42  		// by the particular scanner as well.
    43  		filtered := make(indexer.VersionedScanners, 0, len(s.Vscnrs))
    44  		for i := range s.Vscnrs {
    45  			ok, err := s.Store.ManifestScanned(ctx, s.manifest.Hash, s.Vscnrs[i:i+1]) // slice this to avoid allocations
    46  			if err != nil {
    47  				return Terminal, err
    48  			}
    49  			if !ok {
    50  				filtered = append(filtered, s.Vscnrs[i])
    51  			}
    52  		}
    53  		s.Vscnrs = filtered
    54  
    55  		err := s.Store.PersistManifest(ctx, *s.manifest)
    56  		if err != nil {
    57  			return Terminal, fmt.Errorf("failed to persist manifest: %w", err)
    58  		}
    59  		return FetchLayers, nil
    60  	}
    61  
    62  	// we have seen this manifest before and it's been been processed with the desired scanners
    63  	// retrieve the existing one and transition to Terminal.
    64  	zlog.Info(ctx).Msg("manifest already scanned")
    65  	sr, ok, err := s.Store.IndexReport(ctx, s.manifest.Hash)
    66  	if err != nil {
    67  		return Terminal, fmt.Errorf("failed to retrieve manifest: %w", err)
    68  	}
    69  	if !ok {
    70  		return Terminal, fmt.Errorf("failed to retrieve manifest: %w", err)
    71  	}
    72  	s.report = sr
    73  
    74  	return Terminal, nil
    75  }