@@ -10,6 +10,10 @@ import (
1010 log "github.com/sirupsen/logrus"
1111
1212 "gopkg.in/yaml.v2"
13+ "helm.sh/helm/v3/pkg/chartutil"
14+
15+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
16+ k8syaml "k8s.io/apimachinery/pkg/util/yaml"
1317)
1418
1519const (
@@ -93,34 +97,51 @@ func GenerateFunc(directory, packageName, channels, channelDefault string, overw
9397 return nil
9498}
9599
96- // GenerateFunc determines mediatype from files (yaml) in given directory
100+ // GetMediaType determines mediatype from files (yaml) in given directory
97101// Currently able to detect helm chart, registry+v1 (CSV) and plain k8s resources
98102// such as CRD.
99103func GetMediaType (directory string ) (string , error ) {
100104 var files []string
105+ k8sFiles := make (map [string ]* unstructured.Unstructured )
101106
102107 // Read all file names in directory
103108 items , _ := ioutil .ReadDir (directory )
104109 for _ , item := range items {
105110 if item .IsDir () {
106111 continue
107- } else if filepath .Ext (item .Name ()) == ".yaml" {
108- files = append (files , item .Name ())
112+ }
113+
114+ files = append (files , item .Name ())
115+
116+ fileWithPath := filepath .Join (directory , item .Name ())
117+ fileBlob , err := ioutil .ReadFile (fileWithPath )
118+ if err != nil {
119+ return "" , fmt .Errorf ("Unable to read file %s in bundle" , fileWithPath )
120+ }
121+
122+ dec := k8syaml .NewYAMLOrJSONDecoder (strings .NewReader (string (fileBlob )), 10 )
123+ unst := & unstructured.Unstructured {}
124+ if err := dec .Decode (unst ); err == nil {
125+ k8sFiles [item .Name ()] = unst
109126 }
110127 }
111128
112129 if len (files ) == 0 {
113130 return "" , fmt .Errorf ("The directory %s contains no yaml files" , directory )
114131 }
115132
116- // Validate the file names to determine media type
117- for _ , file := range files {
118- if file == "Chart.yaml" {
119- return HelmType , nil
120- } else if strings .HasSuffix (file , "clusterserviceversion.yaml" ) {
121- return RegistryV1Type , nil
122- } else {
123- continue
133+ // Validate if bundle is helm chart type
134+ if _ , err := chartutil .IsChartDir (directory ); err == nil {
135+ return HelmType , nil
136+ }
137+
138+ // Validate the files to determine media type
139+ for _ , fileName := range files {
140+ // Check if one of the k8s files is a CSV
141+ if k8sFile , ok := k8sFiles [fileName ]; ok {
142+ if k8sFile .GetObjectKind ().GroupVersionKind ().Kind == "ClusterServiceVersion" {
143+ return RegistryV1Type , nil
144+ }
124145 }
125146 }
126147
@@ -167,7 +188,7 @@ func ValidateAnnotations(existing, expected []byte) error {
167188 return nil
168189}
169190
170- // ValidateAnnotations validates provided default channel to ensure it exists in
191+ // ValidateChannelDefault validates provided default channel to ensure it exists in
171192// provided channel list.
172193func ValidateChannelDefault (channels , channelDefault string ) (string , error ) {
173194 var chanDefault string
0 commit comments