From 6237d7abfd96f7076776f26a6be6b0651ccd8cb3 Mon Sep 17 00:00:00 2001 From: Jerome Brette Date: Mon, 30 Sep 2019 04:17:17 +0000 Subject: [PATCH] Update to Kustomize 3.2.0 Ensure that kustomization.yaml is matching picked version of kustomize and does not use deprecated fields --- go.mod | 3 +- go.sum | 4 +- pkg/internal/util/kustomize/kustomize.go | 56 +++++++++++++++--------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index be3e4414..22fdd3d0 100644 --- a/go.mod +++ b/go.mod @@ -11,5 +11,6 @@ require ( golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b // indirect k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d - sigs.k8s.io/kustomize/v3 v3.1.1-0.20190821175718-4b67a6de1296 + sigs.k8s.io/kustomize/v3 v3.2.0 + sigs.k8s.io/yaml v1.1.0 ) diff --git a/go.sum b/go.sum index 46fd07d6..8869a0d1 100644 --- a/go.sum +++ b/go.sum @@ -156,8 +156,8 @@ k8s.io/klog v0.3.3 h1:niceAagH1tzskmaie/icWd7ci1wbG7Bf2c6YGcQv+3c= k8s.io/klog v0.3.3/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= k8s.io/kube-openapi v0.0.0-20190603182131-db7b694dc208 h1:5sW+fEHvlJI3Ngolx30CmubFulwH28DhKjGf70Xmtco= k8s.io/kube-openapi v0.0.0-20190603182131-db7b694dc208/go.mod h1:nfDlWeOsu3pUf4yWGL+ERqohP4YsZcBJXWMK+gkzOA4= -sigs.k8s.io/kustomize/v3 v3.1.1-0.20190821175718-4b67a6de1296 h1:iQaIG5Dq+3qSiaFrJ/l/0MjjxKmdwyVNpKRYJwUe/+0= -sigs.k8s.io/kustomize/v3 v3.1.1-0.20190821175718-4b67a6de1296/go.mod h1:ztX4zYc/QIww3gSripwF7TBOarBTm5BvyAMem0kCzOE= +sigs.k8s.io/kustomize/v3 v3.2.0 h1:EKcEubO29vCbigcMoNynfyZH+ANWkML2UHWibt1Do7o= +sigs.k8s.io/kustomize/v3 v3.2.0/go.mod h1:ztX4zYc/QIww3gSripwF7TBOarBTm5BvyAMem0kCzOE= sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/pkg/internal/util/kustomize/kustomize.go b/pkg/internal/util/kustomize/kustomize.go index 58699181..f19d9c67 100644 --- a/pkg/internal/util/kustomize/kustomize.go +++ b/pkg/internal/util/kustomize/kustomize.go @@ -31,8 +31,10 @@ import ( "sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resource" + "sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/kind/pkg/internal/apis/config" + "sigs.k8s.io/yaml" ) // Build takes a set of resource blobs (yaml), patches (strategic merge patch) @@ -43,7 +45,6 @@ func Build(resources, patches []string, patchesJSON6902 []config.PatchJSON6902) // write the resources and patches to an in memory fs with a generated // kustomization.yaml memFS := fs.MakeFakeFS() - var kustomization bytes.Buffer fakeDir := "/" // for Windows we need this to be a drive because kustomize uses filepath.Abs() // which will add a drive letter if there is none. which drive letter is @@ -53,47 +54,62 @@ func Build(resources, patches []string, patchesJSON6902 []config.PatchJSON6902) } // NOTE: we always write this header as you cannot build without any resources - kustomization.WriteString("resources:\n") + kustomization := &types.Kustomization{ + TypeMeta: types.TypeMeta{ + APIVersion: types.KustomizationVersion, + Kind: types.KustomizationKind, + }, + Bases: make([]string, 0), + CommonLabels: make(map[string]string), + CommonAnnotations: make(map[string]string), + PatchesStrategicMerge: make([]types.PatchStrategicMerge, 0), + PatchesJson6902: make([]types.PatchJson6902, 0), + Vars: make([]types.Var, 0), + Crds: make([]string, 0), + Resources: make([]string, 0), + ConfigMapGenerator: make([]types.ConfigMapArgs, 0), + SecretGenerator: make([]types.SecretArgs, 0), + Configurations: make([]string, 0), + } for i, resource := range resources { // this cannot error per docs name := fmt.Sprintf("resource-%d.yaml", i) _ = memFS.WriteFile(filepath.Join(fakeDir, name), []byte(resource)) - fmt.Fprintf(&kustomization, " - %s\n", name) + kustomization.Resources = append(kustomization.Resources, name) } - if len(patches) > 0 { - kustomization.WriteString("patches:\n") - } for i, patch := range patches { // this cannot error per docs name := fmt.Sprintf("patch-%d.yaml", i) _ = memFS.WriteFile(filepath.Join(fakeDir, name), []byte(patch)) - fmt.Fprintf(&kustomization, " - %s\n", name) + kustomization.PatchesStrategicMerge = append(kustomization.PatchesStrategicMerge, types.PatchStrategicMerge(name)) } - if len(patchesJSON6902) > 0 { - kustomization.WriteString("patchesJson6902:\n") - } for i, patch := range patchesJSON6902 { // this cannot error per docs name := fmt.Sprintf("patch-json6902-%d.yaml", i) _ = memFS.WriteFile(filepath.Join(fakeDir, name), []byte(patch.Patch)) - fmt.Fprintf(&kustomization, " - path: %s\n", name) - fmt.Fprintf(&kustomization, " target:\n") - fmt.Fprintf(&kustomization, " group: %s\n", patch.Group) - fmt.Fprintf(&kustomization, " version: %s\n", patch.Version) - fmt.Fprintf(&kustomization, " kind: %s\n", patch.Kind) + + jsonPatch := new(types.PatchJson6902) + jsonPatch.Path = name + jsonPatch.Target = new(types.PatchTarget) + jsonPatch.Target.Group = patch.Group + jsonPatch.Target.Version = patch.Version + jsonPatch.Target.Kind = patch.Kind if patch.Name != "" { - fmt.Fprintf(&kustomization, " name: %s\n", patch.Name) + jsonPatch.Target.Name = patch.Name } if patch.Namespace != "" { - fmt.Fprintf(&kustomization, " namespace: %s\n", patch.Namespace) + jsonPatch.Target.Namespace = patch.Namespace } + kustomization.PatchesJson6902 = append(kustomization.PatchesJson6902, *jsonPatch) } - if err := memFS.WriteFile( - filepath.Join(fakeDir, "kustomization.yaml"), kustomization.Bytes(), - ); err != nil { + buf, bufErr := yaml.Marshal(kustomization) + if bufErr != nil { + return "", bufErr + } + if err := memFS.WriteFile(filepath.Join(fakeDir, "kustomization.yaml"), buf); err != nil { return "", err }