-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathrender.go
More file actions
204 lines (179 loc) · 5.96 KB
/
Copy pathrender.go
File metadata and controls
204 lines (179 loc) · 5.96 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
package kubernetes
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
sigsyaml "sigs.k8s.io/yaml"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/pkg/data"
"github.com/cloudposse/atmos/pkg/provisioner/target"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
u "github.com/cloudposse/atmos/pkg/utils"
)
// fileNameSep separates the parts of a generated manifest file name.
const fileNameSep = "_"
type renderOptions struct {
Output string
OutputDir string
Split bool
AtmosConfig *schema.AtmosConfiguration
}
func resolveRenderOptions(flags map[string]any, componentSection map[string]any) renderOptions {
options := renderOptionsFromComponent(componentSection)
if value, ok := flags["output"].(string); ok && value != "" {
options.Output = value
options.OutputDir = ""
// --output is single-file mode; clear any component-configured split so it
// does not fail validation ("--split requires --output-dir").
options.Split = false
}
if value, ok := flags["output_dir"].(string); ok && value != "" {
options.OutputDir = value
options.Output = ""
}
if value, ok := flags["split"].(bool); ok && value {
options.Split = true
}
return options
}
func renderOptionsFromComponent(componentSection map[string]any) renderOptions {
renderSection, ok := componentSection["render"].(map[string]any)
if !ok {
return renderOptions{}
}
outputSection, ok := renderSection["output"].(map[string]any)
if !ok {
return renderOptions{}
}
options := renderOptions{}
if split, ok := outputSection["split"].(bool); ok {
options.Split = split
}
if path, ok := outputSection["path"].(string); ok && path != "" {
if options.Split {
options.OutputDir = path
} else {
options.Output = path
}
}
return options
}
func renderObjects(objects []*unstructured.Unstructured, options renderOptions) error {
if err := validateRenderOptions(options); err != nil {
return err
}
return dispatchRender(objects, options)
}
// validateRenderOptions verifies the render output flag combination is valid.
func validateRenderOptions(options renderOptions) error {
if options.Output != "" && options.OutputDir != "" {
return errUtils.ErrKubernetesOutputDirMutuallyExclusive
}
if options.Output != "" && options.Split {
return errUtils.ErrKubernetesSplitRequiresOutputDir
}
if options.Split && options.OutputDir == "" {
return errUtils.ErrKubernetesSplitNeedsOutputDir
}
return nil
}
// dispatchRender writes the rendered objects according to the resolved output mode.
func dispatchRender(objects []*unstructured.Unstructured, options renderOptions) error {
switch {
case options.Output != "":
return writeSingleManifestFile(options.Output, objects)
case options.OutputDir != "" && options.Split:
return writeSplitManifestFiles(options.OutputDir, objects)
case options.OutputDir != "":
return writeSingleManifestFile(filepath.Join(options.OutputDir, "manifest.yaml"), objects)
default:
manifests, renderErr := multiDocumentYAML(objects)
if renderErr != nil {
return renderErr
}
return writeRenderedManifestStdout(manifests, options.AtmosConfig)
}
}
func writeRenderedManifestStdout(manifests []byte, atmosConfig *schema.AtmosConfiguration) error {
output := string(manifests)
if highlighted, err := u.HighlightCodeWithConfig(atmosConfig, output, "yaml"); err == nil {
output = highlighted
}
return data.Write(output)
}
func writeSingleManifestFile(path string, objects []*unstructured.Unstructured) error {
if err := os.MkdirAll(filepath.Dir(path), dirPerm); err != nil {
return fmt.Errorf("%w: creating output directory for %q: %w", errUtils.ErrKubernetesRenderOutput, path, err)
}
manifests, err := multiDocumentYAML(objects)
if err != nil {
return err
}
if err := os.WriteFile(path, manifests, filePerm); err != nil {
return fmt.Errorf("%w: writing to %q: %w", errUtils.ErrKubernetesRenderOutput, path, err)
}
ui.Infof("rendered %d Kubernetes object(s) to %s", len(objects), path)
return nil
}
func writeSplitManifestFiles(outputDir string, objects []*unstructured.Unstructured) error {
if err := os.MkdirAll(outputDir, dirPerm); err != nil {
return fmt.Errorf("%w: creating output directory %q: %w", errUtils.ErrKubernetesRenderOutput, outputDir, err)
}
for i, obj := range objects {
path := filepath.Join(outputDir, objectManifestFileName(i, obj))
manifest, err := objectYAML(obj)
if err != nil {
return err
}
if err := os.WriteFile(path, manifest, filePerm); err != nil {
return fmt.Errorf("%w: writing manifest %q: %w", errUtils.ErrKubernetesRenderOutput, path, err)
}
}
ui.Infof("rendered %d Kubernetes object(s) to %s", len(objects), outputDir)
return nil
}
func multiDocumentYAML(objects []*unstructured.Unstructured) ([]byte, error) {
docs := make([][]byte, 0, len(objects))
for _, obj := range objects {
manifest, err := objectYAML(obj)
if err != nil {
return nil, err
}
docs = append(docs, manifest)
}
return target.MergeYAMLDocuments(docs), nil
}
func objectYAML(obj *unstructured.Unstructured) ([]byte, error) {
data, err := sigsyaml.Marshal(obj.Object)
if err != nil {
return nil, fmt.Errorf("%w: %s/%s: %w", errUtils.ErrKubernetesRender, obj.GetKind(), obj.GetName(), err)
}
return data, nil
}
func objectManifestFileName(index int, obj *unstructured.Unstructured) string {
parts := []string{
fmt.Sprintf("%03d", index+1),
obj.GroupVersionKind().Group,
obj.GetAPIVersion(),
obj.GetKind(),
obj.GetNamespace(),
obj.GetName(),
}
nonEmpty := make([]string, 0, len(parts))
for _, part := range parts {
if part != "" {
nonEmpty = append(nonEmpty, sanitizeFileNamePart(part))
}
}
return strings.Join(nonEmpty, fileNameSep) + ".yaml"
}
var invalidFileNameChars = regexp.MustCompile(`[^A-Za-z0-9_.-]+`)
func sanitizeFileNamePart(value string) string {
value = strings.ReplaceAll(value, "/", "_")
value = invalidFileNameChars.ReplaceAllString(value, "_")
return strings.Trim(value, "_")
}