zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/cli/server/validate_sync_enabled.go (about)

     1  //go:build sync
     2  // +build sync
     3  
     4  package server
     5  
     6  import (
     7  	"path"
     8  
     9  	"zotregistry.io/zot/pkg/api/config"
    10  	syncconf "zotregistry.io/zot/pkg/extensions/config/sync"
    11  	"zotregistry.io/zot/pkg/extensions/sync"
    12  	zlog "zotregistry.io/zot/pkg/log"
    13  )
    14  
    15  func validateRetentionSyncOverlaps(config *config.Config, content syncconf.Content, urls []string, log zlog.Logger) {
    16  	cm := sync.NewContentManager([]syncconf.Content{content}, log)
    17  
    18  	prefix := content.Prefix
    19  	if content.Destination != "" {
    20  		prefix = cm.GetRepoDestination(content.Prefix)
    21  	}
    22  
    23  	repoPolicy := getRepoPolicyByPrefix(config, prefix)
    24  	if repoPolicy == nil {
    25  		return
    26  	}
    27  
    28  	if content.Tags != nil && content.Tags.Regex != nil {
    29  		areTagsRetained := false
    30  
    31  		for _, tagPolicy := range repoPolicy.KeepTags {
    32  			for _, tagRegex := range tagPolicy.Patterns {
    33  				if tagRegex == *content.Tags.Regex {
    34  					areTagsRetained = true
    35  				}
    36  			}
    37  		}
    38  
    39  		if !areTagsRetained {
    40  			log.Warn().Str("repositories pattern", prefix).
    41  				Str("tags regex", *content.Tags.Regex).
    42  				Interface("sync urls", urls).
    43  				Interface("overlapping sync content", content).
    44  				Interface("overlapping repo policy", repoPolicy).
    45  				Msgf("retention policy can overlap with the sync config, "+
    46  					"make sure retention doesn't remove syncing images with next tag regex: %s", *content.Tags.Regex)
    47  		}
    48  	} else {
    49  		log.Warn().Str("repositories pattern", prefix).
    50  			Interface("sync urls", urls).
    51  			Interface("overlapping sync content", content).
    52  			Interface("overlapping repo policy", repoPolicy).
    53  			Msg("retention policy can overlap with the sync config, make sure retention doesn't remove syncing images")
    54  	}
    55  }
    56  
    57  func getRepoPolicyByPrefixFromStorageConfig(config config.StorageConfig, subpath string, prefix string,
    58  ) *config.RetentionPolicy {
    59  	for _, repoPolicy := range config.Retention.Policies {
    60  		for _, repo := range repoPolicy.Repositories {
    61  			if subpath != "" {
    62  				repo = path.Join(subpath, repo)[1:] // remove startin '/'
    63  			}
    64  
    65  			if repo == prefix {
    66  				return &repoPolicy
    67  			}
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func getRepoPolicyByPrefix(config *config.Config, prefix string) *config.RetentionPolicy {
    75  	if repoPolicy := getRepoPolicyByPrefixFromStorageConfig(config.Storage.StorageConfig, "", prefix); repoPolicy != nil {
    76  		return repoPolicy
    77  	}
    78  
    79  	for subpath, subpathConfig := range config.Storage.SubPaths {
    80  		if repoPolicy := getRepoPolicyByPrefixFromStorageConfig(subpathConfig, subpath, prefix); repoPolicy != nil {
    81  			return repoPolicy
    82  		}
    83  	}
    84  
    85  	return nil
    86  }