-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.go
119 lines (98 loc) · 3.61 KB
/
build.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
// Copyright 2024 Stefan Prodan.
// SPDX-License-Identifier: AGPL-3.0
package builder
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"sort"
"github.com/fluxcd/pkg/kustomize"
"github.com/fluxcd/pkg/ssa"
ssautil "github.com/fluxcd/pkg/ssa/utils"
"github.com/opencontainers/go-digest"
cp "github.com/otiai10/copy"
)
// Build copies the source directory to a temporary directory, generates the
// required manifests and runs kustomize to build the final resources.
// The function returns a slice of unstructured objects.
func Build(srcDir, tmpDir string, options Options) (*Result, error) {
if err := cp.Copy(srcDir, tmpDir); err != nil {
return nil, err
}
if err := generate(tmpDir, options); err != nil {
return nil, err
}
resources, err := kustomize.SecureBuild(tmpDir, tmpDir, false)
if err != nil {
return nil, err
}
data, err := resources.AsYaml()
if err != nil {
return nil, err
}
objects, err := ssautil.ReadObjects(bytes.NewReader(data))
if err != nil {
return nil, err
}
sort.Sort(ssa.SortableUnstructureds(objects))
d := digest.FromBytes(data)
return &Result{
Version: options.Version,
Objects: objects,
Digest: d.String(),
Revision: fmt.Sprintf("%s@%s", options.Version, d.String()),
ComponentImages: options.ComponentImages,
}, nil
}
func generate(base string, options Options) error {
if containsItemString(options.Components, options.NotificationController) {
options.EventsAddr = fmt.Sprintf("http://%s.%s.svc.%s./", options.NotificationController, options.Namespace, options.ClusterDomain)
}
if err := execTemplate(options, namespaceTmpl, path.Join(base, "namespace.yaml")); err != nil {
return fmt.Errorf("generate namespace failed: %w", err)
}
if err := execTemplate(options, annotationsTmpl, path.Join(base, "annotations.yaml")); err != nil {
return fmt.Errorf("generate annotations failed: %w", err)
}
if err := execTemplate(options, labelsTmpl, path.Join(base, "labels.yaml")); err != nil {
return fmt.Errorf("generate labels failed: %w", err)
}
if err := execTemplate(options, nodeSelectorTmpl, path.Join(base, "node-selector.yaml")); err != nil {
return fmt.Errorf("generate node selector failed: %w", err)
}
if options.ArtifactStorage != nil {
if err := execTemplate(options, pvcTmpl, path.Join(base, "pvc.yaml")); err != nil {
return fmt.Errorf("generate pvc failed: %w", err)
}
}
if options.Sync != nil {
if err := execTemplate(options, syncTmpl, path.Join(base, "sync.yaml")); err != nil {
return fmt.Errorf("generate sync failed: %w", err)
}
}
if err := execTemplate(options, kustomizationTmpl, path.Join(base, "kustomization.yaml")); err != nil {
return fmt.Errorf("generate kustomization failed: %w", err)
}
rbacFile := filepath.Join(base, "roles", "rbac.yaml")
if err := cp.Copy(filepath.Join(base, "rbac.yaml"), rbacFile); err != nil {
return fmt.Errorf("generate rbac failed: %w", err)
}
if err := execTemplate(options, kustomizationRolesTmpl, path.Join(base, "roles", "kustomization.yaml")); err != nil {
return fmt.Errorf("generate roles kustomization failed: %w", err)
}
// workaround for kustomize not being able to patch the SA in ClusterRoleBindings
defaultNS := MakeDefaultOptions().Namespace
if defaultNS != options.Namespace {
rbac, err := os.ReadFile(rbacFile)
if err != nil {
return fmt.Errorf("reading rbac file failed: %w", err)
}
rbac = bytes.ReplaceAll(rbac, []byte(defaultNS), []byte(options.Namespace))
if err := os.WriteFile(rbacFile, rbac, os.ModePerm); err != nil {
return fmt.Errorf("replacing service account namespace in rbac failed: %w", err)
}
}
return nil
}