patchjson6902json.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transformer
  14. import (
  15. "fmt"
  16. "github.com/evanphx/json-patch"
  17. "github.com/ghodss/yaml"
  18. "github.com/pkg/errors"
  19. "sigs.k8s.io/kustomize/pkg/resid"
  20. "sigs.k8s.io/kustomize/pkg/resmap"
  21. "sigs.k8s.io/kustomize/pkg/resource"
  22. "sigs.k8s.io/kustomize/pkg/transformers"
  23. )
  24. // patchJson6902JSONTransformer applies patches.
  25. type patchJson6902JSONTransformer struct {
  26. target resid.ResId
  27. patch jsonpatch.Patch
  28. rawOp []byte
  29. }
  30. var _ transformers.Transformer = &patchJson6902JSONTransformer{}
  31. // newPatchJson6902JSONTransformer constructs a PatchJson6902 transformer.
  32. func newPatchJson6902JSONTransformer(
  33. id resid.ResId, rawOp []byte) (transformers.Transformer, error) {
  34. op := rawOp
  35. var err error
  36. if !isJsonFormat(op) {
  37. // if it isn't JSON, try to parse it as YAML
  38. op, err = yaml.YAMLToJSON(rawOp)
  39. if err != nil {
  40. return nil, err
  41. }
  42. }
  43. decodedPatch, err := jsonpatch.DecodePatch(op)
  44. if err != nil {
  45. return nil, err
  46. }
  47. if len(decodedPatch) == 0 {
  48. return transformers.NewNoOpTransformer(), nil
  49. }
  50. return &patchJson6902JSONTransformer{target: id, patch: decodedPatch, rawOp: rawOp}, nil
  51. }
  52. // Transform apply the json patches on top of the base resources.
  53. func (t *patchJson6902JSONTransformer) Transform(m resmap.ResMap) error {
  54. obj, err := t.findTargetObj(m)
  55. if err != nil {
  56. return err
  57. }
  58. rawObj, err := obj.MarshalJSON()
  59. if err != nil {
  60. return err
  61. }
  62. modifiedObj, err := t.patch.Apply(rawObj)
  63. if err != nil {
  64. return errors.Wrapf(err, "failed to apply json patch '%s'", string(t.rawOp))
  65. }
  66. err = obj.UnmarshalJSON(modifiedObj)
  67. if err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func (t *patchJson6902JSONTransformer) findTargetObj(
  73. m resmap.ResMap) (*resource.Resource, error) {
  74. var matched []resid.ResId
  75. // TODO(monopole): namespace bug in json patch?
  76. // Since introduction in PR #300
  77. // (see pkg/patch/transformer/util.go),
  78. // this code has treated an empty namespace like a wildcard
  79. // rather than like an additional restriction to match
  80. // only the empty namespace. No test coverage to confirm.
  81. // Not sure if desired, keeping it for now.
  82. if t.target.Namespace() != "" {
  83. matched = m.GetMatchingIds(t.target.NsGvknEquals)
  84. } else {
  85. matched = m.GetMatchingIds(t.target.GvknEquals)
  86. }
  87. if len(matched) == 0 {
  88. return nil, fmt.Errorf(
  89. "couldn't find target %v for json patch", t.target)
  90. }
  91. if len(matched) > 1 {
  92. return nil, fmt.Errorf(
  93. "found multiple targets %v matching %v for json patch",
  94. matched, t.target)
  95. }
  96. return m[matched[0]], nil
  97. }