diff --git a/pkg/patch/json6902.go b/pkg/patch/json6902.go deleted file mode 100644 index 9ddb1faa1..000000000 --- a/pkg/patch/json6902.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package patch - -import "sigs.k8s.io/kustomize/pkg/gvk" - -// Json6902 represents a json patch for an object -// with format documented https://tools.ietf.org/html/rfc6902. -type Json6902 struct { - // Target refers to a Kubernetes object that the json patch will be - // applied to. It must refer to a Kubernetes resource under the - // purview of this kustomization. Target should use the - // raw name of the object (the name specified in its YAML, - // before addition of a namePrefix and a nameSuffix). - Target *Target `json:"target" yaml:"target"` - - // relative file path for a json patch file inside a kustomization - Path string `json:"path,omitempty" yaml:"path,omitempty"` -} - -// Target represents the kubernetes object that the patch is applied to -type Target struct { - gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"` - Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` - Name string `json:"name" yaml:"name"` -} diff --git a/pkg/patch/strategicmerge.go b/pkg/patch/strategicmerge.go index 596cc346d..113319821 100644 --- a/pkg/patch/strategicmerge.go +++ b/pkg/patch/strategicmerge.go @@ -16,23 +16,20 @@ limitations under the License. package patch -// StrategicMerge represents a relative path to a -// stategic merge patch with the format -// https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md -type StrategicMerge string +import "sigs.k8s.io/kustomize/pkg/types" -// Append appends a slice of patch paths to a StrategicMerge slice -func Append(patches []StrategicMerge, paths ...string) []StrategicMerge { +// Append appends a slice of patch paths to a PatchStrategicMerge slice +func Append(patches []types.PatchStrategicMerge, paths ...string) []types.PatchStrategicMerge { for _, p := range paths { - patches = append(patches, StrategicMerge(p)) + patches = append(patches, types.PatchStrategicMerge(p)) } return patches } -// Exist determines if a patch path exists in a slice of StrategicMerge -func Exist(patches []StrategicMerge, path string) bool { +// Exist determines if a patch path exists in a slice of PatchStrategicMerge +func Exist(patches []types.PatchStrategicMerge, path string) bool { for _, p := range patches { - if p == StrategicMerge(path) { + if p == types.PatchStrategicMerge(path) { return true } } diff --git a/pkg/patch/transformer/factory.go b/pkg/patch/transformer/factory.go index b373dfb72..b446868a4 100644 --- a/pkg/patch/transformer/factory.go +++ b/pkg/patch/transformer/factory.go @@ -18,15 +18,15 @@ package transformer import ( "fmt" - "sigs.k8s.io/kustomize/pkg/ifc" - "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/gvk" - "sigs.k8s.io/kustomize/pkg/patch" + "sigs.k8s.io/kustomize/pkg/ifc" + "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/transformers" + "sigs.k8s.io/kustomize/pkg/types" ) -// PatchJson6902Factory makes Json6902 transformers +// PatchJson6902Factory makes PatchJson6902 transformers type PatchJson6902Factory struct { loader ifc.Loader } @@ -36,8 +36,8 @@ func NewPatchJson6902Factory(l ifc.Loader) PatchJson6902Factory { return PatchJson6902Factory{loader: l} } -// MakePatchJson6902Transformer returns a transformer for applying Json6902 patch -func (f PatchJson6902Factory) MakePatchJson6902Transformer(patches []patch.Json6902) (transformers.Transformer, error) { +// MakePatchJson6902Transformer returns a transformer for applying PatchJson6902 patch +func (f PatchJson6902Factory) MakePatchJson6902Transformer(patches []types.PatchJson6902) (transformers.Transformer, error) { var ts []transformers.Transformer for _, p := range patches { t, err := f.makeOnePatchJson6902Transformer(p) @@ -51,7 +51,7 @@ func (f PatchJson6902Factory) MakePatchJson6902Transformer(patches []patch.Json6 return transformers.NewMultiTransformerWithConflictCheck(ts), nil } -func (f PatchJson6902Factory) makeOnePatchJson6902Transformer(p patch.Json6902) (transformers.Transformer, error) { +func (f PatchJson6902Factory) makeOnePatchJson6902Transformer(p types.PatchJson6902) (transformers.Transformer, error) { if p.Target == nil { return nil, fmt.Errorf("must specify the target field in patchesJson6902") } diff --git a/pkg/patch/transformer/factory_test.go b/pkg/patch/transformer/factory_test.go index cb8837890..f6855fa89 100644 --- a/pkg/patch/transformer/factory_test.go +++ b/pkg/patch/transformer/factory_test.go @@ -25,17 +25,17 @@ import ( "sigs.k8s.io/kustomize/internal/loadertest" "sigs.k8s.io/kustomize/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/pkg/gvk" - "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/types" ) var rf = resource.NewFactory( kunstruct.NewKunstructuredFactoryImpl()) func TestNewPatchJson6902FactoryNoTarget(t *testing.T) { - p := patch.Json6902{} + p := types.PatchJson6902{} _, err := NewPatchJson6902Factory(nil).makeOnePatchJson6902Transformer(p) if err == nil { t.Fatal("expected error") @@ -51,7 +51,7 @@ target: name: some-name kind: Deployment `) - p := patch.Json6902{} + p := types.PatchJson6902{} err := yaml.Unmarshal(jsonPatch, &p) if err != nil { t.Fatalf("expected error %v", err) @@ -84,7 +84,7 @@ target: name: some-name path: patch.json `) - p := patch.Json6902{} + p := types.PatchJson6902{} err = yaml.Unmarshal(jsonPatch, &p) if err != nil { t.Fatal("expected error") @@ -122,7 +122,7 @@ target: kind: Deployment path: patch.yaml `) - p := patch.Json6902{} + p := types.PatchJson6902{} err = yaml.Unmarshal(jsonPatch, &p) if err != nil { t.Fatalf("unexpected error : %v", err) @@ -169,7 +169,7 @@ func TestNewPatchJson6902FactoryMulti(t *testing.T) { name: some-name path: patch.yaml `) - var p []patch.Json6902 + var p []types.PatchJson6902 err = yaml.Unmarshal(jsonPatches, &p) if err != nil { t.Fatalf("unexpected error : %v", err) @@ -283,7 +283,7 @@ func TestNewPatchJson6902FactoryMultiConflict(t *testing.T) { name: some-name path: patch.yaml `) - var p []patch.Json6902 + var p []types.PatchJson6902 err = yaml.Unmarshal(jsonPatches, &p) if err != nil { t.Fatalf("unexpected error : %v", err) diff --git a/pkg/resource/factory.go b/pkg/resource/factory.go index 6a5a8f8cc..d6760a53d 100644 --- a/pkg/resource/factory.go +++ b/pkg/resource/factory.go @@ -24,7 +24,6 @@ import ( "sigs.k8s.io/kustomize/internal/kusterr" "sigs.k8s.io/kustomize/pkg/ifc" - "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/types" ) @@ -69,7 +68,7 @@ func (rf *Factory) FromKunstructured( // SliceFromPatches returns a slice of resources given a patch path // slice from a kustomization file. func (rf *Factory) SliceFromPatches( - ldr ifc.Loader, paths []patch.StrategicMerge) ([]*Resource, error) { + ldr ifc.Loader, paths []types.PatchStrategicMerge) ([]*Resource, error) { var result []*Resource for _, path := range paths { content, err := ldr.Load(string(path)) diff --git a/pkg/resource/factory_test.go b/pkg/resource/factory_test.go index 54875a3f7..aa5b7b274 100644 --- a/pkg/resource/factory_test.go +++ b/pkg/resource/factory_test.go @@ -21,20 +21,20 @@ import ( "testing" "sigs.k8s.io/kustomize/internal/loadertest" - "sigs.k8s.io/kustomize/pkg/patch" . "sigs.k8s.io/kustomize/pkg/resource" + "sigs.k8s.io/kustomize/pkg/types" ) func TestSliceFromPatches(t *testing.T) { - patchGood1 := patch.StrategicMerge("patch1.yaml") + patchGood1 := types.PatchStrategicMerge("patch1.yaml") patch1 := ` apiVersion: apps/v1 kind: Deployment metadata: name: pooh ` - patchGood2 := patch.StrategicMerge("patch2.yaml") + patchGood2 := types.PatchStrategicMerge("patch2.yaml") patch2 := ` apiVersion: v1 kind: ConfigMap @@ -46,11 +46,11 @@ metadata: --- --- ` - patchBad := patch.StrategicMerge("patch3.yaml") + patchBad := types.PatchStrategicMerge("patch3.yaml") patch3 := ` WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT: woot ` - patchList := patch.StrategicMerge("patch4.yaml") + patchList := types.PatchStrategicMerge("patch4.yaml") patch4 := ` apiVersion: v1 kind: List @@ -65,7 +65,7 @@ items: name: winnie namespace: hundred-acre-wood ` - patchList2 := patch.StrategicMerge("patch5.yaml") + patchList2 := types.PatchStrategicMerge("patch5.yaml") patch5 := ` apiVersion: v1 kind: DeploymentList @@ -88,13 +88,13 @@ items: spec: <<: *hostAliases ` - patchList3 := patch.StrategicMerge("patch6.yaml") + patchList3 := types.PatchStrategicMerge("patch6.yaml") patch6 := ` apiVersion: v1 kind: List items: ` - patchList4 := patch.StrategicMerge("patch7.yaml") + patchList4 := types.PatchStrategicMerge("patch7.yaml") patch7 := ` apiVersion: v1 kind: List @@ -142,49 +142,49 @@ kind: List tests := []struct { name string - input []patch.StrategicMerge + input []types.PatchStrategicMerge expectedOut []*Resource expectedErr bool }{ { name: "happy", - input: []patch.StrategicMerge{patchGood1, patchGood2}, + input: []types.PatchStrategicMerge{patchGood1, patchGood2}, expectedOut: []*Resource{testDeployment, testConfigMap}, expectedErr: false, }, { name: "badFileName", - input: []patch.StrategicMerge{patchGood1, "doesNotExist"}, + input: []types.PatchStrategicMerge{patchGood1, "doesNotExist"}, expectedOut: []*Resource{}, expectedErr: true, }, { name: "badData", - input: []patch.StrategicMerge{patchGood1, patchBad}, + input: []types.PatchStrategicMerge{patchGood1, patchBad}, expectedOut: []*Resource{}, expectedErr: true, }, { name: "listOfPatches", - input: []patch.StrategicMerge{patchList}, + input: []types.PatchStrategicMerge{patchList}, expectedOut: []*Resource{testDeployment, testConfigMap}, expectedErr: false, }, { name: "listWithAnchorReference", - input: []patch.StrategicMerge{patchList2}, + input: []types.PatchStrategicMerge{patchList2}, expectedOut: []*Resource{testDeploymentA, testDeploymentB}, expectedErr: false, }, { name: "listWithNoEntries", - input: []patch.StrategicMerge{patchList3}, + input: []types.PatchStrategicMerge{patchList3}, expectedOut: []*Resource{}, expectedErr: false, }, { name: "listWithNo'items:'", - input: []patch.StrategicMerge{patchList4}, + input: []types.PatchStrategicMerge{patchList4}, expectedOut: []*Resource{}, expectedErr: false, }, diff --git a/pkg/target/kusttarget_configplugin.go b/pkg/target/kusttarget_configplugin.go index 6f7e862f3..78bd77e76 100644 --- a/pkg/target/kusttarget_configplugin.go +++ b/pkg/target/kusttarget_configplugin.go @@ -6,7 +6,6 @@ package target import ( "github.com/pkg/errors" "sigs.k8s.io/kustomize/pkg/image" - "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/transformers" "sigs.k8s.io/kustomize/pkg/transformers/config" @@ -128,7 +127,7 @@ func (kt *KustTarget) configureBuiltinPatchJson6902Transformer( tConfig *config.TransformerConfig) ( result []transformers.Transformer, err error) { var c struct { - Patches []patch.Json6902 + Patches []types.PatchJson6902 } c.Patches = kt.kustomization.PatchesJson6902 p := builtin.NewPatchJson6902TransformerPlugin() diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index eca0fd1fb..3bba41e2a 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -20,8 +20,8 @@ package types import ( "regexp" + "sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/image" - "sigs.k8s.io/kustomize/pkg/patch" ) const ( @@ -67,12 +67,12 @@ type Kustomization struct { // containing a strategic merge patch. Format documented at // https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md // URLs and globs are not supported. - PatchesStrategicMerge []patch.StrategicMerge `json:"patchesStrategicMerge,omitempty" yaml:"patchesStrategicMerge,omitempty"` + PatchesStrategicMerge []PatchStrategicMerge `json:"patchesStrategicMerge,omitempty" yaml:"patchesStrategicMerge,omitempty"` // JSONPatches is a list of JSONPatch for applying JSON patch. // Format documented at https://tools.ietf.org/html/rfc6902 // and http://jsonpatch.com - PatchesJson6902 []patch.Json6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"` + PatchesJson6902 []PatchJson6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"` // Images is a list of (image name, new name, new tag or digest) // for changing image names, tags or digests. This can also be achieved with a @@ -333,3 +333,29 @@ type NameArgs struct { Name string `json:"name,omitempty" yaml:"name,omitempty"` Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` } + +// PatchJson6902 represents a json patch for an object +// with format documented https://tools.ietf.org/html/rfc6902. +type PatchJson6902 struct { + // PatchTarget refers to a Kubernetes object that the json patch will be + // applied to. It must refer to a Kubernetes resource under the + // purview of this kustomization. PatchTarget should use the + // raw name of the object (the name specified in its YAML, + // before addition of a namePrefix and a nameSuffix). + Target *PatchTarget `json:"target" yaml:"target"` + + // relative file path for a json patch file inside a kustomization + Path string `json:"path,omitempty" yaml:"path,omitempty"` +} + +// PatchTarget represents the kubernetes object that the patch is applied to +type PatchTarget struct { + gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"` + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` + Name string `json:"name" yaml:"name"` +} + +// PatchStrategicMerge represents a relative path to a +// stategic merge patch with the format +// https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md +type PatchStrategicMerge string diff --git a/plugin/builtin/PatchJson6902Transformer.go b/plugin/builtin/PatchJson6902Transformer.go index b43579994..79b9d7ff1 100644 --- a/plugin/builtin/PatchJson6902Transformer.go +++ b/plugin/builtin/PatchJson6902Transformer.go @@ -3,15 +3,15 @@ package builtin import ( "sigs.k8s.io/kustomize/pkg/ifc" - "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/patch/transformer" "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/yaml" ) type PatchJson6902TransformerPlugin struct { ldr ifc.Loader - Patches []patch.Json6902 `json:"patches,omitempty" yaml:"patches,omitempty"` + Patches []types.PatchJson6902 `json:"patches,omitempty" yaml:"patches,omitempty"` } func NewPatchJson6902TransformerPlugin() *PatchJson6902TransformerPlugin { diff --git a/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go b/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go index 79119f55c..670768b54 100644 --- a/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go +++ b/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go @@ -6,15 +6,15 @@ package main import ( "sigs.k8s.io/kustomize/pkg/ifc" - "sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/patch/transformer" "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/yaml" ) type plugin struct { ldr ifc.Loader - Patches []patch.Json6902 `json:"patches,omitempty" yaml:"patches,omitempty"` + Patches []types.PatchJson6902 `json:"patches,omitempty" yaml:"patches,omitempty"` } var KustomizePlugin plugin