istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/kube/kubetypes/types.go (about)

     1  // Copyright Istio 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 kubetypes
    16  
    17  import (
    18  	"context"
    19  
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  	"k8s.io/apimachinery/pkg/runtime/schema"
    23  	"k8s.io/apimachinery/pkg/types"
    24  	"k8s.io/apimachinery/pkg/watch"
    25  
    26  	"istio.io/istio/pkg/cluster"
    27  	"istio.io/istio/pkg/util/sets"
    28  )
    29  
    30  type InformerOptions struct {
    31  	// A selector to restrict the list of returned objects by their labels.
    32  	LabelSelector string
    33  	// A selector to restrict the list of returned objects by their fields.
    34  	FieldSelector string
    35  	// Namespace to watch.
    36  	Namespace string
    37  	// Cluster name for watch error handlers
    38  	Cluster cluster.ID
    39  	// ObjectTransform allows arbitrarily modifying objects stored in the underlying cache.
    40  	// If unset, a default transform is provided to remove ManagedFields (high cost, low value)
    41  	ObjectTransform func(obj any) (any, error)
    42  	// InformerType dictates the type of informer that should be created.
    43  	InformerType InformerType
    44  }
    45  
    46  type InformerType int
    47  
    48  const (
    49  	StandardInformer InformerType = iota
    50  	DynamicInformer
    51  	MetadataInformer
    52  )
    53  
    54  type DynamicObjectFilter interface {
    55  	// Filter returns true if the input object or namespace string resides in a namespace selected for discovery
    56  	Filter(obj any) bool
    57  	// AddHandler registers a handler on namespace, which will be triggered when namespace selected or deselected.
    58  	AddHandler(func(selected, deselected sets.String))
    59  }
    60  
    61  type staticFilter struct {
    62  	f func(obj interface{}) bool
    63  }
    64  
    65  func (s staticFilter) Filter(obj any) bool {
    66  	return s.f(obj)
    67  }
    68  
    69  func (s staticFilter) AddHandler(func(selected, deselected sets.String)) {
    70  	// Do nothing
    71  }
    72  
    73  var _ DynamicObjectFilter = staticFilter{}
    74  
    75  // NewStaticObjectFilter returns a DynamicObjectFilter that does not ever change (so does not need an AddHandler)
    76  func NewStaticObjectFilter(f func(obj any) bool) DynamicObjectFilter {
    77  	return staticFilter{f}
    78  }
    79  
    80  // Filter allows filtering read operations
    81  type Filter struct {
    82  	// A selector to restrict the list of returned objects by their labels.
    83  	// This is a *server side* filter.
    84  	LabelSelector string
    85  	// A selector to restrict the list of returned objects by their fields.
    86  	// This is a *server side* filter.
    87  	FieldSelector string
    88  	// Namespace to watch.
    89  	// This is a *server side* filter.
    90  	Namespace string
    91  	// ObjectFilter allows arbitrary filtering logic.
    92  	// This is a *client side* filter. This means CPU/memory costs are still present for filtered objects.
    93  	// Use LabelSelector or FieldSelector instead, if possible.
    94  	ObjectFilter DynamicObjectFilter
    95  	// ObjectTransform allows arbitrarily modifying objects stored in the underlying cache.
    96  	// If unset, a default transform is provided to remove ManagedFields (high cost, low value)
    97  	ObjectTransform func(obj any) (any, error)
    98  }
    99  
   100  // CrdWatcher exposes an interface to watch CRDs
   101  type CrdWatcher interface {
   102  	// HasSynced returns true once all existing state has been synced.
   103  	HasSynced() bool
   104  	// KnownOrCallback returns `true` immediately if the resource is known.
   105  	// If it is not known, `false` is returned. If the resource is later added, the callback will be triggered.
   106  	KnownOrCallback(s schema.GroupVersionResource, f func(stop <-chan struct{})) bool
   107  	// WaitForCRD waits until the request CRD exists, and returns true on success. A false return value
   108  	// indicates the CRD does not exist but the wait failed or was canceled.
   109  	// This is useful to conditionally enable controllers based on CRDs being created.
   110  	WaitForCRD(s schema.GroupVersionResource, stop <-chan struct{}) bool
   111  	// Run starts the controller. This must be called.
   112  	Run(stop <-chan struct{})
   113  }
   114  
   115  // DelayedFilter exposes an interface for a filter create delayed informers, which start
   116  // once the underlying resource is available. See kclient.NewDelayedInformer.
   117  type DelayedFilter interface {
   118  	HasSynced() bool
   119  	KnownOrCallback(f func(stop <-chan struct{})) bool
   120  }
   121  
   122  // WriteAPI exposes a generic API for a client go type for write operations.
   123  type WriteAPI[T runtime.Object] interface {
   124  	Create(ctx context.Context, object T, opts metav1.CreateOptions) (T, error)
   125  	Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result T, err error)
   126  	Update(ctx context.Context, object T, opts metav1.UpdateOptions) (T, error)
   127  	Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
   128  }
   129  
   130  // WriteAPI exposes a generic API for a client go type for status operations.
   131  // Not all types have status, so they need to be split out
   132  type WriteStatusAPI[T runtime.Object] interface {
   133  	UpdateStatus(ctx context.Context, object T, opts metav1.UpdateOptions) (T, error)
   134  }
   135  
   136  // ReadAPI exposes a generic API for a client go type for read operations.
   137  type ReadAPI[T runtime.Object, TL runtime.Object] interface {
   138  	Get(ctx context.Context, name string, opts metav1.GetOptions) (T, error)
   139  	List(ctx context.Context, opts metav1.ListOptions) (TL, error)
   140  	Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error)
   141  }
   142  
   143  // ReadWriteAPI exposes a generic API for read and write operations.
   144  type ReadWriteAPI[T runtime.Object, TL runtime.Object] interface {
   145  	ReadAPI[T, TL]
   146  	WriteAPI[T]
   147  }
   148  
   149  // ApplyAPI exposes a generic API for a client go type for apply operations.
   150  type ApplyAPI[T runtime.Object, TA runtime.Object] interface {
   151  	Apply(ctx context.Context, secret TA, opts metav1.ApplyOptions) (result T, err error)
   152  }
   153  
   154  // FullAPI exposes a generic API for a client go type for all operations.
   155  // Note each type can also have per-type specific "Expansions" not covered here.
   156  type FullAPI[T runtime.Object, TL runtime.Object, TA runtime.Object] interface {
   157  	ReadAPI[T, TL]
   158  	WriteAPI[T]
   159  	ApplyAPI[T, TA]
   160  }