This commit is contained in:
Jeffrey Regan
2019-06-27 17:05:58 -07:00
parent dd0334536b
commit a60d99fdc9
4 changed files with 25 additions and 47 deletions

View File

@@ -13,7 +13,6 @@ import (
"k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/kustomize/v3/pkg/gvk" "sigs.k8s.io/kustomize/v3/pkg/gvk"
"sigs.k8s.io/kustomize/v3/pkg/resid"
"sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/resource" "sigs.k8s.io/kustomize/v3/pkg/resource"
"sigs.k8s.io/kustomize/v3/pkg/transformers" "sigs.k8s.io/kustomize/v3/pkg/transformers"
@@ -44,7 +43,7 @@ func (tf *transformer) Transform(m resmap.ResMap) error {
return err return err
} }
for _, patch := range patches.Resources() { for _, patch := range patches.Resources() {
target, err := tf.findPatchTarget(m, patch.OrgId()) target, err := m.GetById(patch.OrgId())
if err != nil { if err != nil {
return err return err
} }
@@ -96,21 +95,6 @@ func (tf *transformer) Transform(m resmap.ResMap) error {
return nil return nil
} }
func (tf *transformer) findPatchTarget(
m resmap.ResMap, id resid.ResId) (*resource.Resource, error) {
match, err1 := m.GetByOriginalId(id)
if err1 == nil {
return match, nil
}
match, err2 := m.GetByCurrentId(id)
if err2 == nil {
return match, nil
}
return nil, fmt.Errorf(
"%s; %s; failed to find unique target for patch %s",
err1.Error(), err2.Error(), id.GvknString())
}
// mergePatches merge and index patches by OrgId. // mergePatches merge and index patches by OrgId.
// It errors out if there is conflict between patches. // It errors out if there is conflict between patches.
func (tf *transformer) mergePatches() (resmap.ResMap, error) { func (tf *transformer) mergePatches() (resmap.ResMap, error) {

View File

@@ -23,7 +23,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/resid" "sigs.k8s.io/kustomize/v3/pkg/resid"
"sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/resource"
"sigs.k8s.io/kustomize/v3/pkg/transformers" "sigs.k8s.io/kustomize/v3/pkg/transformers"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
@@ -66,7 +65,7 @@ func newPatchJson6902JSONTransformer(
// Transform apply the json patches on top of the base resources. // Transform apply the json patches on top of the base resources.
func (t *patchJson6902JSONTransformer) Transform(m resmap.ResMap) error { func (t *patchJson6902JSONTransformer) Transform(m resmap.ResMap) error {
obj, err := t.findTargetObj(m) obj, err := m.GetById(t.target)
if err != nil { if err != nil {
return err return err
} }
@@ -84,30 +83,3 @@ func (t *patchJson6902JSONTransformer) Transform(m resmap.ResMap) error {
} }
return nil return nil
} }
func (t *patchJson6902JSONTransformer) findTargetObj(
m resmap.ResMap) (*resource.Resource, error) {
var matched []*resource.Resource
// TODO(monopole): namespace bug in json patch?
// Since introduction in PR #300
// (see pkg/patch/transformer/util.go),
// this code has treated an empty namespace like a wildcard
// rather than like an additional restriction to match
// only the empty namespace. No test coverage to confirm.
// Not sure if desired, keeping it for now.
if t.target.Namespace != "" {
matched = m.GetMatchingResourcesByOriginalId(t.target.Equals)
} else {
matched = m.GetMatchingResourcesByOriginalId(t.target.GvknEquals)
}
if len(matched) == 0 {
return nil, fmt.Errorf(
"couldn't find target %v for json patch", t.target)
}
if len(matched) > 1 {
return nil, fmt.Errorf(
"found multiple targets %v matching %v for json patch",
matched, t.target)
}
return matched[0], nil
}

View File

@@ -89,6 +89,12 @@ type ResMap interface {
// an exact match, returning an error on multiple or no matches. // an exact match, returning an error on multiple or no matches.
GetByOriginalId(resid.ResId) (*resource.Resource, error) GetByOriginalId(resid.ResId) (*resource.Resource, error)
// GetById is a helper function which first
// attempts GetByOriginalId, then GetByCurrentId,
// returning an error if both fail to find a single
// match.
GetById(resid.ResId) (*resource.Resource, error)
// GroupedByNamespace returns a map of namespace // GroupedByNamespace returns a map of namespace
// to a slice of *Resource in that namespace. // to a slice of *Resource in that namespace.
// Resources for whom IsNamespaceableKind is false are // Resources for whom IsNamespaceableKind is false are
@@ -333,6 +339,22 @@ func (m *resWrangler) GetByOriginalId(
return demandOneMatch(m.GetMatchingResourcesByOriginalId, id, "Original") return demandOneMatch(m.GetMatchingResourcesByOriginalId, id, "Original")
} }
// GetById implements ResMap.
func (m *resWrangler) GetById(
id resid.ResId) (*resource.Resource, error) {
match, err1 := m.GetByOriginalId(id)
if err1 == nil {
return match, nil
}
match, err2 := m.GetByCurrentId(id)
if err2 == nil {
return match, nil
}
return nil, fmt.Errorf(
"%s; %s; failed to find unique target for patch %s",
err1.Error(), err2.Error(), id.GvknString())
}
type resFinder func(IdMatcher) []*resource.Resource type resFinder func(IdMatcher) []*resource.Resource
func demandOneMatch( func demandOneMatch(

View File

@@ -419,7 +419,7 @@ patchesJson6902:
- target: - target:
version: v1 version: v1
kind: Service kind: Service
name: myService # BUG (https://github.com/kubernetes-sigs/kustomize/issues/972): this should be a-myService, because that is what the output for the base contains name: a-myService
path: service-patch.yaml path: service-patch.yaml
`) `)