rest

package
v0.0.0-...-24a1fde Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

README

some code comes from client-go.

Documentation

Index

Constants

View Source
const (
	DefaultQPS   float32 = 5.0
	DefaultBurst int     = 10
)

Variables

This section is empty.

Functions

func CertsFromFile

func CertsFromFile(file string) ([]*x509.Certificate, error)

CertsFromFile returns the x509.Certificates contained in the given PEM-encoded file. Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates

func DefaultServerURL

func DefaultServerURL(host, apiPath string, groupVersion schema.GroupVersion, defaultTLS bool) (*url.URL, error)

DefaultServerURL converts a host, host:port, or URL string to the default base server API path to use with a Ros at a given API version following the standard conventions for a Kubernetes API.

func IsConfigTransportTLS

func IsConfigTransportTLS(config Config) bool

IsConfigTransportTLS returns true if and only if the provided config will result in a protected connection to the server when it is passed to restclient.RESTClientFor(). Use to determine when to send credentials over the wire.

Note: the Insecure flag is ignored when testing for this value, so MITM attacks are still possible.

func LoadTLSFiles

func LoadTLSFiles(c *Config) error

LoadTLSFiles copies the data from the CertFile, KeyFile, and CAFile fields into the CertData, KeyData, and CAFile fields, or returns an error. If no error is returned, all three fields are either populated or were empty to start.

func NewPool

func NewPool(filename string) (*x509.CertPool, error)

NewPool returns an x509.CertPool containing the certificates in the given PEM-encoded file. Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates

func ParseCertsPEM

func ParseCertsPEM(pemCerts []byte) ([]*x509.Certificate, error)

ParseCertsPEM returns the x509.Certificates contained in the given PEM-encoded byte array Returns an error if a certificate could not be parsed, or if the data does not contain any certificates

func SetDefaultUserAgent

func SetDefaultUserAgent(config *Config) error

SetKubernetesDefaults sets default values on the provided client config for accessing the Kubernetes API or returns an error if any of the defaults are impossible or invalid.

func TransportFor

func TransportFor(config *Config) (*http.Transport, error)

Types

type Config

type Config struct {
	ContentType string
	// Host must be a host string, a host:port pair, or a URL to the base of the apiserver.
	// If a URL is given then the (optional) Path of that URL represents a prefix that must
	// be appended to all request URIs used to access the apiserver. This allows a frontend
	// proxy to easily relocate all of the apiserver endpoints.
	Host string
	// APIPath is a sub-path that points to an API root.
	APIPath string
	// Prefix is the sub path of the server. If not specified, the client will set
	// a default value.  Use "/" to indicate the server root should be used
	Prefix string

	// Server requires Basic authentication
	Username string
	Password string

	// Server requires Bearer authentication. This client will not attempt to use
	// refresh tokens for an OAuth2 flow.
	// TODO: demonstrate an OAuth2 compatible client.
	BearerToken string

	// CacheDir is the directory where we'll generic HTTP cached responses.
	// If set to empty string, no caching mechanism will be used.
	CacheDir string

	// TLSClientConfig contains settings to enable transport layer security
	TLSClientConfig

	// UserAgent is an optional field that specifies the caller of this request.
	UserAgent string

	// Transport may be used for custom HTTP behavior. This attribute may not
	// be specified with the TLS client certificate options. Use WrapTransport
	// for most client level operations.
	Transport http.RoundTripper
	// WrapTransport will be invoked for custom HTTP behavior after the underlying
	// transport is initialized (either the transport created from TLSClientConfig,
	// Transport, or http.DefaultTransport). The config may layer other RoundTrippers
	// on top of the returned RoundTripper.
	WrapTransport func(rt http.RoundTripper) http.RoundTripper

	// QPS indicates the maximum QPS to the master from this client.
	// If it's zero, the created RESTClient will use DefaultQPS: 5
	QPS float32

	// Maximum burst for throttle.
	// If it's zero, the created RESTClient will use DefaultBurst: 10.
	Burst int

	// The maximum length of time to wait before giving up on a server request. A value of zero means no timeout.
	Timeout time.Duration

	// Dial specifies the dial function for creating unencrypted TCP connections.
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
}

Config holds the common attributes that can be passed to a Kubernetes client on initialization.

func AddUserAgent

func AddUserAgent(config *Config, userAgent string) *Config

func InClusterConfig

func InClusterConfig() (*Config, error)

InClusterConfig returns a config object which uses the service account kubernetes gives to pods. It's intended for clients that expect to be running inside a pod running on kubernetes. It will return an error if called from a process not running in a kubernetes environment.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for testing a request object.

type Interface

type Interface interface {
	Verb(ctx context.Context, verb string) *Request
	Post(ctx context.Context) *Request
	Put(ctx context.Context) *Request
	Patch(ctx context.Context, pt string) *Request
	Get(ctx context.Context) *Request
	Delete(ctx context.Context) *Request
	APIVersion(ctx context.Context) string
}

Interface captures the set of operations for generically interacting with Kubernetes REST apis.

type RESTClient

type RESTClient struct {
	ContentType string

	// Set specific behavior of the client.  If not set http.DefaultClient will be used.
	Client *http.Client
	// contains filtered or unexported fields
}

RESTClient imposes common Kubernetes API conventions on a set of resource paths. The baseURL is expected to point to an HTTP or HTTPS path that is the parent of one or more resources. The server should return a decodable API resource object, or an api.Status object which contains information about the reason for any failure.

Most consumers should use client.New() to get a Kubernetes API client.

func NewRESTClient

func NewRESTClient(
	baseURL *url.URL,
	contentType string,
	maxQPS float32, maxBurst int,

	client *http.Client,
) (*RESTClient, error)

NewRESTClient creates a new RESTClient. This client performs generic REST functions such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and decoding of responses from the server.

func RESTClientFor

func RESTClientFor(config *Config) (*RESTClient, error)

RESTClientFor returns a RESTClient that satisfies the requested attributes on a client Config object. Note that a RESTClient may require fields that are optional when initializing a Ros. A RESTClient created by this method is generic - it expects to operate on an API that follows the Kubernetes conventions, but may not be the Kubernetes API.

func (*RESTClient) APIVersion

func (c *RESTClient) APIVersion(ctx context.Context) string

APIVersion returns the APIVersion this RESTClient is expected to use.

func (*RESTClient) Delete

func (c *RESTClient) Delete(ctx context.Context) *Request

Delete begins a DELETE request. Short for c.Verb("DELETE").

func (*RESTClient) Get

func (c *RESTClient) Get(ctx context.Context) *Request

Get begins a GET request. Short for c.Verb("GET").

func (*RESTClient) Patch

func (c *RESTClient) Patch(ctx context.Context, pt string) *Request

Patch begins a PATCH request. Short for c.Verb("Patch").

func (*RESTClient) Post

func (c *RESTClient) Post(ctx context.Context) *Request

Post begins a POST request. Short for c.Verb("POST").

func (*RESTClient) Put

func (c *RESTClient) Put(ctx context.Context) *Request

Put begins a PUT request. Short for c.Verb("PUT").

func (*RESTClient) Verb

func (c *RESTClient) Verb(ctx context.Context, verb string) *Request

Verb begins a request with a verb (GET, POST, PUT, DELETE).

Example usage of RESTClient's request building interface: c, err := NewRESTClient(...) if err != nil { ... } resp, err := c.Verb("GET").

Path("pods").
SelectorParam("labels", "area=staging").
Timeout(10*time.Second).
Do()

if err != nil { ... } list, ok := resp.(*api.PodList)

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request allows for building up a request to a server in a chained fashion. Any errors are stored until the end of your call, so you only have to check once.

func NewRequest

func NewRequest(
	ctx context.Context,
	client HTTPClient,
	verb string,
	baseURL *url.URL,
	versionedAPIPath string,
	contentType string,
) *Request

func (*Request) Body

func (req *Request) Body(obj interface{}) *Request

Body makes the request use obj as the body. Optional. If obj is a string, try to read a file of that name. If obj is a []byte, send it directly. If obj is an io.Reader, use it directly. If obj is a runtime.Object, marshal it correctly, and set Content-Type header. If obj is a runtime.Object and nil, do nothing. Otherwise, set an error.

func (*Request) Decode

func (req *Request) Decode(data string, api interface{}) error

func (*Request) DirectDo

func (req *Request) DirectDo() error

func (*Request) Do

func (req *Request) Do(api interface{}) error

func (*Request) PathPrefix

func (req *Request) PathPrefix(prefix string) *Request

func (*Request) Resource

func (req *Request) Resource(resource string) *Request

func (*Request) ResourceName

func (req *Request) ResourceName(name string) *Request

func (*Request) SetHeader

func (req *Request) SetHeader(key string, values ...string) *Request

func (*Request) Stream

func (req *Request) Stream() (io.ReadCloser, error)

func (*Request) SubResource

func (req *Request) SubResource(sub string) *Request

func (*Request) Url

func (req *Request) Url() (string, error)

type ResultList

type ResultList struct {
	// contains filtered or unexported fields
}

type TLSClientConfig

type TLSClientConfig struct {
	// Server should be accessed without verifying the TLS certificate. For testing only.
	Insecure bool
	// ServerName is passed to the server for SNI and is used in the client to check server
	// ceritificates against. If ServerName is empty, the hostname used to contact the
	// server is used.
	ServerName string

	// Server requires TLS client certificate authentication
	CertFile string
	// Server requires TLS client certificate authentication
	KeyFile string
	// Trusted root certificates for server
	CAFile string

	// CertData holds PEM-encoded bytes (typically read from a client certificate file).
	// CertData takes precedence over CertFile
	CertData []byte
	// KeyData holds PEM-encoded bytes (typically read from a client certificate key file).
	// KeyData takes precedence over KeyFile
	KeyData []byte
	// CAData holds PEM-encoded bytes (typically read from a root certificates bundle).
	// CAData takes precedence over CAFile
	CAData []byte
}

+k8s:deepcopy-gen=true TLSClientConfig contains settings to enable transport layer security

func (*TLSClientConfig) DeepCopy

func (in *TLSClientConfig) DeepCopy() *TLSClientConfig

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TLSClientConfig.

func (*TLSClientConfig) DeepCopyInto

func (in *TLSClientConfig) DeepCopyInto(out *TLSClientConfig)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

type TimeoutError

type TimeoutError interface {
	error
	Timeout() bool // Is the error a timeout?
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL