client

package
v2.13.15 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BasicHTTPClient

func BasicHTTPClient(t time.Duration) *http.Client

BasicHTTPClient returns http.Client configured with timeout

func Expired added in v2.7.1

func Expired(currentTime time.Time, creationTimestamp int64, ttl time.Duration, jitterMaxPercentage uint) bool

Expired checks, if for a given current time, object creation timestamp and TTL, object should be considered as expired (TTL has been exceeded).

If jitter max percentage is not zero, TTL will be either increased or decreased randomly by maximum of selected TTL percentage. This allows to distribute cache expiration in time to avoid all caches to expire at the same time in multiple clients, e.g. in distributed systems. As an example:

For a TTL of 100 and jitter max percentage of 20, TTL will be within range of 80-120.

If jitter max percentage is 0, TTL remains as given.

func InsecureHTTPClient

func InsecureHTTPClient(t time.Duration) *http.Client

InsecureHTTPClient returns http.Client configured with timeout and InsecureSkipVerify flag enabled

Types

type Composer

type Composer func(source interface{}, cacher *DiscoveryCacher, timeout time.Duration) (HTTPClient, error)

Composer implementers must convert the data from the cached entities to a Client.

type Decomposer

type Decomposer func(source HTTPClient) (interface{}, error)

Decomposer implementers must convert a HTTPClient into a data structure that can be Stored in the cache.

type Discoverer

type Discoverer interface {
	Discover(timeout time.Duration) (HTTPClient, error)
}

Discoverer allows discovering the endpoints from different services in the Kubernetes ecosystem.

type DiscoveryCacher

type DiscoveryCacher struct {
	DiscoveryCacherConfig

	// Discoverer points to the wrapped Discovered used to resolve endpoints when they are not found in the cache
	Discoverer Discoverer
	Compose    Composer
	Decompose  Decomposer

	// CachedDataPtr must be a pointer to an object where the data will be unmarshalled to
	CachedDataPtr interface{}
	// StorageKey is the key for the Storage Cache
	StorageKey string
}

DiscoveryCacher implements the Discoverer API to read endpoints from a cache storage. It also wraps another Discoverer and uses it to discover endpoints when the data is not found in the cache. This type is not thread-safe.

func (*DiscoveryCacher) Discover

func (d *DiscoveryCacher) Discover(timeout time.Duration) (HTTPClient, error)

Discover tries to retrieve a HTTPClient from the cache, and otherwise engage the discovery process from the wrapped Discoverer

type DiscoveryCacherConfig added in v2.7.1

type DiscoveryCacherConfig struct {
	Storage   storage.Storage
	TTL       time.Duration
	TTLJitter uint
	Logger    *logrus.Logger
}

DiscoveryCacherConfig defines common properties for discovery cachers.

type HTTPClient

type HTTPClient interface {
	Do(method, path string) (*http.Response, error)
	NodeIP() string
}

HTTPClient allows to connect to the discovered Kubernetes services

func WrappedClient

func WrappedClient(caClient HTTPClient) HTTPClient

WrappedClient is only aimed for testing. It allows extracting the wrapped client of a given cacheAwareClient.

type Kubernetes

type Kubernetes interface {
	// FindNode returns a Node reference containing the pod named as the argument, if any
	FindNode(name string) (*v1.Node, error)

	// FindPodsByLabel returns a PodList reference containing the pods matching the provided label selector.
	FindPodsByLabel(namespace string, labelSelector metav1.LabelSelector) (*v1.PodList, error)

	// FindServicesByLabel returns a ServiceList containing the services matching the provided label selector.
	FindServicesByLabel(namespace string, labelSelector metav1.LabelSelector) (*v1.ServiceList, error)

	// ListServices returns a ServiceList containing all the services.
	ListServices(namespace string) (*v1.ServiceList, error)

	// Config returns a config of API client
	Config() *rest.Config

	// SecureHTTPClient returns http.Client configured with timeout and CA Cert
	SecureHTTPClient(time.Duration) (*http.Client, error)

	// FindSecret returns the secret with the given name, if any
	FindSecret(name, namespace string) (*v1.Secret, error)

	// ServerVersion returns the kubernetes server version.
	ServerVersion() (*version.Info, error)
}

Kubernetes provides an interface to common Kubernetes API operations

func NewKubernetes

func NewKubernetes(tryLocalKubeConfig bool) (Kubernetes, error)

NewKubernetes instantiates a Kubernetes API client if tryLocalKubeConfig is true, this will try to load your kubeconfig from ~/.kube/config

type MockDiscoveredHTTPClient

type MockDiscoveredHTTPClient struct {
	mock.Mock
}

MockDiscoveredHTTPClient is a mock implementation of the HTTPClient interface

func (*MockDiscoveredHTTPClient) Do

func (m *MockDiscoveredHTTPClient) Do(method, path string) (*http.Response, error)

Do provides a mock implementation for HTTPClient interface

func (*MockDiscoveredHTTPClient) NodeIP

func (m *MockDiscoveredHTTPClient) NodeIP() string

NodeIP provides a mock implementation for HTTPClient interface

type MockDiscoverer

type MockDiscoverer struct {
	mock.Mock
}

MockDiscoverer is a mock implementation of the Discoverer interface

func (*MockDiscoverer) Discover

func (m *MockDiscoverer) Discover(timeout time.Duration) (HTTPClient, error)

Discover provides a mock implementation for Discoverer interface

type MockedKubernetes

type MockedKubernetes struct {
	mock.Mock
}

MockedKubernetes is a Mock for the Kubernetes interface to be used only in tests

func (*MockedKubernetes) Config

func (m *MockedKubernetes) Config() *rest.Config

Config mocks Kubernetes Config

func (*MockedKubernetes) FindNode

func (m *MockedKubernetes) FindNode(name string) (*v1.Node, error)

FindNode mocks Kubernetes FindNode

func (*MockedKubernetes) FindPodsByLabel

func (m *MockedKubernetes) FindPodsByLabel(namespace string, labelSelector metav1.LabelSelector) (*v1.PodList, error)

FindPodsByLabel mocks Kubernetes FindPodsByLabel

func (*MockedKubernetes) FindSecret

func (m *MockedKubernetes) FindSecret(name, namespace string) (*v1.Secret, error)

FindSecret mocks Kubernetes FindSecret

func (*MockedKubernetes) FindServicesByLabel

func (m *MockedKubernetes) FindServicesByLabel(namespace string, labelSelector metav1.LabelSelector) (*v1.ServiceList, error)

FindServicesByLabel mocks Kubernetes FindServicesByLabel

func (*MockedKubernetes) ListServices

func (m *MockedKubernetes) ListServices(namespace string) (*v1.ServiceList, error)

ListServices mocks Kubernetes ListServices

func (*MockedKubernetes) SecureHTTPClient

func (m *MockedKubernetes) SecureHTTPClient(timeout time.Duration) (*http.Client, error)

SecureHTTPClient mocks Kubernetes SecureHTTPClient

func (*MockedKubernetes) ServerVersion

func (m *MockedKubernetes) ServerVersion() (*version.Info, error)

ServerVersion mocks Kubernetes ServerVersion

type MultiComposer

type MultiComposer func(source interface{}, cacher *MultiDiscoveryCacher, timeout time.Duration) ([]HTTPClient, error)

MultiComposer implementers must convert the cached data to a []HTTPClient.

type MultiDecomposer

type MultiDecomposer func(sources []HTTPClient) (interface{}, error)

MultiDecomposer implementers must convert a HTTPClient into a data structure that can be Stored in the cache.

type MultiDiscoverer

type MultiDiscoverer interface {
	Discover(timeout time.Duration) ([]HTTPClient, error)
}

MultiDiscoverer allows for discovery processes that return more than a single HTTP client. For example, in one node we might query more than one KSM instance, so we need two clients.

type MultiDiscoveryCacher

type MultiDiscoveryCacher struct {
	DiscoveryCacherConfig

	Discoverer MultiDiscoverer
	Compose    MultiComposer
	Decompose  MultiDecomposer

	// CachedDataPtr must be a pointer to an object where the data will be unmarshalled to
	CachedDataPtr interface{}
	// StorageKey is the key for the Storage Cache
	StorageKey string
}

MultiDiscoveryCacher is a wrapper for MultiDiscoverer implementations that can cache the results into some storages. It implements the MultiDiscoverer interface. This type is not threadsafe.

func (*MultiDiscoveryCacher) Discover

func (d *MultiDiscoveryCacher) Discover(timeout time.Duration) ([]HTTPClient, error)

Discover runs the underlying discovery and caches its result. If there is a non-expired discover cache in the storage, it will be loaded. If the cache is not present or has expired, it will be written. If the cache read fails, the underlying discovery will still run.

Jump to

Keyboard shortcuts

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