Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add conditions patch utils #3102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions util/conditions/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright 2020 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 conditions

import (
"reflect"

"github.com/pkg/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

// Patch defines a list of operations to change a list of conditions into another
type Patch []PatchOperation

// PatchOperation define an operation that changes a single condition.
type PatchOperation struct {
Target *clusterv1.Condition
Base *clusterv1.Condition
Op PatchOperationType
}

// PatchOperationType defines patch operation types.
type PatchOperationType string

const (
// AddConditionPatch defines an add condition patch operation.
AddConditionPatch PatchOperationType = "Add"

// ChangeConditionPatch defines an change condition patch operation.
ChangeConditionPatch PatchOperationType = "Change"

// RemoveConditionPatch defines a remove condition patch operation.
RemoveConditionPatch PatchOperationType = "Remove"
)

fabriziopandini marked this conversation as resolved.
Show resolved Hide resolved
// NewPatch returns the list of Patch required to align source conditions to target conditions.
func NewPatch(base Getter, target Getter) Patch {
var patch Patch

// Identify AddCondition and ModifyCondition changes.
targetConditions := target.GetConditions()
for i := range targetConditions {
targetCondition := targetConditions[i]
currentCondition := Get(base, targetCondition.Type)
if currentCondition == nil {
patch = append(patch, PatchOperation{Op: AddConditionPatch, Target: &targetCondition})
continue
}

if !reflect.DeepEqual(&targetCondition, currentCondition) {
patch = append(patch, PatchOperation{Op: ChangeConditionPatch, Target: &targetCondition, Base: currentCondition})
}
}

// Identify RemoveCondition changes.
baseConditions := base.GetConditions()
for i := range baseConditions {
baseCondition := baseConditions[i]
targetCondition := Get(target, baseCondition.Type)
if targetCondition == nil {
patch = append(patch, PatchOperation{Op: RemoveConditionPatch, Base: &baseCondition})
}
}
return patch
}

// Apply executes a three-way merge of a list of Patch.
// When merge conflicts are detected (the source deviated from the original base recorded when creating the patch
// in an incompatible way), an error is returned.
func (p Patch) Apply(source Setter) error {
if len(p) == 0 {
return nil
}

for _, conditionPatch := range p {
switch conditionPatch.Op {
case AddConditionPatch:
// If the condition is already on source, check if source and target agree on the change;
// if not, this is a conflict.
if sourceCondition := Get(source, conditionPatch.Target.Type); sourceCondition != nil {
// If source and target agree on the change, then it is a conflict.
if !hasSameState(sourceCondition, conditionPatch.Target) {
return errors.Errorf("error patching conditions: The condition %q on was modified by a different process and this caused a merge/AddCondition conflict", conditionPatch.Target.Type)
}
// otherwise, the source is already as intended.
// NOTE: We are preserving LastTransitionTime from the source in order to avoid altering the existing value.
// XXX Set(source, sourceCondition)
continue
}
// If the condition does not exists on the source, add the new target condition.
Set(source, conditionPatch.Target)

case ChangeConditionPatch:
sourceCondition := Get(source, conditionPatch.Target.Type)

// If the condition does not exist anymore on the source, this is a conflict.
if sourceCondition == nil {
return errors.Errorf("error patching conditions: The condition %q on was deleted by a different process and this caused a merge/ChangeCondition conflict", conditionPatch.Target.Type)
}

// If the condition on the source is different from the base condition, check if
// the target state corresponds to the desired value. If not this is a conflict.
if !reflect.DeepEqual(sourceCondition, conditionPatch.Base) {
if !hasSameState(sourceCondition, conditionPatch.Target) {
return errors.Errorf("error patching conditions: The condition %q on was modified by a different process and this caused a merge/ChangeCondition conflict", conditionPatch.Target.Type)
}
// Otherwise the source is already as intended.
// NOTE: We are preserving LastTransitionTime from the source in order to avoid altering the existing value.
// XXX Set(source, sourceCondition)
continue
}

// Otherwise apply the new target condition.
Set(source, conditionPatch.Target)

case RemoveConditionPatch:
// If the condition is still on the source, check if it is changed in the meantime;
// if so then this is a conflict.
if sourceCondition := Get(source, conditionPatch.Base.Type); sourceCondition != nil {
if !hasSameState(sourceCondition, conditionPatch.Base) {
return errors.Errorf("error patching conditions: The condition %q on was modified by a different process and this caused a merge/RemoveCondition conflict", conditionPatch.Base.Type)
}
}
// Otherwise the source and target agreed on the delete operation, so there's nothing to change.
Delete(source, conditionPatch.Base.Type)
}
}
return nil
}
216 changes: 216 additions & 0 deletions util/conditions/patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
Copyright 2020 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 conditions

import (
"testing"

. "github.com/onsi/gomega"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

func TestNewPatch(t *testing.T) {
fooTrue := TrueCondition("foo")
fooFalse := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityInfo, "message foo")

tests := []struct {
name string
base Getter
target Getter
want Patch
}{
{
name: "No changes return empty patch",
base: getterWithConditions(),
target: getterWithConditions(),
want: nil,
},
{
name: "No changes return empty patch",
base: getterWithConditions(fooTrue),
target: getterWithConditions(fooTrue),
want: nil,
},
{
name: "Detects AddConditionPatch",
base: getterWithConditions(),
target: getterWithConditions(fooTrue),
want: Patch{
{
Base: nil,
Target: fooTrue,
Op: AddConditionPatch,
},
},
},
{
name: "Detects ChangeConditionPatch",
base: getterWithConditions(fooTrue),
target: getterWithConditions(fooFalse),
want: Patch{
{
Base: fooTrue,
Target: fooFalse,
Op: ChangeConditionPatch,
},
},
},
{
name: "Detects RemoveConditionPatch",
base: getterWithConditions(fooTrue),
target: getterWithConditions(),
want: Patch{
{
Base: fooTrue,
Target: nil,
Op: RemoveConditionPatch,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

got := NewPatch(tt.base, tt.target)

g.Expect(got).To(Equal(tt.want))
})
}
}

func TestApply(t *testing.T) {
fooTrue := TrueCondition("foo")
fooFalse := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityInfo, "message foo")
fooWarning := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityWarning, "message foo")

tests := []struct {
name string
base Getter
target Getter
source Setter
want clusterv1.Conditions
wantErr bool
}{
{
name: "No patch return same list",
base: getterWithConditions(fooTrue),
target: getterWithConditions(fooTrue),
source: setterWithConditions(fooTrue),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Add: When a condition does not exists, it should add",
base: getterWithConditions(),
target: getterWithConditions(fooTrue),
source: setterWithConditions(),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Add: When a condition already exists but without conflicts, it should add",
base: getterWithConditions(),
target: getterWithConditions(fooTrue),
source: setterWithConditions(fooTrue),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Add: When a condition already exists but with conflicts, it should error",
base: getterWithConditions(),
target: getterWithConditions(fooTrue),
source: setterWithConditions(fooFalse),
want: nil,
wantErr: true,
},
{
name: "Remove: When a condition was already deleted, it should pass",
base: getterWithConditions(fooTrue),
target: getterWithConditions(),
source: setterWithConditions(),
want: conditionList(),
wantErr: false,
},
{
name: "Remove: When a condition already exists but without conflicts, it should delete",
base: getterWithConditions(fooTrue),
target: getterWithConditions(),
source: setterWithConditions(fooTrue),
want: conditionList(),
wantErr: false,
},
{
name: "Remove: When a condition already exists but with conflicts, it should error",
base: getterWithConditions(fooTrue),
target: getterWithConditions(),
source: setterWithConditions(fooFalse),
want: nil,
wantErr: true,
},
{
name: "Change: When a condition exists without conflicts, it should change",
base: getterWithConditions(fooTrue),
target: getterWithConditions(fooFalse),
source: setterWithConditions(fooTrue),
want: conditionList(fooFalse),
wantErr: false,
},
{
name: "Change: When a condition exists with conflicts but there is agreement on the final state, it should change",
base: getterWithConditions(fooFalse),
target: getterWithConditions(fooTrue),
source: setterWithConditions(fooTrue),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Change: When a condition exists with conflicts but there is no agreement on the final state, it should error",
base: getterWithConditions(fooWarning),
target: getterWithConditions(fooFalse),
source: setterWithConditions(fooTrue),
want: nil,
wantErr: true,
},
{
name: "Change: When a condition was deleted, it should error",
base: getterWithConditions(fooTrue),
target: getterWithConditions(fooFalse),
source: setterWithConditions(),
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

patch := NewPatch(tt.base, tt.target)

err := patch.Apply(tt.source)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())

g.Expect(tt.source.GetConditions()).To(haveSameConditionsOf(tt.want))
})
}
}
Loading