@@ -14,6 +14,7 @@ import (
14
14
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
15
15
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
16
16
"sigs.k8s.io/kustomize/kyaml/kio"
17
+ "sigs.k8s.io/kustomize/kyaml/kio/filters"
17
18
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
18
19
"sigs.k8s.io/kustomize/kyaml/openapi"
19
20
"sigs.k8s.io/kustomize/kyaml/sets"
@@ -55,7 +56,7 @@ type Result struct {
55
56
Message string
56
57
}
57
58
58
- const SettersConfigFileName = "setters-config .yaml"
59
+ const SettersConfigFileName = "setters.yaml"
59
60
60
61
// Filter implements Fix as a yaml.Filter
61
62
func (s * Fix ) Filter (nodes []* yaml.RNode ) ([]* yaml.RNode , error ) {
@@ -230,15 +231,18 @@ func (s *Fix) FunctionsInPkg(nodes []*yaml.RNode, pkgPath string) []v1.Function
230
231
}
231
232
fnSpec := runtimeutil .GetFunctionSpec (node )
232
233
if fnSpec != nil {
234
+ // in order to make sure there is only one function config per file,
235
+ // rename the file with the name(and hash(name)) of the function config resource
236
+ fnFileName := fmt .Sprintf ("%s.yaml" , node .GetName ())
233
237
// in v1, fn-config must be present in the package directory
234
238
// so configPath must be just the file name
235
- fnFileName := filepath .Base ( meta . Annotations [ kioutil . PathAnnotation ] )
239
+ fnFilePath := filepath .Join ( pkgPath , fnFileName )
236
240
res = append (res , v1.Function {
237
241
Image : fnSpec .Container .Image ,
238
242
ConfigPath : fnFileName ,
239
243
})
240
244
// move the fn-config to the top level directory of the package
241
- meta .Annotations [kioutil .PathAnnotation ] = filepath . Join ( pkgPath , fnFileName )
245
+ meta .Annotations [kioutil .PathAnnotation ] = fnFilePath
242
246
delete (meta .Annotations , runtimeutil .FunctionAnnotationKey )
243
247
delete (meta .Annotations , "config.k8s.io/function" )
244
248
err = node .SetAnnotations (meta .Annotations )
@@ -278,7 +282,7 @@ func (s *Fix) FixKptfile(node *yaml.RNode, functions []v1.Function) (*yaml.RNode
278
282
if kf .Pipeline != nil {
279
283
for i , fn := range kf .Pipeline .Mutators {
280
284
if strings .Contains (fn .Image , "apply-setters:v0.1" ) && len (fn .ConfigMap ) > 0 {
281
- settersConfig , err := ConfigFromSetters (kf .Pipeline .Mutators [0 ].ConfigMap , settersConfigFilePath )
285
+ settersConfig , err := SettersNodeFromSetters (kf .Pipeline .Mutators [0 ].ConfigMap , settersConfigFilePath )
282
286
if err != nil {
283
287
return node , err
284
288
}
@@ -392,9 +396,9 @@ func (s *Fix) FixKptfile(node *yaml.RNode, functions []v1.Function) (*yaml.RNode
392
396
if len (setters ) > 0 {
393
397
fn := v1.Function {
394
398
Image : "gcr.io/kpt-fn/apply-setters:v0.1" ,
395
- ConfigPath : "setters-config.yaml" ,
399
+ ConfigPath : SettersConfigFileName ,
396
400
}
397
- settersConfig , err := ConfigFromSetters (setters , settersConfigFilePath )
401
+ settersConfig , err := SettersNodeFromSetters (setters , settersConfigFilePath )
398
402
if err != nil {
399
403
return node , err
400
404
}
@@ -431,12 +435,12 @@ func (s *Fix) FixKptfile(node *yaml.RNode, functions []v1.Function) (*yaml.RNode
431
435
return kNode , err
432
436
}
433
437
434
- // ConfigFromSetters returns the ConfigMap node with input setters in data field
435
- func ConfigFromSetters (setters map [string ]string , path string ) (* yaml.RNode , error ) {
438
+ // SettersNodeFromSetters returns the ConfigMap node with input setters in data field
439
+ func SettersNodeFromSetters (setters map [string ]string , path string ) (* yaml.RNode , error ) {
436
440
configNode , err := yaml .Parse (fmt .Sprintf (`apiVersion: v1
437
441
kind: ConfigMap
438
442
metadata:
439
- name: setters-config
443
+ name: setters
440
444
annotations:
441
445
%s: %s
442
446
%s: "0"
@@ -452,28 +456,48 @@ metadata:
452
456
sort .Strings (keys )
453
457
454
458
for _ , k := range keys {
455
- v := setters [k ]
456
- vNode , err := yaml .Parse (v )
459
+ v , err := standardizeArrayValue (setters [k ])
457
460
if err != nil {
458
461
return nil , err
459
462
}
460
- if vNode .YNode ().Kind == yaml .SequenceNode {
461
- // standardize array values to folded style
462
- vNode .YNode ().Style = yaml .FoldedStyle
463
- v , err = vNode .String ()
464
- if err != nil {
465
- return nil , err
466
- }
463
+ vNode := yaml .NewScalarRNode (v )
464
+ if v == "" {
465
+ vNode .YNode ().Style = yaml .DoubleQuotedStyle
467
466
}
468
-
469
467
err = configNode .PipeE (
470
468
yaml .LookupCreate (yaml .ScalarNode , "data" , k ),
471
- yaml.FieldSetter {Value : yaml . NewScalarRNode ( v ) })
469
+ yaml.FieldSetter {Value : vNode , OverrideStyle : true })
472
470
if err != nil {
473
471
return nil , err
474
472
}
475
473
}
476
- return configNode , nil
474
+ // format the created resource
475
+ _ , err = filters.FormatFilter {UseSchema : true }.Filter ([]* yaml.RNode {configNode })
476
+ return configNode , err
477
+ }
478
+
479
+ // standardizeArrayValue returns the folded style array node string
480
+ // e.g. for input value [foo, bar], it returns
481
+ // - foo
482
+ // - bar
483
+ func standardizeArrayValue (val string ) (string , error ) {
484
+ if ! strings .HasPrefix (val , "[" ) {
485
+ // the value is either standardized, or is not a sequence node value
486
+ return val , nil
487
+ }
488
+ vNode , err := yaml .Parse (val )
489
+ if err != nil {
490
+ return val , fmt .Errorf ("failed to parse the array node value %q with error %q" , val , err .Error ())
491
+ }
492
+ if vNode .YNode ().Kind == yaml .SequenceNode {
493
+ // standardize array values to folded style
494
+ vNode .YNode ().Style = yaml .FoldedStyle
495
+ val , err = vNode .String ()
496
+ if err != nil {
497
+ return val , fmt .Errorf ("failed to serialize the array node value %q with error %q" , val , err .Error ())
498
+ }
499
+ }
500
+ return val , nil
477
501
}
478
502
479
503
// getPkgPathToSettersSchema returns the, map of pkgPath to the setters schema in Kptfile
@@ -507,13 +531,16 @@ func (s *Fix) visitMapping(object *yaml.RNode) error {
507
531
// return if it is not a sequence node
508
532
return nil
509
533
}
510
-
534
+ oldCommentPrefixes := [] string { `# {"$kpt-set":"` , `# {"$ref":"#/definitions/io.k8s.cli.` }
511
535
comment := node .Key .YNode ().LineComment
512
- // # {"$kpt-set":"foo"} must be converted to # kpt-set: ${foo}
513
- if strings .Contains (comment , "$kpt-set" ) {
514
- comment := strings .TrimPrefix (comment , `# {"$kpt-set":"` )
515
- comment = strings .TrimSuffix (comment , `"}` )
516
- node .Key .YNode ().LineComment = fmt .Sprintf ("kpt-set: ${%s}" , comment )
536
+ for _ , cp := range oldCommentPrefixes {
537
+ // # {"$kpt-set":"foo"} must be converted to # kpt-set: ${foo}
538
+ // # {"$ref":"#/definitions/io.k8s.cli.list"} must be converted to # kpt-set: ${list}
539
+ if strings .Contains (comment , cp ) {
540
+ comment := strings .TrimPrefix (comment , cp )
541
+ comment = strings .TrimSuffix (comment , `"}` )
542
+ node .Key .YNode ().LineComment = fmt .Sprintf ("kpt-set: ${%s}" , comment )
543
+ }
517
544
}
518
545
return nil
519
546
})
0 commit comments