This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
kustomize.go
176 lines (151 loc) · 5.18 KB
/
kustomize.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
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kustomize
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/krusty"
kustomizeapi "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml"
"github.com/crossplaneio/resourcepacks/pkg/resource"
)
const (
defaultRootPath = "resources"
templateFileName = "kustomization.yaml.tmpl"
kustomizationFileName = "kustomization.yaml"
temporaryCRFileName = "cr.yaml"
errPatch = "patch call failed"
errOverlayPreparation = "overlay preparation failed"
errKustomizeCall = "kustomize call failed"
)
// KustomizeOption is used to manipulate default KustomizeEngine parameters.
type KustomizeOption func(*KustomizeEngine)
// WithResourcePath allows you to specify a kustomization folder other than default.
func WithResourcePath(path string) KustomizeOption {
return func(ko *KustomizeEngine) {
ko.ResourcePath = path
}
}
// AdditionalKustomizationPatcher allows you to append KustomizationPatcher objects
// to the patch pipeline.
func AdditionalKustomizationPatcher(op ...KustomizationPatcher) KustomizeOption {
return func(ko *KustomizeEngine) {
ko.Patcher = append(ko.Patcher, op...)
}
}
// NewKustomizeEngine returns a KustomizeEngine object. rootPath should
// point to the folder where your base kustomization.yaml resides and patcher
// is the chain of KustomizationPatcher that makes modifications of Kustomization
// object.
func NewKustomizeEngine(opt ...KustomizeOption) *KustomizeEngine {
ko := &KustomizeEngine{
ResourcePath: defaultRootPath,
Patcher: KustomizationPatcherChain{
NewNamePrefixer(),
NewLabelPropagator(),
NewVarReferenceFiller(),
},
}
for _, f := range opt {
f(ko)
}
return ko
}
type KustomizeEngine struct {
// ResourcePath is the folder that the base resources reside in the
// filesystem. It should be given as absolute path.
ResourcePath string
// Patcher contains the modifications that you'd like to make to
// the overlay Kustomization object before calling kustomize.
Patcher KustomizationPatcherChain
}
func (o *KustomizeEngine) Run(cr resource.ParentResource) ([]resource.ChildResource, error) {
k := &kustomizeapi.Kustomization{}
tmpl, err := ioutil.ReadFile(filepath.Join(o.ResourcePath, templateFileName))
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if !os.IsNotExist(err) {
if err := yaml.Unmarshal(tmpl, k); err != nil {
return nil, err
}
}
if err := o.Patcher.Patch(cr, k); err != nil {
return nil, errors.Wrap(err, errPatch)
}
dir, err := o.prepareOverlay(cr, k)
defer os.RemoveAll(dir)
if err != nil {
return nil, errors.Wrap(err, errOverlayPreparation)
}
kustomizer := krusty.MakeKustomizer(filesys.MakeFsOnDisk(), krusty.MakeDefaultOptions())
resMap, err := kustomizer.Run(dir)
if err != nil {
return nil, errors.Wrap(err, errKustomizeCall)
}
var objects []resource.ChildResource
for _, res := range resMap.Resources() {
u := &unstructured.Unstructured{}
// NOTE(muvaf): This is magic.
u.SetUnstructuredContent(res.Map())
// NOTE(muvaf): ParentResource is written to kustomization directory
// only to be used for value retrieval. We remove it from the render
// results here.
if u.GroupVersionKind() == cr.GetObjectKind().GroupVersionKind() {
continue
}
objects = append(objects, u)
}
return objects, nil
}
func (o *KustomizeEngine) prepareOverlay(cr resource.ParentResource, k *kustomizeapi.Kustomization) (string, error) {
// NOTE(muvaf): Kustomize does not work with symlinked paths, so, we're
// using their temp directory generation function that handles this instead
// of Golang's.
tempConfirmedDir, err := filesys.NewTmpConfirmedDir()
if err != nil {
return "", err
}
tempDir := string(tempConfirmedDir)
crYAML, err := yaml.Marshal(cr)
if err != nil {
return "", err
}
if err := ioutil.WriteFile(filepath.Join(tempDir, temporaryCRFileName), crYAML, os.ModePerm); err != nil {
return "", err
}
// NOTE(muvaf): Kustomize doesn't work with absolute paths, all paths have
// to be relative to the root path of the folder where kustomize points to,
// which is the temporary directory we created.
absPath, err := filepath.Abs(o.ResourcePath)
if err != nil {
return "", err
}
relPath, err := filepath.Rel(tempDir, absPath)
if err != nil {
return "", err
}
k.Resources = append(k.Resources, []string{relPath, temporaryCRFileName}...)
yamlData, err := yaml.Marshal(k)
if err != nil {
return "", err
}
if err := ioutil.WriteFile(filepath.Join(tempDir, kustomizationFileName), yamlData, os.ModePerm); err != nil {
return "", err
}
return tempDir, nil
}