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

     1  package kubestate
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  type State interface {
     8  	isState()
     9  
    10  	Terminal() bool
    11  	// Add() AddedState
    12  	// Update() UpdatedState
    13  	// Delete() DeletedState
    14  }
    15  
    16  type ExistsState interface {
    17  	State
    18  
    19  	isExistsState()
    20  }
    21  
    22  type AddedState interface {
    23  	ExistsState
    24  
    25  	isAddedState()
    26  }
    27  
    28  type UpdatedState interface {
    29  	ExistsState
    30  
    31  	isUpdatedState()
    32  }
    33  
    34  type DoesNotExistState interface {
    35  	State
    36  
    37  	isDoesNotExistState()
    38  }
    39  
    40  type DeletedState interface {
    41  	DoesNotExistState
    42  
    43  	isDeletedState()
    44  }
    45  
    46  type state struct{}
    47  
    48  func (s state) isState() {}
    49  
    50  func (s state) Terminal() bool {
    51  	// Not terminal by default
    52  	return false
    53  }
    54  
    55  func (s state) Add() AddedState {
    56  	return &addedState{
    57  		ExistsState: &existsState{
    58  			State: s,
    59  		},
    60  	}
    61  }
    62  
    63  func (s state) Update() UpdatedState {
    64  	return &updatedState{
    65  		ExistsState: &existsState{
    66  			State: s,
    67  		},
    68  	}
    69  }
    70  
    71  func (s state) Delete() DeletedState {
    72  	return &deletedState{
    73  		DoesNotExistState: &doesNotExistState{
    74  			State: s,
    75  		},
    76  	}
    77  }
    78  
    79  func NewState() State {
    80  	return &state{}
    81  }
    82  
    83  type existsState struct {
    84  	State
    85  }
    86  
    87  func (e existsState) isExistsState() {}
    88  
    89  type addedState struct {
    90  	ExistsState
    91  }
    92  
    93  func (a addedState) isAddedState() {}
    94  
    95  type updatedState struct {
    96  	ExistsState
    97  }
    98  
    99  func (u updatedState) isUpdatedState() {}
   100  
   101  type doesNotExistState struct {
   102  	State
   103  }
   104  
   105  func (d doesNotExistState) isDoesNotExistState() {}
   106  
   107  type deletedState struct {
   108  	DoesNotExistState
   109  }
   110  
   111  func (d deletedState) isDeletedState() {}
   112  
   113  type Reconciler interface {
   114  	Reconcile(ctx context.Context, in State) (out State, err error)
   115  }
   116  
   117  type ReconcilerFunc func(ctx context.Context, in State) (out State, err error)
   118  
   119  func (r ReconcilerFunc) Reconcile(ctx context.Context, in State) (out State, err error) {
   120  	return r(ctx, in)
   121  }
   122  
   123  type ReconcilerChain []Reconciler
   124  
   125  func (r ReconcilerChain) Reconcile(ctx context.Context, in State) (out State, err error) {
   126  	out = in
   127  	for _, rec := range r {
   128  		if out, err = rec.Reconcile(ctx, out); err != nil || out == nil || out.Terminal() {
   129  			break
   130  		}
   131  	}
   132  
   133  	return
   134  }
   135  
   136  // ResourceEventType tells an operator what kind of event has occurred on a given resource.
   137  type ResourceEventType string
   138  
   139  const (
   140  	// ResourceAdded tells the operator that a given resources has been added.
   141  	ResourceAdded ResourceEventType = "add"
   142  	// ResourceUpdated tells the operator that a given resources has been updated.
   143  	ResourceUpdated ResourceEventType = "update"
   144  	// ResourceDeleted tells the operator that a given resources has been deleted.
   145  	ResourceDeleted ResourceEventType = "delete"
   146  )
   147  
   148  type ResourceEvent interface {
   149  	Type() ResourceEventType
   150  	Resource() interface{}
   151  }
   152  
   153  type resourceEvent struct {
   154  	eventType ResourceEventType
   155  	resource  interface{}
   156  }
   157  
   158  func (r resourceEvent) Type() ResourceEventType {
   159  	return r.eventType
   160  }
   161  
   162  func (r resourceEvent) Resource() interface{} {
   163  	return r.resource
   164  }
   165  
   166  func NewResourceEvent(eventType ResourceEventType, resource interface{}) ResourceEvent {
   167  	return resourceEvent{
   168  		eventType: eventType,
   169  		resource:  resource,
   170  	}
   171  }
   172  
   173  type Notifier interface {
   174  	Notify(event ResourceEvent)
   175  }
   176  
   177  type NotifyFunc func(event ResourceEvent)
   178  
   179  // SyncFunc syncs resource events.
   180  type SyncFunc func(ctx context.Context, event ResourceEvent) error
   181  
   182  // Sync lets a sync func implement Syncer.
   183  func (s SyncFunc) Sync(ctx context.Context, event ResourceEvent) error {
   184  	return s(ctx, event)
   185  }
   186  
   187  // Syncer describes something that syncs resource events.
   188  type Syncer interface {
   189  	Sync(ctx context.Context, event ResourceEvent) error
   190  }