request

package
v0.31.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 16 Imported by: 4,730

Documentation

Overview

Package request contains everything around extracting info from a http request object. TODO: this package is temporary. Handlers must move into pkg/apiserver/handlers to avoid dependency cycle

Index

Constants

This section is empty.

Variables

View Source
var NamespaceSubResourcesForTest = sets.NewString(namespaceSubresources.List()...)

NamespaceSubResourcesForTest exports namespaceSubresources for testing in pkg/controlplane/master_test.go, so we never drift

Functions

func AuditAnnotationsFromLatencyTrackers added in v0.24.0

func AuditAnnotationsFromLatencyTrackers(ctx context.Context) map[string]string

AuditAnnotationsFromLatencyTrackers will inspect each latency tracker associated with the request context and return a set of audit annotations that can be added to the API audit entry.

func NamespaceFrom

func NamespaceFrom(ctx context.Context) (string, bool)

NamespaceFrom returns the value of the namespace key on the ctx

func NamespaceValue

func NamespaceValue(ctx context.Context) string

NamespaceValue returns the value of the namespace key on the ctx, or the empty string if none

func NewContext

func NewContext() context.Context

NewContext instantiates a base context object for request flows.

func NewDefaultContext

func NewDefaultContext() context.Context

NewDefaultContext instantiates a base context object for request flows in the default namespace

func ReceivedTimestampFrom added in v0.20.0

func ReceivedTimestampFrom(ctx context.Context) (time.Time, bool)

ReceivedTimestampFrom returns the value of the ReceivedTimestamp key from the specified context.

func TrackAPFQueueWaitLatency added in v0.27.0

func TrackAPFQueueWaitLatency(ctx context.Context, d time.Duration)

TrackAPFQueueWaitLatency is used to track latency incurred by priority and fairness queues.

func TrackDecodeLatency added in v0.30.0

func TrackDecodeLatency(ctx context.Context, d time.Duration)

TrackDecodeLatency is used to track latency incurred inside the function that takes an object returned from the underlying storage layer (etcd) and performs decoding of the response object. When called multiple times, the latency incurred inside to decode func each time will be summed up.

func TrackResponseWriteLatency added in v0.24.0

func TrackResponseWriteLatency(ctx context.Context, d time.Duration)

TrackResponseWriteLatency is used to track latency incurred in writing the serialized raw bytes to the http ResponseWriter object (via the Write method) associated with the request. When called multiple times, the latency provided will be summed up.

func TrackSerializeResponseObjectLatency added in v0.24.0

func TrackSerializeResponseObjectLatency(ctx context.Context, f func())

TrackSerializeResponseObjectLatency is used to track latency incurred in serialization (json or protobuf) of the response object. When called multiple times, the latency provided will be summed up.

func TrackStorageLatency added in v0.24.0

func TrackStorageLatency(ctx context.Context, d time.Duration)

TrackStorageLatency is used to track latency incurred inside the underlying storage layer. When called multiple times, the latency provided will be summed up.

func TrackTransformResponseObjectLatency added in v0.24.0

func TrackTransformResponseObjectLatency(ctx context.Context, transform func())

TrackTransformResponseObjectLatency is used to track latency incurred inside the function that takes an object returned from the underlying storage layer (etcd) and performs any necessary transformations of the response object. This does not include the latency incurred in serialization (json or protobuf) of the response object or writing of it to the http ResponseWriter object. When called multiple times, the latency incurred inside the transform func each time will be summed up.

func UserFrom

func UserFrom(ctx context.Context) (user.Info, bool)

UserFrom returns the value of the user key on the ctx

func WithLatencyTrackers added in v0.24.0

func WithLatencyTrackers(parent context.Context) context.Context

WithLatencyTrackers returns a copy of parent context to which an instance of LatencyTrackers is added.

func WithLatencyTrackersAndCustomClock added in v0.24.0

func WithLatencyTrackersAndCustomClock(parent context.Context, c clock.Clock) context.Context

WithLatencyTrackersAndCustomClock returns a copy of parent context to which an instance of LatencyTrackers is added. Tracers use given clock.

func WithNamespace

func WithNamespace(parent context.Context, namespace string) context.Context

WithNamespace returns a copy of parent in which the namespace value is set

func WithReceivedTimestamp added in v0.20.0

func WithReceivedTimestamp(parent context.Context, receivedTimestamp time.Time) context.Context

WithReceivedTimestamp returns a copy of parent context in which the ReceivedTimestamp (the time the request reached the apiserver) is set.

If the specified ReceivedTimestamp is zero, no value is set and the parent context is returned as is.

func WithRequestInfo

func WithRequestInfo(parent context.Context, info *RequestInfo) context.Context

WithRequestInfo returns a copy of parent in which the request info value is set

func WithServerShutdownSignal added in v0.27.0

func WithServerShutdownSignal(parent context.Context, window ServerShutdownSignal) context.Context

WithServerShutdownSignal returns a new context that stores the ServerShutdownSignal interface instance.

func WithUser

func WithUser(parent context.Context, user user.Info) context.Context

WithUser returns a copy of parent in which the user value is set

func WithValue

func WithValue(parent context.Context, key interface{}, val interface{}) context.Context

WithValue returns a copy of parent in which the value associated with key is val.

Types

type DurationTracker added in v0.23.0

type DurationTracker interface {
	// Track measures time spent in the given function f and
	// aggregates measured duration using aggregateFunction.
	// if Track is invoked with f from multiple goroutines concurrently,
	// then f must be safe to be invoked concurrently by multiple goroutines.
	Track(f func())

	// TrackDuration tracks latency from the specified duration
	// and aggregate it using aggregateFunction
	TrackDuration(time.Duration)

	// GetLatency returns the total latency incurred so far
	GetLatency() time.Duration
}

DurationTracker is a simple interface for tracking functions duration, it is safe for concurrent use by multiple goroutines.

type LatencyTrackers added in v0.24.0

type LatencyTrackers struct {
	// MutatingWebhookTracker tracks the latency incurred in mutating webhook(s).
	// Since mutating webhooks are done sequentially, latency
	// is aggregated using sum function.
	MutatingWebhookTracker DurationTracker

	// ValidatingWebhookTracker tracks the latency incurred in validating webhook(s).
	// Validate webhooks are done in parallel, so max function is used.
	ValidatingWebhookTracker DurationTracker

	// APFQueueWaitTracker tracks the latency incurred by queue wait times
	// from priority & fairness.
	APFQueueWaitTracker DurationTracker

	// StorageTracker tracks the latency incurred inside the storage layer,
	// it accounts for the time it takes to send data to the underlying
	// storage layer (etcd) and get the complete response back.
	// If a request involves N (N>=1) round trips to the underlying
	// stogare layer, the latency will account for the total duration
	// from these N round trips.
	// It does not include the time incurred in admission, or validation.
	StorageTracker DurationTracker

	// TransformTracker tracks the latency incurred in transforming the
	// response object(s) returned from the underlying storage layer.
	// This includes transforming the object to user's desired form
	// (ie. as Table), and also setting appropriate API level fields.
	// This does not include the latency incurred in serialization
	// (json or protobuf) of the response object or writing
	// of it to the http ResponseWriter object.
	TransformTracker DurationTracker

	// SerializationTracker tracks the latency incurred in serialization
	// (json or protobuf) of the response object.
	// NOTE: serialization and writing of the serialized raw bytes to the
	// associated http ResponseWriter object are interleaved, and hence
	// the latency measured here will include the time spent writing the
	// serialized raw bytes to the http ResponseWriter object.
	SerializationTracker DurationTracker

	// ResponseWriteTracker tracks the latency incurred in writing the
	// serialized raw bytes to the http ResponseWriter object (via the
	// Write method) associated with the request.
	// The Write method can be invoked multiple times, so we use a
	// latency tracker that sums up the duration from each call.
	ResponseWriteTracker DurationTracker

	// DecodeTracker is used to track latency incurred inside the function
	// that takes an object returned from the underlying storage layer
	// (etcd) and performs decoding of the response object.
	// When called multiple times, the latency incurred inside to
	// decode func each time will be summed up.
	DecodeTracker DurationTracker
}

LatencyTrackers stores trackers used to measure latecny incurred in components within the apiserver.

func LatencyTrackersFrom added in v0.24.0

func LatencyTrackersFrom(ctx context.Context) (*LatencyTrackers, bool)

LatencyTrackersFrom returns the associated LatencyTrackers instance from the specified context.

type LongRunningRequestCheck

type LongRunningRequestCheck func(r *http.Request, requestInfo *RequestInfo) bool

LongRunningRequestCheck is a predicate which is true for long-running http requests.

type RequestInfo

type RequestInfo struct {
	// IsResourceRequest indicates whether or not the request is for an API resource or subresource
	IsResourceRequest bool
	// Path is the URL path of the request
	Path string
	// Verb is the kube verb associated with the request for API requests, not the http verb.  This includes things like list and watch.
	// for non-resource requests, this is the lowercase http verb
	Verb string

	APIPrefix  string
	APIGroup   string
	APIVersion string
	Namespace  string
	// Resource is the name of the resource being requested.  This is not the kind.  For example: pods
	Resource string
	// Subresource is the name of the subresource being requested.  This is a different resource, scoped to the parent resource, but it may have a different kind.
	// For instance, /pods has the resource "pods" and the kind "Pod", while /pods/foo/status has the resource "pods", the sub resource "status", and the kind "Pod"
	// (because status operates on pods). The binding resource for a pod though may be /pods/foo/binding, which has resource "pods", subresource "binding", and kind "Binding".
	Subresource string
	// Name is empty for some verbs, but if the request directly indicates a name (not in body content) then this field is filled in.
	Name string
	// Parts are the path parts for the request, always starting with /{resource}/{name}
	Parts []string

	// FieldSelector contains the unparsed field selector from a request.  It is only present if the apiserver
	// honors field selectors for the verb this request is associated with.
	FieldSelector string
	// LabelSelector contains the unparsed field selector from a request.  It is only present if the apiserver
	// honors field selectors for the verb this request is associated with.
	LabelSelector string
}

RequestInfo holds information parsed from the http.Request

func RequestInfoFrom

func RequestInfoFrom(ctx context.Context) (*RequestInfo, bool)

RequestInfoFrom returns the value of the RequestInfo key on the ctx

type RequestInfoFactory

type RequestInfoFactory struct {
	APIPrefixes          sets.String // without leading and trailing slashes
	GrouplessAPIPrefixes sets.String // without leading and trailing slashes
}

func (*RequestInfoFactory) NewRequestInfo

func (r *RequestInfoFactory) NewRequestInfo(req *http.Request) (*RequestInfo, error)

TODO write an integration test against the swagger doc to test the RequestInfo and match up behavior to responses NewRequestInfo returns the information from the http request. If error is not nil, RequestInfo holds the information as best it is known before the failure It handles both resource and non-resource requests and fills in all the pertinent information for each. Valid Inputs: Resource paths /apis/{api-group}/{version}/namespaces /api/{version}/namespaces /api/{version}/namespaces/{namespace} /api/{version}/namespaces/{namespace}/{resource} /api/{version}/namespaces/{namespace}/{resource}/{resourceName} /api/{version}/{resource} /api/{version}/{resource}/{resourceName}

Special verbs without subresources: /api/{version}/proxy/{resource}/{resourceName} /api/{version}/proxy/namespaces/{namespace}/{resource}/{resourceName}

Special verbs with subresources: /api/{version}/watch/{resource} /api/{version}/watch/namespaces/{namespace}/{resource}

NonResource paths /apis/{api-group}/{version} /apis/{api-group} /apis /api/{version} /api /healthz /

type RequestInfoResolver

type RequestInfoResolver interface {
	NewRequestInfo(req *http.Request) (*RequestInfo, error)
}

type ServerShutdownSignal added in v0.27.0

type ServerShutdownSignal interface {
	// Signaled when the apiserver is not receiving any new request
	ShuttingDown() <-chan struct{}
}

ServerShutdownSignal is associated with the request context so the request handler logic has access to signals rlated to the server shutdown events

func ServerShutdownSignalFrom added in v0.27.0

func ServerShutdownSignalFrom(ctx context.Context) ServerShutdownSignal

ServerShutdownSignalFrom returns the ServerShutdownSignal instance associated with the request context. If there is no ServerShutdownSignal asscoaied with the context, nil is returned.

Jump to

Keyboard shortcuts

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