@@ -45,7 +45,7 @@ import (
45
45
const (
46
46
inventoryTemplate = "inventory-template.yaml"
47
47
kptHydrated = ".kpt-hydrated"
48
- pipeline = ".pipeline "
48
+ tmpSinkDir = ".tmp-sink-dir "
49
49
kptFnAnnotation = "config.kubernetes.io/function"
50
50
kptFnLocalConfig = "config.kubernetes.io/local-config"
51
51
)
@@ -72,7 +72,12 @@ func NewDeployer(cfg types.Config, labels map[string]string) *Deployer {
72
72
// outputs them to the applyDir, and runs `kpt live apply` against applyDir to create resources in the cluster.
73
73
// `kpt live apply` supports automated pruning declaratively via resources in the applyDir.
74
74
func (k * Deployer ) Deploy (ctx context.Context , out io.Writer , builds []build.Artifact ) ([]string , error ) {
75
- manifests , err := k .renderManifests (ctx , out , builds )
75
+ flags , err := k .getKptFnRunArgs ()
76
+ if err != nil {
77
+ return []string {}, err
78
+ }
79
+
80
+ manifests , err := k .renderManifests (ctx , out , builds , flags )
76
81
if err != nil {
77
82
return nil , err
78
83
}
@@ -93,6 +98,12 @@ func (k *Deployer) Deploy(ctx context.Context, out io.Writer, builds []build.Art
93
98
}
94
99
95
100
manifest .Write (manifests .String (), filepath .Join (applyDir , "resources.yaml" ), out )
101
+ // Hydrated config is stored in applyDir, no need to dup the config in temporary sink dir.
102
+ if k .Fn .SinkDir == tmpSinkDir {
103
+ if err := os .RemoveAll (tmpSinkDir ); err != nil {
104
+ return nil , fmt .Errorf ("deleting temporary directory %s: %w" , k .Fn .SinkDir , err )
105
+ }
106
+ }
96
107
97
108
cmd := exec .CommandContext (ctx , "kpt" , kptCommandArgs (applyDir , []string {"live" , "apply" }, k .getKptLiveApplyArgs (), nil )... )
98
109
cmd .Stdout = out
@@ -149,7 +160,12 @@ func (k *Deployer) Cleanup(ctx context.Context, out io.Writer) error {
149
160
150
161
// Render hydrates manifests using both kustomization and kpt functions.
151
162
func (k * Deployer ) Render (ctx context.Context , out io.Writer , builds []build.Artifact , _ bool , filepath string ) error {
152
- manifests , err := k .renderManifests (ctx , out , builds )
163
+ flags , err := k .getKptFnRunArgs ()
164
+ if err != nil {
165
+ return err
166
+ }
167
+
168
+ manifests , err := k .renderManifests (ctx , out , builds , flags )
153
169
if err != nil {
154
170
return err
155
171
}
@@ -160,21 +176,21 @@ func (k *Deployer) Render(ctx context.Context, out io.Writer, builds []build.Art
160
176
// renderManifests handles a majority of the hydration process for manifests.
161
177
// This involves reading configs from a source directory, running kustomize build, running kpt pipelines,
162
178
// adding image digests, and adding run-id labels.
163
- func (k * Deployer ) renderManifests (ctx context.Context , _ io.Writer , builds []build.Artifact ) (manifest.ManifestList , error ) {
179
+ func (k * Deployer ) renderManifests (ctx context.Context , _ io.Writer , builds []build.Artifact ,
180
+ flags []string ) (manifest.ManifestList , error ) {
164
181
debugHelpersRegistry , err := config .GetDebugHelpersRegistry (k .globalConfig )
165
182
if err != nil {
166
183
return nil , fmt .Errorf ("retrieving debug helpers registry: %w" , err )
167
184
}
185
+ if k .Fn .SinkDir == "" {
186
+ k .Fn .SinkDir = tmpSinkDir
187
+ }
168
188
169
- // .pipeline is a temp dir used to store output between steps of the desired workflow
170
- // This can be removed once kpt can fully support the desired workflow independently.
171
- if err := os .RemoveAll (filepath .Join (pipeline , k .Dir )); err != nil {
172
- return nil , fmt .Errorf ("deleting temporary directory %s: %w" , filepath .Join (pipeline , k .Dir ), err )
189
+ if err := os .RemoveAll (filepath .Join (k .Fn .SinkDir , k .Dir )); err != nil {
190
+ return nil , fmt .Errorf ("deleting temporary directory %s: %w" , filepath .Join (k .Fn .SinkDir , k .Dir ), err )
173
191
}
174
- // 0755 is a permission setting where the owner can read, write, and execute.
175
- // Others can read and execute but not modify the directory.
176
- if err := os .MkdirAll (filepath .Join (pipeline , k .Dir ), 0755 ); err != nil {
177
- return nil , fmt .Errorf ("creating temporary directory %s: %w" , filepath .Join (pipeline , k .Dir ), err )
192
+ if err := os .MkdirAll (filepath .Join (k .Fn .SinkDir , k .Dir ), os .ModePerm ); err != nil {
193
+ return nil , fmt .Errorf ("creating temporary directory %s: %w" , filepath .Join (k .Fn .SinkDir , k .Dir ), err )
178
194
}
179
195
180
196
if err := k .readConfigs (ctx ); err != nil {
@@ -185,7 +201,7 @@ func (k *Deployer) renderManifests(ctx context.Context, _ io.Writer, builds []bu
185
201
return nil , fmt .Errorf ("kustomize build: %w" , err )
186
202
}
187
203
188
- manifests , err := k .kptFnRun (ctx )
204
+ manifests , err := k .kptFnRun (ctx , flags )
189
205
if err != nil {
190
206
return nil , fmt .Errorf ("running kpt functions: %w" , err )
191
207
}
@@ -213,15 +229,15 @@ func (k *Deployer) renderManifests(ctx context.Context, _ io.Writer, builds []bu
213
229
}
214
230
215
231
// readConfigs uses `kpt fn source` to read config manifests from k.Dir
216
- // and uses `kpt fn sink` to output those manifests to .pipeline .
232
+ // and uses `kpt fn sink` to output those manifests to sinkDir .
217
233
func (k * Deployer ) readConfigs (ctx context.Context ) error {
218
234
cmd := exec .CommandContext (ctx , "kpt" , kptCommandArgs (k .Dir , []string {"fn" , "source" }, nil , nil )... )
219
235
b , err := util .RunCmdOut (cmd )
220
236
if err != nil {
221
237
return err
222
238
}
223
239
224
- cmd = exec .CommandContext (ctx , "kpt" , kptCommandArgs (filepath .Join (pipeline , k .Dir ), []string {"fn" , "sink" }, nil , nil )... )
240
+ cmd = exec .CommandContext (ctx , "kpt" , kptCommandArgs (filepath .Join (k . Fn . SinkDir , k .Dir ), []string {"fn" , "sink" }, nil , nil )... )
225
241
cmd .Stdin = bytes .NewBuffer (b )
226
242
if _ , err := util .RunCmdOut (cmd ); err != nil {
227
243
return err
@@ -237,7 +253,8 @@ func (k *Deployer) kustomizeBuild(ctx context.Context) error {
237
253
return nil
238
254
}
239
255
240
- cmd := exec .CommandContext (ctx , "kustomize" , append ([]string {"build" }, kustomize .BuildCommandArgs ([]string {"-o" , filepath .Join (pipeline , k .Dir )}, k .Dir )... )... )
256
+ cmd := exec .CommandContext (ctx , "kustomize" , append ([]string {"build" },
257
+ kustomize .BuildCommandArgs ([]string {"-o" , filepath .Join (k .Fn .SinkDir , k .Dir )}, k .Dir )... )... )
241
258
if _ , err := util .RunCmdOut (cmd ); err != nil {
242
259
return err
243
260
}
@@ -247,28 +264,22 @@ func (k *Deployer) kustomizeBuild(ctx context.Context) error {
247
264
return fmt .Errorf ("finding kustomization dependencies: %w" , err )
248
265
}
249
266
250
- // Kustomize build outputs hydrated configs to .pipeline , so the dry configs must be removed.
267
+ // Kustomize build outputs hydrated configs to sinkDir , so the dry configs must be removed.
251
268
for _ , v := range deps {
252
- if err := os .RemoveAll (filepath .Join (pipeline , v )); err != nil {
269
+ if err := os .RemoveAll (filepath .Join (k . Fn . SinkDir , v )); err != nil {
253
270
return err
254
271
}
255
272
}
256
273
257
274
return nil
258
275
}
259
276
260
- // kptFnRun does a dry run with the specified kpt functions (fn-path XOR image) against .pipeline .
261
- // If neither fn-path nor image are specified, functions will attempt to be discovered in .pipeline .
277
+ // kptFnRun does a dry run with the specified kpt functions (fn-path XOR image) against sinkDir .
278
+ // If neither fn-path nor image are specified, functions will attempt to be discovered in sinkDir .
262
279
// An error occurs if both fn-path and image are specified.
263
- func (k * Deployer ) kptFnRun (ctx context.Context ) (manifest.ManifestList , error ) {
280
+ func (k * Deployer ) kptFnRun (ctx context.Context , flags [] string ) (manifest.ManifestList , error ) {
264
281
var manifests manifest.ManifestList
265
-
266
- flags , err := k .getKptFnRunArgs ()
267
- if err != nil {
268
- return nil , fmt .Errorf ("getting kpt fn run args: %w" , err )
269
- }
270
-
271
- cmd := exec .CommandContext (ctx , "kpt" , kptCommandArgs (pipeline , []string {"fn" , "run" }, flags , nil )... )
282
+ cmd := exec .CommandContext (ctx , "kpt" , kptCommandArgs (k .Fn .SinkDir , []string {"fn" , "run" }, flags , nil )... )
272
283
out , err := util .RunCmdOut (cmd )
273
284
if err != nil {
274
285
return nil , err
@@ -282,7 +293,7 @@ func (k *Deployer) kptFnRun(ctx context.Context) (manifest.ManifestList, error)
282
293
}
283
294
284
295
// excludeKptFn adds an annotation "config.kubernetes.io/local-config: 'true'" to kpt function.
285
- // This will exclude kpt functions from deployed to the cluster in kpt live apply.
296
+ // This will exclude kpt functions from deployed to the cluster in ` kpt live apply` .
286
297
func (k * Deployer ) excludeKptFn (originalManifest manifest.ManifestList ) (manifest.ManifestList , error ) {
287
298
var newManifest manifest.ManifestList
288
299
for _ , yByte := range originalManifest {
@@ -332,7 +343,7 @@ func (k *Deployer) getApplyDir(ctx context.Context) (string, error) {
332
343
333
344
// 0755 is a permission setting where the owner can read, write, and execute.
334
345
// Others can read and execute but not modify the directory.
335
- if err := os .MkdirAll (kptHydrated , 0755 ); err != nil {
346
+ if err := os .MkdirAll (kptHydrated , os . ModePerm ); err != nil {
336
347
return "" , fmt .Errorf ("applyDir was unspecified. creating applyDir: %w" , err )
337
348
}
338
349
0 commit comments