mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #3177 from Shell32-Natsu/json-patch
Improve json patch plugin
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
// Code generated by pluginator on HelmChartInflationGenerator; DO NOT EDIT.
|
// Code generated by pluginator on HelmChartInflationGenerator; DO NOT EDIT.
|
||||||
// pluginator {unknown 1970-01-01T00:00:00Z }
|
// pluginator {unknown 1970-01-01T00:00:00Z }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
package builtins
|
package builtins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -176,5 +174,5 @@ func (p *HelmChartInflationGeneratorPlugin) checkHelmVersion() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewHelmChartInflationGeneratorPlugin() resmap.GeneratorPlugin {
|
func NewHelmChartInflationGeneratorPlugin() resmap.GeneratorPlugin {
|
||||||
return &HelmChartInflationGeneratorPlugin{}
|
return &HelmChartInflationGeneratorPlugin{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
@@ -20,9 +19,9 @@ import (
|
|||||||
type PatchJson6902TransformerPlugin struct {
|
type PatchJson6902TransformerPlugin struct {
|
||||||
ldr ifc.Loader
|
ldr ifc.Loader
|
||||||
decodedPatch jsonpatch.Patch
|
decodedPatch jsonpatch.Patch
|
||||||
Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||||
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PatchJson6902TransformerPlugin) Config(
|
func (p *PatchJson6902TransformerPlugin) Config(
|
||||||
@@ -72,22 +71,22 @@ func (p *PatchJson6902TransformerPlugin) Config(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PatchJson6902TransformerPlugin) Transform(m resmap.ResMap) error {
|
func (p *PatchJson6902TransformerPlugin) Transform(m resmap.ResMap) error {
|
||||||
id := resid.NewResIdWithNamespace(
|
if p.Target == nil {
|
||||||
resid.Gvk{
|
return fmt.Errorf("must specify a target for patch %s", p.JsonOp)
|
||||||
Group: p.Target.Group,
|
}
|
||||||
Version: p.Target.Version,
|
resources, err := m.Select(*p.Target)
|
||||||
Kind: p.Target.Kind,
|
|
||||||
},
|
|
||||||
p.Target.Name,
|
|
||||||
p.Target.Namespace,
|
|
||||||
)
|
|
||||||
obj, err := m.GetById(id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return filtersutil.ApplyToJSON(patchjson6902.Filter{
|
for _, res := range resources {
|
||||||
Patch: p.JsonOp,
|
err = filtersutil.ApplyToJSON(patchjson6902.Filter{
|
||||||
}, obj)
|
Patch: p.JsonOp,
|
||||||
|
}, res)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPatchJson6902TransformerPlugin() resmap.TransformerPlugin {
|
func NewPatchJson6902TransformerPlugin() resmap.TransformerPlugin {
|
||||||
|
|||||||
@@ -159,12 +159,12 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
|
|||||||
kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f tFactory, _ *builtinconfig.TransformerConfig) (
|
kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f tFactory, _ *builtinconfig.TransformerConfig) (
|
||||||
result []resmap.Transformer, err error) {
|
result []resmap.Transformer, err error) {
|
||||||
var c struct {
|
var c struct {
|
||||||
Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||||
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
||||||
}
|
}
|
||||||
for _, args := range kt.kustomization.PatchesJson6902 {
|
for _, args := range kt.kustomization.PatchesJson6902 {
|
||||||
c.Target = *args.Target
|
c.Target = args.Target
|
||||||
c.Path = args.Path
|
c.Path = args.Path
|
||||||
c.JsonOp = args.Patch
|
c.JsonOp = args.Patch
|
||||||
p := f()
|
p := f()
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ type Kustomization struct {
|
|||||||
// JSONPatches is a list of JSONPatch for applying JSON patch.
|
// JSONPatches is a list of JSONPatch for applying JSON patch.
|
||||||
// Format documented at https://tools.ietf.org/html/rfc6902
|
// Format documented at https://tools.ietf.org/html/rfc6902
|
||||||
// and http://jsonpatch.com
|
// and http://jsonpatch.com
|
||||||
PatchesJson6902 []PatchJson6902 `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
|
PatchesJson6902 []Patch `json:"patchesJson6902,omitempty" yaml:"patchesJson6902,omitempty"`
|
||||||
|
|
||||||
// Patches is a list of patches, where each one can be either a
|
// Patches is a list of patches, where each one can be either a
|
||||||
// Strategic Merge Patch or a JSON patch.
|
// Strategic Merge Patch or a JSON patch.
|
||||||
@@ -172,9 +172,7 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() {
|
|||||||
// has been processed.
|
// has been processed.
|
||||||
func (k *Kustomization) FixKustomizationPreMarshalling() {
|
func (k *Kustomization) FixKustomizationPreMarshalling() {
|
||||||
// PatchesJson6902 should be under the Patches field.
|
// PatchesJson6902 should be under the Patches field.
|
||||||
for _, patch := range k.PatchesJson6902 {
|
k.Patches = append(k.Patches, k.PatchesJson6902...)
|
||||||
k.Patches = append(k.Patches, patch.ToPatch())
|
|
||||||
}
|
|
||||||
k.PatchesJson6902 = nil
|
k.PatchesJson6902 = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package types
|
|
||||||
|
|
||||||
// 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"`
|
|
||||||
|
|
||||||
// inline patch string
|
|
||||||
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToPatch converts a PatchJson6902 to its superset Patch.
|
|
||||||
func (patch *PatchJson6902) ToPatch() Patch {
|
|
||||||
selector := patch.Target.ToSelector()
|
|
||||||
return Patch{Path: patch.Path, Patch: patch.Patch, Target: &selector}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
// Copyright 2019 The Kubernetes Authors.
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package types
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
|
||||||
)
|
|
||||||
|
|
||||||
// PatchTarget represents the kubernetes object that the patch is applied to
|
|
||||||
type PatchTarget struct {
|
|
||||||
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
|
||||||
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
|
||||||
Name string `json:"name" yaml:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToSelector converts a PatchTarget to a Selector.
|
|
||||||
func (target *PatchTarget) ToSelector() Selector {
|
|
||||||
return Selector{Name: target.Name, Namespace: target.Namespace, Gvk: target.Gvk}
|
|
||||||
}
|
|
||||||
@@ -31,7 +31,7 @@ func (s StringList) Has(val []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func isStringSliceEqual (s []string, t []string) bool {
|
func isStringSliceEqual(s []string, t []string) bool {
|
||||||
if len(s) != len(t) {
|
if len(s) != len(t) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
@@ -21,9 +20,9 @@ import (
|
|||||||
type plugin struct {
|
type plugin struct {
|
||||||
ldr ifc.Loader
|
ldr ifc.Loader
|
||||||
decodedPatch jsonpatch.Patch
|
decodedPatch jsonpatch.Patch
|
||||||
Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"`
|
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||||
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection GoUnusedGlobalVariable
|
//noinspection GoUnusedGlobalVariable
|
||||||
@@ -76,20 +75,20 @@ func (p *plugin) Config(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *plugin) Transform(m resmap.ResMap) error {
|
func (p *plugin) Transform(m resmap.ResMap) error {
|
||||||
id := resid.NewResIdWithNamespace(
|
if p.Target == nil {
|
||||||
resid.Gvk{
|
return fmt.Errorf("must specify a target for patch %s", p.JsonOp)
|
||||||
Group: p.Target.Group,
|
}
|
||||||
Version: p.Target.Version,
|
resources, err := m.Select(*p.Target)
|
||||||
Kind: p.Target.Kind,
|
|
||||||
},
|
|
||||||
p.Target.Name,
|
|
||||||
p.Target.Namespace,
|
|
||||||
)
|
|
||||||
obj, err := m.GetById(id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return filtersutil.ApplyToJSON(patchjson6902.Filter{
|
for _, res := range resources {
|
||||||
Patch: p.JsonOp,
|
err = filtersutil.ApplyToJSON(patchjson6902.Filter{
|
||||||
}, obj)
|
Patch: p.JsonOp,
|
||||||
|
}, res)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user