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

     1  package convert
     2  
     3  import (
     4  	zcommon "zotregistry.dev/zot/pkg/common"
     5  	gql_gen "zotregistry.dev/zot/pkg/extensions/search/gql_generated"
     6  	mTypes "zotregistry.dev/zot/pkg/meta/types"
     7  )
     8  
     9  func ImgSumAcceptedByFilter(imageSummary *gql_gen.ImageSummary, filter mTypes.Filter) bool {
    10  	osFilters := strSliceFromRef(filter.Os)
    11  	archFilters := strSliceFromRef(filter.Arch)
    12  	platforms := getImagePlatforms(imageSummary)
    13  
    14  	platformMatchFound := len(platforms) == 0 && filter.Os == nil && filter.Arch == nil
    15  
    16  	for _, platform := range platforms {
    17  		osCheck := true
    18  
    19  		if len(osFilters) > 0 {
    20  			osCheck = platform.Os != nil && zcommon.ContainsStringIgnoreCase(osFilters, *platform.Os)
    21  		}
    22  
    23  		archCheck := true
    24  
    25  		if len(archFilters) > 0 {
    26  			archCheck = platform.Arch != nil && zcommon.ContainsStringIgnoreCase(archFilters, *platform.Arch)
    27  		}
    28  
    29  		if osCheck && archCheck {
    30  			platformMatchFound = true
    31  
    32  			break
    33  		}
    34  	}
    35  
    36  	if !platformMatchFound {
    37  		return false
    38  	}
    39  
    40  	if filter.HasToBeSigned != nil && *filter.HasToBeSigned && !*imageSummary.IsSigned {
    41  		return false
    42  	}
    43  
    44  	return true
    45  }
    46  
    47  func getImagePlatforms(imageSummary *gql_gen.ImageSummary) []*gql_gen.Platform {
    48  	platforms := []*gql_gen.Platform{}
    49  
    50  	for _, manifest := range imageSummary.Manifests {
    51  		if manifest.Platform != nil {
    52  			platforms = append(platforms, manifest.Platform)
    53  		}
    54  	}
    55  
    56  	return platforms
    57  }
    58  
    59  func RepoSumAcceptedByFilter(repoSummary *gql_gen.RepoSummary, filter mTypes.Filter) bool {
    60  	osFilters := strSliceFromRef(filter.Os)
    61  	archFilters := strSliceFromRef(filter.Arch)
    62  
    63  	platformMatchFound := len(repoSummary.Platforms) == 0 && filter.Os == nil && filter.Arch == nil
    64  
    65  	for _, platform := range repoSummary.Platforms {
    66  		osCheck := true
    67  
    68  		if len(osFilters) > 0 {
    69  			osCheck = platform.Os != nil && zcommon.ContainsStringIgnoreCase(osFilters, *platform.Os)
    70  		}
    71  
    72  		archCheck := true
    73  
    74  		if len(archFilters) > 0 {
    75  			archCheck = platform.Arch != nil && zcommon.ContainsStringIgnoreCase(archFilters, *platform.Arch)
    76  		}
    77  
    78  		if osCheck && archCheck {
    79  			platformMatchFound = true
    80  
    81  			break
    82  		}
    83  	}
    84  
    85  	if !platformMatchFound {
    86  		return false
    87  	}
    88  
    89  	if filter.HasToBeSigned != nil && *filter.HasToBeSigned && !*repoSummary.NewestImage.IsSigned {
    90  		return false
    91  	}
    92  
    93  	if filter.IsBookmarked != nil && *filter.IsBookmarked != *repoSummary.IsBookmarked {
    94  		return false
    95  	}
    96  
    97  	if filter.IsStarred != nil && *filter.IsStarred != *repoSummary.IsStarred {
    98  		return false
    99  	}
   100  
   101  	return true
   102  }
   103  
   104  func strSliceFromRef(slice []*string) []string {
   105  	resultSlice := make([]string, len(slice))
   106  
   107  	for i := range slice {
   108  		if slice[i] != nil {
   109  			resultSlice[i] = *slice[i]
   110  		}
   111  	}
   112  
   113  	return resultSlice
   114  }