Add coverage for kusttarget.Load

This commit is contained in:
jregan
2020-05-19 17:23:35 -07:00
parent 765a4888df
commit e28af71b6f
5 changed files with 93 additions and 21 deletions

View File

@@ -4,7 +4,6 @@
package types
import (
"log"
"regexp"
"sigs.k8s.io/yaml"
@@ -13,7 +12,7 @@ import (
// FixKustomizationPreUnmarshalling modies the raw data
// before marshalling - e.g. changes old field names to
// new field names.
func FixKustomizationPreUnmarshalling(data []byte) []byte {
func FixKustomizationPreUnmarshalling(data []byte) ([]byte, error) {
deprecateFieldsMap := map[string]string{
"imageTags:": "images:",
}
@@ -21,24 +20,28 @@ func FixKustomizationPreUnmarshalling(data []byte) []byte {
pattern := regexp.MustCompile(oldname)
data = pattern.ReplaceAll(data, []byte(newname))
}
if useLegacyPatch(data) {
doLegacy, err := useLegacyPatch(data)
if err != nil {
return nil, err
}
if doLegacy {
pattern := regexp.MustCompile("patches:")
data = pattern.ReplaceAll(data, []byte("patchesStrategicMerge:"))
}
return data
return data, nil
}
func useLegacyPatch(data []byte) bool {
func useLegacyPatch(data []byte) (bool, error) {
found := false
var object map[string]interface{}
err := yaml.Unmarshal(data, &object)
if err != nil {
log.Fatalf("invalid content from %s\n", string(data))
return false, err
}
if rawPatches, ok := object["patches"]; ok {
patches, ok := rawPatches.([]interface{})
if !ok {
log.Fatalf("invalid patches from %v\n", rawPatches)
return false, err
}
for _, p := range patches {
_, ok := p.(string)
@@ -47,5 +50,5 @@ func useLegacyPatch(data []byte) bool {
}
}
}
return found
return found, nil
}