Skip to content

Commit

Permalink
Add support for loading UTF16 encoded files
Browse files Browse the repository at this point in the history
If they have a correct BOM (Byte-Order-Mark https://en.wikipedia.org/wiki/Byte_order_mark) at the beginning
of the file, support non utf8 files. Falls back on UTF8 encoding, if no understandable BOM is present.

Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Sep 11, 2023
1 parent eedb1a0 commit 253e316
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions kustomize/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/onsi/gomega v1.27.10
github.com/otiai10/copy v1.12.0
golang.org/x/text v0.13.0
k8s.io/api v0.27.4
k8s.io/apiextensions-apiserver v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
sigs.k8s.io/controller-runtime v0.15.1
sigs.k8s.io/kustomize/api v0.13.4
sigs.k8s.io/kustomize/api v0.14.0
sigs.k8s.io/kustomize/kyaml v0.14.2
sigs.k8s.io/yaml v1.3.0
)
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions kustomize/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
13 changes: 12 additions & 1 deletion kustomize/kustomize_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"sync"

securefs "github.com/fluxcd/pkg/kustomize/filesys"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -510,7 +512,16 @@ func scanManifests(fs filesys.FileSystem, base string) ([]string, error) {
return
}

fContents, err := fs.ReadFile(path)
f, err := os.Open(path)
if err != nil {
walkErr = err
return
}
defer f.Close()

utf16bom := unicode.BOMOverride(unicode.UTF8.NewDecoder())
reader := transform.NewReader(f, utf16bom)
fContents, err := io.ReadAll(reader)
if err != nil {
walkErr = err
return
Expand Down
8 changes: 8 additions & 0 deletions kustomize/kustomize_generator_whitebox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func TestScanManifests(t *testing.T) {
base: "./testdata/nokustomization/panic",
wantErr: true,
},
{
name: "utf-16LE with BOM files - should be valid",
base: "./testdata/nokustomization/utf16le",
wantPaths: []string{
"testdata/nokustomization/utf16le/configmap.yaml",
"testdata/nokustomization/utf16le/secret.yaml",
},
},
}

for _, tt := range tests {
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 253e316

Please sign in to comment.