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

197: commit objects to memory when fake K8s apply method is invoked #200

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
22 changes: 17 additions & 5 deletions pkg/k8s/fake/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

tektonv1beta1api "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
tektonv1beta1fakeclient "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/typed/pipeline/v1beta1/fake"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -39,9 +40,9 @@ func NewSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, error) {

var _ k8s.ClientInterface = (*DraconClientSet)(nil)

// ApplyHookType is the signature of the function that will be called to mock
// an Apply invocation
type ApplyHookType func(clientset ClientsetSubset, obj runtime.Object, namespace string, forceConflicts bool) error
// ApplyHookType is the signature of the function that will be called to
// examine the parameters that the apply function was called with.
type ApplyHookType func(obj runtime.Object, namespace string, forceConflicts bool) error

// ClientsetSubset is just the fake clientset struct
type ClientsetSubset struct {
Expand All @@ -52,6 +53,7 @@ type ClientsetSubset struct {
// DraconClientSet is a mock implementation of the `k8s.Clientset`
type DraconClientSet struct {
ClientsetSubset
objectTracker testing.ObjectTracker
discovery *fakediscovery.FakeDiscovery
ApplyHook ApplyHookType
MetaRESTMapper meta.RESTMapper
Expand All @@ -62,7 +64,7 @@ type DraconClientSet struct {
// a correct response for all known types offered by the `k8s.ClientInterface`.
func NewFakeTypedClient(objects ...runtime.Object) (DraconClientSet, error) {
return NewFakeTypedClientWithApplyHook(
func(_ ClientsetSubset, _ runtime.Object, _ string, _ bool) error { return nil },
func(_ runtime.Object, _ string, _ bool) error { return nil },
objects...,
)
}
Expand Down Expand Up @@ -96,6 +98,7 @@ func NewFakeTypedClientWithApplyHook(applyHook ApplyHookType, objects ...runtime
})

return DraconClientSet{
objectTracker: objectTracker,
discovery: &fakediscovery.FakeDiscovery{
Fake: &fakeCoreK8sClient.Fake,
FakedServerVersion: &version.Info{
Expand All @@ -118,7 +121,16 @@ func NewFakeTypedClientWithApplyHook(applyHook ApplyHookType, objects ...runtime

// Apply mocks the `kubectl apply`
func (f DraconClientSet) Apply(_ context.Context, obj runtime.Object, namespace string, forceConflicts bool) error {
return f.ApplyHook(f.ClientsetSubset, obj, namespace, forceConflicts)
if err := f.ApplyHook(obj, namespace, forceConflicts); err != nil {
return err
}
gvk := obj.GetObjectKind().GroupVersionKind()
gvr, _ := meta.UnsafeGuessKindToResource(gvk)

if err := f.objectTracker.Create(gvr, obj, namespace); k8serrors.IsAlreadyExists(err) {
return f.objectTracker.Update(gvr, obj, namespace)
}
return nil
}

// RESTMapper returns an instance implementing the `meta.RESTMapper` interface
Expand Down
Loading