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 support for v1beta2 conditions to patch helper #11150

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
5 changes: 4 additions & 1 deletion internal/test/builder/v1beta2_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,16 @@ type Phase2ObjStatusDeprecatedV1Beta1 struct {

// GetConditions returns the set of conditions for this object.
func (o *Phase2Obj) GetConditions() clusterv1.Conditions {
if o.Status.Deprecated == nil || o.Status.Deprecated.V1Beta1 == nil {
return nil
}
return o.Status.Deprecated.V1Beta1.Conditions
}

// SetConditions sets the conditions on this object.
func (o *Phase2Obj) SetConditions(conditions clusterv1.Conditions) {
if o.Status.Deprecated == nil && conditions != nil {
o.Status.Deprecated = &Phase2ObjStatusDeprecated{}
o.Status.Deprecated = &Phase2ObjStatusDeprecated{V1Beta1: &Phase2ObjStatusDeprecatedV1Beta1{}}
}
if o.Status.Deprecated.V1Beta1 == nil && conditions != nil {
o.Status.Deprecated.V1Beta1 = &Phase2ObjStatusDeprecatedV1Beta1{}
Expand Down
15 changes: 15 additions & 0 deletions util/conditions/v1beta2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,18 @@ type StepCounter bool
func (t StepCounter) ApplyToSummary(opts *SummaryOptions) {
opts.stepCounter = bool(t)
}

// OwnedConditionTypes allows to define condition types owned by the controller when performing patch apply.
// In case of conflicts for the owned conditions, the patch helper will always use the value provided by the controller.
func OwnedConditionTypes(conditionTypes ...string) ApplyOption {
return func(c *applyOptions) {
c.ownedConditionTypes = conditionTypes
}
}

// ForceOverwrite instructs patch apply to always use the value provided by the controller (no matter of what value exists currently).
func ForceOverwrite(v bool) ApplyOption {
return func(c *applyOptions) {
c.forceOverwrite = v
}
}
224 changes: 224 additions & 0 deletions util/conditions/v1beta2/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
Copyright 2024 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 v1beta2

import (
"reflect"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/cluster-api/util"
)

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

// PatchOperation defines an operation that changes a single condition.
type PatchOperation struct {
Before *metav1.Condition
After *metav1.Condition
Op PatchOperationType
}

// PatchOperationType defines a condition patch operation type.
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"
)

// NewPatch returns the Patch required to align source conditions to after conditions.
func NewPatch(before, after Getter) (Patch, error) {
var patch Patch

if util.IsNil(before) {
return nil, errors.New("error creating patch: before object is nil")
}
beforeConditions := before.GetV1Beta2Conditions()

if util.IsNil(after) {
return nil, errors.New("error creating patch: after object is nil")
}
afterConditions := after.GetV1Beta2Conditions()

// Identify AddCondition and ModifyCondition changes.
for i := range afterConditions {
afterCondition := afterConditions[i]
beforeCondition := meta.FindStatusCondition(beforeConditions, afterCondition.Type)
if beforeCondition == nil {
patch = append(patch, PatchOperation{Op: AddConditionPatch, After: &afterCondition})
continue
}

if !reflect.DeepEqual(&afterCondition, beforeCondition) {
fabriziopandini marked this conversation as resolved.
Show resolved Hide resolved
patch = append(patch, PatchOperation{Op: ChangeConditionPatch, After: &afterCondition, Before: beforeCondition})
}
}

// Identify RemoveCondition changes.
for i := range beforeConditions {
beforeCondition := beforeConditions[i]
afterCondition := meta.FindStatusCondition(afterConditions, beforeCondition.Type)
if afterCondition == nil {
patch = append(patch, PatchOperation{Op: RemoveConditionPatch, Before: &beforeCondition})
}
}
return patch, nil
}

// applyOptions allows to set strategies for patch apply.
type applyOptions struct {
ownedConditionTypes []string
forceOverwrite bool
}

func (o *applyOptions) isOwnedConditionType(conditionType string) bool {
for _, i := range o.ownedConditionTypes {
if i == conditionType {
return true
}
}
return false
}

fabriziopandini marked this conversation as resolved.
Show resolved Hide resolved
// ApplyOption defines an option for applying a condition patch.
type ApplyOption func(*applyOptions)

// Apply executes a three-way merge of a list of Patch.
// When merge conflicts are detected (latest deviated from before in an incompatible way), an error is returned.
func (p Patch) Apply(latest Setter, options ...ApplyOption) error {
if p.IsZero() {
return nil
}

if util.IsNil(latest) {
return errors.New("error patching conditions: latest object is nil")
}
latestConditions := latest.GetV1Beta2Conditions()

applyOpt := &applyOptions{}
for _, o := range options {
if util.IsNil(o) {
return errors.New("error patching conditions: ApplyOption is nil")
}
o(applyOpt)
}

for _, conditionPatch := range p {
switch conditionPatch.Op {
case AddConditionPatch:
// If the condition is owned, always keep the after value.
if applyOpt.forceOverwrite || applyOpt.isOwnedConditionType(conditionPatch.After.Type) {
meta.SetStatusCondition(&latestConditions, *conditionPatch.After)
continue
}

// If the condition is already on latest, check if latest and after agree on the change; if not, this is a conflict.
if latestCondition := meta.FindStatusCondition(latestConditions, conditionPatch.After.Type); latestCondition != nil {
// If latest and after disagree on the change, then it is a conflict
if !hasSameState(latestCondition, conditionPatch.After) {
return errors.Errorf("error patching conditions: The condition %q was modified by a different process and this caused a merge/AddCondition conflict: %v", conditionPatch.After.Type, cmp.Diff(latestCondition, conditionPatch.After))
}
// otherwise, the latest is already as intended.
// NOTE: We are preserving LastTransitionTime from the latest in order to avoid altering the existing value.
continue
}
// If the condition does not exists on the latest, add the new after condition.
meta.SetStatusCondition(&latestConditions, *conditionPatch.After)

case ChangeConditionPatch:
// If the conditions is owned, always keep the after value.
if applyOpt.forceOverwrite || applyOpt.isOwnedConditionType(conditionPatch.After.Type) {
meta.SetStatusCondition(&latestConditions, *conditionPatch.After)
continue
}

latestCondition := meta.FindStatusCondition(latestConditions, conditionPatch.After.Type)

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

// If the condition on the latest is different from the base condition, check if
// the after state corresponds to the desired value. If not this is a conflict (unless we should ignore conflicts for this condition type).
if !reflect.DeepEqual(latestCondition, conditionPatch.Before) {
if !hasSameState(latestCondition, conditionPatch.After) {
return errors.Errorf("error patching conditions: The condition %q was modified by a different process and this caused a merge/ChangeCondition conflict: %v", conditionPatch.After.Type, cmp.Diff(latestCondition, conditionPatch.After))
}
// Otherwise the latest is already as intended.
// NOTE: We are preserving LastTransitionTime from the latest in order to avoid altering the existing value.
continue
}
// Otherwise apply the new after condition.
meta.SetStatusCondition(&latestConditions, *conditionPatch.After)

case RemoveConditionPatch:
// If latestConditions is nil or empty, nothing to remove.
if len(latestConditions) == 0 {
continue
}

// If the conditions is owned, always keep the after value (condition should be deleted).
if applyOpt.forceOverwrite || applyOpt.isOwnedConditionType(conditionPatch.Before.Type) {
meta.RemoveStatusCondition(&latestConditions, conditionPatch.Before.Type)
continue
}

// If the condition is still on the latest, check if it is changed in the meantime;
// if so then this is a conflict.
if latestCondition := meta.FindStatusCondition(latestConditions, conditionPatch.Before.Type); latestCondition != nil {
if !hasSameState(latestCondition, conditionPatch.Before) {
return errors.Errorf("error patching conditions: The condition %q was modified by a different process and this caused a merge/RemoveCondition conflict: %v", conditionPatch.Before.Type, cmp.Diff(latestCondition, conditionPatch.Before))
}
}
// Otherwise the latest and after agreed on the delete operation, so there's nothing to change.
meta.RemoveStatusCondition(&latestConditions, conditionPatch.Before.Type)
}
}

latest.SetV1Beta2Conditions(latestConditions)
return nil
}

// IsZero returns true if the patch is nil or has no changes.
func (p Patch) IsZero() bool {
if p == nil {
return true
}
return len(p) == 0
}

// hasSameState returns true if a condition has the same state of another; state is defined
// by the union of following fields: Type, Status, Reason, ObservedGeneration and Message (it excludes LastTransitionTime).
func hasSameState(i, j *metav1.Condition) bool {
return i.Type == j.Type &&
i.Status == j.Status &&
i.ObservedGeneration == j.ObservedGeneration &&
i.Reason == j.Reason &&
i.Message == j.Message
}
Loading
Loading