-
Notifications
You must be signed in to change notification settings - Fork 248
/
generate.go
450 lines (384 loc) · 13.5 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package bundle
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)
const (
DefaultPermission = 0644
RegistryV1Type = "registry+v1"
PlainType = "plain"
HelmType = "helm"
AnnotationsFile = "annotations.yaml"
DockerFile = "bundle.Dockerfile"
ManifestsDir = "manifests/"
MetadataDir = "metadata/"
ManifestsLabel = "operators.operatorframework.io.bundle.manifests.v1"
MetadataLabel = "operators.operatorframework.io.bundle.metadata.v1"
MediatypeLabel = "operators.operatorframework.io.bundle.mediatype.v1"
PackageLabel = "operators.operatorframework.io.bundle.package.v1"
ChannelsLabel = "operators.operatorframework.io.bundle.channels.v1"
ChannelDefaultLabel = "operators.operatorframework.io.bundle.channel.default.v1"
)
type AnnotationMetadata struct {
Annotations map[string]string `yaml:"annotations" json:"annotations"`
}
// GenerateFunc builds annotations.yaml with mediatype, manifests &
// metadata directories in bundle image, package name, channels and default
// channels information and then writes the file to `/metadata` directory.
// Inputs:
// @directory: The local directory where bundle manifests and metadata are located
// @outputDir: Optional generated path where the /manifests and /metadata directories are copied
// as they would appear on the bundle image
// @packageName: The name of the package that bundle image belongs to
// @channels: The list of channels that bundle image belongs to
// @channelDefault: The default channel for the bundle image
// @overwrite: Boolean flag to enable overwriting annotations.yaml locally if existed
func GenerateFunc(directory, outputDir, packageName, channels, channelDefault string, overwrite bool) error {
// clean the input so that we know the absolute paths of input directories
directory, err := filepath.Abs(directory)
if err != nil {
return err
}
if outputDir != "" {
outputDir, err = filepath.Abs(outputDir)
if err != nil {
return err
}
}
_, err = os.Stat(directory)
if os.IsNotExist(err) {
return err
}
// Determine mediaType
mediaType, err := GetMediaType(directory)
if err != nil {
return err
}
// Get directory context for file output
workingDir, err := os.Getwd()
if err != nil {
return err
}
// Channels and packageName are required fields where as default channel is automatically filled if unspecified
// and that either of the required field is missing. We are interpreting the bundle information through
// bundle directory embedded in the package folder.
if channels == "" || packageName == "" {
var notProvided []string
if channels == "" {
notProvided = append(notProvided, "channels")
}
if packageName == "" {
notProvided = append(notProvided, "package name")
}
log.Infof("Bundle %s information not provided, inferring from parent package directory",
strings.Join(notProvided, " and "))
i, err := NewBundleDirInterperter(directory)
if err != nil {
return fmt.Errorf("please manually input channels and packageName, "+
"error interpreting bundle from directory %s, %v", directory, err)
}
if channels == "" {
channels = strings.Join(i.GetBundleChannels(), ",")
if channels == "" {
return fmt.Errorf("error interpreting channels, please manually input channels instead")
}
log.Infof("Inferred channels: %s", channels)
}
if packageName == "" {
packageName = i.GetPackageName()
log.Infof("Inferred package name: %s", packageName)
}
if channelDefault == "" {
channelDefault = i.GetDefaultChannel()
log.Infof("Inferred default channel: %s", channelDefault)
}
}
log.Info("Building annotations.yaml")
// Generate annotations.yaml
content, err := GenerateAnnotations(mediaType, ManifestsDir, MetadataDir, packageName, channels, channelDefault)
if err != nil {
return err
}
// Push the output yaml content to the correct directory and conditionally copy the manifest dir
outManifestDir, outMetadataDir, err := CopyYamlOutput(content, directory, outputDir, workingDir, overwrite)
if err != nil {
return err
}
log.Info("Building Dockerfile")
// Generate Dockerfile
content, err = GenerateDockerfile(mediaType, ManifestsDir, MetadataDir, outManifestDir, outMetadataDir, workingDir, packageName, channels, channelDefault)
if err != nil {
return err
}
_, err = os.Stat(filepath.Join(workingDir, DockerFile))
if os.IsNotExist(err) || overwrite {
err = WriteFile(DockerFile, workingDir, content)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
log.Infof("A bundle.Dockerfile already exists in current working directory: %s", workingDir)
}
return nil
}
// CopyYamlOutput takes the generated annotations yaml and writes it to disk.
// If an outputDir is specified, it will copy the input manifests
// It returns two strings. resultMetadata is the path to the output metadata/ folder.
// resultManifests is the path to the output manifests/ folder -- if no copy occured,
// it just returns the input manifestDir
func CopyYamlOutput(annotationsContent []byte, manifestDir, outputDir, workingDir string, overwrite bool) (resultManifests, resultMetadata string, err error) {
// First, determine the parent directory of the metadata and manifest directories
copyDir := ""
// If an output directory is not defined defined, generate metadata folder into the same parent dir as existing manifest dir
if outputDir == "" {
copyDir = filepath.Dir(manifestDir)
resultManifests = manifestDir
} else { // otherwise copy the manifests into $outputDir/manifests and create the annotations file in $outputDir/metadata
copyDir = outputDir
log.Info("Generating output manifests directory")
resultManifests = filepath.Join(copyDir, "/manifests/")
// copy the manifest directory into $pwd/manifests/
err := copyManifestDir(manifestDir, resultManifests, overwrite)
if err != nil {
return "", "", err
}
}
// Now, generate the `metadata/` dir and write the annotations
file, err := ioutil.ReadFile(filepath.Join(copyDir, MetadataDir, AnnotationsFile))
if os.IsNotExist(err) || overwrite {
writeDir := filepath.Join(copyDir, MetadataDir)
err = WriteFile(AnnotationsFile, writeDir, annotationsContent)
if err != nil {
return "", "", err
}
} else if err != nil {
return "", "", err
} else {
log.Infof("An annotations.yaml already exists in the directory: %s", MetadataDir)
if err = ValidateAnnotations(file, annotationsContent); err != nil {
return "", "", err
}
}
resultMetadata = filepath.Join(copyDir, "metadata")
return resultManifests, resultMetadata, nil
}
// GetMediaType determines mediatype from files (yaml) in given directory
// Currently able to detect helm chart, registry+v1 (CSV) and plain k8s resources
// such as CRD.
func GetMediaType(directory string) (string, error) {
var files []string
k8sFiles := make(map[string]*unstructured.Unstructured)
// Read all file names in directory
items, _ := ioutil.ReadDir(directory)
for _, item := range items {
if item.IsDir() {
continue
}
files = append(files, item.Name())
fileWithPath := filepath.Join(directory, item.Name())
fileBlob, err := ioutil.ReadFile(fileWithPath)
if err != nil {
return "", fmt.Errorf("Unable to read file %s in bundle", fileWithPath)
}
dec := k8syaml.NewYAMLOrJSONDecoder(strings.NewReader(string(fileBlob)), 10)
unst := &unstructured.Unstructured{}
if err := dec.Decode(unst); err == nil {
k8sFiles[item.Name()] = unst
}
}
if len(files) == 0 {
return "", fmt.Errorf("The directory %s contains no yaml files", directory)
}
// Validate if bundle is helm chart type
if _, err := IsChartDir(directory); err == nil {
return HelmType, nil
}
// Validate the files to determine media type
for _, fileName := range files {
// Check if one of the k8s files is a CSV
if k8sFile, ok := k8sFiles[fileName]; ok {
if k8sFile.GetObjectKind().GroupVersionKind().Kind == "ClusterServiceVersion" {
return RegistryV1Type, nil
}
}
}
return PlainType, nil
}
// ValidateAnnotations validates existing annotations.yaml against generated
// annotations.yaml to ensure existing annotations.yaml contains expected values.
func ValidateAnnotations(existing, expected []byte) error {
var fileAnnotations AnnotationMetadata
var expectedAnnotations AnnotationMetadata
log.Info("Validating existing annotations.yaml")
err := yaml.Unmarshal(existing, &fileAnnotations)
if err != nil {
log.Errorf("Unable to parse existing annotations.yaml")
return err
}
err = yaml.Unmarshal(expected, &expectedAnnotations)
if err != nil {
log.Errorf("Unable to parse expected annotations.yaml")
return err
}
// Ensure each expected annotation key and value exist in existing.
var errs []error
for label, item := range expectedAnnotations.Annotations {
value, hasAnnotation := fileAnnotations.Annotations[label]
if !hasAnnotation {
errs = append(errs, fmt.Errorf("Missing field: %s", label))
continue
}
if item != value {
errs = append(errs, fmt.Errorf("Expect field %q to have value %q instead of %q",
label, item, value))
}
}
return utilerrors.NewAggregate(errs)
}
// GenerateAnnotations builds annotations.yaml with mediatype, manifests &
// metadata directories in bundle image, package name, channels and default
// channels information.
func GenerateAnnotations(mediaType, manifests, metadata, packageName, channels, channelDefault string) ([]byte, error) {
annotations := &AnnotationMetadata{
Annotations: map[string]string{
MediatypeLabel: mediaType,
ManifestsLabel: manifests,
MetadataLabel: metadata,
PackageLabel: packageName,
ChannelsLabel: channels,
},
}
// Only add defaultChannel annotation if present
if channelDefault != "" {
annotations.Annotations[ChannelDefaultLabel] = channelDefault
}
afile, err := yaml.Marshal(annotations)
if err != nil {
return nil, err
}
return afile, nil
}
// GenerateDockerfile builds Dockerfile with mediatype, manifests &
// metadata directories in bundle image, package name, channels and default
// channels information in LABEL section.
func GenerateDockerfile(mediaType, manifests, metadata, copyManifestDir, copyMetadataDir, workingDir, packageName, channels, channelDefault string) ([]byte, error) {
var fileContent string
relativeManifestDirectory, err := filepath.Rel(workingDir, copyManifestDir)
if err != nil {
return nil, err
}
relativeManifestDirectory = filepath.ToSlash(relativeManifestDirectory)
relativeMetadataDirectory, err := filepath.Rel(workingDir, copyMetadataDir)
if err != nil {
return nil, err
}
relativeMetadataDirectory = filepath.ToSlash(relativeMetadataDirectory)
// FROM
fileContent += "FROM scratch\n\n"
// LABEL
fileContent += fmt.Sprintf("LABEL %s=%s\n", MediatypeLabel, mediaType)
fileContent += fmt.Sprintf("LABEL %s=%s\n", ManifestsLabel, manifests)
fileContent += fmt.Sprintf("LABEL %s=%s\n", MetadataLabel, metadata)
fileContent += fmt.Sprintf("LABEL %s=%s\n", PackageLabel, packageName)
fileContent += fmt.Sprintf("LABEL %s=%s\n", ChannelsLabel, channels)
// Only add defaultChannel annotation if present
if channelDefault != "" {
fileContent += fmt.Sprintf("LABEL %s=%s\n\n", ChannelDefaultLabel, channelDefault)
}
// CONTENT
fileContent += fmt.Sprintf("COPY %s %s\n", relativeManifestDirectory, "/manifests/")
fileContent += fmt.Sprintf("COPY %s %s\n", relativeMetadataDirectory, "/metadata/")
return []byte(fileContent), nil
}
// WriteFile writes `fileName` file with `content` into a `directory`
// Note: Will overwrite the existing `fileName` file if it exists
func WriteFile(fileName, directory string, content []byte) error {
if _, err := os.Stat(directory); os.IsNotExist(err) {
err := os.MkdirAll(directory, os.ModePerm)
if err != nil {
return err
}
}
log.Infof("Writing %s in %s", fileName, directory)
err := ioutil.WriteFile(filepath.Join(directory, fileName), content, DefaultPermission)
if err != nil {
return err
}
return nil
}
// copy the contents of a potentially nested manifest dir into an output dir.
func copyManifestDir(from, to string, overwrite bool) error {
fromFiles, err := ioutil.ReadDir(from)
if err != nil {
return err
}
if _, err := os.Stat(to); os.IsNotExist(err) {
if err = os.MkdirAll(to, os.ModePerm); err != nil {
return err
}
}
for _, fromFile := range fromFiles {
if fromFile.IsDir() {
nestedTo := filepath.Join(to, filepath.Base(from))
nestedFrom := filepath.Join(from, fromFile.Name())
err = copyManifestDir(nestedFrom, nestedTo, overwrite)
if err != nil {
return err
}
continue
}
contents, err := os.Open(filepath.Join(from, fromFile.Name()))
if err != nil {
return err
}
defer func() {
if err := contents.Close(); err != nil {
log.Fatal(err)
}
}()
toFilePath := filepath.Join(to, fromFile.Name())
_, err = os.Stat(toFilePath)
if err == nil && !overwrite {
continue
} else if err != nil && !os.IsNotExist(err) {
return err
}
toFile, err := os.Create(toFilePath)
if err != nil {
return err
}
defer func() {
if err := toFile.Close(); err != nil {
log.Fatal(err)
}
}()
_, err = io.Copy(toFile, contents)
if err != nil {
return err
}
err = os.Chmod(toFilePath, fromFile.Mode())
if err != nil {
return err
}
}
return nil
}
func containsString(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}