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

runtime/patch: Add SerialPatcher #379

Merged
merged 1 commit into from
Oct 14, 2022
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
60 changes: 60 additions & 0 deletions runtime/patch/serial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2022 The Flux 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 patch

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/client"
)

// SerialPatcher provides serial patching of object using the patch helper. It
// remembers the state of the last patched object and uses that to calculate
// the patch aginst a new object.
type SerialPatcher struct {
client client.Client
beforeObject client.Object
}

// NewSerialPatcher returns a SerialPatcher with the given object as the initial
// base object for the patching operations.
func NewSerialPatcher(obj client.Object, c client.Client) *SerialPatcher {
return &SerialPatcher{
client: c,
beforeObject: obj.DeepCopyObject().(client.Object),
}
}

// Patch performs patching operation of the SerialPatcher and updates the
// beforeObject after a successful patch for subsequent patching.
func (sp *SerialPatcher) Patch(ctx context.Context, obj client.Object, options ...Option) error {
// Create a new patch helper with the before object.
patcher, err := NewHelper(sp.beforeObject, sp.client)
if err != nil {
return err
}

// Patch with the changes from the new object.
if err := patcher.Patch(ctx, obj, options...); err != nil {
return err
}

// Update the before object for next patch.
sp.beforeObject = obj.DeepCopyObject().(client.Object)

return nil
}
135 changes: 135 additions & 0 deletions runtime/patch/serial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright 2022 The Flux 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 patch

import (
"reflect"
"testing"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"
"github.com/fluxcd/pkg/runtime/conditions/testdata"
)

func TestSerialPatcher(t *testing.T) {
t.Run("should be able to patch object consecutively", func(t *testing.T) {
g := NewWithT(t)

testFinalizer := "test.finalizer.flux.io"
obj := &testdata.Fake{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Namespace: "default",
},
}

ownedConditions := []string{
meta.ReadyCondition,
meta.ReconcilingCondition,
meta.StalledCondition,
}

t.Log("Creating the object")
g.Expect(env.Create(ctx, obj)).To(Succeed())
defer func() {
g.Expect(env.Delete(ctx, obj)).To(Succeed())
}()
key := client.ObjectKeyFromObject(obj)

t.Log("Checking that the object has been created")
g.Eventually(func() error {
objAfter := obj.DeepCopy()
if err := env.Get(ctx, key, objAfter); err != nil {
return err
}
return nil
}).Should(Succeed())

t.Log("Creating a new serial patcher")
patcher := NewSerialPatcher(obj, env.Client)

t.Log("Add a finalizer")
controllerutil.AddFinalizer(obj, testFinalizer)

t.Log("Patching the object")
g.Expect(patcher.Patch(ctx, obj)).To(Succeed())

t.Log("Validating that the finalizer is added")
g.Eventually(func() bool {
objAfter := obj.DeepCopy()
if err := env.Get(ctx, key, objAfter); err != nil {
return false
}
return reflect.DeepEqual(obj.Finalizers, objAfter.Finalizers)
}, timeout).Should(BeTrue())

t.Log("Add status condition")
conditions.MarkReconciling(obj, "reason", "")
conditions.MarkFalse(obj, meta.ReadyCondition, "reason", "")

t.Log("Patch the object")
patchOpts := []Option{
WithOwnedConditions{ownedConditions},
}
g.Expect(patcher.Patch(ctx, obj, patchOpts...))

t.Log("Validating that the conditions are added")
g.Eventually(func() bool {
objAfter := obj.DeepCopy()
if err := env.Get(ctx, key, objAfter); err != nil {
return false
}
return !conditions.IsReady(objAfter) && conditions.IsReconciling(objAfter)
}, timeout).Should(BeTrue())

t.Log("Remove and update conditions")
conditions.Delete(obj, meta.ReconcilingCondition)
conditions.MarkTrue(obj, meta.ReadyCondition, "reason", "")

t.Log("Patch the object")
g.Expect(patcher.Patch(ctx, obj, patchOpts...))

t.Log("Validating that the conditions are updated")
g.Eventually(func() bool {
objAfter := obj.DeepCopy()
if err := env.Get(ctx, key, objAfter); err != nil {
return false
}
return conditions.IsReady(objAfter) && !conditions.IsReconciling(objAfter)
})

t.Log("Remove finalizer")
controllerutil.RemoveFinalizer(obj, testFinalizer)

t.Log("Patch the object")
g.Expect(patcher.Patch(ctx, obj)).To(Succeed())

t.Log("Validating that the finalizer is removed")
g.Eventually(func() bool {
objAfter := obj.DeepCopy()
if err := env.Get(ctx, key, objAfter); err != nil {
return false
}
return len(objAfter.Finalizers) == 0
}, timeout).Should(BeTrue())
})
}