@@ -2,10 +2,15 @@ package macpool
22
33import (
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+
7595func 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