zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/extensions/search/convert/annotations.go (about)

     1  package convert
     2  
     3  import (
     4  	ispec "github.com/opencontainers/image-spec/specs-go/v1"
     5  )
     6  
     7  const (
     8  	// See https://github.com/opencontainers/image-spec/blob/main/annotations.md#back-compatibility-with-label-schema
     9  	AnnotationLabels             = "org.label-schema.labels"
    10  	LabelAnnotationCreated       = "org.label-schema.build-date"
    11  	LabelAnnotationVendor        = "org.label-schema.vendor"
    12  	LabelAnnotationDescription   = "org.label-schema.description"
    13  	LabelAnnotationLicenses      = "org.label-schema.license"
    14  	LabelAnnotationTitle         = "org.label-schema.name"
    15  	LabelAnnotationDocumentation = "org.label-schema.usage"
    16  	LabelAnnotationSource        = "org.label-schema.vcs-url"
    17  )
    18  
    19  type ImageAnnotations struct {
    20  	Description   string
    21  	Licenses      string
    22  	Title         string
    23  	Documentation string
    24  	Source        string
    25  	Labels        string
    26  	Vendor        string
    27  	Authors       string
    28  }
    29  
    30  /*
    31  	OCI annotation/label with backwards compatibility
    32  
    33  arg can be either labels or annotations
    34  https://github.com/opencontainers/image-spec/blob/main/annotations.md.
    35  */
    36  func GetAnnotationValue(annotations map[string]string, annotationKey, labelKey string) string {
    37  	value, ok := annotations[annotationKey]
    38  	if !ok || value == "" {
    39  		value, ok = annotations[labelKey]
    40  		if !ok {
    41  			value = ""
    42  		}
    43  	}
    44  
    45  	return value
    46  }
    47  
    48  func GetDescription(annotations map[string]string) string {
    49  	return GetAnnotationValue(annotations, ispec.AnnotationDescription, LabelAnnotationDescription)
    50  }
    51  
    52  func GetLicenses(annotations map[string]string) string {
    53  	return GetAnnotationValue(annotations, ispec.AnnotationLicenses, LabelAnnotationLicenses)
    54  }
    55  
    56  func GetVendor(annotations map[string]string) string {
    57  	return GetAnnotationValue(annotations, ispec.AnnotationVendor, LabelAnnotationVendor)
    58  }
    59  
    60  func GetAuthors(annotations map[string]string) string {
    61  	authors := annotations[ispec.AnnotationAuthors]
    62  
    63  	return authors
    64  }
    65  
    66  func GetTitle(annotations map[string]string) string {
    67  	return GetAnnotationValue(annotations, ispec.AnnotationTitle, LabelAnnotationTitle)
    68  }
    69  
    70  func GetDocumentation(annotations map[string]string) string {
    71  	return GetAnnotationValue(annotations, ispec.AnnotationDocumentation, LabelAnnotationDocumentation)
    72  }
    73  
    74  func GetSource(annotations map[string]string) string {
    75  	return GetAnnotationValue(annotations, ispec.AnnotationSource, LabelAnnotationSource)
    76  }
    77  
    78  func GetCategories(labels map[string]string) string {
    79  	categories := labels[AnnotationLabels]
    80  
    81  	return categories
    82  }
    83  
    84  func GetAnnotations(annotations, labels map[string]string) ImageAnnotations {
    85  	description := GetDescription(annotations)
    86  	if description == "" {
    87  		description = GetDescription(labels)
    88  	}
    89  
    90  	title := GetTitle(annotations)
    91  	if title == "" {
    92  		title = GetTitle(labels)
    93  	}
    94  
    95  	documentation := GetDocumentation(annotations)
    96  	if documentation == "" {
    97  		documentation = GetDocumentation(labels)
    98  	}
    99  
   100  	source := GetSource(annotations)
   101  	if source == "" {
   102  		source = GetSource(labels)
   103  	}
   104  
   105  	licenses := GetLicenses(annotations)
   106  	if licenses == "" {
   107  		licenses = GetLicenses(labels)
   108  	}
   109  
   110  	categories := GetCategories(annotations)
   111  	if categories == "" {
   112  		categories = GetCategories(labels)
   113  	}
   114  
   115  	vendor := GetVendor(annotations)
   116  	if vendor == "" {
   117  		vendor = GetVendor(labels)
   118  	}
   119  
   120  	authors := GetAuthors(annotations)
   121  	if authors == "" {
   122  		authors = GetAuthors(labels)
   123  	}
   124  
   125  	return ImageAnnotations{
   126  		Description:   description,
   127  		Title:         title,
   128  		Documentation: documentation,
   129  		Source:        source,
   130  		Licenses:      licenses,
   131  		Labels:        categories,
   132  		Vendor:        vendor,
   133  		Authors:       authors,
   134  	}
   135  }
   136  
   137  func GetIndexAnnotations(
   138  	indexAnnotations map[string]string,
   139  	annotationsFromManifest *ImageAnnotations,
   140  ) ImageAnnotations {
   141  	description := GetDescription(indexAnnotations)
   142  	if description == "" {
   143  		description = annotationsFromManifest.Description
   144  	}
   145  
   146  	title := GetTitle(indexAnnotations)
   147  	if title == "" {
   148  		title = annotationsFromManifest.Title
   149  	}
   150  
   151  	documentation := GetDocumentation(indexAnnotations)
   152  	if documentation == "" {
   153  		documentation = annotationsFromManifest.Documentation
   154  	}
   155  
   156  	source := GetSource(indexAnnotations)
   157  	if source == "" {
   158  		source = annotationsFromManifest.Source
   159  	}
   160  
   161  	licenses := GetLicenses(indexAnnotations)
   162  	if licenses == "" {
   163  		licenses = annotationsFromManifest.Licenses
   164  	}
   165  
   166  	categories := GetCategories(indexAnnotations)
   167  	if categories == "" {
   168  		categories = annotationsFromManifest.Labels
   169  	}
   170  
   171  	vendor := GetVendor(indexAnnotations)
   172  	if vendor == "" {
   173  		vendor = annotationsFromManifest.Vendor
   174  	}
   175  
   176  	authors := GetAuthors(indexAnnotations)
   177  	if authors == "" {
   178  		authors = annotationsFromManifest.Authors
   179  	}
   180  
   181  	return ImageAnnotations{
   182  		Description:   description,
   183  		Title:         title,
   184  		Documentation: documentation,
   185  		Source:        source,
   186  		Licenses:      licenses,
   187  		Labels:        categories,
   188  		Vendor:        vendor,
   189  		Authors:       authors,
   190  	}
   191  }