Skip to content

Commit 5633448

Browse files
committed
REVIEW action mac-pool to use old create and destroy
Signed-off-by: Adrian Riobo <[email protected]>
1 parent 6d50e7a commit 5633448

File tree

5 files changed

+269
-134
lines changed

5 files changed

+269
-134
lines changed

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

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package macpool
22

33
import (
4+
"fmt"
45
"os"
6+
"strings"
57

68
"github.com/pulumi/pulumi/sdk/v3/go/auto"
79
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
810
"github.com/redhat-developer/mapt/pkg/manager"
911
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
1012
"github.com/redhat-developer/mapt/pkg/provider/aws"
13+
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
14+
dedicatedhost "github.com/redhat-developer/mapt/pkg/provider/aws/modules/ec2/compute/dedicated-host"
1115
"github.com/redhat-developer/mapt/pkg/provider/aws/modules/iam"
1216
macPool "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/pool"
1317
macUtil "github.com/redhat-developer/mapt/pkg/provider/aws/modules/mac/util"
18+
"github.com/redhat-developer/mapt/pkg/provider/aws/services/ec2/compute"
1419
"github.com/redhat-developer/mapt/pkg/util/logging"
1520
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
1621
)
@@ -20,12 +25,83 @@ const (
2025
awsMacPoolID = "amp"
2126
)
2227

28+
func Create(ctx *maptContext.ContextArgs, r *PoolRequestArgs) error {
29+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
30+
return err
31+
}
32+
re := os.Getenv("AWS_DEFAULT_REGION")
33+
it := "c5.xlarge"
34+
hostId, azId, err := dedicatedHost(&re, &it, nil)
35+
if err != nil {
36+
return err
37+
}
38+
dhArgs := dedicatedhost.DedicatedHostArgs{
39+
Region: &re,
40+
AzId: azId,
41+
InstanceType: &it,
42+
HostId: hostId,
43+
Import: true,
44+
UnProtect: false,
45+
}
46+
backerUrl := maptContext.BackedURL()
47+
_, err = dedicatedhost.Create(&backerUrl, false, &dhArgs)
48+
if err != nil {
49+
return err
50+
}
51+
dhArgs.Import = false
52+
dhArgs.UnProtect = true
53+
_, err = dedicatedhost.Create(&backerUrl, false, &dhArgs)
54+
return err
55+
}
56+
57+
func Destroy(ctx *maptContext.ContextArgs) (err error) {
58+
// Create mapt Context
59+
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
60+
return err
61+
}
62+
63+
return dedicatedhost.Destroy(nil)
64+
}
65+
66+
func dedicatedHost(region, instanceType *string, tags map[string]string) (hostId *string, azId *string, err error) {
67+
azs := data.GetAvailabilityZones(*region)
68+
var isOffered bool
69+
for i := 0; !isOffered && i < len(azs); i++ {
70+
isOffered, err = data.IsInstanceTypeOfferedByAZ(*region, *instanceType, azs[i])
71+
if err != nil {
72+
return
73+
}
74+
if !isOffered {
75+
logging.Debugf("Instancetype %s is not offered at %s", *instanceType, azs[i])
76+
continue
77+
}
78+
hostId, err = compute.DedicatedHost(region, &azs[i], instanceType, tags)
79+
if err != nil {
80+
if isCapacityError(err) {
81+
isOffered = false
82+
continue
83+
}
84+
}
85+
azId = &azs[i]
86+
return
87+
}
88+
if hostId == nil {
89+
return nil, nil, fmt.Errorf("no capacity across the region")
90+
}
91+
return
92+
}
93+
94+
func isCapacityError(err error) bool {
95+
return strings.Contains(err.Error(), "Insufficient") ||
96+
strings.Contains(err.Error(), "capacity")
97+
}
98+
2399
// Create works as an orchestrator for create n machines based on offered capacity
24100
// if pool already exists just change the params for the HouseKeeper
25101
// also the HouseKeep will take care of regulate the capacity
26102

27103
// Even if we want to destroy the pool we will set params to max size 0
28-
func Create(ctx *maptContext.ContextArgs, r *PoolRequestArgs) error {
104+
func CreateOld(ctx *maptContext.ContextArgs, r *PoolRequestArgs) error {
29105
// Create mapt Context
30106
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
31107
return err
@@ -41,7 +117,7 @@ func Create(ctx *maptContext.ContextArgs, r *PoolRequestArgs) error {
41117
return r.results(sr)
42118
}
43119

44-
func Destroy(ctx *maptContext.ContextArgs) (err error) {
120+
func DestroyOld(ctx *maptContext.ContextArgs) (err error) {
45121
logging.Debug("Run mac pool destroy")
46122
// Create mapt Context
47123
if err := maptContext.Init(ctx, aws.Provider()); err != nil {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package dedicatedhost
2+
3+
import (
4+
"fmt"
5+
6+
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
7+
"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
8+
"github.com/pulumi/pulumi/sdk/v3/go/auto"
9+
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
10+
"github.com/redhat-developer/mapt/pkg/manager"
11+
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
12+
"github.com/redhat-developer/mapt/pkg/provider/aws"
13+
awsConstants "github.com/redhat-developer/mapt/pkg/provider/aws/constants"
14+
"github.com/redhat-developer/mapt/pkg/provider/aws/data"
15+
"github.com/redhat-developer/mapt/pkg/provider/util/output"
16+
"github.com/redhat-developer/mapt/pkg/util/logging"
17+
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
18+
)
19+
20+
const (
21+
// mapt internal ID for the component: dedicated host
22+
awsDH = "adh"
23+
24+
outputDedicatedHostID = "adhDedicatedHostID"
25+
outputDedicatedHostAZ = "adhDedicatedHostAZ"
26+
outputRegion = "adhRegion"
27+
)
28+
29+
var (
30+
defaultPrefix = "mapt"
31+
maptDHDefaultPrefix = "mapt-dh"
32+
)
33+
34+
type DedicatedHostArgs struct {
35+
Prefix *string
36+
Region *string
37+
AzId *string
38+
InstanceType *string
39+
HostId *string
40+
Tags map[string]string
41+
Import bool
42+
UnProtect bool
43+
}
44+
45+
func Create(backedURL *string, exportOutputs bool, args *DedicatedHostArgs) (*ec2Types.Host, error) {
46+
if args.Prefix == nil {
47+
args.Prefix = &defaultPrefix
48+
}
49+
cs := manager.Stack{
50+
StackName: maptContext.StackNameByProject(maptDHDefaultPrefix),
51+
ProjectName: fmt.Sprintf("%s-%s", *args.Prefix, maptContext.ProjectName()),
52+
BackedURL: *backedURL,
53+
ProviderCredentials: aws.GetClouProviderCredentials(
54+
map[string]string{
55+
awsConstants.CONFIG_AWS_REGION: *args.Region}),
56+
DeployFunc: args.deploy,
57+
}
58+
sr, err := manager.UpStack(cs)
59+
if err != nil {
60+
return nil, err
61+
}
62+
dhID, _, err := manageResultsDedicatedHost(sr, *args.Prefix, exportOutputs)
63+
if err != nil {
64+
return nil, err
65+
}
66+
logging.Debugf("mac dedicated host with host id %s has been created successfully", *dhID)
67+
return data.GetDedicatedHost(*dhID)
68+
}
69+
70+
func Destroy(prefix *string) (err error) {
71+
logging.Debug("Destroy dedicated host resources")
72+
if prefix == nil {
73+
prefix = &defaultPrefix
74+
}
75+
return aws.DestroyStack(
76+
aws.DestroyStackRequest{
77+
ProjectName: fmt.Sprintf("%s-%s",
78+
*prefix,
79+
maptContext.ProjectName()),
80+
Stackname: maptContext.StackNameByProject(maptDHDefaultPrefix),
81+
})
82+
}
83+
84+
// this function will create the dedicated host resource
85+
func (r *DedicatedHostArgs) deploy(ctx *pulumi.Context) (err error) {
86+
ctx.Export(fmt.Sprintf("%s-%s", *r.Prefix, outputRegion), pulumi.String(*r.Region))
87+
opts := maptContext.CommonOptions(ctx)
88+
// Dedicated host will be created through sdk and then
89+
// imported to be a pulumi managed resource
90+
// also resource will be destroyed by pulumi
91+
if r.Import {
92+
opts = append(opts, pulumi.Import(pulumi.ID(*r.HostId)))
93+
}
94+
if r.UnProtect {
95+
opts = append(opts, pulumi.Protect(false))
96+
}
97+
dha := &ec2.DedicatedHostArgs{
98+
AutoPlacement: pulumi.String("off"),
99+
AvailabilityZone: pulumi.String(*r.AzId),
100+
InstanceType: pulumi.String(*r.InstanceType),
101+
}
102+
if r.Tags != nil {
103+
dha.Tags = maptContext.ResourceTagsWithCustom(r.Tags)
104+
}
105+
dh, err := ec2.NewDedicatedHost(ctx,
106+
resourcesUtil.GetResourceName(*r.Prefix, awsDH, "dh"),
107+
dha, opts...)
108+
if err != nil {
109+
return err
110+
}
111+
ctx.Export(fmt.Sprintf("%s-%s", *r.Prefix, outputDedicatedHostID),
112+
dh.ID())
113+
ctx.Export(fmt.Sprintf("%s-%s", *r.Prefix, outputDedicatedHostAZ),
114+
pulumi.String(*r.AzId))
115+
return nil
116+
}
117+
118+
// results for dedicated host it will return dedicatedhost ID and dedicatedhost AZ
119+
// also write results to files on the target folder
120+
func manageResultsDedicatedHost(
121+
stackResult auto.UpResult, prefix string, export bool) (*string, *string, error) {
122+
if export {
123+
if err := output.Write(stackResult, maptContext.GetResultsOutputPath(), map[string]string{
124+
fmt.Sprintf("%s-%s", prefix, outputDedicatedHostID): "dedicated_host_id",
125+
}); err != nil {
126+
return nil, nil, err
127+
}
128+
}
129+
dhID, ok := stackResult.Outputs[fmt.Sprintf("%s-%s", prefix, outputDedicatedHostID)].Value.(string)
130+
if !ok {
131+
return nil, nil, fmt.Errorf("error getting dedicated host ID")
132+
}
133+
dhAZ, ok := stackResult.Outputs[fmt.Sprintf("%s-%s", prefix, outputDedicatedHostAZ)].Value.(string)
134+
if !ok {
135+
return nil, nil, fmt.Errorf("error getting dedicated host AZ")
136+
}
137+
return &dhID, &dhAZ, nil
138+
}

0 commit comments

Comments
 (0)