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

     1  package binary
     2  
     3  import (
     4  	"context"
     5  
     6  	enginetypes "github.com/projecteru2/core/engine/types"
     7  	binarytypes "github.com/projecteru2/core/resource/plugins/binary/types"
     8  	plugintypes "github.com/projecteru2/core/resource/plugins/types"
     9  )
    10  
    11  // AddNode .
    12  func (p Plugin) AddNode(ctx context.Context, nodename string, resource plugintypes.NodeResourceRequest, info *enginetypes.Info) (*plugintypes.AddNodeResponse, error) {
    13  	req := &binarytypes.AddNodeRequest{
    14  		Nodename: nodename,
    15  		Resource: resource,
    16  		Info:     info,
    17  	}
    18  	resp := &plugintypes.AddNodeResponse{}
    19  	return resp, p.call(ctx, AddNodeCommand, req, resp)
    20  }
    21  
    22  // RemoveNode .
    23  func (p Plugin) RemoveNode(ctx context.Context, nodename string) (*plugintypes.RemoveNodeResponse, error) {
    24  	req := &binarytypes.RemoveNodeRequest{
    25  		Nodename: nodename,
    26  	}
    27  	resp := &plugintypes.RemoveNodeResponse{}
    28  	return resp, p.call(ctx, RemoveNodeCommand, req, resp)
    29  }
    30  
    31  // GetNodesDeployCapacity .
    32  func (p Plugin) GetNodesDeployCapacity(ctx context.Context, nodenames []string, resource plugintypes.WorkloadResourceRequest) (*plugintypes.GetNodesDeployCapacityResponse, error) {
    33  	req := &binarytypes.GetNodesDeployCapacityRequest{
    34  		Nodenames:        nodenames,
    35  		WorkloadResource: resource,
    36  	}
    37  	resp := &plugintypes.GetNodesDeployCapacityResponse{}
    38  	return resp, p.call(ctx, GetNodesDeployCapacityCommand, req, resp)
    39  }
    40  
    41  // SetNodeResourceCapacity .
    42  func (p Plugin) SetNodeResourceCapacity(ctx context.Context, nodename string, resource plugintypes.NodeResource, resourceRequest plugintypes.NodeResourceRequest, delta bool, incr bool) (*plugintypes.SetNodeResourceCapacityResponse, error) {
    43  	req := &binarytypes.SetNodeResourceCapacityRequest{
    44  		Nodename:        nodename,
    45  		Resource:        resource,
    46  		ResourceRequest: resourceRequest,
    47  		Delta:           delta,
    48  		Incr:            incr,
    49  	}
    50  
    51  	resp := &plugintypes.SetNodeResourceCapacityResponse{}
    52  	return resp, p.call(ctx, SetNodeResourceCapacityCommand, req, resp)
    53  }
    54  
    55  // GetNodeResourceInfo .
    56  func (p Plugin) GetNodeResourceInfo(ctx context.Context, nodename string, workloadsResource []plugintypes.WorkloadResource) (*plugintypes.GetNodeResourceInfoResponse, error) {
    57  	return p.doGetNodeResourceInfo(ctx, nodename, workloadsResource, false) // Get do not fix the resource
    58  }
    59  
    60  // SetNodeResourceInfo .
    61  func (p Plugin) SetNodeResourceInfo(ctx context.Context, nodename string, capacity plugintypes.NodeResource, usage plugintypes.NodeResource) (*plugintypes.SetNodeResourceInfoResponse, error) {
    62  	req := &binarytypes.SetNodeResourceInfoRequest{
    63  		Nodename: nodename,
    64  		Capacity: capacity,
    65  		Usage:    usage,
    66  	}
    67  	resp := &plugintypes.SetNodeResourceInfoResponse{}
    68  	return resp, p.call(ctx, SetNodeResourceInfoCommand, req, resp)
    69  
    70  }
    71  
    72  // SetNodeResourceUsage .
    73  func (p Plugin) SetNodeResourceUsage(ctx context.Context, nodename string, resource plugintypes.NodeResource, resourceRequest plugintypes.NodeResourceRequest, workloadsResource []plugintypes.WorkloadResource, delta bool, incr bool) (*plugintypes.SetNodeResourceUsageResponse, error) {
    74  	req := &binarytypes.SetNodeResourceUsageRequest{
    75  		Nodename:          nodename,
    76  		WorkloadsResource: workloadsResource,
    77  		Resource:          resource,
    78  		ResourceRequest:   resourceRequest,
    79  		Delta:             delta,
    80  		Incr:              incr,
    81  	}
    82  
    83  	resp := &plugintypes.SetNodeResourceUsageResponse{}
    84  	return resp, p.call(ctx, SetNodeResourceUsageCommand, req, resp)
    85  }
    86  
    87  // GetMostIdleNode .
    88  func (p Plugin) GetMostIdleNode(ctx context.Context, nodenames []string) (*plugintypes.GetMostIdleNodeResponse, error) {
    89  	req := &binarytypes.GetMostIdleNodeRequest{
    90  		Nodenames: nodenames,
    91  	}
    92  	resp := &plugintypes.GetMostIdleNodeResponse{}
    93  	return resp, p.call(ctx, GetMostIdleNodeCommand, req, resp)
    94  }
    95  
    96  // FixNodeResource .
    97  func (p Plugin) FixNodeResource(ctx context.Context, nodename string, workloadsResource []plugintypes.WorkloadResource) (*plugintypes.GetNodeResourceInfoResponse, error) {
    98  	return p.doGetNodeResourceInfo(ctx, nodename, workloadsResource, true)
    99  }
   100  
   101  func (p Plugin) doGetNodeResourceInfo(ctx context.Context, nodename string, workloadsResource []plugintypes.WorkloadResource, fix bool) (*plugintypes.GetNodeResourceInfoResponse, error) {
   102  	req := &binarytypes.GetNodeResourceInfoRequest{
   103  		Nodename:          nodename,
   104  		WorkloadsResource: workloadsResource,
   105  	}
   106  	resp := &plugintypes.GetNodeResourceInfoResponse{}
   107  	if !fix {
   108  		return resp, p.call(ctx, GetNodeResourceInfoCommand, req, resp)
   109  	}
   110  	return resp, p.call(ctx, FixNodeResourceCommand, req, resp)
   111  }