k3s/vendor/sigs.k8s.io/structured-merge-diff/v4/merge/update.go

303 lines
11 KiB
Go
Raw Normal View History

2019-08-30 18:33:25 +00:00
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package merge
import (
"fmt"
2020-08-10 17:43:49 +00:00
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
"sigs.k8s.io/structured-merge-diff/v4/typed"
2019-08-30 18:33:25 +00:00
)
// Converter is an interface to the conversion logic. The converter
// needs to be able to convert objects from one version to another.
type Converter interface {
2019-09-27 21:51:53 +00:00
Convert(object *typed.TypedValue, version fieldpath.APIVersion) (*typed.TypedValue, error)
2019-08-30 18:33:25 +00:00
IsMissingVersionError(error) bool
}
// Updater is the object used to compute updated FieldSets and also
// merge the object on Apply.
type Updater struct {
2020-08-10 17:43:49 +00:00
Converter Converter
IgnoredFields map[fieldpath.APIVersion]*fieldpath.Set
2019-09-27 21:51:53 +00:00
enableUnions bool
}
// EnableUnionFeature turns on union handling. It is disabled by default until the
// feature is complete.
func (s *Updater) EnableUnionFeature() {
s.enableUnions = true
2019-08-30 18:33:25 +00:00
}
2019-12-12 01:27:03 +00:00
func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, workflow string, force bool) (fieldpath.ManagedFields, *typed.Comparison, error) {
2019-08-30 18:33:25 +00:00
conflicts := fieldpath.ManagedFields{}
removed := fieldpath.ManagedFields{}
2019-09-27 21:51:53 +00:00
compare, err := oldObject.Compare(newObject)
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, nil, fmt.Errorf("failed to compare objects: %v", err)
2019-08-30 18:33:25 +00:00
}
2019-09-27 21:51:53 +00:00
versions := map[fieldpath.APIVersion]*typed.Comparison{
2020-08-10 17:43:49 +00:00
version: compare.ExcludeFields(s.IgnoredFields[version]),
2019-08-30 18:33:25 +00:00
}
for manager, managerSet := range managers {
if manager == workflow {
continue
}
2019-09-27 21:51:53 +00:00
compare, ok := versions[managerSet.APIVersion()]
2019-08-30 18:33:25 +00:00
if !ok {
var err error
2019-09-27 21:51:53 +00:00
versionedOldObject, err := s.Converter.Convert(oldObject, managerSet.APIVersion())
2019-08-30 18:33:25 +00:00
if err != nil {
if s.Converter.IsMissingVersionError(err) {
delete(managers, manager)
continue
}
2019-12-12 01:27:03 +00:00
return nil, nil, fmt.Errorf("failed to convert old object: %v", err)
2019-08-30 18:33:25 +00:00
}
2019-09-27 21:51:53 +00:00
versionedNewObject, err := s.Converter.Convert(newObject, managerSet.APIVersion())
2019-08-30 18:33:25 +00:00
if err != nil {
if s.Converter.IsMissingVersionError(err) {
delete(managers, manager)
continue
}
2019-12-12 01:27:03 +00:00
return nil, nil, fmt.Errorf("failed to convert new object: %v", err)
2019-08-30 18:33:25 +00:00
}
2019-09-27 21:51:53 +00:00
compare, err = versionedOldObject.Compare(versionedNewObject)
if err != nil {
2019-12-12 01:27:03 +00:00
return nil, nil, fmt.Errorf("failed to compare objects: %v", err)
2019-09-27 21:51:53 +00:00
}
2020-08-10 17:43:49 +00:00
versions[managerSet.APIVersion()] = compare.ExcludeFields(s.IgnoredFields[managerSet.APIVersion()])
2019-08-30 18:33:25 +00:00
}
2019-09-27 21:51:53 +00:00
conflictSet := managerSet.Set().Intersection(compare.Modified.Union(compare.Added))
2019-08-30 18:33:25 +00:00
if !conflictSet.Empty() {
2019-09-27 21:51:53 +00:00
conflicts[manager] = fieldpath.NewVersionedSet(conflictSet, managerSet.APIVersion(), false)
2019-08-30 18:33:25 +00:00
}
if !compare.Removed.Empty() {
2019-09-27 21:51:53 +00:00
removed[manager] = fieldpath.NewVersionedSet(compare.Removed, managerSet.APIVersion(), false)
2019-08-30 18:33:25 +00:00
}
}
if !force && len(conflicts) != 0 {
2019-12-12 01:27:03 +00:00
return nil, nil, ConflictsFromManagers(conflicts)
2019-08-30 18:33:25 +00:00
}
for manager, conflictSet := range conflicts {
2019-09-27 21:51:53 +00:00
managers[manager] = fieldpath.NewVersionedSet(managers[manager].Set().Difference(conflictSet.Set()), managers[manager].APIVersion(), managers[manager].Applied())
2019-08-30 18:33:25 +00:00
}
for manager, removedSet := range removed {
2019-09-27 21:51:53 +00:00
managers[manager] = fieldpath.NewVersionedSet(managers[manager].Set().Difference(removedSet.Set()), managers[manager].APIVersion(), managers[manager].Applied())
2019-08-30 18:33:25 +00:00
}
for manager := range managers {
2019-09-27 21:51:53 +00:00
if managers[manager].Set().Empty() {
2019-08-30 18:33:25 +00:00
delete(managers, manager)
}
}
2019-12-12 01:27:03 +00:00
return managers, compare, nil
2019-08-30 18:33:25 +00:00
}
// Update is the method you should call once you've merged your final
// object on CREATE/UPDATE/PATCH verbs. newObject must be the object
// that you intend to persist (after applying the patch if this is for a
// PATCH call), and liveObject must be the original object (empty if
// this is a CREATE call).
2019-09-27 21:51:53 +00:00
func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string) (*typed.TypedValue, fieldpath.ManagedFields, error) {
2019-08-30 18:33:25 +00:00
var err error
2019-09-27 21:51:53 +00:00
if s.enableUnions {
newObject, err = liveObject.NormalizeUnions(newObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
}
}
2019-08-30 18:33:25 +00:00
managers = shallowCopyManagers(managers)
2019-12-12 01:27:03 +00:00
managers, compare, err := s.update(liveObject, newObject, version, managers, manager, true)
2019-08-30 18:33:25 +00:00
if err != nil {
2019-09-27 21:51:53 +00:00
return nil, fieldpath.ManagedFields{}, err
2019-08-30 18:33:25 +00:00
}
if _, ok := managers[manager]; !ok {
2019-09-27 21:51:53 +00:00
managers[manager] = fieldpath.NewVersionedSet(fieldpath.NewSet(), version, false)
2019-08-30 18:33:25 +00:00
}
2020-08-10 17:43:49 +00:00
ignored := s.IgnoredFields[version]
if ignored == nil {
ignored = fieldpath.NewSet()
}
2019-09-27 21:51:53 +00:00
managers[manager] = fieldpath.NewVersionedSet(
2020-08-10 17:43:49 +00:00
managers[manager].Set().Union(compare.Modified).Union(compare.Added).Difference(compare.Removed).RecursiveDifference(ignored),
2019-09-27 21:51:53 +00:00
version,
false,
)
if managers[manager].Set().Empty() {
2019-08-30 18:33:25 +00:00
delete(managers, manager)
}
2019-09-27 21:51:53 +00:00
return newObject, managers, nil
2019-08-30 18:33:25 +00:00
}
// Apply should be called when Apply is run, given the current object as
// well as the configuration that is applied. This will merge the object
2020-03-26 21:07:15 +00:00
// and return it. If the object hasn't changed, nil is returned (the
// managers can still have changed though).
2019-09-27 21:51:53 +00:00
func (s *Updater) Apply(liveObject, configObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (*typed.TypedValue, fieldpath.ManagedFields, error) {
2019-08-30 18:33:25 +00:00
managers = shallowCopyManagers(managers)
2019-09-27 21:51:53 +00:00
var err error
if s.enableUnions {
configObject, err = configObject.NormalizeUnionsApply(configObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
}
}
2019-08-30 18:33:25 +00:00
newObject, err := liveObject.Merge(configObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to merge config: %v", err)
}
2019-09-27 21:51:53 +00:00
if s.enableUnions {
newObject, err = configObject.NormalizeUnionsApply(newObject)
if err != nil {
return nil, fieldpath.ManagedFields{}, err
}
}
2019-08-30 18:33:25 +00:00
lastSet := managers[manager]
set, err := configObject.ToFieldSet()
if err != nil {
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to get field set: %v", err)
}
2020-08-10 17:43:49 +00:00
ignored := s.IgnoredFields[version]
if ignored != nil {
set = set.RecursiveDifference(ignored)
// TODO: is this correct. If we don't remove from lastSet pruning might remove the fields?
if lastSet != nil {
lastSet.Set().RecursiveDifference(ignored)
}
}
2019-09-27 21:51:53 +00:00
managers[manager] = fieldpath.NewVersionedSet(set, version, true)
2019-08-30 18:33:25 +00:00
newObject, err = s.prune(newObject, managers, manager, lastSet)
if err != nil {
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to prune fields: %v", err)
}
2020-03-26 21:07:15 +00:00
managers, compare, err := s.update(liveObject, newObject, version, managers, manager, force)
2019-08-30 18:33:25 +00:00
if err != nil {
return nil, fieldpath.ManagedFields{}, err
}
2020-03-26 21:07:15 +00:00
if compare.IsSame() {
newObject = nil
}
2019-08-30 18:33:25 +00:00
return newObject, managers, nil
}
func shallowCopyManagers(managers fieldpath.ManagedFields) fieldpath.ManagedFields {
newManagers := fieldpath.ManagedFields{}
for manager, set := range managers {
newManagers[manager] = set
}
return newManagers
}
2020-08-10 17:43:49 +00:00
// prune will remove a field, list or map item, iff:
2019-08-30 18:33:25 +00:00
// * applyingManager applied it last time
// * applyingManager didn't apply it this time
// * no other applier claims to manage it
2019-09-27 21:51:53 +00:00
func (s *Updater) prune(merged *typed.TypedValue, managers fieldpath.ManagedFields, applyingManager string, lastSet fieldpath.VersionedSet) (*typed.TypedValue, error) {
if lastSet == nil || lastSet.Set().Empty() {
2019-08-30 18:33:25 +00:00
return merged, nil
}
2019-09-27 21:51:53 +00:00
convertedMerged, err := s.Converter.Convert(merged, lastSet.APIVersion())
2019-08-30 18:33:25 +00:00
if err != nil {
if s.Converter.IsMissingVersionError(err) {
return merged, nil
}
return nil, fmt.Errorf("failed to convert merged object to last applied version: %v", err)
}
2020-03-26 21:07:15 +00:00
2019-09-27 21:51:53 +00:00
pruned := convertedMerged.RemoveItems(lastSet.Set())
2019-08-30 18:33:25 +00:00
pruned, err = s.addBackOwnedItems(convertedMerged, pruned, managers, applyingManager)
if err != nil {
return nil, fmt.Errorf("failed add back owned items: %v", err)
}
pruned, err = s.addBackDanglingItems(convertedMerged, pruned, lastSet)
if err != nil {
return nil, fmt.Errorf("failed add back dangling items: %v", err)
}
2019-09-27 21:51:53 +00:00
return s.Converter.Convert(pruned, managers[applyingManager].APIVersion())
2019-08-30 18:33:25 +00:00
}
2020-08-10 17:43:49 +00:00
// addBackOwnedItems adds back any fields, list and map items that were removed by prune,
// but other appliers or updaters (or the current applier's new config) claim to own.
2019-09-27 21:51:53 +00:00
func (s *Updater) addBackOwnedItems(merged, pruned *typed.TypedValue, managedFields fieldpath.ManagedFields, applyingManager string) (*typed.TypedValue, error) {
2019-08-30 18:33:25 +00:00
var err error
managedAtVersion := map[fieldpath.APIVersion]*fieldpath.Set{}
for _, managerSet := range managedFields {
2020-08-10 17:43:49 +00:00
if _, ok := managedAtVersion[managerSet.APIVersion()]; !ok {
managedAtVersion[managerSet.APIVersion()] = fieldpath.NewSet()
2019-08-30 18:33:25 +00:00
}
2020-08-10 17:43:49 +00:00
managedAtVersion[managerSet.APIVersion()] = managedAtVersion[managerSet.APIVersion()].Union(managerSet.Set())
2019-08-30 18:33:25 +00:00
}
for version, managed := range managedAtVersion {
merged, err = s.Converter.Convert(merged, version)
if err != nil {
if s.Converter.IsMissingVersionError(err) {
continue
}
return nil, fmt.Errorf("failed to convert merged object at version %v: %v", version, err)
}
pruned, err = s.Converter.Convert(pruned, version)
if err != nil {
if s.Converter.IsMissingVersionError(err) {
continue
}
return nil, fmt.Errorf("failed to convert pruned object at version %v: %v", version, err)
}
mergedSet, err := merged.ToFieldSet()
if err != nil {
return nil, fmt.Errorf("failed to create field set from merged object at version %v: %v", version, err)
}
prunedSet, err := pruned.ToFieldSet()
if err != nil {
return nil, fmt.Errorf("failed to create field set from pruned object at version %v: %v", version, err)
}
pruned = merged.RemoveItems(mergedSet.Difference(prunedSet.Union(managed)))
}
return pruned, nil
}
2020-08-10 17:43:49 +00:00
// addBackDanglingItems makes sure that the fields list and map items removed by prune were
// previously owned by the currently applying manager. This will add back fields list and map items
// that are unowned or that are owned by Updaters and shouldn't be removed.
2019-09-27 21:51:53 +00:00
func (s *Updater) addBackDanglingItems(merged, pruned *typed.TypedValue, lastSet fieldpath.VersionedSet) (*typed.TypedValue, error) {
convertedPruned, err := s.Converter.Convert(pruned, lastSet.APIVersion())
2019-08-30 18:33:25 +00:00
if err != nil {
if s.Converter.IsMissingVersionError(err) {
return merged, nil
}
return nil, fmt.Errorf("failed to convert pruned object to last applied version: %v", err)
}
prunedSet, err := convertedPruned.ToFieldSet()
if err != nil {
return nil, fmt.Errorf("failed to create field set from pruned object in last applied version: %v", err)
}
mergedSet, err := merged.ToFieldSet()
if err != nil {
return nil, fmt.Errorf("failed to create field set from merged object in last applied version: %v", err)
}
2019-09-27 21:51:53 +00:00
return merged.RemoveItems(mergedSet.Difference(prunedSet).Intersection(lastSet.Set())), nil
2019-08-30 18:33:25 +00:00
}