-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b28511
commit 2c38fe7
Showing
7 changed files
with
253 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package taskrun | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"github.com/go-logr/logr" | ||
"github.com/redhat-appstudio/multi-platform-controller/pkg/cloud" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"time" | ||
) | ||
|
||
type DynamicHostPool struct { | ||
CloudProvider cloud.CloudProvider | ||
SshSecret string | ||
Platform string | ||
MaxInstances int | ||
Concurrency int | ||
MaxAge time.Duration | ||
InstanceTag string | ||
} | ||
|
||
func (a DynamicHostPool) buildHostPool(r *ReconcileTaskRun, ctx context.Context, log *logr.Logger, instanceTag string) (*HostPool, int, error) { | ||
ret := map[string]*Host{} | ||
instances, err := a.CloudProvider.ListInstances(r.client, log, ctx, instanceTag) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
oldInstanceCount := 0 | ||
for _, inst := range instances { | ||
if inst.StartTime.Add(a.MaxAge).Before(time.Now()) { | ||
// These are shut down on deallocation | ||
// TODO: scan for active tasks, and shut down here if not found | ||
oldInstanceCount++ | ||
} else { | ||
ret[string(inst.InstanceId)] = &Host{Name: string(inst.InstanceId), Address: inst.Address, User: a.CloudProvider.SshUser(), Concurrency: a.Concurrency, Platform: a.Platform, Secret: a.SshSecret, StartTime: &inst.StartTime} | ||
Check failure Code scanning / gosec Implicit memory aliasing in for loop. Error
Implicit memory aliasing in for loop.
|
||
} | ||
} | ||
return &HostPool{hosts: ret}, oldInstanceCount, nil | ||
} | ||
|
||
func (a DynamicHostPool) Deallocate(r *ReconcileTaskRun, ctx context.Context, log *logr.Logger, tr *v1.TaskRun, secretName string, selectedHost string) error { | ||
|
||
hostPool, oldInstanceCount, err := a.buildHostPool(r, ctx, log, a.InstanceTag) | ||
if err != nil { | ||
return err | ||
} | ||
err = hostPool.Deallocate(r, ctx, log, tr, secretName, selectedHost) | ||
if err != nil { | ||
return err | ||
} | ||
if oldInstanceCount > 0 { | ||
// Maybe this is an old instance | ||
startTime := hostPool.hosts[selectedHost].StartTime | ||
if startTime.Add(a.MaxAge).Before(time.Now()) { | ||
// Old host, check if other tasks are using it | ||
trs := v1.TaskRunList{} | ||
err := r.client.List(ctx, &trs, client.MatchingLabels{AssignedHost: selectedHost}) | ||
if err != nil { | ||
return err | ||
} | ||
if len(trs.Items) == 0 { | ||
log.Info("deallocating old instance", "instance", selectedHost) | ||
} | ||
err = a.CloudProvider.TerminateInstance(r.client, log, ctx, cloud.InstanceIdentifier(selectedHost)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (a DynamicHostPool) Allocate(r *ReconcileTaskRun, ctx context.Context, log *logr.Logger, tr *v1.TaskRun, secretName string, instanceTag string) (reconcile.Result, error) { | ||
|
||
hostPool, oldInstanceCount, err := a.buildHostPool(r, ctx, log, instanceTag) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
_, err = hostPool.Allocate(r, ctx, log, tr, secretName, instanceTag) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
if tr.Labels == nil || tr.Labels[WaitingForPlatformLabel] == "" { | ||
//We only need to launch an instance if the task run is waiting for a label | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// Count will handle instances that are not ready yet | ||
count, err := a.CloudProvider.CountInstances(r.client, log, ctx, instanceTag) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
// We don't count old instances towards the total, as they will shut down soon | ||
if count-oldInstanceCount >= a.MaxInstances { | ||
log.Info("cannot provision new instances") | ||
// Too many instances, we just have to wait | ||
return reconcile.Result{RequeueAfter: time.Minute}, err | ||
} | ||
name, err := getRandomString(8) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// Counter intuitively we don't need the instance id | ||
// It will be picked up on the list call | ||
inst, err := a.CloudProvider.LaunchInstance(r.client, log, ctx, name, instanceTag) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
log.Info("allocated instance", "instance", inst) | ||
return reconcile.Result{RequeueAfter: time.Minute}, err | ||
|
||
} | ||
|
||
func getRandomString(length int) (string, error) { | ||
bytes := make([]byte, length/2+1) | ||
if _, err := rand.Read(bytes); err != nil { | ||
return "", err | ||
} | ||
return hex.EncodeToString(bytes)[0:length], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters