mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
This PR - defines a patch conflict detector interface, - extracts implementations of the interface from the merginator code, making the merginator code independent of --enable_kyaml. - injects those implementations into kustomize as a function of --enable_kyaml. So, instead of using different merginators to combine resmaps, this pr allows the use of a single patch merge code path that uses different conflict detectors. So instead of debating how to merge, we're now only considering whether to warn on conflict detection in one transformer. This PR is in service of #3304, eliminating seven instances where --enable_kyaml was consulted. These were cases where conflict detection wasn't an issue (but merging patches was).
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package conflict
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
|
"k8s.io/apimachinery/pkg/util/mergepatch"
|
|
"sigs.k8s.io/kustomize/api/resource"
|
|
)
|
|
|
|
// conflictDetectorJson detects conflicts in a list of JSON patches.
|
|
type conflictDetectorJson struct {
|
|
resourceFactory *resource.Factory
|
|
}
|
|
|
|
var _ resource.ConflictDetector = &conflictDetectorJson{}
|
|
|
|
func (cd *conflictDetectorJson) HasConflict(
|
|
p1, p2 *resource.Resource) (bool, error) {
|
|
return mergepatch.HasConflicts(p1.Map(), p2.Map())
|
|
}
|
|
|
|
func (cd *conflictDetectorJson) MergePatches(
|
|
patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
|
baseBytes, err := json.Marshal(patch1.Map())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
patchBytes, err := json.Marshal(patch2.Map())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mergedBytes, err := jsonpatch.MergeMergePatches(baseBytes, patchBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mergedMap := make(map[string]interface{})
|
|
err = json.Unmarshal(mergedBytes, &mergedMap)
|
|
return cd.resourceFactory.FromMap(mergedMap), err
|
|
}
|