Skip to content

Commit d25e97e

Browse files
committed
chore: aws added tag data function to get any type ofresource tags and match with a expected set of tags
this is now used within mac-pool request operation on remote mode...to get the fargate task spec to run request through it Signed-off-by: Adrian Riobo <[email protected]>
1 parent 5d19373 commit d25e97e

39 files changed

+9198
-28
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ func request() *cobra.Command {
175175

176176
ctx := &maptContext.ContextArgs{
177177
ResultsOutput: viper.GetString(params.ConnectionDetailsOutput),
178-
Serverless: viper.IsSet(params.Serverless),
179178
Debug: viper.IsSet(params.Debug),
180179
DebugLevel: viper.GetUint(params.DebugLevel),
180+
Serverless: viper.IsSet(params.Serverless),
181+
Remote: viper.IsSet(params.Remote),
181182
Tags: viper.GetStringMapString(params.Tags),
182183
}
183184

@@ -223,6 +224,7 @@ func request() *cobra.Command {
223224
flagSet.StringP(awsParams.MACOSVersion, "", awsParams.MACOSVersion, awsParams.MACOSVersionDefault)
224225
flagSet.StringP(params.Timeout, "", "", params.TimeoutDesc)
225226
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
227+
flagSet.Bool(params.Remote, false, params.RemoteDesc)
226228
flagSet.AddFlagSet(params.GetGHActionsFlagset())
227229
params.AddCirrusFlags(flagSet)
228230
c.PersistentFlags().AddFlagSet(flagSet)
@@ -243,6 +245,7 @@ func release() *cobra.Command {
243245
Debug: viper.IsSet(params.Debug),
244246
DebugLevel: viper.GetUint(params.DebugLevel),
245247
Serverless: viper.IsSet(params.Serverless),
248+
Remote: viper.IsSet(params.Remote),
246249
},
247250
viper.GetString(awsParams.MACDHID)); err != nil {
248251
logging.Error(err)
@@ -253,6 +256,7 @@ func release() *cobra.Command {
253256
flagSet := pflag.NewFlagSet(awsParams.MACReleaseCmd, pflag.ExitOnError)
254257
flagSet.StringP(awsParams.MACDHID, "", "", awsParams.MACDHIDDesc)
255258
flagSet.Bool(params.Serverless, false, params.ServerlessDesc)
259+
flagSet.Bool(params.Remote, false, params.RemoteDesc)
256260
c.PersistentFlags().AddFlagSet(flagSet)
257261
err := c.MarkPersistentFlagRequired(awsParams.MACDHID)
258262
if err != nil {

cmd/mapt/cmd/constants/constants.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ const (
7777
RhelVersionDefault string = "9.4"
7878

7979
// Serverless
80-
Timeout string = "timeout"
81-
TimeoutDesc string = "if timeout is set a serverless destroy actions will be set on the time according to the timeout. The Timeout value is a duration conforming to Go ParseDuration format."
80+
Timeout string = "timeout"
81+
TimeoutDesc string = "if timeout is set a serverless destroy actions will be set on the time according to the timeout. The Timeout value is a duration conforming to Go ParseDuration format."
82+
// TODO we should be able to know if it is being executed serverless just checking the ENVs??
8283
Serverless string = "serverless"
8384
ServerlessDesc string = "if serverless is set the command will be executed as a serverless action."
8485

@@ -87,10 +88,12 @@ const (
8788
ForceDestroyDesc string = "if force-destroy is set the command will destroy even if there is a lock."
8889

8990
// Kind
90-
KindCmd = "kind"
91-
KindCmdDesc = "Manage a Kind cluster. This is not intended for production use"
92-
KindK8SVersion = "version"
93-
KindK8SVersionDesc = "version for k8s offered through Kind."
91+
KindCmd = "kind"
92+
KindCmdDesc = "Manage a Kind cluster. This is not intended for production use"
93+
KindK8SVersion = "version"
94+
KindK8SVersionDesc = "version for k8s offered through Kind."
95+
Remote string = "remote"
96+
RemoteDesc string = "if remote is set the command will be executed through the invocation of a serverless task"
9497
)
9598

9699
func GetGHActionsFlagset() *pflag.FlagSet {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/aws/aws-sdk-go-v2 v1.36.3
2222
github.com/aws/aws-sdk-go-v2/config v1.29.14
2323
github.com/aws/aws-sdk-go-v2/service/ec2 v1.218.0
24+
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.26.3
2425
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.3
2526
github.com/pulumi/pulumi-aws-native/sdk v1.27.0
2627
github.com/pulumi/pulumi-aws/sdk/v6 v6.80.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.15 h1:moLQUoVq91Liq
7575
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.15/go.mod h1:ZH34PJUc8ApjBIfgQCFvkWcUDBtl/WTD+uiYHjd8igA=
7676
github.com/aws/aws-sdk-go-v2/service/pricing v1.34.3 h1:vAv0hi3SWcc8cotkWRP4mPkmRbp/XqWKFyPW4Nwpzv0=
7777
github.com/aws/aws-sdk-go-v2/service/pricing v1.34.3/go.mod h1:giTP9ufzBQJRB6bc7P30PO8s35hCp6au5uM70zkohU4=
78+
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.26.3 h1:P87jejqS8WvQvRWyXlHUylt99VXt0y/WUIFuU6gBU7A=
79+
github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.26.3/go.mod h1:cgPfPTC/V3JqwCKed7Q6d0FrgarV7ltz4Bz6S4Q+Dqk=
7880
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.3 h1:BRXS0U76Z8wfF+bnkilA2QwpIch6URlm++yPUt9QPmQ=
7981
github.com/aws/aws-sdk-go-v2/service/s3 v1.79.3/go.mod h1:bNXKFFyaiVvWuR6O16h/I1724+aXe/tAkA9/QS01t5k=
8082
github.com/aws/aws-sdk-go-v2/service/sso v1.25.3 h1:1Gw+9ajCV1jogloEv1RRnvfRFia2cL6c9cuKV2Ps+G8=

pkg/manager/context/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type ContextArgs struct {
4141
Serverless bool
4242
// This forces destroy even when lock exists
4343
ForceDestroy bool
44+
// If remote is set we will run the action through the serverless task spec
45+
Remote bool
4446
// integrations
4547
GHRunnerArgs *github.GithubRunnerArgs
4648
CirrusPWArgs *cirrus.PersistentWorkerArgs
@@ -55,6 +57,7 @@ type context struct {
5557
debugLevel uint
5658
serverless bool
5759
forceDestroy bool
60+
remote bool
5861
tags map[string]string
5962
tagsAsPulumiStringMap pulumi.StringMap
6063
}
@@ -77,6 +80,7 @@ func Init(ca *ContextArgs, provider Provider) error {
7780
tags: ca.Tags,
7881
serverless: ca.Serverless,
7982
forceDestroy: ca.ForceDestroy,
83+
remote: ca.Remote,
8084
}
8185
addCommonTags()
8286
// Init provider
@@ -113,6 +117,8 @@ func IsServerless() bool { return mc.serverless }
113117

114118
func IsForceDestroy() bool { return mc.forceDestroy }
115119

120+
func IsRemote() bool { return mc.remote }
121+
116122
// It will create a runID
117123
// if context has been intialized it will set it as the runID for the context
118124
// otherwise it will return the value (one time value)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func HouseKeeper(ctx *maptContext.ContextArgs, r *MacPoolRequestArgs) error {
6161

6262
func Request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
6363
// If remote run through serverless
64-
if r.Remote {
64+
if ctx.Remote {
6565
return requestRemote(ctx, r)
6666
}
6767
return request(ctx, r)

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package macpool
22

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

67
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
8+
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
79
macConstants "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/constants"
10+
macHost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/host"
811
macMachine "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/machine"
912
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/serverless"
1013
"github.com/redhat-developer/mapt/pkg/provider/aws/services/tag"
1114
)
1215

1316
func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
1417
// If remote run through serverless
15-
if r.Remote {
18+
if maptContext.IsRemote() {
1619
return requestRemote(ctx, r)
1720
}
1821
// First get full info on the pool and the next machine for request
@@ -55,6 +58,24 @@ func request(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
5558
}
5659

5760
func requestRemote(ctx *maptContext.ContextArgs, r *RequestMachineArgs) error {
61+
if err := maptContext.Init(ctx); err != nil {
62+
return err
63+
}
64+
rARNs, err := data.GetResourcesMatchingTags(
65+
data.ResourceTypeECS,
66+
requestTags(
67+
r.PoolName,
68+
r.Architecture,
69+
r.OSVersion))
70+
if err != nil {
71+
return err
72+
}
73+
if len(rARNs) > 1 {
74+
return fmt.Errorf(
75+
"should be only one task spec matching tags. Found %s",
76+
strings.Join(rARNs, ","))
77+
}
78+
// We got the arn value for the task
5879
return fmt.Errorf("not implemented yet")
5980
}
6081

@@ -72,15 +93,27 @@ func (r *MacPoolRequestArgs) createRequestTaskSpec() error {
7293
r.PoolName,
7394
r.Architecture,
7495
r.OSVersion),
75-
Tags: map[string]string{
76-
macConstants.TagKeyArch: r.Architecture,
77-
macConstants.TagKeyOSVersion: r.OSVersion,
78-
macConstants.TagKeyPoolName: r.PoolName,
79-
}})
96+
Tags: requestTags(
97+
r.PoolName,
98+
r.Architecture,
99+
r.OSVersion)})
80100
}
81101

82102
func requestCommand(poolName, arch, osVersion string) string {
83103
cmd := fmt.Sprintf(requestCommandRegex,
84104
poolName, arch, osVersion)
85105
return cmd
86106
}
107+
108+
// Return the map of tags wich should identify unique
109+
// resquest operation spec for a pool
110+
func requestTags(poolName, arch, osVersion string) (m map[string]string) {
111+
poolID := macHost.PoolID{
112+
PoolName: poolName,
113+
Arch: arch,
114+
OSVersion: osVersion,
115+
}
116+
m = poolID.AsTags()
117+
m[macConstants.TagKeyPoolOperationName] = requestOperation
118+
return
119+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
// https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html#eb-rate-expressions
1111
houseKeepingInterval = "27 minutes"
1212

13+
requestOperation = "request"
1314
requestCommandRegex = "aws mac-pool request --name %s --arch %s --version %s --serverless "
1415
// requestTimeoutParam = "--timeout "
1516
// itCirrusPWTokenParam = "--it-cirrus-pw-token "
@@ -44,8 +45,6 @@ type RequestMachineArgs struct {
4445
OSVersion string
4546
// If timeout is set a severless scheduled task will be created to self destroy the resources
4647
Timeout string
47-
// If remote is set we will run the action through the serverless task spec
48-
Remote bool
4948
}
5049

5150
type ReleaseMachineArgs struct {

pkg/provider/aws/data/serverless.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/provider/aws/data/tags.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package data
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi"
8+
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi/types"
9+
"github.com/redhat-developer/mapt/pkg/util"
10+
)
11+
12+
const (
13+
ResourceTypeECS = "ecs:task-definition"
14+
)
15+
16+
var (
17+
ErrorNoResourcesFound = fmt.Errorf("no resources matching tags")
18+
)
19+
20+
// Return a list of arns for the type of resource which have all tags
21+
func GetResourcesMatchingTags(resourceType string, tags map[string]string) ([]string, error) {
22+
cfg, err := getGlobalConfig()
23+
if err != nil {
24+
return nil, err
25+
}
26+
tagsClient := resourcegroupstaggingapi.NewFromConfig(cfg)
27+
ro, err := tagsClient.GetResources(context.Background(),
28+
&resourcegroupstaggingapi.GetResourcesInput{
29+
ResourceTypeFilters: []string{resourceType},
30+
})
31+
if err != nil {
32+
return nil, err
33+
}
34+
rFiltered := util.ArrayFilter(ro.ResourceTagMappingList,
35+
func(r types.ResourceTagMapping) bool {
36+
ft := util.ArrayFilter(r.Tags,
37+
func(rt types.Tag) bool {
38+
for k, v := range tags {
39+
if *rt.Key == k && *rt.Value == v {
40+
return true
41+
}
42+
}
43+
return false
44+
})
45+
return len(tags) == len(ft)
46+
})
47+
if len(rFiltered) == 0 {
48+
return nil, ErrorNoResourcesFound
49+
}
50+
return util.ArrayConvert(
51+
rFiltered,
52+
func(rt types.ResourceTagMapping) string {
53+
return *rt.ResourceARN
54+
}), nil
55+
}

0 commit comments

Comments
 (0)