Documentation
¶
Overview ¶
Package externaldata provides caching and request handling for external data providers.
Index ¶
- Constants
- func HandleError(statusCode int, err error) (*ast.Term, error)
- func PrepareRegoResponse(regoResponse *RegoResponse) (*ast.Term, error)
- type CacheKey
- type CacheValue
- type ClientCache
- type Item
- type ProviderCache
- type ProviderKind
- type ProviderRequest
- type ProviderResponse
- type ProviderResponseCache
- type RegoRequest
- type RegoResponse
- type Request
- type Response
- type SendRequestToProvider
Constants ¶
const ( // HTTPScheme represents the HTTP URL scheme. HTTPScheme = "http" // HTTPSScheme represents the HTTPS URL scheme. HTTPSScheme = "https" )
Variables ¶
This section is empty.
Functions ¶
func HandleError ¶
HandleError creates an error response with the given status code and error.
func PrepareRegoResponse ¶
func PrepareRegoResponse(regoResponse *RegoResponse) (*ast.Term, error)
PrepareRegoResponse marshals a RegoResponse into an AST term.
Types ¶
type CacheValue ¶
CacheValue holds a cached response from an external data provider.
type ClientCache ¶
type ClientCache struct {
// contains filtered or unexported fields
}
ClientCache caches HTTP clients per provider to prevent goroutine leaks from creating a new transport on every request.
func DefaultClientCache ¶
func DefaultClientCache() *ClientCache
DefaultClientCache returns the package-level ClientCache used by DefaultSendRequestToProvider. Use this to wire invalidation into a ProviderCache via SetClientCache.
func (*ClientCache) Invalidate ¶
func (c *ClientCache) Invalidate(name string)
Invalidate closes idle connections and removes the cached client for a provider.
type Item ¶
type Item struct {
// Key is the request from the provider.
Key string `json:"key,omitempty"`
// Value is the response from the provider.
Value interface{} `json:"value,omitempty"`
// Error is the error from the provider.
Error string `json:"error,omitempty"`
}
Item is the struct that contains the key, value or error from a provider response.
type ProviderCache ¶
type ProviderCache struct {
// contains filtered or unexported fields
}
ProviderCache caches external data provider configurations.
func (*ProviderCache) Get ¶
func (c *ProviderCache) Get(key string) (unversioned.Provider, error)
Get retrieves a provider by name from the cache.
func (*ProviderCache) Remove ¶
func (c *ProviderCache) Remove(name string)
Remove deletes a provider from the cache by name.
func (*ProviderCache) SetClientCache ¶
func (c *ProviderCache) SetClientCache(cc *ClientCache)
SetClientCache sets the HTTP client cache for transport cleanup when providers are removed or updated. Must be called before any Upsert/Remove operations.
Lock order invariant: ProviderCache.mux is always acquired before ClientCache.mu. This order is maintained in Upsert() and Remove() which hold mux while calling ClientCache.Invalidate(). No code path acquires these locks in reverse order.
func (*ProviderCache) Upsert ¶
func (c *ProviderCache) Upsert(provider *unversioned.Provider) error
Upsert inserts or updates a provider in the cache.
type ProviderKind ¶
type ProviderKind string
ProviderKind strings are special string constants for Providers. +kubebuilder:validation:Enum=ProviderRequestKind;ProviderResponseKind
const ( // ProviderRequestKind is the kind of the request. ProviderRequestKind ProviderKind = "ProviderRequest" // ProviderResponseKind is the kind of the response. ProviderResponseKind ProviderKind = "ProviderResponse" )
type ProviderRequest ¶
type ProviderRequest struct {
// APIVersion is the API version of the external data provider.
APIVersion string `json:"apiVersion,omitempty"`
// Kind is kind of the external data provider API call. This can be "ProviderRequest" or "ProviderResponse".
Kind ProviderKind `json:"kind,omitempty"`
// Request contains the request for the external data provider.
Request Request `json:"request,omitempty"`
}
ProviderRequest is the API request for the external data provider.
func NewProviderRequest ¶
func NewProviderRequest(keys []string) *ProviderRequest
NewProviderRequest creates a new request for the external data provider.
type ProviderResponse ¶
type ProviderResponse struct {
// APIVersion is the API version of the external data provider.
APIVersion string `json:"apiVersion,omitempty"`
// Kind is kind of the external data provider API call. This can be "ProviderRequest" or "ProviderResponse".
Kind ProviderKind `json:"kind,omitempty"`
// Response contains the response from the provider.
Response Response `json:"response,omitempty"`
}
ProviderResponse is the API response from a provider.
func DefaultSendRequestToProvider ¶
func DefaultSendRequestToProvider(ctx context.Context, provider *unversioned.Provider, keys []string, clientCert *tls.Certificate) (*ProviderResponse, int, error)
DefaultSendRequestToProvider is the default function to send the request to the external data provider.
type ProviderResponseCache ¶
ProviderResponseCache caches responses from external data providers.
func NewProviderResponseCache ¶
func NewProviderResponseCache(ctx context.Context, ttl time.Duration) *ProviderResponseCache
NewProviderResponseCache creates a new ProviderResponseCache with the specified TTL.
func (*ProviderResponseCache) Get ¶
func (c *ProviderResponseCache) Get(key CacheKey) (*CacheValue, error)
Get retrieves a cached value by key.
func (*ProviderResponseCache) Remove ¶
func (c *ProviderResponseCache) Remove(key CacheKey)
Remove deletes a cached value by key.
func (*ProviderResponseCache) Upsert ¶
func (c *ProviderResponseCache) Upsert(key CacheKey, value CacheValue)
Upsert inserts or updates a cached value.
type RegoRequest ¶
type RegoRequest struct {
// ProviderName is the name of the external data provider.
ProviderName string `json:"provider"`
// Keys is the list of keys to send to the external data provider.
Keys []string `json:"keys"`
}
RegoRequest is the request for external_data rego function.
type RegoResponse ¶
type RegoResponse struct {
// Responses contains the response from the provider.
// In each element of the outer array, the first element is the key and the second is the corresponding value from the provider.
Responses [][]interface{} `json:"responses"`
// Errors contains the errors from the provider.
// In each item of the outer array, the first element is the key and the second is the corresponding error from the provider.
Errors [][]interface{} `json:"errors"`
// StatusCode contains the status code of the response.
StatusCode int `json:"status_code"`
// SystemError is the system error of the response.
SystemError string `json:"system_error"`
}
RegoResponse is the response inside rego.
func NewRegoResponse ¶
func NewRegoResponse(statusCode int, pr *ProviderResponse) *RegoResponse
NewRegoResponse creates a new rego response from the given provider response.
type Request ¶
type Request struct {
// Keys is the list of keys to send to the external data provider.
Keys []string `json:"keys,omitempty"`
}
Request is the struct that contains the keys to query.
type Response ¶
type Response struct {
// Idempotent indicates that the responses from the provider are idempotent.
// Applies to mutation only and must be true for mutation.
Idempotent bool `json:"idempotent,omitempty"`
// Items contains the key, value and error from the provider.
Items []Item `json:"items,omitempty"`
// SystemError is the system error of the response.
SystemError string `json:"systemError,omitempty"`
}
Response is the struct that holds the response from a provider.
type SendRequestToProvider ¶
type SendRequestToProvider func(ctx context.Context, provider *unversioned.Provider, keys []string, clientCert *tls.Certificate) (*ProviderResponse, int, error)
SendRequestToProvider is a function that sends a request to the external data provider.