Skip to content

Commit

Permalink
Address conflicting bases by converting resources into patches
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrette committed Jul 1, 2019
1 parent b9ef9b5 commit 0e1242d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
32 changes: 32 additions & 0 deletions pkg/accumulator/resaccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ package accumulator
import (
"fmt"
"log"
"reflect"
"strings"

"sigs.k8s.io/kustomize/v3/pkg/resid"
"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/config"
"sigs.k8s.io/kustomize/v3/pkg/types"
Expand Down Expand Up @@ -42,6 +44,36 @@ func (ra *ResAccumulator) Vars() []types.Var {
return ra.varSet.AsSlice()
}

// RemoveIds removes resources of the internal resMap which are member
// of the resId slice past in parameter.
// The removed Resources are returned by the method.
func (ra *ResAccumulator) RemoveConflicts(rightResources []*resource.Resource) ([]*resource.Resource, error) {
conflicting := []*resource.Resource{}
for _, rightResource := range rightResources {
rightId := rightResource.CurId()
leftResources := ra.resMap.GetMatchingResourcesByCurrentId(rightId.Equals)

if len(leftResources) == 0 {
// no conflict
continue
}

if len(leftResources) != 1 || !reflect.DeepEqual(leftResources[0], rightResource) {
// conflict detected. More than one resource or left and right are different.
conflicting = append(conflicting, leftResources...)
}

// Remove the resource from that resMap
err := ra.resMap.Remove(rightId)
if err != nil {
return nil, err
}
}
return conflicting, nil
}

// AllIds returns all CurrentIds.

func (ra *ResAccumulator) AppendAll(
resources resmap.ResMap) error {
return ra.resMap.AppendAll(resources)
Expand Down
12 changes: 4 additions & 8 deletions pkg/target/diamondcomposition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package target_test

import (
"strings"
"testing"

kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
Expand Down Expand Up @@ -125,14 +124,11 @@ resources:
- ../restart
`)

_, err := th.MakeKustTarget().MakeCustomizedResMap()
if err == nil {
t.Fatalf("Expected resource accumulation error")
}
if !strings.Contains(
err.Error(), "already registered id: apps_v1_Deployment|~X|my-deployment") {
t.Fatalf("Unexpected err: %v", err)
m, err := th.MakeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("Err: %v", err)
}
th.AssertActualEqualsExpected(m, expectedPatchedDeployment)
}

const expectedPatchedDeployment = `
Expand Down
23 changes: 23 additions & 0 deletions pkg/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,24 @@ func (kt *KustTarget) configureExternalTransformers() ([]transformers.Transforme
return kt.pLdr.LoadTransformers(kt.ldr, ra.ResMap())
}

func (kt *KustTarget) solveConflicts(ra *accumulator.ResAccumulator, subRa *accumulator.ResAccumulator) error {
conflicting, err := subRa.RemoveConflicts(ra.ResMap().Resources())
if err != nil {
return errors.Wrapf(
err, "converting diamond imported resources into patches %v",
subRa.ResMap().AllIds())
}

if len(conflicting) == 0 {
return nil
}
t, err := kt.tFactory.MakePatchTransformer((conflicting), kt.rFactory.RF())
if err != nil {
return err
}
return ra.Transform(t)
}

// accumulateResources fills the given resourceAccumulator
// with resources read from the given list of paths.
func (kt *KustTarget) accumulateResources(
Expand Down Expand Up @@ -364,6 +382,11 @@ func (kt *KustTarget) accumulateDirectory(
return errors.Wrapf(
err, "recursed accumulation of path '%s'", path)
}
err = subKt.solveConflicts(ra, subRa)
if err != nil {
return errors.Wrapf(
err, "recursed merging from path '%s'", path)
}
err = ra.MergeAccumulator(subRa)
if err != nil {
return errors.Wrapf(
Expand Down

0 comments on commit 0e1242d

Please sign in to comment.