-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbaker.go
More file actions
1112 lines (981 loc) · 29.8 KB
/
Copy pathbaker.go
File metadata and controls
1112 lines (981 loc) · 29.8 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package carvel
import (
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/pivotal-cf/kiln/internal/carvel/models"
"github.com/pivotal-cf/kiln/internal/component"
"github.com/pivotal-cf/kiln/pkg/cargo"
"github.com/pivotal-cf/kiln/pkg/proofing"
"github.com/hashicorp/go-version"
"gopkg.in/yaml.v3"
)
type BakeOptions struct {
SkipFetch bool
ReleasesDirectory string
}
// Baker transforms an imgpkg bundle and tile metadata into a BOSH release
// and kiln-compatible tile structure that can be baked into a .pivotal file.
type Baker interface {
Bake(source string, kilnfile cargo.Kilnfile, kilnfileLock cargo.KilnfileLock, opts BakeOptions) error
BakeFromLockfile(source string, kilnfile cargo.Kilnfile, kilnfileLock cargo.KilnfileLock, releaseLock cargo.BOSHReleaseTarballLock, localTarball string, opts BakeOptions) error
KilnBake(destination string) error
ParseMetadata(source string) error
GetName() string
// GetVersion returns the product version from base.yml or the version file.
GetVersion() (string, error)
// GetReleaseVersion returns the BOSH release version, which includes a
// content fingerprint suffix (e.g., "10.4.0+a1b2c3d4e5f6"). Only valid
// after Bake() or BakeFromLockfile() has been called.
GetReleaseVersion() string
GetReleaseTarball() (string, error)
SetWriter(w io.Writer)
SetProgressWriter(w io.Writer)
}
// NewBaker creates a new Baker for transforming imgpkg bundles into BOSH releases.
func NewBaker() Baker {
return &baker{
writer: io.Discard,
progressWriter: io.Discard,
}
}
type baker struct {
metadata models.Metadata
source, destination string
releaseVersion string
writer io.Writer
progressWriter io.Writer
}
func (b *baker) KilnBake(destination string) error {
b.progress("Assembling final .pivotal file...")
cmd := exec.Command("kiln",
"bake",
"--skip-fetch",
"--output-file", destination,
)
cmd.Dir = b.destination
out, err := cmd.CombinedOutput()
b.log(string(out))
if err != nil {
b.log("failed to invoke kiln: " + string(out))
return err
}
return nil
}
func (b *baker) Bake(source string, kilnfile cargo.Kilnfile, kilnfileLock cargo.KilnfileLock, opts BakeOptions) error {
b.source = source
b.destination = path.Join(source, ".carvel-tile")
b.progress("Reading tile metadata from " + path.Join(source, "base.yml"))
yamlPath := path.Join(source, "base.yml")
yamlData, err := os.ReadFile(yamlPath)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlData, &b.metadata)
if err != nil {
return err
}
if err := validateVariables(b.metadata.Variables); err != nil {
return err
}
ver, err := b.GetVersion()
if err != nil {
return err
}
b.progress(fmt.Sprintf("Tile: %s version %s (metadata_version %s)", b.metadata.Name, ver, b.metadata.MetadataVersion))
metadataVersion, err := version.NewVersion(b.metadata.MetadataVersion)
if err != nil {
return err
}
minVersion, _ := version.NewVersion("3.2.0")
if metadataVersion.LessThan(minVersion) {
return errors.New("tile metadata_version too old for kubernetes support (must be >=3.2.0)")
}
b.progress("Generating BOSH release structure...")
err = b.generateBoshReleaseDir()
if err != nil {
b.log(err.Error())
return err
}
b.progress("Generating tile layout in " + b.destination)
err = b.generateOutputTile(kilnfile, kilnfileLock, opts)
if err != nil {
b.log(err.Error())
return err
}
return nil
}
func (b *baker) BakeFromLockfile(source string, kilnfile cargo.Kilnfile, kilnfileLock cargo.KilnfileLock, releaseLock cargo.BOSHReleaseTarballLock, localTarball string, opts BakeOptions) error {
b.source = source
b.destination = path.Join(source, ".carvel-tile")
b.progress("Reading tile metadata from " + path.Join(source, "base.yml"))
yamlPath := path.Join(source, "base.yml")
yamlData, err := os.ReadFile(yamlPath)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlData, &b.metadata)
if err != nil {
return err
}
if err := validateVariables(b.metadata.Variables); err != nil {
return err
}
ver, err := b.GetVersion()
if err != nil {
return err
}
b.progress(fmt.Sprintf("Tile: %s version %s (metadata_version %s)", b.metadata.Name, ver, b.metadata.MetadataVersion))
if releaseLock.Name != b.metadata.Name {
return fmt.Errorf("lockfile release name %q does not match tile name %q", releaseLock.Name, b.metadata.Name)
}
b.releaseVersion = releaseLock.Version
err = os.RemoveAll(b.destination)
if err != nil {
return err
}
err = os.MkdirAll(b.destination, 0755)
if err != nil {
return err
}
b.progress("Generating tile layout in " + b.destination)
err = b.generateBaseYaml()
if err != nil {
return err
}
err = b.copyFiles()
if err != nil {
return err
}
err = b.generateJobFiles()
if err != nil {
return err
}
err = b.generateInstanceGroupFiles()
if err != nil {
return err
}
err = b.generateRuntimeConfigs()
if err != nil {
return err
}
releasesDir := path.Join(b.destination, "releases")
err = os.MkdirAll(releasesDir, 0755)
if err != nil {
return err
}
destTarball := path.Join(releasesDir, b.metadata.Name+"-"+releaseLock.Version+".tgz")
b.progress("Copying cached BOSH release from " + localTarball)
b.log("copying cached BOSH release from " + localTarball)
err = copyFileContents(localTarball, destTarball)
if err != nil {
return fmt.Errorf("failed to copy cached release tarball: %w", err)
}
// We also need to fetch any additional releases when baking from lockfile,
// otherwise the final .pivotal assembly will fail because it looks for them.
if len(b.metadata.AdditionalReleases) > 0 {
b.progress(" Fetching additional BOSH releases")
err = b.fetchAdditionalReleases(kilnfile, kilnfileLock, opts)
if err != nil {
return err
}
}
return nil
}
func (b *baker) GetReleaseTarball() (string, error) {
if b.releaseVersion == "" {
return "", fmt.Errorf("release version not set -- call Bake() or BakeFromLockfile() first")
}
tarball := path.Join(b.destination, "releases", b.metadata.Name+"-"+b.releaseVersion+".tgz")
if _, err := os.Stat(tarball); err != nil {
return "", fmt.Errorf("release tarball not found at %s: %w", tarball, err)
}
return tarball, nil
}
func (b *baker) ParseMetadata(source string) error {
b.source = source
baseYMLPath := path.Join(source, "base.yml")
raw, err := os.ReadFile(baseYMLPath)
if err != nil {
return fmt.Errorf("failed to read base.yml: %w", err)
}
if err := yaml.Unmarshal(raw, &b.metadata); err != nil {
return fmt.Errorf("failed to parse base.yml: %w", err)
}
return nil
}
func (b *baker) GetName() string {
return b.metadata.Name
}
func (b *baker) hookJobName(hookName string) string {
prefix := b.metadata.Name + "-"
if strings.HasPrefix(hookName, prefix) {
return hookName
}
return prefix + hookName
}
func (b *baker) GetReleaseVersion() string {
return b.releaseVersion
}
func (b *baker) GetVersion() (string, error) {
re := regexp.MustCompile(`\s+`)
// Replace all occurrences of whitespace with an empty string
versionNoSpace := re.ReplaceAllString(b.metadata.ProductVersion, "")
if versionNoSpace != `$(version)` {
return versionNoSpace, nil
} else {
// find the version from a "version" file
version, err := os.ReadFile(path.Join(b.source, "version"))
return strings.Trim(string(version), " \t\n\r"), err
}
}
func (b *baker) SetWriter(w io.Writer) {
b.writer = w
}
func (b *baker) SetProgressWriter(w io.Writer) {
b.progressWriter = w
}
func (b *baker) log(message string) {
_, _ = fmt.Fprintln(b.writer, message)
}
func (b *baker) progress(message string) {
_, _ = fmt.Fprintln(b.progressWriter, message)
}
// deduplicateConsumes removes duplicate BOSH link consumer entries by name.
// Identical duplicates are dropped silently. If two entries share a name but
// differ in type or optional, the first is kept and a WARNING is emitted —
// BOSH rejects duplicate link names in job.MF, so the second is always ignored.
func (b *baker) deduplicateConsumes(consumes []boshLinkConsumer) []boshLinkConsumer {
seen := make(map[string]boshLinkConsumer)
var deduped []boshLinkConsumer
for _, c := range consumes {
existing, ok := seen[c.Name]
if !ok {
seen[c.Name] = c
deduped = append(deduped, c)
continue
}
if existing != c {
b.progress(fmt.Sprintf(
"WARNING: duplicate BOSH link consumer name %q found across packageinstalls.\n"+
" Keeping: {type: %s, optional: %v}\n"+
" Ignoring: {type: %s, optional: %v}\n"+
" Ensure all packageinstalls agree on the link definition.",
c.Name, existing.Type, existing.Optional, c.Type, c.Optional,
))
}
}
return deduped
}
// boshLinkConsumer declares a BOSH link the registry-data job should consume.
// Populated from per-packageinstall *.job-spec-overlay.yml sidecar files.
type boshLinkConsumer struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Optional bool `yaml:"optional"`
}
// jobSpecOverlay is the schema for <entry>.job-spec-overlay.yml sidecar files.
// kiln reads these from packageinstalls/ and merges the consumes entries into
// the generated registry-data job.MF alongside the hardcoded cluster-info link.
type jobSpecOverlay struct {
Consumes []boshLinkConsumer `yaml:"consumes"`
}
func validateVariables(vars []proofing.Variable) error {
var errs []error
for i, v := range vars {
if v.Name == "" {
errs = append(errs, fmt.Errorf("variables[%d]: missing required field 'name'", i))
} else if v.Type == "" {
errs = append(errs, fmt.Errorf("variables[%d] (%q): missing required field 'type'", i, v.Name))
}
}
return errors.Join(errs...)
}
func (b *baker) generateBoshReleaseDir() error {
dirName := path.Join(b.source, ".boshrelease")
err := os.RemoveAll(dirName)
if err != nil {
return err
}
b.progress(" Initializing BOSH release")
commands := []*exec.Cmd{
exec.Command("bosh", "init-release", "--dir="+dirName),
exec.Command("bosh", "add-blob", "--dir="+dirName, path.Join(b.source, "bundle.tar"), "imgpkg/bundle.tar"),
exec.Command("bosh", "generate-package", "--dir="+dirName, "registry-data"),
exec.Command("bosh", "generate-job", "--dir="+dirName, "registry-data"),
}
for _, cmd := range commands {
b.log("executing " + cmd.String())
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
b.log("output: " + string(out))
}
// Now populate the specs for packages and jobs
fileContents := map[string]string{
"packages/registry-data/packaging": `set -eu
mkdir -p ${BOSH_INSTALL_TARGET}/imgpkg
cp imgpkg/*.tar ${BOSH_INSTALL_TARGET}/imgpkg
`,
"packages/registry-data/spec": `---
name: registry-data
dependencies: []
files:
- imgpkg/bundle.tar
`,
}
for outpath, contents := range fileContents {
err = os.WriteFile(path.Join(dirName, outpath), []byte(contents), 0644)
if err != nil {
return err
}
}
registryDataTemplates := ""
registryDataProperties := ""
var allConsumes []boshLinkConsumer
b.progress(" Configuring package installs")
for _, entry := range b.metadata.PackageInstalls {
entry = strings.Trim(entry, "$() ")
entry = strings.TrimPrefix(entry, "package")
entry = strings.Trim(entry, `"' `)
b.progress(" - " + entry)
b.log("looking for package install: " + entry)
// find this entry in the packageinstalls directory
matches, err := filepath.Glob(path.Join(b.source, "packageinstalls/*.yml"))
if err != nil {
return err
}
for _, match := range matches {
yamlData, err := os.ReadFile(match)
if err != nil {
return err
}
var pi models.PackageInstall
err = yaml.Unmarshal(yamlData, &pi)
if err != nil {
return err
}
if pi.Name != entry {
continue
}
b.log("found " + pi.Name + " at " + match)
}
registryDataTemplates += fmt.Sprintf(" packageinstalls/%s.yml.erb: packageinstalls/%s.yml\n", entry, entry)
registryDataProperties += " " + entry + ":\n"
registryDataProperties += ` name:
description: "package name"
version:
description: "package version"
values:
description: "values.yml contents"
`
if err = os.MkdirAll(path.Join(dirName, "jobs", "registry-data", "templates", "packageinstalls"), 0755); err != nil {
return err
}
// Read optional values-overlay ERB file alongside the packageinstall YAML.
overlayContent := ""
overlayData, overlayErr := os.ReadFile(path.Join(b.source, "packageinstalls", entry+".values-overlay.erb"))
if overlayErr != nil {
if !errors.Is(overlayErr, os.ErrNotExist) {
return overlayErr
}
} else {
overlayContent = string(overlayData)
}
// Read optional job-spec-overlay sidecar to discover additional BOSH link consumptions.
jobSpecOverlayPath := path.Join(b.source, "packageinstalls", entry+".job-spec-overlay.yml")
overlayData, overlayErr = os.ReadFile(jobSpecOverlayPath)
if overlayErr != nil {
if !errors.Is(overlayErr, os.ErrNotExist) {
return overlayErr
}
} else {
var overlay jobSpecOverlay
if parseErr := yaml.Unmarshal(overlayData, &overlay); parseErr != nil {
return fmt.Errorf("parsing %s: %w", jobSpecOverlayPath, parseErr)
}
allConsumes = append(allConsumes, overlay.Consumes...)
}
manifestTemplate := generateManifestTemplate(entry, overlayContent)
err = os.WriteFile(
path.Join(dirName, "jobs", "registry-data", "templates", "packageinstalls", entry+".yml.erb"),
[]byte(manifestTemplate),
0644,
)
if err != nil {
return err
}
}
deduped := b.deduplicateConsumes(allConsumes)
registryDataSpec, err := buildRegistryDataSpec(registryDataTemplates, registryDataProperties, deduped)
if err != nil {
return err
}
err = os.WriteFile(path.Join(dirName, "jobs", "registry-data", "spec"), []byte(registryDataSpec), 0644)
if err != nil {
return err
}
type hookModeGroup struct {
mode string
hooks []models.HookDeclaration
}
for _, group := range []hookModeGroup{
{mode: "pre-install", hooks: b.metadata.PreInstallHooks},
{mode: "post-install", hooks: b.metadata.PostInstallHooks},
} {
for _, hook := range group.hooks {
if hook.Name == "" || hook.Command == "" {
return fmt.Errorf("%s hook declaration missing name or command", group.mode)
}
jobName := b.hookJobName(hook.Name)
genCmd := exec.Command("bosh", "generate-job", "--dir="+dirName, jobName)
b.log("executing " + genCmd.String())
out, err := genCmd.CombinedOutput()
b.log("output: " + string(out))
if err != nil {
return err
}
templateName := "hooks-" + group.mode + ".erb"
jobSpec := fmt.Sprintf(`---
name: %s
templates:
%s: bin/hooks/%s
packages: []
properties: {}
`, jobName, templateName, group.mode)
if err = os.WriteFile(path.Join(dirName, "jobs", jobName, "spec"), []byte(jobSpec), 0644); err != nil {
return err
}
if err = os.MkdirAll(path.Join(dirName, "jobs", jobName, "templates"), 0755); err != nil {
return err
}
templateContent := fmt.Sprintf("#!/bin/bash\nset -euo pipefail\nexec %s\n", hook.Command)
err = os.WriteFile(
path.Join(dirName, "jobs", jobName, "templates", templateName),
[]byte(templateContent),
0644,
)
if err != nil {
return err
}
}
}
return nil
}
// buildRegistryDataSpec constructs the job.MF content for the registry-data BOSH job.
// It always includes the hardcoded cluster-info link and appends any additional links
// collected from *.job-spec-overlay.yml sidecars in the packageinstalls/ directory.
func buildRegistryDataSpec(templates, properties string, additionalLinks []boshLinkConsumer) (string, error) {
extraLinks := ""
if len(additionalLinks) > 0 {
data, err := yaml.Marshal(additionalLinks)
if err != nil {
return "", err
}
extraLinks = string(data)
}
return `---
name: registry-data
templates:
` + templates + `packages:
- registry-data
consumes:
- name: cluster
type: cluster-info
optional: true
` + extraLinks + `properties:
` + properties, nil
}
// generateManifestTemplate produces the ERB template for the registry-data BOSH job.
// overlayContent is optional ERB code injected into the values manipulation block
// before YAML.dump(values) is called, enabling BOSH link-based value overrides.
func generateManifestTemplate(entry, overlayContent string) string {
return `---
apiVersion: v1
kind: ServiceAccount
metadata:
name: <%= p("` + entry + `.name") %>-sa
namespace: <%= link("cluster").p("content-namespace") rescue "default" %>
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: <%= p("` + entry + `.name") %>-sa-cluster-role
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: <%= p("` + entry + `.name") %>-sa-cluster-role-binding
subjects:
- kind: ServiceAccount
name: <%= p("` + entry + `.name") %>-sa
namespace: <%= link("cluster").p("content-namespace") rescue "default" %>
roleRef:
kind: ClusterRole
name: <%= p("` + entry + `.name") %>-sa-cluster-role
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: Secret
metadata:
name: <%= p("` + entry + `.name") %>-values
namespace: <%= link("cluster").p("content-namespace") rescue "default" %>
type: Opaque
stringData:
values.yaml: |
<% require 'yaml' %>
<%
values = p("` + entry + `.values")
values = YAML.load(values) if values.is_a?(String)
# Inject namespace from BOSH link into context
if values.is_a?(Hash) && values["context"].is_a?(Hash)
values["context"]["namespace"] = link("cluster").p("content-namespace") rescue "default"
end
%>
` + overlayContent + `
<%= YAML.dump(values).split("\n").map { |line| " " + line }.join("\n") %>
---
apiVersion: packaging.carvel.dev/v1alpha1
kind: PackageInstall
metadata:
name: <%= p("` + entry + `.name") %>
namespace: <%= link("cluster").p("content-namespace") rescue "default" %>
spec:
serviceAccountName: <%= p("` + entry + `.name") %>-sa
packageRef:
refName: <%= p("` + entry + `.name") %>
versionSelection:
constraints: <%= p("` + entry + `.version") %>
values:
- secretRef:
name: <%= p("` + entry + `.name") %>-values
`
}
func (b *baker) generateOutputTile(kilnfile cargo.Kilnfile, kilnfileLock cargo.KilnfileLock, opts BakeOptions) error {
err := os.RemoveAll(b.destination)
if err != nil {
return err
}
err = os.MkdirAll(b.destination, 0755)
if err != nil {
return err
}
b.progress(" Generating base.yml")
err = b.generateBaseYaml()
if err != nil {
return err
}
b.progress(" Copying forms, properties, and static assets")
err = b.copyFiles()
if err != nil {
return err
}
err = b.generateJobFiles()
if err != nil {
return err
}
err = b.generateInstanceGroupFiles()
if err != nil {
return err
}
b.progress(" Generating runtime configs")
err = b.generateRuntimeConfigs()
if err != nil {
return err
}
if len(b.metadata.AdditionalReleases) > 0 {
b.progress(" Fetching additional BOSH releases")
err = b.fetchAdditionalReleases(kilnfile, kilnfileLock, opts)
if err != nil {
return err
}
}
b.progress(" Creating BOSH release tarball (this may take a while)...")
err = b.createBoshRelease()
if err != nil {
return err
}
return nil
}
func (b *baker) generateBaseYaml() error {
meta := models.MetadataOut{}
meta.Name = b.metadata.Name
meta.Label = b.metadata.Label
meta.IconImage = b.metadata.IconImage
meta.ProductVersion = b.metadata.ProductVersion
meta.MetadataVersion = b.metadata.MetadataVersion
meta.Rank = b.metadata.Rank
meta.Serial = b.metadata.Serial
meta.CompatibleKubernetesDistributions = b.metadata.CompatibleKubernetesDistributions
meta.FormTypes = b.metadata.FormTypes
meta.PropertyBlueprints = b.metadata.PropertyBlueprints
meta.Variables = b.metadata.Variables
meta.MinimumVersionForUpgrade = b.metadata.MinimumVersionForUpgrade
meta.RequiresKubernetes = true
// stemcell criteria are dummy data that OM will ignore when the tile is folded into
// TKR, but we need them as kiln inputs.
meta.StemcellCriteria.Os = "ubuntu-jammy"
meta.StemcellCriteria.Version = "1.446"
meta.InstanceGroups = []string{}
meta.RuntimeConfigs = []string{
`$( runtime_config "` + b.metadata.Name + `-pkgr" )`,
}
meta.Releases = []string{
`$( release "` + b.metadata.Name + `" )`,
}
for _, ar := range b.metadata.AdditionalReleases {
meta.Releases = append(meta.Releases, `$( release "`+ar.Name+`" )`)
}
yamlData, err := yaml.Marshal(&meta)
if err != nil {
return err
}
err = os.WriteFile(path.Join(b.destination, "base.yml"), yamlData, 0644) // 0644 sets file permissions
if err != nil {
return err
}
return nil
}
// copyfiles schleps all the files that we can collect from the source directory without
// modification:
// - variables, properties and forms
// - the icon file
// - the version file
func (b *baker) copyFiles() error {
for _, subdir := range []string{"bosh_variables", "forms", "properties"} {
info, err := os.Stat(path.Join(b.source, subdir))
if err == nil && info.IsDir() {
err = os.CopyFS(path.Join(b.destination, subdir), os.DirFS(path.Join(b.source, subdir)))
if err != nil {
return err
}
}
}
for _, fn := range []string{"icon.png", "version"} {
info, statErr := os.Stat(path.Join(b.source, fn))
if statErr == nil && !info.IsDir() {
if err := copyFileContents(path.Join(b.source, fn), path.Join(b.destination, fn)); err != nil {
return err
}
}
}
return nil
}
func (b *baker) generateRuntimeConfigs() error {
err := os.MkdirAll(path.Join(b.destination, "runtime_configs"), 0755)
if err != nil {
return err
}
registryDataProps := map[string]interface{}{}
// we need one PackageInstall for each entry in the metadata.
for _, entry := range b.metadata.PackageInstalls {
entry = strings.Trim(entry, "$() ")
entry = strings.TrimPrefix(entry, "package")
entry = strings.Trim(entry, `"' `)
// find this entry in the packageinstalls directory
matches, err := filepath.Glob(path.Join(b.source, "packageinstalls/*.yml"))
if err != nil {
return err
}
found := false
for _, match := range matches {
yamlData, err := os.ReadFile(match)
if err != nil {
return err
}
var pi models.PackageInstall
err = yaml.Unmarshal(yamlData, &pi)
if err != nil {
return err
}
if pi.Name != entry {
continue
}
found = true
registryDataProps[entry] = models.PackageInstallProps{
Name: pi.PackageName,
Version: pi.PackageVersion,
Values: pi.Values,
}
}
if !found {
return errors.New("package install not found: " + entry)
}
}
registryDataJob := models.Job{
Name: "registry-data",
Release: b.metadata.Name,
Properties: registryDataProps,
}
releases := []string{`$( release "` + b.metadata.Name + `" )`}
addonJobs := []models.Job{registryDataJob}
for _, hook := range append(
append([]models.HookDeclaration{}, b.metadata.PreInstallHooks...),
b.metadata.PostInstallHooks...,
) {
if hook.Name == "" || hook.Command == "" {
return fmt.Errorf("hook declaration missing name or command")
}
addonJobs = append(addonJobs, models.Job{Name: b.hookJobName(hook.Name), Release: b.metadata.Name})
}
for _, ar := range b.metadata.AdditionalReleases {
releases = append(releases, `$( release "`+ar.Name+`" )`)
for _, job := range ar.Jobs {
addonJobs = append(addonJobs, models.Job{
Name: job.Name,
Release: ar.Name,
Properties: job.Properties,
})
}
}
inner := models.RuntimeConfigInner{
Releases: releases,
Addons: []models.Addon{
{
Name: b.metadata.Name + "-pkgr",
Include: models.Inclusion{
Deployments: []string{
`(( ..` + b.metadata.Name + `.deployment_name ))`,
},
Jobs: []models.Job{
{Name: "install-package-repository", Release: "tanzu-content"},
{Name: "install-packages", Release: "tanzu-content"},
},
},
Jobs: addonJobs,
},
},
}
yamlData, err := yaml.Marshal(&inner)
if err != nil {
return err
}
rc := models.RuntimeConfigOuter{
Name: b.metadata.Name + "-pkgr",
RuntimeConfig: string(yamlData),
}
yamlData, err = yaml.Marshal(&rc)
if err != nil {
return err
}
err = os.WriteFile(path.Join(b.destination, "runtime_configs", b.metadata.Name+"-pkgr.yml"), yamlData, 0644)
if err != nil {
return err
}
return nil
}
// generateInstanceGroups creates an empty instance group folder
func (b *baker) generateInstanceGroupFiles() error {
err := os.MkdirAll(path.Join(b.destination, "instance_groups"), 0755)
if err != nil {
return err
}
return nil
}
// generateJobFiles creates an empty jobs folder
func (b *baker) generateJobFiles() error {
err := os.MkdirAll(path.Join(b.destination, "jobs"), 0755)
if err != nil {
return err
}
return nil
}
func (b *baker) fetchAdditionalReleases(kilnfile cargo.Kilnfile, kilnfileLock cargo.KilnfileLock, opts BakeOptions) error {
releasesDir := path.Join(b.destination, "releases")
if err := os.MkdirAll(releasesDir, 0755); err != nil {
return err
}
sources := component.NewReleaseSourceRepo(kilnfile)
for _, ar := range b.metadata.AdditionalReleases {
lockEntry, found := findReleaseLock(kilnfileLock, ar.Name)
if !found {
return fmt.Errorf("additional_releases entry %q not found in Kilnfile.lock — run `kiln fetch` or add it to Kilnfile", ar.Name)
}
dst := path.Join(releasesDir, ar.Name+"-"+lockEntry.Version+".tgz")
src := path.Join(opts.ReleasesDirectory, additionalReleaseLocalFilename(sources, ar.Name, lockEntry))
if opts.SkipFetch {
if _, err := os.Stat(src); err != nil {
return fmt.Errorf("release %q not found in %s and --skip-fetch was set: %w", ar.Name, opts.ReleasesDirectory, err)
}
if src != dst {
if err := copyFileContents(src, dst); err != nil {
return fmt.Errorf("failed to copy additional release %q: %w", ar.Name, err)
}
}
continue
}
if _, err := os.Stat(src); err == nil {
f, err := os.Open(src)
if err == nil {
h := sha1.New()
if _, err := io.Copy(h, f); err == nil {
actualSHA1 := hex.EncodeToString(h.Sum(nil))
if lockEntry.SHA1 == "" || actualSHA1 == lockEntry.SHA1 {
b.progress(fmt.Sprintf(" Release %s %s already exists locally with correct SHA1 — skipping fetch", lockEntry.Name, lockEntry.Version))
f.Close()
if src != dst {
if err := copyFileContents(src, dst); err != nil {
return fmt.Errorf("failed to copy additional release %q: %w", ar.Name, err)
}
}
continue
}
}
f.Close()
}
}
b.progress(fmt.Sprintf(" Fetching %s %s from %s", lockEntry.Name, lockEntry.Version, lockEntry.RemoteSource))
local, err := sources.DownloadRelease(opts.ReleasesDirectory, lockEntry)
if err != nil {
return fmt.Errorf("failed to download additional release %q: %w", ar.Name, err)
}
if lockEntry.SHA1 != "" && local.Lock.SHA1 != lockEntry.SHA1 {
return fmt.Errorf("downloaded release %q had incorrect SHA1 - expected %q, got %q", ar.Name, lockEntry.SHA1, local.Lock.SHA1)
}
if local.LocalPath != dst {
if err := copyFileContents(local.LocalPath, dst); err != nil {
return err
}
}
}
return nil
}
func additionalReleaseLocalFilename(sources component.ReleaseSourceList, name string, lockEntry cargo.BOSHReleaseTarballLock) string {
defaultName := name + "-" + lockEntry.Version + ".tgz"
if lockEntry.RemotePath == "" || lockEntry.RemoteSource == "" {
return defaultName
}
source, err := sources.FindByID(lockEntry.RemoteSource)
if err != nil {
return defaultName
}
switch source.Configuration().Type {
case cargo.BOSHReleaseTarballSourceTypeS3, cargo.BOSHReleaseTarballSourceTypeArtifactory:
return filepath.Base(lockEntry.RemotePath)
default:
return defaultName
}
}
func findReleaseLock(lock cargo.KilnfileLock, name string) (cargo.BOSHReleaseTarballLock, bool) {
for _, r := range lock.Releases {
if r.Name == name {
return r, true
}
}
return cargo.BOSHReleaseTarballLock{}, false
}
func (b *baker) createBoshRelease() error {