mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Patchtransformers - drop copied code, improve deletion handling.
This commit is contained in:
@@ -240,4 +240,9 @@ type ResMap interface {
|
||||
// ToRNodeSlice converts the resources in the resmp
|
||||
// to a list of RNodes
|
||||
ToRNodeSlice() ([]*yaml.RNode, error)
|
||||
|
||||
// ApplySmPatch applies a strategic-merge patch to the
|
||||
// selected set of resources.
|
||||
ApplySmPatch(
|
||||
selectedSet *resource.IdSet, patch *resource.Resource) error
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ package resmap
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"sigs.k8s.io/kustomize/api/resid"
|
||||
@@ -578,3 +579,46 @@ func (m *resWrangler) ToRNodeSlice() ([]*kyaml_yaml.RNode, error) {
|
||||
}
|
||||
return rnodes, nil
|
||||
}
|
||||
|
||||
func (m *resWrangler) ApplySmPatch(
|
||||
selectedSet *resource.IdSet, patch *resource.Resource) error {
|
||||
newRm := New()
|
||||
for _, res := range m.Resources() {
|
||||
if !selectedSet.Contains(res.CurId()) {
|
||||
newRm.Append(res)
|
||||
continue
|
||||
}
|
||||
patchCopy := patch.DeepCopy()
|
||||
patchCopy.SetName(res.GetName())
|
||||
patchCopy.SetNamespace(res.GetNamespace())
|
||||
patchCopy.SetGvk(res.GetGvk())
|
||||
err := res.ApplySmPatch(patchCopy)
|
||||
if err != nil {
|
||||
// Check for an error string from UnmarshalJSON that's indicative
|
||||
// of an object that's missing basic KRM fields, and thus may have been
|
||||
// entirely deleted (an acceptable outcome). This error handling should
|
||||
// be deleted along with use of ResMap and apimachinery functions like
|
||||
// UnmarshalJSON.
|
||||
if !strings.Contains(err.Error(), "Object 'Kind' is missing") {
|
||||
// Some unknown error, let it through.
|
||||
return err
|
||||
}
|
||||
if !res.IsEmpty() {
|
||||
return errors.Wrapf(
|
||||
err, "with unexpectedly non-empty object map of size %d",
|
||||
len(res.Map()))
|
||||
}
|
||||
// Fall through to handle deleted object.
|
||||
}
|
||||
if !res.IsEmpty() {
|
||||
// IsEmpty means all fields have been removed from the object.
|
||||
// This can happen if a patch required deletion of the
|
||||
// entire resource (not just a part of it). This means
|
||||
// the overall resmap must shrink by one.
|
||||
newRm.Append(res)
|
||||
}
|
||||
}
|
||||
m.Clear()
|
||||
m.AppendAll(newRm)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user