Skip to content

Commit ca2d613

Browse files
committed
chore: aws mac-pool action split mac-pool functions
Signed-off-by: Adrian Riobo <[email protected]>
1 parent 1623b33 commit ca2d613

File tree

4 files changed

+175
-135
lines changed

4 files changed

+175
-135
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package macpool
2+
3+
import (
4+
"fmt"
5+
6+
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
7+
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
8+
"github.com/redhat-developer/mapt/pkg/util/logging"
9+
)
10+
11+
// House keeper is the function executed serverless to check if is there any
12+
// machine non locked which had been running more than 24h.
13+
// It should check if capacity allows to remove the machine
14+
func houseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
15+
// Create mapt Context, this is a special case where we need change the context
16+
// based on the operation
17+
if err := maptContext.Init(ctx); err != nil {
18+
return err
19+
}
20+
21+
// Get full info on the pool
22+
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
23+
if err != nil {
24+
return err
25+
}
26+
// Pool under expected offered capacity
27+
if p.currentOfferedCapacity() < r.OfferedCapacity {
28+
if p.currentPoolSize() < r.MaxSize {
29+
logging.Debug("house keeper will try to add machines as offered capacity is lower than expected")
30+
maptContext.SetProjectName(r.PoolName)
31+
return r.addCapacity(p)
32+
}
33+
// if number of machines in the pool + to max machines
34+
// we do nothing
35+
logging.Debug("house keeper will not do any action as pool size is currently at max size")
36+
return nil
37+
}
38+
// Pool over expected offered capacity need to destroy machines
39+
if p.currentOfferedCapacity() > r.OfferedCapacity {
40+
if len(p.destroyableMachines) > 0 {
41+
logging.Debug("house keeper will try to destroy machines as offered capacity is higher than expected")
42+
// Need to check if any offered can be destroy
43+
return r.destroyCapacity(p)
44+
}
45+
}
46+
logging.Debug("house keeper will not do any action as offered capacity is met by the pool")
47+
// Otherwise nonLockedMachines meet Capacity so we do nothing
48+
return nil
49+
}
50+
51+
// Run serverless operation for house keeping
52+
func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
53+
return serverless.Create(
54+
&serverless.ServerlessArgs{
55+
Command: houseKeepingCommand(
56+
r.PoolName,
57+
r.Architecture,
58+
r.OSVersion,
59+
r.OfferedCapacity,
60+
r.MaxSize,
61+
r.FixedLocation),
62+
ScheduleType: &serverless.Repeat,
63+
Schedulexpression: houseKeepingInterval,
64+
LogGroupName: fmt.Sprintf("%s-%s-%s",
65+
r.PoolName,
66+
r.Architecture,
67+
r.OSVersion)})
68+
}
69+
70+
func houseKeepingCommand(poolName, arch, osVersion string,
71+
offeredCapacity, maxSize int,
72+
fixedLocation bool) string {
73+
cmd := fmt.Sprintf(houseKeepingCommandRegex,
74+
poolName, arch, osVersion,
75+
offeredCapacity, maxSize)
76+
if fixedLocation {
77+
cmd += houseKeepingFixedLocationParam
78+
}
79+
return cmd
80+
}

pkg/provider/aws/action/mac-pool/mac-pool.go

Lines changed: 2 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ import (
1010
"github.com/redhat-developer/mapt/pkg/provider/aws"
1111
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/iam"
1212
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac"
13-
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
1413
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
1514
macMachine "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/machine"
1615
macUtil "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/util"
1716
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
18-
"github.com/redhat-developer/mapt/pkg/provider/aws/services/tag"
1917
"github.com/redhat-developer/mapt/pkg/util"
2018
"github.com/redhat-developer/mapt/pkg/util/logging"
2119
)
@@ -58,88 +56,15 @@ func Destroy(ctx *maptContext.ContextArgs) error {
5856
// machine non locked which had been running more than 24h.
5957
// It should check if capacity allows to remove the machine
6058
func HouseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
61-
// Create mapt Context, this is a special case where we need change the context
62-
// based on the operation
63-
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
64-
return err
65-
}
66-
67-
// Get full info on the pool
68-
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
69-
if err != nil {
70-
return err
71-
}
72-
// Pool under expected offered capacity
73-
if p.currentOfferedCapacity() < r.OfferedCapacity {
74-
if p.currentPoolSize() < r.MaxSize {
75-
logging.Debug("house keeper will try to add machines as offered capacity is lower than expected")
76-
maptContext.SetProjectName(r.PoolName)
77-
return r.addCapacity(p)
78-
}
79-
// if number of machines in the pool + to max machines
80-
// we do nothing
81-
logging.Debug("house keeper will not do any action as pool size is currently at max size")
82-
return nil
83-
}
84-
// Pool over expected offered capacity need to destroy machines
85-
if p.currentOfferedCapacity() > r.OfferedCapacity {
86-
if len(p.destroyableMachines) > 0 {
87-
logging.Debug("house keeper will try to destroy machines as offered capacity is higher than expected")
88-
// Need to check if any offered can be destroy
89-
return r.destroyCapacity(p)
90-
}
91-
}
92-
logging.Debug("house keeper will not do any action as offered capacity is met by the pool")
93-
// Otherwise nonLockedMachines meet Capacity so we do nothing
94-
return nil
59+
return houseKeeper(ctx, r)
9560
}
9661

9762
func Request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
9863
// If remote run through serverless
9964
if r.Remote {
10065
return requestRemote(ctx, r)
10166
}
102-
// First get full info on the pool and the next machine for request
103-
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
104-
if err != nil {
105-
return err
106-
}
107-
hi, err := p.getNextMachineForRequest()
108-
if err != nil {
109-
return err
110-
}
111-
112-
// Create mapt Context
113-
ctx.ProjectName = *hi.ProjectName
114-
ctx.BackedURL = *hi.BackedURL
115-
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
116-
return err
117-
}
118-
119-
mr := macMachine.Request{
120-
Prefix: *hi.Prefix,
121-
Version: *hi.OSVersion,
122-
Architecture: *hi.Arch,
123-
Timeout: r.Timeout,
124-
}
125-
126-
// TODO here we would change based on the integration-mode requested
127-
// possible values remote-shh, gh-selfhosted-runner, cirrus-persistent-worker
128-
err = mr.ManageRequest(hi)
129-
if err != nil {
130-
return err
131-
}
132-
133-
// We update the runID on the dedicated host
134-
return tag.Update(maptContext.TagKeyRunID,
135-
maptContext.RunID(),
136-
*hi.Region,
137-
*hi.Host.HostId)
138-
}
139-
140-
func requestRemote(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
141-
142-
return fmt.Errorf("not implemented yet")
67+
return request(ctx, r)
14368
}
14469

14570
func Release(ctx *maptContext.ContextArgs, hostID string) error {
@@ -164,64 +89,6 @@ func (r *MacPoolRequestArgs) addMachinesToPool(n int) error {
16489
return nil
16590
}
16691

167-
// Run serverless operation for house keeping
168-
func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
169-
return serverless.Create(
170-
&serverless.ServerlessArgs{
171-
Command: houseKeepingCommand(
172-
r.PoolName,
173-
r.Architecture,
174-
r.OSVersion,
175-
r.OfferedCapacity,
176-
r.MaxSize,
177-
r.FixedLocation),
178-
ScheduleType: &serverless.Repeat,
179-
Schedulexpression: houseKeepingInterval,
180-
LogGroupName: fmt.Sprintf("%s-%s-%s",
181-
r.PoolName,
182-
r.Architecture,
183-
r.OSVersion)})
184-
}
185-
186-
// Run serverless operation request
187-
// check how we will call it from the request?
188-
// may add tags and find or add arn to stack?
189-
func (r *MacPoolRequestArgs) createRequestTaskSpec() error {
190-
return serverless.Create(
191-
&serverless.ServerlessArgs{
192-
Command: requestCommand(
193-
r.PoolName,
194-
r.Architecture,
195-
r.OSVersion),
196-
LogGroupName: fmt.Sprintf("%s-%s-%s-request",
197-
r.PoolName,
198-
r.Architecture,
199-
r.OSVersion),
200-
Tags: map[string]string{
201-
macConstants.TagKeyArch: r.Architecture,
202-
macConstants.TagKeyOSVersion: r.OSVersion,
203-
macConstants.TagKeyPoolName: r.PoolName,
204-
}})
205-
}
206-
207-
func houseKeepingCommand(poolName, arch, osVersion string,
208-
offeredCapacity, maxSize int,
209-
fixedLocation bool) string {
210-
cmd := fmt.Sprintf(houseKeepingCommandRegex,
211-
poolName, arch, osVersion,
212-
offeredCapacity, maxSize)
213-
if fixedLocation {
214-
cmd += houseKeepingFixedLocationParam
215-
}
216-
return cmd
217-
}
218-
219-
func requestCommand(poolName, arch, osVersion string) string {
220-
cmd := fmt.Sprintf(requestCommandRegex,
221-
poolName, arch, osVersion)
222-
return cmd
223-
}
224-
22592
// If we need less or equal than the max allowed on the pool we create all of them
22693
// if need are more than allowed we can create just the allowed
22794
func (r *MacPoolRequestArgs) addCapacity(p *pool) error {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package macpool
2+
3+
import (
4+
"fmt"
5+
6+
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
7+
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
8+
macMachine "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/machine"
9+
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
10+
"github.com/redhat-developer/mapt/pkg/provider/aws/services/tag"
11+
)
12+
13+
func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
14+
// If remote run through serverless
15+
if r.Remote {
16+
return requestRemote(ctx, r)
17+
}
18+
// First get full info on the pool and the next machine for request
19+
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
20+
if err != nil {
21+
return err
22+
}
23+
hi, err := p.getNextMachineForRequest()
24+
if err != nil {
25+
return err
26+
}
27+
28+
// Create mapt Context
29+
ctx.ProjectName = *hi.ProjectName
30+
ctx.BackedURL = *hi.BackedURL
31+
if err := maptContext.Init(ctx); err != nil {
32+
return err
33+
}
34+
35+
mr := macMachine.Request{
36+
Prefix: *hi.Prefix,
37+
Version: *hi.OSVersion,
38+
Architecture: *hi.Arch,
39+
SetupGHActionsRunner: r.SetupGHActionsRunner,
40+
Timeout: r.Timeout,
41+
}
42+
43+
// TODO here we would change based on the integration-mode requested
44+
// possible values remote-shh, gh-selfhosted-runner, cirrus-persistent-worker
45+
err = mr.ManageRequest(hi)
46+
if err != nil {
47+
return err
48+
}
49+
50+
// We update the runID on the dedicated host
51+
return tag.Update(maptContext.TagKeyRunID,
52+
maptContext.RunID(),
53+
*hi.Region,
54+
*hi.Host.HostId)
55+
}
56+
57+
func requestRemote(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
58+
return fmt.Errorf("not implemented yet")
59+
}
60+
61+
// Run serverless operation request
62+
// check how we will call it from the request?
63+
// may add tags and find or add arn to stack?
64+
func (r *MacPoolRequestArgs) createRequestTaskSpec() error {
65+
return serverless.Create(
66+
&serverless.ServerlessArgs{
67+
Command: requestCommand(
68+
r.PoolName,
69+
r.Architecture,
70+
r.OSVersion),
71+
LogGroupName: fmt.Sprintf("%s-%s-%s-request",
72+
r.PoolName,
73+
r.Architecture,
74+
r.OSVersion),
75+
Tags: map[string]string{
76+
macConstants.TagKeyArch: r.Architecture,
77+
macConstants.TagKeyOSVersion: r.OSVersion,
78+
macConstants.TagKeyPoolName: r.PoolName,
79+
}})
80+
}
81+
82+
func requestCommand(poolName, arch, osVersion string) string {
83+
cmd := fmt.Sprintf(requestCommandRegex,
84+
poolName, arch, osVersion)
85+
return cmd
86+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package data
2+
3+
import "fmt"
4+
5+
func GetFargateTaskByTags() (*string, error) {
6+
return nil, fmt.Errorf("not implemented yet")
7+
}

0 commit comments

Comments
 (0)