github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/resource/plugins/plugin.go (about)

     1  package plugins
     2  
     3  import (
     4  	"context"
     5  
     6  	enginetypes "github.com/projecteru2/core/engine/types"
     7  	plugintypes "github.com/projecteru2/core/resource/plugins/types"
     8  )
     9  
    10  const (
    11  	// Incr increase
    12  	Incr = true
    13  
    14  	// Decr decrease
    15  	Decr = false
    16  )
    17  
    18  // Plugin .
    19  type Plugin interface {
    20  	// CalculateDeploy tries to allocate resource, returns engine params for each workload, format: [{"cpus": 1.2}, {"cpus": 1.2}]
    21  	// also returns resource params for each workload, format: [{"cpus": 1.2}, {"cpus": 1.2}]
    22  	// pure calculation
    23  	CalculateDeploy(ctx context.Context, nodename string, deployCount int, resourceRequest plugintypes.WorkloadResourceRequest) (*plugintypes.CalculateDeployResponse, error)
    24  
    25  	// CalculateRealloc tries to reallocate resource, returns engine params, delta resource params and final resource params.
    26  	// should return error if resource of some node is not enough for the realloc operation.
    27  	// pure calculation
    28  	CalculateRealloc(ctx context.Context, nodename string, resource plugintypes.WorkloadResource, resourceRequest plugintypes.WorkloadResourceRequest) (*plugintypes.CalculateReallocResponse, error)
    29  
    30  	// CalculateRemap tries to remap resource based on workload metadata and node resource usage, then returns engine params for workloads.
    31  	// pure calculation, for clarification, here use map's key to store the workload ID
    32  	CalculateRemap(ctx context.Context, nodename string, workloadsResource map[string]plugintypes.WorkloadResource) (*plugintypes.CalculateRemapResponse, error)
    33  
    34  	// AddNode adds a node with requested resource, returns resource capacity and (empty) resource usage
    35  	// should return error if the node already exists
    36  	AddNode(ctx context.Context, nodename string, resource plugintypes.NodeResourceRequest, info *enginetypes.Info) (*plugintypes.AddNodeResponse, error)
    37  
    38  	// RemoveNode removes node
    39  	RemoveNode(ctx context.Context, nodename string) (*plugintypes.RemoveNodeResponse, error)
    40  
    41  	// GetNodesDeployCapacity returns available nodes and total capacity
    42  	GetNodesDeployCapacity(ctx context.Context, nodenames []string, resource plugintypes.WorkloadResourceRequest) (*plugintypes.GetNodesDeployCapacityResponse, error)
    43  
    44  	// SetNodeResourceCapacity sets the amount of total resource info
    45  	SetNodeResourceCapacity(ctx context.Context, nodename string, resource plugintypes.NodeResource, resourceRequest plugintypes.NodeResourceRequest, delta bool, incr bool) (*plugintypes.SetNodeResourceCapacityResponse, error)
    46  
    47  	// GetNodeResourceInfo returns total resource info and available resource info of the node, format: {"cpu": 2}
    48  	// also returns diffs, format: ["node.VolumeUsed != sum(workload.VolumeRequest"]
    49  	GetNodeResourceInfo(ctx context.Context, nodename string, workloadsResource []plugintypes.WorkloadResource) (*plugintypes.GetNodeResourceInfoResponse, error)
    50  
    51  	// SetNodeResourceInfo sets both total node resource info and allocated resource info
    52  	// used for rollback of RemoveNode
    53  	// notice: here uses absolute values, not delta values
    54  	SetNodeResourceInfo(ctx context.Context, nodename string, capacity plugintypes.NodeResource, usage plugintypes.NodeResource) (*plugintypes.SetNodeResourceInfoResponse, error)
    55  
    56  	// SetNodeResourceUsage sets the amount of allocated resource info
    57  	SetNodeResourceUsage(ctx context.Context, nodename string, resource plugintypes.NodeResource, resourceRequest plugintypes.NodeResourceRequest, workloadsResource []plugintypes.WorkloadResource, delta bool, incr bool) (*plugintypes.SetNodeResourceUsageResponse, error)
    58  
    59  	// GetMostIdleNode returns the most idle node for building
    60  	GetMostIdleNode(ctx context.Context, nodenames []string) (*plugintypes.GetMostIdleNodeResponse, error)
    61  
    62  	// FixNodeResource fixes the node resource usage by its workloads
    63  	FixNodeResource(ctx context.Context, nodename string, workloadsResource []plugintypes.WorkloadResource) (*plugintypes.GetNodeResourceInfoResponse, error)
    64  
    65  	// GetMetricsDescription returns metrics description
    66  	GetMetricsDescription(ctx context.Context) (*plugintypes.GetMetricsDescriptionResponse, error)
    67  
    68  	// GetMetrics resolves node resource info to metrics
    69  	GetMetrics(ctx context.Context, podname, nodename string) (*plugintypes.GetMetricsResponse, error)
    70  
    71  	// Name returns the name of plugin
    72  	Name() string
    73  }