Skip to content

Commit 2bf5269

Browse files
committed
chore: aws added tags to fargate task spec
Signed-off-by: Adrian Riobo <ariobolo@redhat.com>
1 parent 5214e6f commit 2bf5269

10 files changed

Lines changed: 103 additions & 51 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func request() *cobra.Command {
175175

176176
ctx := &maptContext.ContextArgs{
177177
ResultsOutput: viper.GetString(params.ConnectionDetailsOutput),
178+
Serverless: viper.IsSet(params.Serverless),
178179
Debug: viper.IsSet(params.Debug),
179180
DebugLevel: viper.GetUint(params.DebugLevel),
180181
Tags: viper.GetStringMapString(params.Tags),
@@ -221,6 +222,7 @@ func request() *cobra.Command {
221222
flagSet.StringP(awsParams.MACArch, "", awsParams.MACArchDefault, awsParams.MACArchDesc)
222223
flagSet.StringP(awsParams.MACOSVersion, "", awsParams.MACOSVersion, awsParams.MACOSVersionDefault)
223224
flagSet.StringP(params.Timeout, "", "", params.TimeoutDesc)
225+
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
224226
flagSet.AddFlagSet(params.GetGHActionsFlagset())
225227
params.AddCirrusFlags(flagSet)
226228
c.PersistentFlags().AddFlagSet(flagSet)

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

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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"
1314
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
1415
macMachine "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/machine"
1516
macUtil "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/util"
@@ -35,6 +36,9 @@ func Create(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
3536
if err := r.scheduleHouseKeeper(); err != nil {
3637
return err
3738
}
39+
if err := r.createRequestTaskSpec(); err != nil {
40+
return err
41+
}
3842
return r.requestReleaserAccount()
3943
}
4044

@@ -91,6 +95,10 @@ func HouseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
9195
}
9296

9397
func Request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
98+
// If remote run through serverless
99+
if r.Remote {
100+
return requestRemote(ctx, r)
101+
}
94102
// First get full info on the pool and the next machine for request
95103
p, err := getPool(r.PoolName, r.Architecture, r.OSVersion)
96104
if err != nil {
@@ -129,6 +137,11 @@ func Request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
129137
*hi.Host.HostId)
130138
}
131139

140+
func requestRemote(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
141+
142+
return fmt.Errorf("not implemented yet")
143+
}
144+
132145
func Release(ctx *maptContext.ContextArgs, hostID string) error {
133146
return macUtil.Release(ctx, hostID)
134147
}
@@ -155,7 +168,7 @@ func (r *MacPoolRequestArgs) addMachinesToPool(n int) error {
155168
func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
156169
return serverless.Create(
157170
&serverless.ServerlessArgs{
158-
Command: getHouseKeepingCommand(
171+
Command: houseKeepingCommand(
159172
r.PoolName,
160173
r.Architecture,
161174
r.OSVersion,
@@ -170,28 +183,31 @@ func (r *MacPoolRequestArgs) scheduleHouseKeeper() error {
170183
r.OSVersion)})
171184
}
172185

173-
// // Run serverless operation request
174-
// func (r *MacPoolRequestArgs) requester() error {
175-
// return serverless.Create(
176-
// getHouseKeepingCommand(
177-
// r.PoolName,
178-
// r.Architecture,
179-
// r.OSVersion,
180-
// r.OfferedCapacity,
181-
// r.MaxSize,
182-
// r.FixedLocation),
183-
// serverless.Repeat,
184-
// houseKeepingInterval,
185-
// fmt.Sprintf("%s-%s-%s",
186-
// r.PoolName,
187-
// r.Architecture,
188-
// r.OSVersion))
189-
// }
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+
}
190206

191-
func getHouseKeepingCommand(poolName, arch, osVersion string,
207+
func houseKeepingCommand(poolName, arch, osVersion string,
192208
offeredCapacity, maxSize int,
193209
fixedLocation bool) string {
194-
cmd := fmt.Sprintf(houseKeepingCommand,
210+
cmd := fmt.Sprintf(houseKeepingCommandRegex,
195211
poolName, arch, osVersion,
196212
offeredCapacity, maxSize)
197213
if fixedLocation {
@@ -200,6 +216,12 @@ func getHouseKeepingCommand(poolName, arch, osVersion string,
200216
return cmd
201217
}
202218

219+
func requestCommand(poolName, arch, osVersion string) string {
220+
cmd := fmt.Sprintf(requestCommandRegex,
221+
poolName, arch, osVersion)
222+
return cmd
223+
}
224+
203225
// If we need less or equal than the max allowed on the pool we create all of them
204226
// if need are more than allowed we can create just the allowed
205227
func (r *MacPoolRequestArgs) addCapacity(p *pool) error {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import (
55
)
66

77
const (
8-
houseKeepingCommand = "aws mac-pool house-keep --name %s --arch %s --version %s --offered-capacity %d --max-size %d --serverless "
8+
houseKeepingCommandRegex = "aws mac-pool house-keep --name %s --arch %s --version %s --offered-capacity %d --max-size %d --serverless "
99
houseKeepingFixedLocationParam = "--fixed-location "
1010
// https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html#eb-rate-expressions
1111
houseKeepingInterval = "27 minutes"
12+
13+
requestCommandRegex = "aws mac-pool request --name %s --arch %s --version %s --serverless "
14+
// requestTimeoutParam = "--timeout "
15+
// itCirrusPWTokenParam = "--it-cirrus-pw-token "
16+
// itCirrusPWLabelsParam = "--it-cirrus-pw-labels "
1217
)
1318

1419
type MacPoolRequestArgs struct {
@@ -39,17 +44,13 @@ type RequestMachineArgs struct {
3944
OSVersion string
4045
// If timeout is set a severless scheduled task will be created to self destroy the resources
4146
Timeout string
47+
// If remote is set we will run the action through the serverless task spec
48+
Remote bool
4249
}
4350

4451
type ReleaseMachineArgs struct {
4552
}
4653

47-
// type PoolID struct {
48-
// PoolName string
49-
// Arch string
50-
// OSVersion string
51-
// }
52-
5354
type pool struct {
5455
// pool params
5556
name string
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package constants
2+
3+
const (
4+
TagKeyPrefix = "prefix"
5+
TagKeyBackedURL = "backedURL"
6+
TagKeyArch = "arch"
7+
// tags added when dedicated host is part of a pool
8+
TagKeyOSVersion = "osVersion"
9+
TagKeyPoolName = "poolName"
10+
)

pkg/provider/aws/modules/mac/host/host.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
awsConstants "github.com/redhat-developer/mapt/pkg/provider/aws/constants"
1414
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
1515
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac"
16+
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
1617
"github.com/redhat-developer/mapt/pkg/provider/util/output"
1718
"github.com/redhat-developer/mapt/pkg/util/logging"
1819
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
@@ -28,9 +29,9 @@ import (
2829

2930
func CreatePoolDedicatedHost(args *PoolMacDedicatedHostRequestArgs) (dhi *mac.HostInformation, err error) {
3031
tags := map[string]string{
31-
tagKeyBackedURL: args.BackedURL,
32-
tagKeyPrefix: args.MacDedicatedHost.Prefix,
33-
maptContext.TagKeyRunID: maptContext.RunID(),
32+
macConstants.TagKeyBackedURL: args.BackedURL,
33+
macConstants.TagKeyPrefix: args.MacDedicatedHost.Prefix,
34+
maptContext.TagKeyRunID: maptContext.RunID(),
3435
}
3536
maps.Copy(tags, args.PoolID.asTags())
3637
return createDedicatedHost(args.MacDedicatedHost, args.BackedURL, tags, false)
@@ -40,10 +41,10 @@ func CreatePoolDedicatedHost(args *PoolMacDedicatedHostRequestArgs) (dhi *mac.Ho
4041
func CreateDedicatedHost(args *MacDedicatedHostRequestArgs) (dhi *mac.HostInformation, err error) {
4142
backedURL := getBackedURL()
4243
tags := map[string]string{
43-
tagKeyBackedURL: backedURL,
44-
tagKeyPrefix: args.Prefix,
45-
tagKeyArch: args.Architecture,
46-
maptContext.TagKeyRunID: maptContext.RunID(),
44+
macConstants.TagKeyBackedURL: backedURL,
45+
macConstants.TagKeyPrefix: args.Prefix,
46+
macConstants.TagKeyArch: args.Architecture,
47+
maptContext.TagKeyRunID: maptContext.RunID(),
4748
}
4849
return createDedicatedHost(args, backedURL, tags, true)
4950
}

pkg/provider/aws/modules/mac/host/types.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package host
22

3+
import (
4+
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
5+
)
6+
37
const (
48
// mapt internal ID for the component: nac dedicated host
59
awsMacHostID = "amh"
610

7-
tagKeyPrefix = "prefix"
8-
tagKeyBackedURL = "backedURL"
9-
tagKeyArch = "arch"
10-
// tags added when dedicated host is part of a pool
11-
tagKeyOSVersion = "osVersion"
12-
tagKeyPoolName = "poolName"
13-
1411
outputDedicatedHostID = "ammDedicatedHostID"
1512
outputDedicatedHostAZ = "ammDedicatedHostAZ"
1613
outputRegion = "ammRegion"
@@ -32,9 +29,9 @@ type PoolID struct {
3229

3330
func (p *PoolID) asTags() map[string]string {
3431
return map[string]string{
35-
tagKeyArch: p.Arch,
36-
tagKeyOSVersion: p.OSVersion,
37-
tagKeyPoolName: p.PoolName,
32+
macConstants.TagKeyArch: p.Arch,
33+
macConstants.TagKeyOSVersion: p.OSVersion,
34+
macConstants.TagKeyPoolName: p.PoolName,
3835
}
3936
}
4037

pkg/provider/aws/modules/mac/host/util.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
1111
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
1212
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac"
13+
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
1314
"github.com/redhat-developer/mapt/pkg/util/logging"
1415
"golang.org/x/exp/slices"
1516
)
@@ -18,7 +19,7 @@ import (
1819
// it will return the list ordered by allocation time
1920
func GetMatchingHostsInformation(arch string) ([]*mac.HostInformation, error) {
2021
matchingTags := maptContext.GetTags()
21-
matchingTags[tagKeyArch] = arch
22+
matchingTags[macConstants.TagKeyArch] = arch
2223
return GetMatchingHostsInStateInformation(matchingTags, nil)
2324
}
2425

@@ -72,12 +73,12 @@ func GetMatchingHostsInStateInformation(matchingTags map[string]string, state *e
7273
func GetHostInformation(h ec2Types.Host) *mac.HostInformation {
7374
az := *h.AvailabilityZone
7475
region := az[:len(az)-1]
75-
archValue := awsArchIDbyArch[*getTagValue(h.Tags, tagKeyArch)]
76+
archValue := awsArchIDbyArch[*getTagValue(h.Tags, macConstants.TagKeyArch)]
7677
return &mac.HostInformation{
7778
Arch: &archValue,
78-
OSVersion: getTagValue(h.Tags, tagKeyOSVersion),
79-
BackedURL: getTagValue(h.Tags, tagKeyBackedURL),
80-
Prefix: getTagValue(h.Tags, tagKeyPrefix),
79+
OSVersion: getTagValue(h.Tags, macConstants.TagKeyOSVersion),
80+
BackedURL: getTagValue(h.Tags, macConstants.TagKeyBackedURL),
81+
Prefix: getTagValue(h.Tags, macConstants.TagKeyPrefix),
8182
ProjectName: getTagValue(h.Tags, maptContext.TagKeyProjectName),
8283
RunID: getTagValue(h.Tags, maptContext.TagKeyRunID),
8384
Region: &region,

pkg/provider/aws/modules/serverless/serverless.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func (a *serverlessRequestArgs) deploy(ctx *pulumi.Context) error {
8686
lga.Args.SkipDestroy = pulumi.Bool(false)
8787
lga.Args.Name = pulumi.String(a.logGroupName)
8888
}
89+
tags := maptContext.ResourceTags()
90+
if a.tags != nil {
91+
tags = a.tags
92+
}
8993
td, err := awsxecs.NewFargateTaskDefinition(ctx,
9094
resourcesUtil.GetResourceName(a.prefix, a.componentID, "fg-task"),
9195
&awsxecs.FargateTaskDefinitionArgs{
@@ -104,6 +108,7 @@ func (a *serverlessRequestArgs) deploy(ctx *pulumi.Context) error {
104108
RoleArn: roleArn,
105109
},
106110
LogGroup: lga,
111+
Tags: pulumi.StringMapInput(tags),
107112
})
108113
if err != nil {
109114
return err

pkg/provider/aws/modules/serverless/stack.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package serverless
33
import (
44
"os"
55

6+
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
67
"github.com/redhat-developer/mapt/pkg/manager"
78
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
89
"github.com/redhat-developer/mapt/pkg/provider/aws"
910
"github.com/redhat-developer/mapt/pkg/util/logging"
11+
utilMaps "github.com/redhat-developer/mapt/pkg/util/maps"
1012
)
1113

1214
// function to create a mapt servless cmd which will be executed repeatedly
@@ -29,6 +31,11 @@ func Create(args *ServerlessArgs) error {
2931
prefix: "mapt",
3032
componentID: "sf",
3133
}
34+
if args.Tags != nil {
35+
r.tags = utilMaps.Convert(args.Tags,
36+
func(name string) string { return name },
37+
func(value string) pulumi.StringInput { return pulumi.String(value) })
38+
}
3239
stack := manager.Stack{
3340
StackName: maptContext.StackNameByProject(maptServerlessDefaultPrefix),
3441
ProjectName: maptContext.ProjectName(),

pkg/provider/aws/modules/serverless/types.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package serverless
22

3+
import "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
4+
35
var (
46
// stackName = "mapt-serverless"
57

@@ -23,10 +25,13 @@ const (
2325
)
2426

2527
type ServerlessArgs struct {
26-
Command string
28+
Command string
29+
// From here params are optional
30+
LogGroupName string
31+
Tags map[string]string
32+
// If no schedule info is added just create the task spec
2733
ScheduleType *scheduleType
2834
Schedulexpression string
29-
LogGroupName string
3035
}
3136

3237
type serverlessRequestArgs struct {
@@ -42,4 +47,5 @@ type serverlessRequestArgs struct {
4247
logGroupName string
4348
// optional params in case we create serverless inside a stack
4449
prefix, componentID string
50+
tags pulumi.StringMap
4551
}

0 commit comments

Comments
 (0)