github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/package-server/apis/operators/packagemanifest.go (about)

     1  package operators
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	opregistry "github.com/operator-framework/operator-registry/pkg/registry"
     7  
     8  	operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
     9  )
    10  
    11  const (
    12  	// The yaml attribute that specifies the related images of the ClusterServiceVersion
    13  	relatedImages = "relatedImages"
    14  )
    15  
    16  // CreateCSVDescription creates a CSVDescription from a given CSV
    17  func CreateCSVDescription(csv *operatorsv1alpha1.ClusterServiceVersion, csvJSON string) CSVDescription {
    18  	desc := CSVDescription{
    19  		DisplayName: csv.Spec.DisplayName,
    20  		Version:     csv.Spec.Version,
    21  		Provider: AppLink{
    22  			Name: csv.Spec.Provider.Name,
    23  			URL:  csv.Spec.Provider.URL,
    24  		},
    25  		Annotations:     csv.GetAnnotations(),
    26  		LongDescription: csv.Spec.Description,
    27  		InstallModes:    csv.Spec.InstallModes,
    28  		CustomResourceDefinitions: operatorsv1alpha1.CustomResourceDefinitions{
    29  			Owned:    descriptionsForCRDs(csv.Spec.CustomResourceDefinitions.Owned),
    30  			Required: descriptionsForCRDs(csv.Spec.CustomResourceDefinitions.Required),
    31  		},
    32  		APIServiceDefinitions: operatorsv1alpha1.APIServiceDefinitions{
    33  			Owned:    descriptionsForAPIServices(csv.Spec.APIServiceDefinitions.Owned),
    34  			Required: descriptionsForAPIServices(csv.Spec.APIServiceDefinitions.Required),
    35  		},
    36  		NativeAPIs:     csv.Spec.NativeAPIs,
    37  		MinKubeVersion: csv.Spec.MinKubeVersion,
    38  		RelatedImages:  GetImages(csvJSON),
    39  		Keywords:       csv.Spec.Keywords,
    40  		Maturity:       csv.Spec.Maturity,
    41  	}
    42  
    43  	icons := make([]Icon, len(csv.Spec.Icon))
    44  	for i, icon := range csv.Spec.Icon {
    45  		icons[i] = Icon{
    46  			Base64Data: icon.Data,
    47  			Mediatype:  icon.MediaType,
    48  		}
    49  	}
    50  
    51  	if len(icons) > 0 {
    52  		desc.Icon = icons
    53  	}
    54  
    55  	desc.Links = make([]AppLink, len(csv.Spec.Links))
    56  	for i, link := range csv.Spec.Links {
    57  		desc.Links[i] = AppLink{
    58  			Name: link.Name,
    59  			URL:  link.URL,
    60  		}
    61  	}
    62  
    63  	desc.Maintainers = make([]Maintainer, len(csv.Spec.Maintainers))
    64  	for i, maintainer := range csv.Spec.Maintainers {
    65  		desc.Maintainers[i] = Maintainer{
    66  			Name:  maintainer.Name,
    67  			Email: maintainer.Email,
    68  		}
    69  	}
    70  
    71  	return desc
    72  }
    73  
    74  // descriptionsForCRDs filters certain fields from provided API descriptions to reduce response size.
    75  func descriptionsForCRDs(crds []operatorsv1alpha1.CRDDescription) []operatorsv1alpha1.CRDDescription {
    76  	descriptions := []operatorsv1alpha1.CRDDescription{}
    77  	for _, crd := range crds {
    78  		descriptions = append(descriptions, operatorsv1alpha1.CRDDescription{
    79  			Name:        crd.Name,
    80  			Version:     crd.Version,
    81  			Kind:        crd.Kind,
    82  			DisplayName: crd.DisplayName,
    83  			Description: crd.Description,
    84  		})
    85  	}
    86  	return descriptions
    87  }
    88  
    89  // descriptionsForAPIServices filters certain fields from provided API descriptions to reduce response size.
    90  func descriptionsForAPIServices(apis []operatorsv1alpha1.APIServiceDescription) []operatorsv1alpha1.APIServiceDescription {
    91  	descriptions := []operatorsv1alpha1.APIServiceDescription{}
    92  	for _, api := range apis {
    93  		descriptions = append(descriptions, operatorsv1alpha1.APIServiceDescription{
    94  			Name:        api.Name,
    95  			Group:       api.Group,
    96  			Version:     api.Version,
    97  			Kind:        api.Kind,
    98  			DisplayName: api.DisplayName,
    99  			Description: api.Description,
   100  		})
   101  	}
   102  	return descriptions
   103  }
   104  
   105  // GetImages returns a list of images listed in CSV (spec and deployments)
   106  func GetImages(csvJSON string) []string {
   107  	var images []string
   108  
   109  	csv := &opregistry.ClusterServiceVersion{}
   110  	err := json.Unmarshal([]byte(csvJSON), &csv)
   111  	if err != nil {
   112  		return images
   113  	}
   114  
   115  	imageSet, err := csv.GetOperatorImages()
   116  	if err != nil {
   117  		return images
   118  	}
   119  
   120  	relatedImgSet, err := csv.GetRelatedImages()
   121  	if err != nil {
   122  		return images
   123  	}
   124  
   125  	for k := range relatedImgSet {
   126  		imageSet[k] = struct{}{}
   127  	}
   128  
   129  	for k := range imageSet {
   130  		images = append(images, k)
   131  	}
   132  
   133  	return images
   134  }