Documentation ¶
Index ¶
- Constants
- Variables
- func ConnectToDocker(ctx context.Context) (*client.Client, error)
- func ContainerIDToEntityName(cid string) string
- func ContainerIDToTaggerEntityName(cid string) string
- func EnableTestingMode()
- func FindRancherIPInLabels(labels map[string]string) (string, bool)
- func GetAgentContainerNetworkMode(ctx context.Context) (string, error)
- func GetContainerNetworkMode(ctx context.Context, cid string) (string, error)
- func GetDockerHostIPs() []string
- func GetInspectCacheKey(ID string, withSize bool) string
- func HostnameProvider(ctx context.Context, options map[string]interface{}) (string, error)
- type Config
- type ContainerEvent
- type ContainerListConfig
- type DockerUtil
- func (d *DockerUtil) AllContainerLabels(ctx context.Context) (map[string]map[string]string, error)
- func (d *DockerUtil) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error)
- func (d *DockerUtil) CountVolumes(ctx context.Context) (int, int, error)
- func (d *DockerUtil) GetContainerStats(ctx context.Context, containerID string) (*types.StatsJSON, error)
- func (d *DockerUtil) GetHostname(ctx context.Context) (string, error)
- func (d *DockerUtil) GetStorageStats(ctx context.Context) ([]*StorageStats, error)
- func (d *DockerUtil) Images(ctx context.Context, includeIntermediate bool) ([]types.ImageSummary, error)
- func (d *DockerUtil) Inspect(ctx context.Context, id string, withSize bool) (types.ContainerJSON, error)
- func (d *DockerUtil) InspectNoCache(ctx context.Context, id string, withSize bool) (types.ContainerJSON, error)
- func (d *DockerUtil) InspectSelf(ctx context.Context) (types.ContainerJSON, error)
- func (d *DockerUtil) LatestContainerEvents(ctx context.Context, since time.Time) ([]*ContainerEvent, time.Time, error)
- func (d *DockerUtil) ListContainers(ctx context.Context, cfg *ContainerListConfig) ([]*containers.Container, error)
- func (d *DockerUtil) RawContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
- func (d *DockerUtil) ResolveImageName(ctx context.Context, image string) (string, error)
- func (d *DockerUtil) ResolveImageNameFromContainer(ctx context.Context, co types.ContainerJSON) (string, error)
- func (d *DockerUtil) SubscribeToContainerEvents(name string) (<-chan *ContainerEvent, <-chan error, error)
- func (d *DockerUtil) UnsubscribeFromContainerEvents(name string) error
- func (d *DockerUtil) UpdateContainerMetrics(cList []*containers.Container) error
- type StorageStats
Constants ¶
const ( // ContainerEventActionStart is the action of starting a docker container ContainerEventActionStart = "start" // ContainerEventActionDie is the action of stopping a docker container ContainerEventActionDie = "die" // ContainerEventActionDied is the action of stopping a podman container ContainerEventActionDied = "died" // ContainerEventActionRename is the action of renaming a docker container ContainerEventActionRename = "rename" )
const ( // DataStorageName represent diskmapper data stats DataStorageName = "data" // MetadataStorageName represent diskmapper metadata stats MetadataStorageName = "metadata" )
Variables ¶
var ( // ErrNotImplemented is the "not implemented" error given by `gopsutil` when an // OS doesn't support and API. Unfortunately it's in an internal package so // we can't import it so we'll copy it here. ErrNotImplemented = errors.New("not implemented yet") // ErrDockerNotAvailable is returned if Docker is not running on the current machine. // We'll use this when configuring the DockerUtil so we don't error on non-docker machines. ErrDockerNotAvailable = errors.New("docker not available") // ErrDockerNotCompiled is returned if docker support is not compiled in. // User classes should handle that case as gracefully as possible. ErrDockerNotCompiled = errors.New("docker support not compiled in") )
var ( ErrAlreadySubscribed = errors.New("already subscribed") ErrNotSubscribed = errors.New("not subscribed") ErrEventTimeout = errors.New("timeout on event sending, re-subscribe") )
Errors client might receive
var ( // ErrStorageStatsNotAvailable is returned if the storage stats are not in the docker info. ErrStorageStatsNotAvailable = errors.New("docker storage stats not available") )
Functions ¶
func ConnectToDocker ¶
ConnectToDocker connects to docker and negotiates the API version
func ContainerIDToEntityName ¶
ContainerIDToEntityName returns a prefixed entity name from a container ID
func ContainerIDToTaggerEntityName ¶
ContainerIDToTaggerEntityName returns a prefixed entity name from a container ID
func EnableTestingMode ¶
func EnableTestingMode()
EnableTestingMode creates a "mocked" DockerUtil you can use for unit tests that will hit on the docker inspect cache. Please note that all calls to the docker server will result in nil pointer exceptions.
func FindRancherIPInLabels ¶
FindRancherIPInLabels looks for the `io.rancher.container.ip` label and parses it. Rancher 1.x containers don't have docker networks as the orchestrator provides its own CNI.
func GetAgentContainerNetworkMode ¶
GetAgentContainerNetworkMode provides the network mode of the Agent container To get this info in an optimal way, consider calling util.GetAgentNetworkMode func GetContainerNetworkMode(cid string) (string, error) { instead to benefit from the cache
func GetContainerNetworkMode ¶
GetContainerNetworkMode returns the network mode of a container
func GetDockerHostIPs ¶
func GetDockerHostIPs() []string
func GetInspectCacheKey ¶
GetInspectCacheKey returns the key to a given container ID inspect in the agent cache
Types ¶
type Config ¶
type Config struct { // CacheDuration is the amount of time we will cache the active docker // containers and cgroups. The actual raw metrics (e.g. MemRSS) will _not_ // be cached but will be re-calculated on all calls to AllContainers. CacheDuration time.Duration // CollectNetwork enables network stats collection. This requires at least // one call to container.Inspect for new containers and reads from the // procfs for stats. CollectNetwork bool // Whitelist is a slice of filter strings in the form of key:regex where key // is either 'image' or 'name' and regex is a valid regular expression. Whitelist []string // Blacklist is the same as whitelist but for exclusion. Blacklist []string // contains filtered or unexported fields }
Config is an exported configuration object that is used when initializing the DockerUtil.
type ContainerEvent ¶
type ContainerEvent struct { ContainerID string ContainerName string ImageName string Action string Timestamp time.Time Attributes map[string]string }
ContainerEvent describes an event from the docker daemon
func (*ContainerEvent) ContainerEntityName ¶
func (ev *ContainerEvent) ContainerEntityName() string
ContainerEntityName returns the event's container as a tagger entity name
type ContainerListConfig ¶
ContainerListConfig allows to pass listing options
type DockerUtil ¶
DockerUtil wraps interactions with a local docker API.
func GetDockerUtil ¶
func GetDockerUtil() (*DockerUtil, error)
GetDockerUtil returns a ready to use DockerUtil. It is backed by a shared singleton.
func GetDockerUtilWithRetrier ¶
func GetDockerUtilWithRetrier() (*DockerUtil, *retry.Retrier)
GetDockerUtilWithRetrier returns a ready to use DockerUtil or a retrier
func (*DockerUtil) AllContainerLabels ¶
AllContainerLabels retrieves all running containers (`docker ps`) and returns a map mapping containerID to container labels as a map[string]string
func (*DockerUtil) ContainerLogs ¶
func (d *DockerUtil) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error)
func (*DockerUtil) CountVolumes ¶
CountVolumes returns the number of attached and dangling volumes.
func (*DockerUtil) GetContainerStats ¶
func (*DockerUtil) GetHostname ¶
func (d *DockerUtil) GetHostname(ctx context.Context) (string, error)
func (*DockerUtil) GetStorageStats ¶
func (d *DockerUtil) GetStorageStats(ctx context.Context) ([]*StorageStats, error)
GetStorageStats returns the docker global storage stats if available or ErrStorageStatsNotAvailable
func (*DockerUtil) Images ¶
func (d *DockerUtil) Images(ctx context.Context, includeIntermediate bool) ([]types.ImageSummary, error)
Images returns a slice of all images.
func (*DockerUtil) Inspect ¶
func (d *DockerUtil) Inspect(ctx context.Context, id string, withSize bool) (types.ContainerJSON, error)
Inspect returns a docker inspect object for a given container ID. It tries to locate the container in the inspect cache before making the docker inspect call
func (*DockerUtil) InspectNoCache ¶
func (d *DockerUtil) InspectNoCache(ctx context.Context, id string, withSize bool) (types.ContainerJSON, error)
InspectNoCache returns a docker inspect object for a given container ID. It ignores the inspect cache, always collecting fresh data from the docker daemon.
func (*DockerUtil) InspectSelf ¶
func (d *DockerUtil) InspectSelf(ctx context.Context) (types.ContainerJSON, error)
InspectSelf returns the inspect content of the container the current agent is running in
func (*DockerUtil) LatestContainerEvents ¶
func (d *DockerUtil) LatestContainerEvents(ctx context.Context, since time.Time) ([]*ContainerEvent, time.Time, error)
LatestContainerEvents returns events matching the filter that occurred after the time passed. It returns the latest event timestamp in the slice for the user to store and pass again in the next call.
func (*DockerUtil) ListContainers ¶
func (d *DockerUtil) ListContainers(ctx context.Context, cfg *ContainerListConfig) ([]*containers.Container, error)
ListContainers gets a list of all containers on the current node using a mix of the Docker APIs and cgroups stats. We attempt to limit syscalls where possible.
func (*DockerUtil) RawContainerList ¶
func (d *DockerUtil) RawContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
RawContainerList wraps around the docker client's ContainerList method. Value validation and error handling are the caller's responsibility.
func (*DockerUtil) ResolveImageName ¶
ResolveImageName will resolve sha image name to their user-friendly name. For non-sha/non-repodigest names we will just return the name as-is.
func (*DockerUtil) ResolveImageNameFromContainer ¶
func (d *DockerUtil) ResolveImageNameFromContainer(ctx context.Context, co types.ContainerJSON) (string, error)
ResolveImageNameFromContainer will resolve the container sha image name to their user-friendly name. It is similar to ResolveImageName except it tries to match the image to the container Config.Image. For non-sha names we will just return the name as-is.
func (*DockerUtil) SubscribeToContainerEvents ¶
func (d *DockerUtil) SubscribeToContainerEvents(name string) (<-chan *ContainerEvent, <-chan error, error)
SubscribeToContainerEvents allows a package to subscribe to events from the event stream. A unique subscriber name should be provided.
func (*DockerUtil) UnsubscribeFromContainerEvents ¶
func (d *DockerUtil) UnsubscribeFromContainerEvents(name string) error
UnsubscribeFromContainerEvents allows a package to unsubscribe. The call is blocking until the request is processed.
func (*DockerUtil) UpdateContainerMetrics ¶
func (d *DockerUtil) UpdateContainerMetrics(cList []*containers.Container) error
UpdateContainerMetrics updates cgroup / network performance metrics for a provided list of Container objects
type StorageStats ¶
StorageStats holds the available stats for a given storage type. Non available stats will result in nil pointer, user has to check for nil before using the value.
func (*StorageStats) GetPercentUsed ¶
func (s *StorageStats) GetPercentUsed() float64
GetPercentUsed computes the used percent (from 0 to 100), even if only two of three stats are available. If only one is available or total is 0, Nan is returned.