istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/kube/kclient/interfaces.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 kclient
    16  
    17  import (
    18  	klabels "k8s.io/apimachinery/pkg/labels"
    19  	apitypes "k8s.io/apimachinery/pkg/types"
    20  	"k8s.io/client-go/tools/cache"
    21  
    22  	"istio.io/istio/pkg/kube/controllers"
    23  )
    24  
    25  type Untyped = Informer[controllers.Object]
    26  
    27  // Reader wraps a Kubernetes client providing cached read access.
    28  // This is based on informers, so most of the same caveats to informers apply here.
    29  type Reader[T controllers.Object] interface {
    30  	// Get looks up an object by name and namespace. If it does not exist, nil is returned
    31  	Get(name, namespace string) T
    32  	// List looks up an object by namespace and labels.
    33  	// Use metav1.NamespaceAll and klabels.Everything() to select everything.
    34  	List(namespace string, selector klabels.Selector) []T
    35  }
    36  
    37  type Informer[T controllers.Object] interface {
    38  	Reader[T]
    39  	// ListUnfiltered is like List but ignores any *client side* filters previously configured.
    40  	ListUnfiltered(namespace string, selector klabels.Selector) []T
    41  	// AddEventHandler inserts a handler. The handler will be called for all Create/Update/Removals.
    42  	// When ShutdownHandlers is called, the handler is removed.
    43  	AddEventHandler(h cache.ResourceEventHandler)
    44  	// HasSynced returns true when the informer is initially populated and that all handlers added
    45  	// via AddEventHandler have been called with the initial state.
    46  	// note: this differs from a standard informer HasSynced, which does not check handlers have been called.
    47  	HasSynced() bool
    48  	// ShutdownHandlers terminates all handlers added by AddEventHandler.
    49  	// Warning: this only applies to handlers called via AddEventHandler; any handlers directly added
    50  	// to the underlying informer are not touched
    51  	ShutdownHandlers()
    52  	// Start starts just this informer. Typically, this is not used. Instead, the `kube.Client.Run()` is
    53  	// used to start all informers at once.
    54  	// However, in some cases we need to run individual informers directly.
    55  	// This function should only be called once. It does not wait for the informer to become ready nor does it block,
    56  	// so it should generally not be called in a goroutine.
    57  	Start(stop <-chan struct{})
    58  }
    59  
    60  type Writer[T controllers.Object] interface {
    61  	// Create creates a resource, returning the newly applied resource.
    62  	Create(object T) (T, error)
    63  	// Update updates a resource, returning the newly applied resource.
    64  	Update(object T) (T, error)
    65  	// UpdateStatus updates a resource's status, returning the newly applied resource.
    66  	UpdateStatus(object T) (T, error)
    67  	// Patch patches the resource, returning the newly applied resource.
    68  	Patch(name, namespace string, pt apitypes.PatchType, data []byte) (T, error)
    69  	// Delete removes a resource.
    70  	Delete(name, namespace string) error
    71  }
    72  
    73  type ReadWriter[T controllers.Object] interface {
    74  	Reader[T]
    75  	Writer[T]
    76  }
    77  
    78  // Client wraps a Kubernetes client providing cached read access and direct write access.
    79  type Client[T controllers.Object] interface {
    80  	Reader[T]
    81  	Writer[T]
    82  	Informer[T]
    83  }