Skip to content

Commit 92fbb86

Browse files
authored
Keep non-yaml files when healing config comments (#3142)
1 parent 1daa4ec commit 92fbb86

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

porch/pkg/engine/engine.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,12 @@ func healConfig(old, new map[string]string) (map[string]string, error) {
583583
},
584584
}
585585

586+
extra := map[string]string{}
587+
586588
if err := (kio.Pipeline{
587589
Inputs: []kio.Reader{&packageReader{
588590
input: repository.PackageResources{Contents: new},
589-
extra: map[string]string{},
591+
extra: extra,
590592
}},
591593
Filters: []kio.Filter{filter},
592594
Outputs: []kio.Writer{out},
@@ -595,5 +597,11 @@ func healConfig(old, new map[string]string) (map[string]string, error) {
595597
return nil, err
596598
}
597599

598-
return out.output.Contents, nil
600+
healed := out.output.Contents
601+
602+
for k, v := range extra {
603+
healed[k] = v
604+
}
605+
606+
return healed, nil
599607
}

porch/pkg/engine/replace_test.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package engine
1717
import (
1818
"bytes"
1919
"context"
20+
"path"
2021
"path/filepath"
2122
"testing"
2223

@@ -63,20 +64,30 @@ func removeComments(t *testing.T, r repository.PackageResources) repository.Pack
6364
}
6465

6566
for k, v := range r.Contents {
66-
var data interface{}
67-
if err := yaml.Unmarshal([]byte(v), &data); err != nil {
68-
t.Fatalf("Failed to unmarshal %q: %v", k, err)
69-
}
67+
base := path.Base(k)
68+
ext := path.Ext(base)
7069

71-
var nocomment bytes.Buffer
72-
encoder := yaml.NewEncoder(&nocomment)
73-
encoder.SetIndent(0)
74-
if err := encoder.Encode(data); err != nil {
75-
t.Fatalf("Failed to re-encode yaml output: %v", err)
70+
if ext == ".yaml" || ext == ".yml" || base == "Kptfile" {
71+
v = removeCommentsFromFile(t, k, v)
7672
}
7773

78-
out.Contents[k] = nocomment.String()
74+
out.Contents[k] = v
7975
}
80-
8176
return out
8277
}
78+
79+
func removeCommentsFromFile(t *testing.T, name, contents string) string {
80+
var data interface{}
81+
if err := yaml.Unmarshal([]byte(contents), &data); err != nil {
82+
t.Fatalf("Failed to unmarshal %q: %v", name, err)
83+
}
84+
85+
var nocomment bytes.Buffer
86+
encoder := yaml.NewEncoder(&nocomment)
87+
encoder.SetIndent(0)
88+
if err := encoder.Encode(data); err != nil {
89+
t.Fatalf("Failed to re-encode yaml output: %v", err)
90+
}
91+
92+
return nocomment.String()
93+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# replace test

0 commit comments

Comments
 (0)