github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/clientfake/client_options.go (about)

     1  package clientfake
     2  
     3  import (
     4  	"testing"
     5  
     6  	"k8s.io/apimachinery/pkg/api/meta"
     7  	"k8s.io/apimachinery/pkg/runtime"
     8  	clitesting "k8s.io/client-go/testing"
     9  )
    10  
    11  // Option configures a ClientsetDecorator
    12  type Option func(ClientsetDecorator)
    13  
    14  // WithSelfLinks returns a fakeClientOption that configures a ClientsetDecorator to write selfLinks to all OLM types on create.
    15  func WithSelfLinks(tb testing.TB) Option {
    16  	return func(c ClientsetDecorator) {
    17  		c.PrependReactor("create", "*", func(a clitesting.Action) (bool, runtime.Object, error) {
    18  			ca, ok := a.(clitesting.CreateAction)
    19  			if !ok {
    20  				tb.Fatalf("expected CreateAction")
    21  			}
    22  
    23  			obj := ca.GetObject()
    24  			accessor, err := meta.Accessor(obj)
    25  			if err != nil {
    26  				return false, nil, err
    27  			}
    28  			if accessor.GetSelfLink() != "" {
    29  				// SelfLink is already set
    30  				return false, nil, nil
    31  			}
    32  
    33  			gvr := ca.GetResource()
    34  			accessor.SetSelfLink(BuildSelfLink(gvr.GroupVersion().String(), gvr.Resource, accessor.GetNamespace(), accessor.GetName()))
    35  
    36  			return false, obj, nil
    37  		})
    38  	}
    39  }
    40  
    41  // WithNameGeneration returns a fakeK8sClientOption that configures a Clientset to write generated names to all types on create.
    42  func WithNameGeneration(tb testing.TB) Option {
    43  	return func(c ClientsetDecorator) {
    44  		c.PrependReactor("create", "*", func(a clitesting.Action) (bool, runtime.Object, error) {
    45  			ca, ok := a.(clitesting.CreateAction)
    46  			if !ok {
    47  				tb.Fatalf("expected CreateAction")
    48  			}
    49  
    50  			return false, AddSimpleGeneratedName(ca.GetObject()), nil
    51  		})
    52  	}
    53  }