Skip to content

Commit 6e32795

Browse files
committed
chore: aws mac pool allow orchestrate serverless stack, to allow several task specs
Signed-off-by: Adrian Riobo <[email protected]>
1 parent 771e992 commit 6e32795

File tree

25 files changed

+1028
-801
lines changed

25 files changed

+1028
-801
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ VERSION ?= 0.9.0-dev
22
CONTAINER_MANAGER ?= podman
33

44
# Image URL to use all building/pushing image targets
5-
IMG ?= quay.io/redhat-developer/mapt:v${VERSION}
6-
TKN_IMG ?= quay.io/redhat-developer/mapt:v${VERSION}-tkn
5+
IMG ?= ghcr.io/redhat-developer/mapt:pr-421
6+
TKN_IMG ?= ghcr.io/redhat-developer/mapt:pr-421-tkn
77

88
# Integrations
99
# renovate: datasource=github-releases depName=cirruslabs/cirrus-cli

cmd/mapt/cmd/aws/services/mac-pool.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ const (
2828
paramMaxSize = "max-size"
2929
paramMaxSizeDesc = "max number of machines in the pool"
3030
paramMaxSizeDefault = 2
31+
paramVPCID = "vpcid"
32+
paramVPCIDDesc = "VPC Id to setup mac machines"
33+
paramVPCIDDefault = ""
34+
paramSubnetID = "subnetid"
35+
paramSubnetIDDesc = "Subnet Id to setup mac machines"
36+
paramSubnetIDDefault = ""
37+
paramSSHSGID = "sgid-ssh"
38+
paramSSHSGIDDesc = "Security group Id to securize ssh access to machines. SSH can only be used from instances with this SG."
39+
paramSSHSGIDDefault = ""
3140
)
3241

3342
func GetMacPoolCmd() *cobra.Command {
@@ -73,8 +82,7 @@ func create() *cobra.Command {
7382
Architecture: viper.GetString(awsParams.MACArch),
7483
OSVersion: viper.GetString(awsParams.MACOSVersion),
7584
OfferedCapacity: viper.GetInt(paramOfferedCapacity),
76-
MaxSize: viper.GetInt(paramMaxSize),
77-
FixedLocation: viper.IsSet(awsParams.MACFixedLocation)}); err != nil {
85+
MaxSize: viper.GetInt(paramMaxSize)}); err != nil {
7886
logging.Error(err)
7987
}
8088
return nil
@@ -89,7 +97,6 @@ func create() *cobra.Command {
8997
flagSet.StringP(awsParams.MACArch, "", awsParams.MACArchDefault, awsParams.MACArchDesc)
9098
flagSet.StringP(awsParams.MACOSVersion, "", awsParams.MACOSVersionDefault, awsParams.MACOSVersionDesc)
9199
flagSet.StringToStringP(params.Tags, "", nil, params.TagsDesc)
92-
flagSet.Bool(awsParams.MACFixedLocation, false, awsParams.MACFixedLocationDesc)
93100
c.PersistentFlags().AddFlagSet(flagSet)
94101
return c
95102
}
@@ -145,7 +152,9 @@ func houseKeep() *cobra.Command {
145152
OSVersion: viper.GetString(awsParams.MACOSVersion),
146153
OfferedCapacity: viper.GetInt(paramOfferedCapacity),
147154
MaxSize: viper.GetInt(paramMaxSize),
148-
FixedLocation: viper.IsSet(awsParams.MACFixedLocation)}); err != nil {
155+
VPCID: viper.GetString(paramVPCID),
156+
SubnetID: viper.GetString(paramSubnetID),
157+
SSHSGID: viper.GetString(paramSSHSGID)}); err != nil {
149158
logging.Error(err)
150159
}
151160
return nil
@@ -158,8 +167,11 @@ func houseKeep() *cobra.Command {
158167
flagSet.Int(paramMaxSize, paramMaxSizeDefault, paramMaxSizeDesc)
159168
flagSet.StringP(awsParams.MACArch, "", awsParams.MACArchDefault, awsParams.MACArchDesc)
160169
flagSet.StringP(awsParams.MACOSVersion, "", awsParams.MACOSVersion, awsParams.MACOSVersionDefault)
161-
flagSet.Bool(awsParams.MACFixedLocation, false, awsParams.MACFixedLocationDesc)
170+
// flagSet.Bool(awsParams.MACFixedLocation, false, awsParams.MACFixedLocationDesc)
162171
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
172+
flagSet.StringP(paramVPCID, "", paramVPCIDDefault, paramVPCIDDesc)
173+
flagSet.StringP(paramSubnetID, "", paramSubnetIDDefault, paramSubnetIDDesc)
174+
flagSet.StringP(paramSSHSGID, "", paramSSHSGIDDefault, paramSSHSGIDDesc)
163175
c.PersistentFlags().AddFlagSet(flagSet)
164176
return c
165177
}

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

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package macpool
22

33
import (
44
"fmt"
5+
"strings"
56

7+
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
68
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
79
"github.com/redhat-developer/mapt/pkg/provider/aws"
10+
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac"
11+
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
812
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
13+
"github.com/redhat-developer/mapt/pkg/util"
914
"github.com/redhat-developer/mapt/pkg/util/logging"
1015
)
1116

@@ -18,12 +23,13 @@ func houseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
1823
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
1924
return err
2025
}
21-
2226
// Get full info on the pool
2327
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
2428
if err != nil {
2529
return err
2630
}
31+
p.maxSize = r.MaxSize
32+
p.offeredCapacity = r.OfferedCapacity
2733
// Pool under expected offered capacity
2834
if p.currentOfferedCapacity() < r.OfferedCapacity {
2935
if p.currentPoolSize() < r.MaxSize {
@@ -50,9 +56,13 @@ func houseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
5056
}
5157

5258
// Run serverless operation for house keeping
53-
func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
54-
return serverless.Create(
59+
func houseKeeperTaskSpecScheduler(ctx *pulumi.Context, r *MacPoolRequestArgs,
60+
vpcID, subnetID, sgID, region *string) error {
61+
// First I need to create
62+
return serverless.Deploy(ctx,
5563
&serverless.ServerlessArgs{
64+
Prefix: houseKeepingOperation,
65+
Region: *region,
5666
ContainerName: fmt.Sprintf("housekeeper-%s-%s-%s",
5767
r.PoolName,
5868
r.Architecture,
@@ -63,15 +73,25 @@ func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
6373
r.OSVersion,
6474
r.OfferedCapacity,
6575
r.MaxSize,
66-
r.FixedLocation),
76+
true),
6777
ScheduleType: &serverless.Repeat,
6878
Schedulexpression: houseKeepingInterval,
69-
LogGroupName: fmt.Sprintf("%s-%s-%s",
79+
LogGroupName: fmt.Sprintf("%s-%s-%s-%s",
80+
houseKeepingOperation,
7081
r.PoolName,
7182
r.Architecture,
72-
r.OSVersion)})
83+
r.OSVersion),
84+
ExecutionDefaults: map[string]*string{
85+
serverless.TaskExecDefaultVPCID: vpcID,
86+
serverless.TaskExecDefaultSubnetID: subnetID,
87+
serverless.TaskExecDefaultSGID: sgID,
88+
}})
7389
}
7490

91+
// func destroyScheduleHouseKeeper() error {
92+
// return serverless.Destroy(&houseKeepingOperation)
93+
// }
94+
7595
func houseKeepingCommand(poolName, arch, osVersion string,
7696
offeredCapacity, maxSize int,
7797
fixedLocation bool) string {
@@ -83,3 +103,92 @@ func houseKeepingCommand(poolName, arch, osVersion string,
83103
}
84104
return cmd
85105
}
106+
107+
func (r *MacPoolRequestArgs) addMachinesToPool(n int) error {
108+
if err := validateBackedURL(); err != nil {
109+
return err
110+
}
111+
for i := 0; i < n; i++ {
112+
hr := r.fillHostRequest()
113+
dh, err := macHost.CreatePoolDedicatedHost(hr)
114+
if err != nil {
115+
return err
116+
}
117+
mr := r.fillMacRequest()
118+
if err = mr.CreateAvailableMacMachine(dh); err != nil {
119+
return err
120+
}
121+
}
122+
return nil
123+
}
124+
125+
// format for remote backed url when creating the dedicated host
126+
// the backed url from param is used as base and the ID is appended as sub path
127+
func validateBackedURL() error {
128+
if strings.Contains(maptContext.BackedURL(), "file://") {
129+
return fmt.Errorf("local backed url is not allowed for mac pool")
130+
}
131+
return nil
132+
}
133+
134+
// transform pool request to host request
135+
// need if we need to expand the pool
136+
func (r *MacPoolRequestArgs) fillHostRequest() *macHost.PoolMacDedicatedHostRequestArgs {
137+
return &macHost.PoolMacDedicatedHostRequestArgs{
138+
MacDedicatedHost: &macHost.MacDedicatedHostRequestArgs{
139+
Prefix: r.Prefix,
140+
Architecture: r.Architecture,
141+
// FixedLocation: r.FixedLocation,
142+
VPCID: &r.VPCID,
143+
SubnetID: &r.SubnetID,
144+
SSHSGID: &r.SSHSGID,
145+
},
146+
PoolID: &macHost.PoolID{
147+
PoolName: r.PoolName,
148+
Arch: r.Architecture,
149+
OSVersion: r.OSVersion,
150+
},
151+
BackedURL: fmt.Sprintf("%s/%s",
152+
maptContext.BackedURL(),
153+
util.RandomID("mapt")),
154+
}
155+
}
156+
157+
// If we need less or equal than the max allowed on the pool we create all of them
158+
// if need are more than allowed we can create just the allowed
159+
func (r *MacPoolRequestArgs) addCapacity(p *pool) error {
160+
allowed := p.maxSize - p.offeredCapacity
161+
needed := p.offeredCapacity - p.currentOfferedCapacity()
162+
if needed <= allowed {
163+
return r.addMachinesToPool(needed)
164+
}
165+
return r.addMachinesToPool(allowed)
166+
}
167+
168+
// If we need less or equal than the max allowed on the pool we create all of them
169+
// if need are more than allowed we can create just the allowed
170+
// TODO review allocation time is on the wrong order
171+
func (r *MacPoolRequestArgs) destroyCapacity(p *pool) error {
172+
machinesToDestroy := p.currentOfferedCapacity() - r.OfferedCapacity
173+
for i := 0; i < machinesToDestroy; i++ {
174+
m := p.destroyableMachines[i]
175+
// TODO change this
176+
maptContext.SetProjectName(*m.ProjectName)
177+
if err := aws.DestroyStack(aws.DestroyStackRequest{
178+
Stackname: mac.StackMacMachine,
179+
Region: *m.Region,
180+
BackedURL: *m.BackedURL,
181+
}); err != nil {
182+
return err
183+
}
184+
if err := aws.DestroyStack(aws.DestroyStackRequest{
185+
Stackname: mac.StackDedicatedHost,
186+
// TODO check if needed to add region for backedURL
187+
Region: *m.Region,
188+
BackedURL: *m.BackedURL,
189+
}); err != nil {
190+
return err
191+
}
192+
}
193+
return nil
194+
}

0 commit comments

Comments
 (0)