| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
//go:generate pluginator |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
|
| 11 |
jsonpatch "gopkg.in/evanphx/json-patch.v4" |
| 12 |
"sigs.k8s.io/kustomize/api/filters/patchjson6902" |
| 13 |
"sigs.k8s.io/kustomize/api/resmap" |
| 14 |
"sigs.k8s.io/kustomize/api/resource" |
| 15 |
"sigs.k8s.io/kustomize/api/types" |
| 16 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 17 |
"sigs.k8s.io/kustomize/kyaml/kio/kioutil" |
| 18 |
"sigs.k8s.io/yaml" |
| 19 |
) |
| 20 |
|
| 21 |
type plugin struct { |
| 22 |
smPatches []*resource.Resource // strategic-merge patches |
| 23 |
jsonPatches jsonpatch.Patch // json6902 patch |
| 24 |
// patchText is pure patch text created by Path or Patch |
| 25 |
patchText string |
| 26 |
// patchSource is patch source message |
| 27 |
patchSource string |
| 28 |
Path string `json:"path,omitempty" yaml:"path,omitempty"` |
| 29 |
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"` |
| 30 |
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"` |
| 31 |
Options *types.PatchArgs `json:"options,omitempty" yaml:"options,omitempty"` |
| 32 |
} |
| 33 |
|
| 34 |
var KustomizePlugin plugin //nolint:gochecknoglobals |
| 35 |
|
| 36 |
func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error { |
| 37 |
if err := yaml.Unmarshal(c, p); err != nil { |
| 38 |
return err |
| 39 |
} |
| 40 |
|
| 41 |
p.Patch = strings.TrimSpace(p.Patch) |
| 42 |
switch { |
| 43 |
case p.Patch == "" && p.Path == "": |
| 44 |
return fmt.Errorf("must specify one of patch and path in\n%s", string(c)) |
| 45 |
case p.Patch != "" && p.Path != "": |
| 46 |
return fmt.Errorf("patch and path can't be set at the same time\n%s", string(c)) |
| 47 |
case p.Patch != "": |
| 48 |
p.patchText = p.Patch |
| 49 |
p.patchSource = fmt.Sprintf("[patch: %q]", p.patchText) |
| 50 |
case p.Path != "": |
| 51 |
loaded, err := h.Loader().Load(p.Path) |
| 52 |
if err != nil { |
| 53 |
return fmt.Errorf("failed to get the patch file from path(%s): %w", p.Path, err) |
| 54 |
} |
| 55 |
p.patchText = string(loaded) |
| 56 |
p.patchSource = fmt.Sprintf("[path: %q]", p.Path) |
| 57 |
} |
| 58 |
|
| 59 |
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText)) |
| 60 |
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText)) |
| 61 |
|
| 62 |
if ((errSM == nil && errJson == nil) || |
| 63 |
(patchesSM != nil && patchesJson != nil)) && |
| 64 |
(len(patchesSM) > 0 && len(patchesJson) > 0) { |
| 65 |
return fmt.Errorf( |
| 66 |
"illegally qualifies as both an SM and JSON patch: %s", |
| 67 |
p.patchSource) |
| 68 |
} |
| 69 |
if errSM != nil && errJson != nil { |
| 70 |
return fmt.Errorf( |
| 71 |
"unable to parse SM or JSON patch from %s", p.patchSource) |
| 72 |
} |
| 73 |
if errSM == nil { |
| 74 |
p.smPatches = patchesSM |
| 75 |
for _, loadedPatch := range p.smPatches { |
| 76 |
if p.Options == nil { |
| 77 |
continue |
| 78 |
} |
| 79 |
|
| 80 |
if p.Options.AllowNameChange { |
| 81 |
loadedPatch.AllowNameChange() |
| 82 |
} |
| 83 |
if p.Options.AllowKindChange { |
| 84 |
loadedPatch.AllowKindChange() |
| 85 |
} |
| 86 |
} |
| 87 |
} else { |
| 88 |
p.jsonPatches = patchesJson |
| 89 |
} |
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
func (p *plugin) Transform(m resmap.ResMap) error { |
| 94 |
if p.smPatches != nil { |
| 95 |
return p.transformStrategicMerge(m) |
| 96 |
} |
| 97 |
if p.jsonPatches != nil { |
| 98 |
return p.transformJson6902(m) |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
// transformStrategicMerge applies each loaded strategic merge patch |
| 104 |
// to the resource in the ResMap that matches the identifier of the patch. |
| 105 |
// If only one patch is specified, the Target can be used instead. |
| 106 |
func (p *plugin) transformStrategicMerge(m resmap.ResMap) error { |
| 107 |
if p.Target != nil { |
| 108 |
if len(p.smPatches) > 1 { |
| 109 |
// detail: https://github.com/kubernetes-sigs/kustomize/issues/5049#issuecomment-1440604403 |
| 110 |
return fmt.Errorf("Multiple Strategic-Merge Patches in one `patches` entry is not allowed to set `patches.target` field: %s", p.patchSource) |
| 111 |
} |
| 112 |
|
| 113 |
// single patch |
| 114 |
patch := p.smPatches[0] |
| 115 |
selected, err := m.Select(*p.Target) |
| 116 |
if err != nil { |
| 117 |
return fmt.Errorf("unable to find patch target %q in `resources`: %w", p.Target, err) |
| 118 |
} |
| 119 |
return errors.Wrap(m.ApplySmPatch(resource.MakeIdSet(selected), patch)) |
| 120 |
} |
| 121 |
|
| 122 |
for _, patch := range p.smPatches { |
| 123 |
target, err := m.GetById(patch.OrgId()) |
| 124 |
if err != nil { |
| 125 |
return fmt.Errorf("no resource matches strategic merge patch %q: %w", patch.OrgId(), err) |
| 126 |
} |
| 127 |
if err := target.ApplySmPatch(patch); err != nil { |
| 128 |
return errors.Wrap(err) |
| 129 |
} |
| 130 |
} |
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
// transformJson6902 applies json6902 Patch to all the resources in the ResMap that match Target. |
| 135 |
func (p *plugin) transformJson6902(m resmap.ResMap) error { |
| 136 |
if p.Target == nil { |
| 137 |
return fmt.Errorf("must specify a target for JSON patch %s", p.patchSource) |
| 138 |
} |
| 139 |
resources, err := m.Select(*p.Target) |
| 140 |
if err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
for _, res := range resources { |
| 144 |
res.StorePreviousId() |
| 145 |
internalAnnotations := kioutil.GetInternalAnnotations(&res.RNode) |
| 146 |
err = res.ApplyFilter(patchjson6902.Filter{ |
| 147 |
Patch: p.patchText, |
| 148 |
}) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
|
| 153 |
annotations := res.GetAnnotations() |
| 154 |
for key, value := range internalAnnotations { |
| 155 |
annotations[key] = value |
| 156 |
} |
| 157 |
err = res.SetAnnotations(annotations) |
| 158 |
} |
| 159 |
return nil |
| 160 |
} |
| 161 |
|
| 162 |
// jsonPatchFromBytes loads a Json 6902 patch from a bytes input |
| 163 |
func jsonPatchFromBytes(in []byte) (jsonpatch.Patch, error) { |
| 164 |
ops := string(in) |
| 165 |
if ops == "" { |
| 166 |
return nil, fmt.Errorf("empty json patch operations") |
| 167 |
} |
| 168 |
|
| 169 |
if ops[0] != '[' { |
| 170 |
// TODO(5049): |
| 171 |
// In the case of multiple yaml documents, return error instead of ignoring all but first. |
| 172 |
// Details: https://github.com/kubernetes-sigs/kustomize/pull/5194#discussion_r1256686728 |
| 173 |
jsonOps, err := yaml.YAMLToJSON(in) |
| 174 |
if err != nil { |
| 175 |
return nil, err |
| 176 |
} |
| 177 |
ops = string(jsonOps) |
| 178 |
} |
| 179 |
return jsonpatch.DecodePatch([]byte(ops)) |
| 180 |
} |