github.skymusic.top/operator-framework/operator-sdk@v0.8.2/pkg/test/client.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	goctx "context"
    19  	"fmt"
    20  
    21  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/apimachinery/pkg/util/wait"
    24  	dynclient "sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  type frameworkClient struct {
    28  	dynclient.Client
    29  }
    30  
    31  var _ FrameworkClient = &frameworkClient{}
    32  
    33  type FrameworkClient interface {
    34  	Get(gCtx goctx.Context, key dynclient.ObjectKey, obj runtime.Object) error
    35  	List(gCtx goctx.Context, opts *dynclient.ListOptions, list runtime.Object) error
    36  	Create(gCtx goctx.Context, obj runtime.Object, cleanupOptions *CleanupOptions) error
    37  	Delete(gCtx goctx.Context, obj runtime.Object, opts ...dynclient.DeleteOptionFunc) error
    38  	Update(gCtx goctx.Context, obj runtime.Object) error
    39  }
    40  
    41  // Create uses the dynamic client to create an object and then adds a
    42  // cleanup function to delete it when Cleanup is called. In addition to
    43  // the standard controller-runtime client options
    44  func (f *frameworkClient) Create(gCtx goctx.Context, obj runtime.Object, cleanupOptions *CleanupOptions) error {
    45  	objCopy := obj.DeepCopyObject()
    46  	err := f.Client.Create(gCtx, obj)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	// if no test context exists, cannot add finalizer function or print to testing log
    51  	if cleanupOptions == nil || cleanupOptions.TestContext == nil {
    52  		return nil
    53  	}
    54  	key, err := dynclient.ObjectKeyFromObject(objCopy)
    55  	// this function fails silently if t is nil
    56  	if cleanupOptions.TestContext.t != nil {
    57  		cleanupOptions.TestContext.t.Logf("resource type %+v with namespace/name (%+v) created\n", objCopy.GetObjectKind().GroupVersionKind().Kind, key)
    58  	}
    59  	cleanupOptions.TestContext.AddCleanupFn(func() error {
    60  		err = f.Client.Delete(gCtx, objCopy)
    61  		if err != nil && !apierrors.IsNotFound(err) {
    62  			return err
    63  		}
    64  		if cleanupOptions.Timeout == 0 && cleanupOptions.RetryInterval != 0 {
    65  			return fmt.Errorf("retry interval is set but timeout is not; cannot poll for cleanup")
    66  		} else if cleanupOptions.Timeout != 0 && cleanupOptions.RetryInterval == 0 {
    67  			return fmt.Errorf("timeout is set but retry interval is not; cannot poll for cleanup")
    68  		}
    69  		if cleanupOptions.Timeout != 0 && cleanupOptions.RetryInterval != 0 {
    70  			return wait.PollImmediate(cleanupOptions.RetryInterval, cleanupOptions.Timeout, func() (bool, error) {
    71  				err = f.Client.Get(gCtx, key, objCopy)
    72  				if err != nil {
    73  					if apierrors.IsNotFound(err) {
    74  						if cleanupOptions.TestContext.t != nil {
    75  							cleanupOptions.TestContext.t.Logf("resource type %+v with namespace/name (%+v) successfully deleted\n", objCopy.GetObjectKind().GroupVersionKind().Kind, key)
    76  						}
    77  						return true, nil
    78  					}
    79  					return false, fmt.Errorf("error encountered during deletion of resource type %v with namespace/name (%+v): %v", objCopy.GetObjectKind().GroupVersionKind().Kind, key, err)
    80  				}
    81  				if cleanupOptions.TestContext.t != nil {
    82  					cleanupOptions.TestContext.t.Logf("waiting for deletion of resource type %+v with namespace/name (%+v)\n", objCopy.GetObjectKind().GroupVersionKind().Kind, key)
    83  				}
    84  				return false, nil
    85  			})
    86  		}
    87  		return nil
    88  	})
    89  	return nil
    90  }
    91  
    92  func (f *frameworkClient) Get(gCtx goctx.Context, key dynclient.ObjectKey, obj runtime.Object) error {
    93  	return f.Client.Get(gCtx, key, obj)
    94  }
    95  
    96  func (f *frameworkClient) List(gCtx goctx.Context, opts *dynclient.ListOptions, list runtime.Object) error {
    97  	return f.Client.List(gCtx, opts, list)
    98  }
    99  
   100  func (f *frameworkClient) Delete(gCtx goctx.Context, obj runtime.Object, opts ...dynclient.DeleteOptionFunc) error {
   101  	return f.Client.Delete(gCtx, obj, opts...)
   102  }
   103  
   104  func (f *frameworkClient) Update(gCtx goctx.Context, obj runtime.Object) error {
   105  	return f.Client.Update(gCtx, obj)
   106  }