v0.0.0-...-0f34a88 Latest Latest

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 19 Imported by: 60




This section is empty.


This section is empty.


func DefaultErrorRetryChecker

func DefaultErrorRetryChecker(err error) (bool, error)

DefaultErrorRetryChecker implements the defaults for retrying on error.

func DefaultResponseRetryChecker

func DefaultResponseRetryChecker(resp *Response) (bool, error)

DefaultResponseRetryChecker implements the defaults for retrying on response.

func IsStatusOK

func IsStatusOK(resp *Response) (bool, error)

IsStatusOK checks that the response code is a 200.

func ResolveEndpoint

func ResolveEndpoint(ctx context.Context, kubeClientset kubernetes.Interface, domain string, resolvable bool, endpointOverride string) (string, func(string) string, error)

ResolveEndpoint resolves the endpoint address considering whether the domain is resolvable and taking into account whether the user overrode the endpoint address externally


type ErrorRetryChecker

type ErrorRetryChecker func(e error) (retry bool, err error)

ErrorRetryChecker is used to determine if an error should be retried or not. If an error should be retried, it should return true and the wrapped error to explain why to retry.

type RequestOption

type RequestOption func(*http.Request)

RequestOption enables configuration of requests when polling for endpoint states.

func WithHeader

func WithHeader(header http.Header) RequestOption

WithHeader will add the provided headers to the request.

type Response

type Response struct {
	Status     string
	StatusCode int
	Header     http.Header
	Body       []byte

Response is a stripped down subset of http.Response. The is primarily useful for ResponseCheckers to inspect the response body without consuming it. Notably, Body is a byte slice instead of an io.ReadCloser.

func (*Response) String

func (r *Response) String() string

type ResponseChecker

type ResponseChecker func(resp *Response) (done bool, err error)

ResponseChecker is used to determine when SpoofingClient.Poll is done polling. This allows you to predicate wait.PollImmediate on the request's http.Response.

See the apimachinery wait package: https://github.com/kubernetes/apimachinery/blob/cf7ae2f57dabc02a3d215f15ca61ae1446f3be8f/pkg/util/wait/wait.go#L172

func IsOneOfStatusCodes

func IsOneOfStatusCodes(codes ...int) ResponseChecker

IsOneOfStatusCodes checks that the response code is equal to the given one.

func MatchesAllBodies

func MatchesAllBodies(all ...string) ResponseChecker

MatchesAllBodies checks that the *first* response body matches the "expected" body, otherwise failing.

func MatchesAllOf

func MatchesAllOf(checkers ...ResponseChecker) ResponseChecker

MatchesAllOf combines multiple ResponseCheckers to one ResponseChecker with a logical AND. The checkers are executed in order. The first function to trigger an error or a retry will short-circuit the other functions (they will not be executed).

This is useful for combining a body with a status check like: MatchesAllOf(IsStatusOK, MatchesBody("test"))

The MatchesBody check will only be executed after the IsStatusOK has passed.

func MatchesBody

func MatchesBody(expected string) ResponseChecker

MatchesBody checks that the *first* response body matches the "expected" body, otherwise failing.

type ResponseRetryChecker

type ResponseRetryChecker func(resp *Response) (retry bool, err error)

ResponseRetryChecker is used to determine if a response should be retried or not. If a response should be retried, it should return true and an error to explain why to retry.

This is distinct from ResponseChecker in that it shall be used to retry responses, where the HTTP request was technically successful (it returned something) but indicates an error (e.g. the overload page of a loadbalancer).

type SpoofingClient

type SpoofingClient struct {
	Client          *http.Client
	RequestInterval time.Duration
	RequestTimeout  time.Duration
	Logf            logging.FormatLogger

SpoofingClient is a minimal HTTP client wrapper that spoofs the domain of requests for non-resolvable domains.

func New

func New(
	ctx context.Context,
	kubeClientset kubernetes.Interface,
	logf logging.FormatLogger,
	domain string,
	resolvable bool,
	endpointOverride string,
	requestInterval, requestTimeout time.Duration,
	opts ...TransportOption) (*SpoofingClient, error)

New returns a SpoofingClient that rewrites requests if the target domain is not `resolvable`. It does this by looking up the ingress at construction time, so reusing a client will not follow the ingress if it moves (or if there are multiple ingresses).

If that's a problem, see test/request.go#WaitForEndpointState for oneshot spoofing.

func (*SpoofingClient) Check

func (sc *SpoofingClient) Check(req *http.Request, inState ResponseChecker, checkers ...interface{}) (*Response, error)

func (*SpoofingClient) CheckEndpointState

func (sc *SpoofingClient) CheckEndpointState(
	ctx context.Context,
	url *url.URL,
	inState ResponseChecker,
	desc string,
	opts ...RequestOption) (*Response, error)

func (*SpoofingClient) Do

func (sc *SpoofingClient) Do(req *http.Request, errorRetryCheckers ...interface{}) (*Response, error)

Do dispatches to the underlying http.Client.Do, spoofing domains as needed and transforming the http.Response into a spoof.Response. Each response is augmented with "ZipkinTraceID" header that identifies the zipkin trace corresponding to the request.

func (*SpoofingClient) Poll

func (sc *SpoofingClient) Poll(req *http.Request, inState ResponseChecker, checkers ...interface{}) (*Response, error)

Poll executes an http request until it satisfies the inState condition or, if there's an error, none of the error retry checkers permit a retry. If no retry checkers are specified `DefaultErrorRetryChecker` will be used.

func (*SpoofingClient) WaitForEndpointState

func (sc *SpoofingClient) WaitForEndpointState(
	ctx context.Context,
	url *url.URL,
	inState ResponseChecker,
	desc string,
	opts ...RequestOption) (*Response, error)

type TransportOption

type TransportOption func(transport *http.Transport) *http.Transport

TransportOption allows callers to customize the http.Transport used by a SpoofingClient

Jump to

Keyboard shortcuts

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