@@ -10,6 +10,10 @@ import (
10
10
log "github.com/sirupsen/logrus"
11
11
12
12
"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"
13
17
)
14
18
15
19
const (
@@ -93,34 +97,51 @@ func GenerateFunc(directory, packageName, channels, channelDefault string, overw
93
97
return nil
94
98
}
95
99
96
- // GenerateFunc determines mediatype from files (yaml) in given directory
100
+ // GetMediaType determines mediatype from files (yaml) in given directory
97
101
// Currently able to detect helm chart, registry+v1 (CSV) and plain k8s resources
98
102
// such as CRD.
99
103
func GetMediaType (directory string ) (string , error ) {
100
104
var files []string
105
+ k8sFiles := make (map [string ]* unstructured.Unstructured )
101
106
102
107
// Read all file names in directory
103
108
items , _ := ioutil .ReadDir (directory )
104
109
for _ , item := range items {
105
110
if item .IsDir () {
106
111
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
109
126
}
110
127
}
111
128
112
129
if len (files ) == 0 {
113
130
return "" , fmt .Errorf ("The directory %s contains no yaml files" , directory )
114
131
}
115
132
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
+ }
124
145
}
125
146
}
126
147
@@ -167,7 +188,7 @@ func ValidateAnnotations(existing, expected []byte) error {
167
188
return nil
168
189
}
169
190
170
- // ValidateAnnotations validates provided default channel to ensure it exists in
191
+ // ValidateChannelDefault validates provided default channel to ensure it exists in
171
192
// provided channel list.
172
193
func ValidateChannelDefault (channels , channelDefault string ) (string , error ) {
173
194
var chanDefault string
0 commit comments