internal

package
v0.0.0-...-f706b47 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const REQ_LIM_RATIO = 10

REQ_LIM_RATIO is the ratio between namespace ResourceQuota CPU requests and CPU limits. E.g. with ratio=10 when a consumer requests 400m CPU, they will have a 4 core CPU limit.

Variables

View Source
var ResizeApiFunc = InvokeResizeApiStub // TODO: replace this with your own stack resize!
View Source
var ResizeNsChan = make(chan NamespaceResizeEvent)
View Source
var ResizeResultChan = make(chan ResizeResult, 1024)

Functions

func CalculatePodResources

func CalculatePodResources(podTemplate v12.PodTemplateSpec, missingReplicas int64) resources.Resources

CalculatePodResources sums the container resources of a Pod and multiplies them by the missing replicas. Zero or negative replicas will result in an empty Resource.

func CalculateScaleDown

func CalculateScaleDown(policy *ActivePolicy) int64

CalculateScaleDown calculates the desired value a quota should have given the scaleDown policy.

func CalculateScaleUp

func CalculateScaleUp(policy *ActivePolicy) int64

CalculateScaleUp calculates the desired value a quota should have given the scaleUp policy.

func GetNormalizedUsedCpu

func GetNormalizedUsedCpu(request, limit *resource.Quantity, ns string) resource.Quantity

GetNormalizedUsedCpu calculates if the CPU limit / 10 is bigger than the CPU requests, if so we should scale based on the CPU limit in order not to breach the namespace quota. We then "fake" the CPU request to be higher so that future calculations only have to worry about CPU requests. If the ratio is not exceeded the requested values are returned. Ns variable only used for logging.

func GetResourcesFromPodEvents

func GetResourcesFromPodEvents(client kubernetes.Interface, events []v12.Event) (*resources.Resources, error)

func InvokeResizeApi

func InvokeResizeApi(ns NamespaceResizeEvent) error

TODO: this is left as an example as a resize endpoint, this code is not used InvokeResizeApi issues a namespace patch operation to the ICHP-API. The provided `cpu` must in Milli Cores and the `memory` must be in Mega Bytes. The ICHP-API is discovered via environment variable `ICHP_API_ENDPOINT`, with bearer token auth using environment variable `TOKEN`.

func InvokeResizeApiAsync

func InvokeResizeApiAsync(namespace, resourcequota string, old, new resources.Resources)

func InvokeResizeApiStub

func InvokeResizeApiStub(ns NamespaceResizeEvent) error

func ParseQuantityWithDefault

func ParseQuantityWithDefault(value string, scale resource.Scale, def int64) int64

ParseQuantityWithDefault attempts to parse the given value as the provided scale. Default is used when parsing fails.

func PublishNamespaceEvent

func PublishNamespaceEvent(client *kubernetes.Clientset, ref v1.ObjectReference, ev ResizeResult) error

func ResourceQuotaUsedCpuLimit

func ResourceQuotaUsedCpuLimit(quota *v12.ResourceQuota) *resource.Quantity

func ResourceQuotaUsedMemoryLimit

func ResourceQuotaUsedMemoryLimit(quota *v12.ResourceQuota) *resource.Quantity

func RunEventHandler

func RunEventHandler()

RunEventHandler listens to Async Resize API requests. Replies are published on ResizeResultChan and must be read.

func WatchQuotas

func WatchQuotas(client *kubernetes.Clientset, startScalers []v14.QuotaAutoscaler, quotas, scalers, events <-chan watch.Event, cmEvents <-chan watch.Event)

WatchQuotas listens to namespaced ResourceQuotas and QuotaAutoscalers. When both are known for a namespace the required behaviour is calculated. If scaling is required, following the behavior, the resize API is invoked. This is a blocking call until either channel terminates.

Types

type ActivePolicy

type ActivePolicy struct {
	IsCpu                  bool
	CurrentMaximum         int64
	CurrentUsagePercentage int64
	PolicyThreshold        int64
	QuotaLimit             int64
	MinimalStep            int64
	MaximumStep            int64
	Used                   int64
}

type IchpApiResponse

type IchpApiResponse struct {
	Clusters []struct {
		Message string `json:"message"`
	} `json:"clusters"`
	Status    string `json:"status"`
	RequestId string `json:"requestID"`
}

type NamespacePatch

type NamespacePatch struct {
	Name     string             `json:"name"`
	Workload string             `json:"workload,omitempty"`
	Spec     NamespacePatchSpec `json:"spec"`
}

NamespacePatch is the expected schema for an ICHP-API namespace patch operation

type NamespacePatchSpec

type NamespacePatchSpec struct {
	Quota NamespacePatchSpecQuota `json:"quota"`
}

type NamespacePatchSpecQuota

type NamespacePatchSpecQuota struct {
	Cpu     int64 `json:"cpu"`
	Memory  int64 `json:"memory"`
	Storage int64 `json:"storage"`
}

type NamespaceResizeEvent

type NamespaceResizeEvent struct {
	Namespace     string
	ResourceQuota string
	Old           resources.Resources
	New           resources.Resources
}

type QuotaWatcher

type QuotaWatcher struct {
	Scalers map[string]v14.QuotaAutoscaler
	Quotas  map[string]v12.ResourceQuota
	Events  map[string][]v12.Event

	Client *kubernetes.Clientset
}

QuotaWatcher internally manages a list of QuotaAutoscalers and ResourceQuotas.

func (*QuotaWatcher) RegisterMissingResourceQuota

func (watcher *QuotaWatcher) RegisterMissingResourceQuota(namespace, quotaName string) error

RegisterMissingResourceQuota fetches the given quota and stores it in the watcher. Does not consume Lock, so it must be called when holding the (Mutex) Lock.

func (*QuotaWatcher) RegisterNamespacedEvent

func (watcher *QuotaWatcher) RegisterNamespacedEvent(event watch.Event) string

RegisterNamespacedEvent stores namespaced Events in watcher. Should be cleaned up by aggregate loop every iteration (events should be consumed once). This function does not delete any stored events.

func (*QuotaWatcher) RegisterQuotaEvent

func (watcher *QuotaWatcher) RegisterQuotaEvent(event watch.Event) string

RegisterQuotaEvent stores a ResourceQuota in watcher, or deletes it.

func (*QuotaWatcher) RegisterScalerEvent

func (watcher *QuotaWatcher) RegisterScalerEvent(event watch.Event) string

RegisterScalerEvent stores a QuotaAutoscaler in watcher, or deletes it.

func (*QuotaWatcher) UpdateNs

func (watcher *QuotaWatcher) UpdateNs(namespace string, readEvents bool)

func (*QuotaWatcher) UpdateQuotaIfRequired

func (watcher *QuotaWatcher) UpdateQuotaIfRequired(quota v12.ResourceQuota, scaler v14.QuotaAutoscaler, events []v12.Event) error

type ResizeCache

type ResizeCache struct {
	Timestamp time.Time
	Event     NamespaceResizeEvent
}

type ResizeResult

type ResizeResult struct {
	NamespaceResizeEvent
	Err error
}

type ValidatedQuotaScaler

type ValidatedQuotaScaler struct {
	MinCpu     int64 `json:"minCpu,omitempty"`
	MaxCpu     int64 `json:"maxCpu,omitempty"`
	MinCpuStep int64 `json:"minCpuStep,omitempty"`
	MaxCpuStep int64 `json:"maxCpuStep,omitempty"`

	MinMemory     int64 `json:"minMemory,omitempty"`
	MaxMemory     int64 `json:"maxMemory,omitempty"`
	MinMemoryStep int64 `json:"minMemoryStep,omitempty"`
	MaxMemoryStep int64 `json:"maxMemoryStep,omitempty"`
}

func ValidateQuotaScaler

func ValidateQuotaScaler(scaler *v1.QuotaAutoscaler) *ValidatedQuotaScaler

ValidateQuotaScaler validates all fields of the given QuotaAutoscaler and converts them to Milli Cores for CPU and Mega Bytes for Memory. When no values are provided defaults are filled in.

func (*ValidatedQuotaScaler) ActivatePolicy

func (scaler *ValidatedQuotaScaler) ActivatePolicy(scaleUp bool, policy v1.QuotaScalePolicy, quota *v12.ResourceQuota) (*ActivePolicy, int64)

ActivatePolicy first converts a QuotaScalePolicy to an ActivePolicy given the scaleUp type and ResourceQuota values. It then uses the active policy to calculate the desired scaling value (Milli Cores for CPU and Mega Bytes for Memory). When a policy is not in effect (no scaling should occur) it returns the converted policy and 0.

func (*ValidatedQuotaScaler) ActivateScalerPolicy

func (scaler *ValidatedQuotaScaler) ActivateScalerPolicy(policy v1.QuotaScalePolicy, quota *v12.ResourceQuota, scaleUp bool) *resources.Resources

func (*ValidatedQuotaScaler) ForceLimitToDefaultMax

func (scaler *ValidatedQuotaScaler) ForceLimitToDefaultMax()

func (*ValidatedQuotaScaler) ToActivePolicy

func (scaler *ValidatedQuotaScaler) ToActivePolicy(scaleUp bool, policy v1.QuotaScalePolicy, quota *v12.ResourceQuota) *ActivePolicy

ToActivePolicy converts a QuotaScalePolicy to an ActivePolicy given the scaleUp type and ResourceQuota values.

Jump to

Keyboard shortcuts

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