|
| 1 | +package cobalt |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/projecteru2/core/log" |
| 7 | + "github.com/projecteru2/core/resource3/plugins" |
| 8 | + plugintypes "github.com/projecteru2/core/resource3/plugins/types" |
| 9 | + "github.com/projecteru2/core/types" |
| 10 | + "github.com/projecteru2/core/utils" |
| 11 | +) |
| 12 | + |
| 13 | +/* |
| 14 | +Alloc |
| 15 | +opts struct |
| 16 | +
|
| 17 | + { |
| 18 | + "plugin1":{ |
| 19 | + "cpu-request": 1.2, |
| 20 | + "cpu-limit": 2.0, |
| 21 | + }, |
| 22 | + "plugin2":{ |
| 23 | + }, |
| 24 | + } |
| 25 | +*/ |
| 26 | +func (m Manager) Alloc(ctx context.Context, nodename string, deployCount int, opts types.Resources) ([]types.Resources, []types.Resources, error) { |
| 27 | + logger := log.WithFunc("resource.coblat.Alloc") |
| 28 | + |
| 29 | + // index -> no, map by plugin name |
| 30 | + workloadsParams := []types.Resources{} |
| 31 | + engineParams := []types.Resources{} |
| 32 | + |
| 33 | + // init engine args |
| 34 | + for i := 0; i < deployCount; i++ { |
| 35 | + workloadsParams[i] = types.Resources{} |
| 36 | + engineParams[i] = types.Resources{} |
| 37 | + } |
| 38 | + |
| 39 | + return workloadsParams, engineParams, utils.PCR(ctx, |
| 40 | + // prepare: calculate engine args and resource args |
| 41 | + func(ctx context.Context) error { |
| 42 | + resps, err := call(ctx, m.plugins, func(plugin plugins.Plugin) (*plugintypes.CalculateDeployResponse, error) { |
| 43 | + resp, err := plugin.CalculateDeploy(ctx, nodename, deployCount, opts[plugin.Name()]) |
| 44 | + if err != nil { |
| 45 | + logger.Errorf(ctx, err, "plugin %+v failed to compute alloc args, request %+v, node %+v, deploy count %+v", plugin.Name(), opts, nodename, deployCount) |
| 46 | + } |
| 47 | + return resp, err |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // calculate engine args |
| 54 | + for plugin, resp := range resps { |
| 55 | + logger.Debug(ctx, plugin.Name()) |
| 56 | + for index, params := range resp.WorkloadsResource { |
| 57 | + workloadsParams[index][plugin.Name()] = params |
| 58 | + } |
| 59 | + for index, params := range resp.EnginesParams { |
| 60 | + v := engineParams[index][plugin.Name()] |
| 61 | + vMerged, err := m.mergeEngineParams(ctx, v, params) |
| 62 | + if err != nil { |
| 63 | + logger.Error(ctx, err, "invalid engine args") |
| 64 | + return err |
| 65 | + } |
| 66 | + engineParams[index][plugin.Name()] = vMerged |
| 67 | + } |
| 68 | + } |
| 69 | + return nil |
| 70 | + }, |
| 71 | + // commit: update node resources |
| 72 | + func(ctx context.Context) error { |
| 73 | + // TODO why incr? |
| 74 | + if _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, workloadsParams, true, plugins.Incr); err != nil { |
| 75 | + logger.Error(ctx, err, "failed to update node resource") |
| 76 | + return err |
| 77 | + } |
| 78 | + return nil |
| 79 | + }, |
| 80 | + // rollback: do nothing |
| 81 | + func(ctx context.Context) error { |
| 82 | + return nil |
| 83 | + }, |
| 84 | + m.config.GlobalTimeout, |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +// RollbackAlloc rollbacks the allocated resource |
| 89 | +func (m Manager) RollbackAlloc(ctx context.Context, nodename string, workloadsParams []types.Resources) error { |
| 90 | + _, _, err := m.SetNodeResourceUsage(ctx, nodename, nil, nil, workloadsParams, true, plugins.Decr) |
| 91 | + return err |
| 92 | +} |
0 commit comments