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

     1  package calcium
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/projecteru2/core/log"
     7  	"github.com/projecteru2/core/types"
     8  	"github.com/projecteru2/core/utils"
     9  )
    10  
    11  type remapMsg struct {
    12  	ID  string
    13  	err error
    14  }
    15  
    16  // RemapResourceAndLog called on changes of resource binding, such as cpu binding
    17  // as an internal api, remap doesn't lock node, the responsibility of that should be taken on by caller
    18  func (c *Calcium) RemapResourceAndLog(ctx context.Context, logger *log.Fields, node *types.Node) {
    19  	ctx, cancel := context.WithTimeout(utils.NewInheritCtx(ctx), c.config.GlobalTimeout)
    20  	defer cancel()
    21  
    22  	err := c.withNodeOperationLocked(ctx, node.Name, func(ctx context.Context, node *types.Node) error {
    23  		if ch, err := c.doRemapResource(ctx, node); err == nil {
    24  			for msg := range ch {
    25  				logger.Infof(ctx, "remap workload ID %+v", msg.ID)
    26  				if msg.err != nil {
    27  					logger.Error(ctx, msg.err)
    28  				}
    29  			}
    30  		}
    31  		return nil
    32  	})
    33  
    34  	if err != nil {
    35  		logger.Error(ctx, err, "remap node failed")
    36  	}
    37  }
    38  
    39  func (c *Calcium) doRemapResource(ctx context.Context, node *types.Node) (ch chan *remapMsg, err error) {
    40  	workloads, err := c.store.ListNodeWorkloads(ctx, node.Name, nil)
    41  	if err != nil {
    42  		return
    43  	}
    44  
    45  	engineParamsMap, err := c.rmgr.Remap(ctx, node.Name, workloads)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	ch = make(chan *remapMsg, len(engineParamsMap))
    51  	_ = c.pool.Invoke(func() {
    52  		defer close(ch)
    53  		for workloadID, engineParams := range engineParamsMap {
    54  			ch <- &remapMsg{
    55  				ID:  workloadID,
    56  				err: node.Engine.VirtualizationUpdateResource(ctx, workloadID, engineParams),
    57  			}
    58  		}
    59  	})
    60  
    61  	return ch, nil
    62  }