Skip to content

Commit

Permalink
Fix unit tests after SSA refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <[email protected]>
  • Loading branch information
turkenh committed Jun 2, 2024
1 parent 7040857 commit db2a37c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 141 deletions.
31 changes: 31 additions & 0 deletions internal/controller/object/fake/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fake

import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/crossplane-contrib/provider-kubernetes/apis/object/v1alpha2"
)

// A ResourceSyncer is a fake ResourceSyncer.
type ResourceSyncer struct {
GetObservedStateFn func(ctx context.Context, obj *v1alpha2.Object, current *unstructured.Unstructured) (*unstructured.Unstructured, error)
GetDesiredStateFn func(ctx context.Context, obj *v1alpha2.Object, manifest *unstructured.Unstructured) (*unstructured.Unstructured, error)
SyncResourceFn func(ctx context.Context, obj *v1alpha2.Object, desired *unstructured.Unstructured) (*unstructured.Unstructured, error)
}

// GetObservedState calls the GetObservedStateFn.
func (r *ResourceSyncer) GetObservedState(ctx context.Context, obj *v1alpha2.Object, current *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return r.GetObservedStateFn(ctx, obj, current)
}

// GetDesiredState calls the GetDesiredStateFn.
func (r *ResourceSyncer) GetDesiredState(ctx context.Context, obj *v1alpha2.Object, manifest *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return r.GetDesiredStateFn(ctx, obj, manifest)
}

// SyncResource calls the SyncResourceFn.
func (r *ResourceSyncer) SyncResource(ctx context.Context, obj *v1alpha2.Object, desired *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return r.SyncResourceFn(ctx, obj, desired)
}
38 changes: 24 additions & 14 deletions internal/controller/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,20 @@ type ResourceSyncer interface {
// GetObservedState extracts the observed state of the current object that
// should be compared with the desired state of the object manifest to
// decide whether the object is up-to-date or not.
// Without server-side apply, the observed state is extracted from the last
// applied annotation, otherwise it is extracted from the current object
// using the server-side apply extractor.
GetObservedState(ctx context.Context, obj *v1alpha2.Object, current *unstructured.Unstructured) (*unstructured.Unstructured, error)
// GetDesiredState calculates the desired state of the object manifest that
// we would like to see at the Kube API so that we can compare it with the
// observed state to decide whether the object is up-to-date or not.
// Without server-side apply, the desired state is the object manifest
// itself, however, with server-side apply, the desired state is extracted
// with a dry-run apply of the object manifest. This is mostly a workaround
// for a limitation/bug in the server-side apply implementation due to poor
// handling of defaulting in certain cases.
// https://github.com/kubernetes/kubernetes/issues/115563
// https://github.com/kubernetes/kubernetes/issues/124605
GetDesiredState(ctx context.Context, obj *v1alpha2.Object, manifest *unstructured.Unstructured) (*unstructured.Unstructured, error)
// SyncResource syncs the desired state of the object manifest to the Kube API.
SyncResource(ctx context.Context, obj *v1alpha2.Object, desired *unstructured.Unstructured) (*unstructured.Unstructured, error)
Expand Down Expand Up @@ -297,30 +307,30 @@ type external struct {
}

func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) { // nolint:gocyclo, mostly branches due to feature flags, hopefully will be refactored once they are promoted
cr, ok := mg.(*v1alpha2.Object)
obj, ok := mg.(*v1alpha2.Object)
if !ok {
return managed.ExternalObservation{}, errors.New(errNotKubernetesObject)
}

c.logger.Debug("Observing", "resource", cr)
c.logger.Debug("Observing", "resource", obj)

if !meta.WasDeleted(cr) {
if !meta.WasDeleted(obj) {
// If the object is not being deleted, we need to resolve references
if err := c.resolveReferencies(ctx, cr); err != nil {
if err := c.resolveReferencies(ctx, obj); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errResolveResourceReferences)
}
}

res, err := parseManifest(cr)
manifest, err := parseManifest(obj)
if err != nil {
return managed.ExternalObservation{}, err
}

if c.shouldWatch(cr) {
c.kindObserver.WatchResources(c.rest, cr.Spec.ProviderConfigReference.Name, res.GroupVersionKind())
if c.shouldWatch(obj) {
c.kindObserver.WatchResources(c.rest, obj.Spec.ProviderConfigReference.Name, manifest.GroupVersionKind())
}

current := res.DeepCopy()
current := manifest.DeepCopy()
err = c.client.Get(ctx, types.NamespacedName{
Namespace: current.GetNamespace(),
Name: current.GetName(),
Expand All @@ -334,27 +344,27 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.Wrap(err, errGetObject)
}

if err = c.setAtProvider(cr, current); err != nil {
if err = c.setAtProvider(obj, current); err != nil {
return managed.ExternalObservation{}, err
}

// observedState contains the extracted state of the current object that
// should be compared with the res state of the object manifest to
// decide whether the object is up-to-date or not.
// should be compared with the desired state of the object to decide whether
// the object is up-to-date or not.
// If serverSideApply is enabled, we will extract the state from the
// current object, otherwise we will extract the state from the last
// applied annotation.
var observedState *unstructured.Unstructured
if observedState, err = c.syncer.GetObservedState(ctx, cr, current); err != nil {
if observedState, err = c.syncer.GetObservedState(ctx, obj, current); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errGetObservedState)
}

var desiredState *unstructured.Unstructured
if desiredState, err = c.syncer.GetDesiredState(ctx, cr, res); err != nil {
if desiredState, err = c.syncer.GetDesiredState(ctx, obj, manifest); err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, errGetDesiredState)
}

return c.handleObservation(ctx, cr, observedState, desiredState)
return c.handleObservation(ctx, obj, observedState, desiredState)
}

func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
Expand Down
Loading

0 comments on commit db2a37c

Please sign in to comment.