Skip to content

Commit

Permalink
Prepare diamond import feature for upcoming high level merge feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrette committed Jul 16, 2019
1 parent f49701e commit c0f3009
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
61 changes: 47 additions & 14 deletions pkg/accumulator/resaccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strings"

"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/resource"
Expand All @@ -21,19 +22,19 @@ import (
// used to customize those resources. It's a ResMap
// plus stuff needed to modify the ResMap.
type ResAccumulator struct {
resMap resmap.ResMap
tConfig *config.TransformerConfig
varSet types.VarSet
conflictingResources []*resource.Resource
unresolvedVars types.VarSet
resMap resmap.ResMap
tConfig *config.TransformerConfig
varSet types.VarSet
patchSet []types.Patch
unresolvedVars types.VarSet
}

func MakeEmptyAccumulator() *ResAccumulator {
ra := &ResAccumulator{}
ra.resMap = resmap.New()
ra.tConfig = &config.TransformerConfig{}
ra.varSet = types.NewVarSet()
ra.conflictingResources = []*resource.Resource{}
ra.patchSet = []types.Patch{}
ra.unresolvedVars = types.NewVarSet()
return ra
}
Expand All @@ -51,11 +52,40 @@ func (ra *ResAccumulator) Vars() []types.Var {
return completeset.AsSlice()
}

// accumlatePatch accumulates the information regarding conflicting
// resources as patches.
func (ra *ResAccumulator) accumlatePatch(id resid.ResId, conflicting ...*resource.Resource) error {
target := types.Selector{
Gvk: gvk.Gvk{
Group: id.Group,
Version: id.Version,
Kind: id.Kind,
},
Namespace: id.Namespace,
Name: id.Name,
AnnotationSelector: "",
LabelSelector: "",
}

for _, res := range conflicting {
out, err := res.AsYAML()
if err != nil {
return err
}
newPatch := types.Patch{
Path: "",
Patch: string(out),
Target: target,
}
ra.patchSet = append(ra.patchSet, newPatch)
}
return nil
}

// HandoverConflictingResources removes conflicting resources from the local accumulator
// and add the conflicting resources list in the other accumulator.
// Conflicting is defined as have the same CurrentId but different Values.
func (ra *ResAccumulator) HandoverConflictingResources(other *ResAccumulator) error {
conflicting := []*resource.Resource{}
for _, rightResource := range other.ResMap().Resources() {
rightId := rightResource.CurId()
leftResources := ra.resMap.GetMatchingResourcesByCurrentId(rightId.Equals)
Expand All @@ -68,10 +98,16 @@ func (ra *ResAccumulator) HandoverConflictingResources(other *ResAccumulator) er
// TODO(jeb): Not sure we want to use DeepEqual here since there are some fields
// which are artifacts (nameprefix, namesuffix, refvar, refby) added to the resources
// by the algorithm here.
// Also we may be dropping here some of the artifacts (nameprefix, namesuffix,...)
// during the conversion from Resource/ResMap to Patch.
if len(leftResources) != 1 || !reflect.DeepEqual(leftResources[0], rightResource) {
// conflict detected. More than one resource or left and right are different.
conflicting = append(conflicting, rightResource)
conflicting = append(conflicting, leftResources...)
if err := other.accumlatePatch(rightId, rightResource); err != nil {
return err
}
if err := other.accumlatePatch(rightId, leftResources...); err != nil {
return err
}
}

// Remove the resource from that resMap
Expand All @@ -81,17 +117,14 @@ func (ra *ResAccumulator) HandoverConflictingResources(other *ResAccumulator) er
}
}

// TODO(jeb): Should the conflictingResources be a ResMap ?. We are dropping here
// some of the artifacts (nameprefix, namesuffix,...)
other.conflictingResources = append(other.conflictingResources, conflicting...)
return nil
}

// ConflictingResources return the list of resources that have been
// put aside. It will let the PatchTransformer decide how to handle
// the conflict, assuming the Transformer can.
func (ra *ResAccumulator) ConflictingResources() []*resource.Resource {
return ra.conflictingResources
func (ra *ResAccumulator) GetPatchSet() []types.Patch {
return ra.patchSet
}

func (ra *ResAccumulator) AppendAll(
Expand Down
26 changes: 10 additions & 16 deletions pkg/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type KustTarget struct {
rFactory *resmap.Factory
tFactory resmap.PatchFactory
pLdr *plugins.Loader
dynamic *types.Kustomization
}

// NewKustTarget returns a new instance of KustTarget primed with a Loader.
Expand Down Expand Up @@ -62,6 +63,7 @@ func NewKustTarget(
rFactory: rFactory,
tFactory: tFactory,
pLdr: pLdr,
dynamic: &types.Kustomization{},
}, nil
}

Expand Down Expand Up @@ -300,24 +302,16 @@ func (kt *KustTarget) configureExternalGenerators() ([]transformers.Generator, e
return kt.pLdr.LoadGenerators(kt.ldr, ra.ResMap())
}

func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
var r []transformers.Transformer
func (kt *KustTarget) absorbDynamicKustomization(ra *accumulator.ResAccumulator) {
orig := ra.GetPatchSet()
kt.dynamic.Patches = make([]types.Patch, len(orig))
copy(kt.dynamic.Patches, orig)
}

// Convertion to the buildin transformer is not compatible with this PR
// differed := ra.ConflictingResources()
// patches, err := kt.rFactory.RF().SliceFromPatches(
// kt.ldr, kt.kustomization.PatchesStrategicMerge)
// if err != nil {
// return errors.Wrapf(
// err, "reading strategic merge patches %v",
// kt.kustomization.PatchesStrategicMerge)
// }
// t, err := kt.tFactory.MakePatchTransformer(append(differed, patches...), kt.rFactory.RF())
// if err != nil {
// return err
// }
// r = append(r, t)
func (kt *KustTarget) runTransformers(ra *accumulator.ResAccumulator) error {
kt.absorbDynamicKustomization(ra)

var r []transformers.Transformer
tConfig := ra.GetTransformerConfig()
lts, err := kt.configureBuiltinTransformers(tConfig)
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions pkg/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package target

import (
"strings"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/v3/pkg/image"
"sigs.k8s.io/kustomize/v3/pkg/plugins"
Expand Down Expand Up @@ -167,18 +169,29 @@ func (kt *KustTarget) configureBuiltinPatchJson6902Transformer(
return
}

// Until Issue 1292 is implemented, use PathStrategicMerge to address
// when possible diamond merge issues.
func (kt *KustTarget) asString(patchSet []types.Patch) string {
res := []string{}
for _, patch := range patchSet {
res = append(res, patch.Patch)
}
return strings.Join(res, "---\n")
}

func (kt *KustTarget) configureBuiltinPatchStrategicMergeTransformer(
tConfig *config.TransformerConfig) (
result []transformers.Transformer, err error) {
if len(kt.kustomization.PatchesStrategicMerge) == 0 {
if len(kt.kustomization.PatchesStrategicMerge) == 0 && len(kt.dynamic.Patches) == 0 {
result = append(result, transformers.NewNoOpTransformer())
return
}
var c struct {
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
}
c.Paths = kt.kustomization.PatchesStrategicMerge
c.Patches = "" // Not implemented for kustomization file yet
c.Patches = kt.asString(kt.dynamic.Patches)
p := builtin.NewPatchStrategicMergeTransformerPlugin()
err = kt.configureBuiltinPlugin(p, c, "patchStrategicMerge")
if err != nil {
Expand Down

0 comments on commit c0f3009

Please sign in to comment.