github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/workload.go (about)

     1  package calcium
     2  
     3  // All functions are just proxy to store, since I don't want store to be exported.
     4  // All these functions are meta data related.
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/projecteru2/core/log"
    10  	"github.com/projecteru2/core/types"
    11  )
    12  
    13  // GetWorkload get a workload
    14  func (c *Calcium) GetWorkload(ctx context.Context, ID string) (workload *types.Workload, err error) {
    15  	logger := log.WithFunc("calcium.GetWorkload").WithField("ID", ID)
    16  	if ID == "" {
    17  		logger.Error(ctx, types.ErrEmptyWorkloadID)
    18  		return workload, types.ErrEmptyWorkloadID
    19  	}
    20  	workload, err = c.store.GetWorkload(ctx, ID)
    21  	logger.Error(ctx, err)
    22  	return workload, err
    23  }
    24  
    25  // GetWorkloads get workloads
    26  func (c *Calcium) GetWorkloads(ctx context.Context, IDs []string) (workloads []*types.Workload, err error) {
    27  	workloads, err = c.store.GetWorkloads(ctx, IDs)
    28  	log.WithFunc("calcium.GetWorkloads").WithField("IDs", IDs).Error(ctx, err)
    29  	return workloads, err
    30  }
    31  
    32  // ListWorkloads list workloads
    33  func (c *Calcium) ListWorkloads(ctx context.Context, opts *types.ListWorkloadsOptions) (workloads []*types.Workload, err error) {
    34  	if workloads, err = c.store.ListWorkloads(ctx, opts.Appname, opts.Entrypoint, opts.Nodename, opts.Limit, opts.Labels); err != nil {
    35  		log.WithFunc("calcium.ListWorkloads").WithField("opts", opts).Errorf(ctx, err, "Calcium.ListWorkloads] store list failed: %+v", err)
    36  	}
    37  	return workloads, err
    38  }
    39  
    40  // ListNodeWorkloads list workloads belong to one node
    41  func (c *Calcium) ListNodeWorkloads(ctx context.Context, nodename string, labels map[string]string) (workloads []*types.Workload, err error) {
    42  	logger := log.WithFunc("calcium.ListNodeWorkloads").WithField("node", nodename).WithField("labels", labels)
    43  	if nodename == "" {
    44  		logger.Error(ctx, types.ErrEmptyNodeName)
    45  		return workloads, types.ErrEmptyNodeName
    46  	}
    47  	workloads, err = c.store.ListNodeWorkloads(ctx, nodename, labels)
    48  	logger.Error(ctx, err)
    49  	return workloads, err
    50  }