...

Source file src/k8s.io/cli-runtime/pkg/resource/interfaces.go

Documentation: k8s.io/cli-runtime/pkg/resource

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resource
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/api/meta"
    21  	"k8s.io/apimachinery/pkg/types"
    22  	"k8s.io/client-go/discovery"
    23  	"k8s.io/client-go/rest"
    24  	"k8s.io/client-go/restmapper"
    25  )
    26  
    27  type RESTClientGetter interface {
    28  	ToRESTConfig() (*rest.Config, error)
    29  	ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error)
    30  	ToRESTMapper() (meta.RESTMapper, error)
    31  }
    32  
    33  type ClientConfigFunc func() (*rest.Config, error)
    34  type RESTMapperFunc func() (meta.RESTMapper, error)
    35  type CategoryExpanderFunc func() (restmapper.CategoryExpander, error)
    36  
    37  // RESTClient is a client helper for dealing with RESTful resources
    38  // in a generic way.
    39  type RESTClient interface {
    40  	Get() *rest.Request
    41  	Post() *rest.Request
    42  	Patch(types.PatchType) *rest.Request
    43  	Delete() *rest.Request
    44  	Put() *rest.Request
    45  }
    46  
    47  // RequestTransform is a function that is given a chance to modify the outgoing request.
    48  type RequestTransform func(*rest.Request)
    49  
    50  // NewClientWithOptions wraps the provided RESTClient and invokes each transform on each
    51  // newly created request.
    52  func NewClientWithOptions(c RESTClient, transforms ...RequestTransform) RESTClient {
    53  	if len(transforms) == 0 {
    54  		return c
    55  	}
    56  	return &clientOptions{c: c, transforms: transforms}
    57  }
    58  
    59  type clientOptions struct {
    60  	c          RESTClient
    61  	transforms []RequestTransform
    62  }
    63  
    64  func (c *clientOptions) modify(req *rest.Request) *rest.Request {
    65  	for _, transform := range c.transforms {
    66  		transform(req)
    67  	}
    68  	return req
    69  }
    70  
    71  func (c *clientOptions) Get() *rest.Request {
    72  	return c.modify(c.c.Get())
    73  }
    74  
    75  func (c *clientOptions) Post() *rest.Request {
    76  	return c.modify(c.c.Post())
    77  }
    78  func (c *clientOptions) Patch(t types.PatchType) *rest.Request {
    79  	return c.modify(c.c.Patch(t))
    80  }
    81  func (c *clientOptions) Delete() *rest.Request {
    82  	return c.modify(c.c.Delete())
    83  }
    84  func (c *clientOptions) Put() *rest.Request {
    85  	return c.modify(c.c.Put())
    86  }
    87  
    88  // ContentValidator is an interface that knows how to validate an API object serialized to a byte array.
    89  type ContentValidator interface {
    90  	ValidateBytes(data []byte) error
    91  }
    92  
    93  // Visitor lets clients walk a list of resources.
    94  type Visitor interface {
    95  	Visit(VisitorFunc) error
    96  }
    97  
    98  // VisitorFunc implements the Visitor interface for a matching function.
    99  // If there was a problem walking a list of resources, the incoming error
   100  // will describe the problem and the function can decide how to handle that error.
   101  // A nil returned indicates to accept an error to continue loops even when errors happen.
   102  // This is useful for ignoring certain kinds of errors or aggregating errors in some way.
   103  type VisitorFunc func(*Info, error) error
   104  

View as plain text