/
hostpool.go
173 lines (160 loc) · 5.54 KB
/
hostpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package taskrun
import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strings"
"time"
)
type HostPool struct {
hosts map[string]*Host
targetPlatform string
}
func (hp HostPool) Allocate(r *ReconcileTaskRun, ctx context.Context, log *logr.Logger, tr *v1.TaskRun, secretName string) (reconcile.Result, error) {
if len(hp.hosts) == 0 {
//no hosts configured
return reconcile.Result{}, fmt.Errorf("no hosts configured")
}
failedString := tr.Annotations[FailedHosts]
failed := strings.Split(failedString, ",")
//get all existing runs that are assigned to a host
taskList := v1.TaskRunList{}
err := r.client.List(ctx, &taskList, client.HasLabels{AssignedHost})
if err != nil {
return reconcile.Result{}, err
}
hostCount := map[string]int{}
for _, tr := range taskList.Items {
if tr.Labels[TaskTypeLabel] == "" {
host := tr.Labels[AssignedHost]
hostCount[host] = hostCount[host] + 1
}
}
for k, v := range hostCount {
log.Info("host count", "host", k, "count", v)
}
//now select the host with the most free spots
//this algorithm is not very complex
var selected *Host
freeSpots := 0
hostWithOurPlatform := false
for k, v := range hp.hosts {
if slices.Contains(failed, k) {
log.Info("ignoring already failed host", "host", k, "targetPlatform", hp.targetPlatform, "hostPlatform", v.Platform)
continue
}
if v.Platform != hp.targetPlatform {
log.Info("ignoring host", "host", k, "targetPlatform", hp.targetPlatform, "hostPlatform", v.Platform)
continue
}
hostWithOurPlatform = true
free := v.Concurrency - hostCount[k]
log.Info("considering host", "host", k, "freeSlots", free)
if free > freeSpots {
selected = v
freeSpots = free
}
}
if !hostWithOurPlatform {
log.Info("no hosts with requested platform", "platform", hp.targetPlatform, "failed", failedString)
return reconcile.Result{}, fmt.Errorf("no hosts configured for platform %s attempted hosts: %s", hp.targetPlatform, failedString)
}
if selected == nil {
if tr.Labels[WaitingForPlatformLabel] == platformLabel(hp.targetPlatform) {
//we are already in a waiting state
return reconcile.Result{}, nil
}
log.Info("no host found, waiting for one to become available")
//no host available
//add the waiting label
//TODO: is the requeue actually a good idea?
//TODO: timeout
tr.Labels[WaitingForPlatformLabel] = platformLabel(hp.targetPlatform)
return reconcile.Result{RequeueAfter: time.Minute}, r.client.Update(ctx, tr)
}
log.Info("allocated host", "host", selected.Name)
tr.Labels[AssignedHost] = selected.Name
delete(tr.Labels, WaitingForPlatformLabel)
//add a finalizer to clean up the secret
controllerutil.AddFinalizer(tr, PipelineFinalizer)
err = r.client.Update(ctx, tr)
if err != nil {
return reconcile.Result{}, err
}
err = launchProvisioningTask(r, ctx, log, tr, secretName, selected.Secret, selected.Address, selected.User, hp.targetPlatform)
if err != nil {
//ugh, try and unassign
delete(tr.Labels, AssignedHost)
updateErr := r.client.Update(ctx, tr)
if updateErr != nil {
log.Error(updateErr, "Could not unassign task after provisioning failure")
return reconcile.Result{}, err
} else {
log.Error(err, "Failed to provision host from pool")
return reconcile.Result{}, err
}
}
return reconcile.Result{}, nil
}
func (hp HostPool) Deallocate(r *ReconcileTaskRun, ctx context.Context, log *logr.Logger, tr *v1.TaskRun, secretName string, selectedHost string) error {
selected := hp.hosts[selectedHost]
if selected != nil {
labelMap := map[string]string{TaskTypeLabel: TaskTypeClean, UserTaskName: tr.Name, UserTaskNamespace: tr.Namespace}
list := v1.TaskRunList{}
err := r.client.List(ctx, &list, client.MatchingLabels(labelMap))
if err != nil {
log.Error(err, "failed to check for existing cleanup task")
} else {
if len(list.Items) > 0 {
log.Info("cleanup task already exists")
return nil
}
}
log.Info("starting cleanup task")
//kick off the clean task
//kick off the provisioning task
provision := v1.TaskRun{}
provision.GenerateName = "cleanup-task"
provision.Namespace = r.operatorNamespace
provision.Labels = labelMap
provision.Annotations = map[string]string{TaskTargetPlatformAnnotation: hp.targetPlatform}
provision.Spec.TaskRef = &v1.TaskRef{Name: "clean-shared-host"}
provision.Spec.Retries = 3
compute := map[v12.ResourceName]resource.Quantity{v12.ResourceCPU: resource.MustParse("100m"), v12.ResourceMemory: resource.MustParse("128Mi")}
provision.Spec.ComputeResources = &v12.ResourceRequirements{Requests: compute}
provision.Spec.Workspaces = []v1.WorkspaceBinding{{Name: "ssh", Secret: &v12.SecretVolumeSource{SecretName: selected.Secret}}}
provision.Spec.ServiceAccountName = ServiceAccountName //TODO: special service account for this
provision.Spec.Params = []v1.Param{
{
Name: "SECRET_NAME",
Value: *v1.NewStructuredValues(secretName),
},
{
Name: "TASKRUN_NAME",
Value: *v1.NewStructuredValues(tr.Name),
},
{
Name: "NAMESPACE",
Value: *v1.NewStructuredValues(tr.Namespace),
},
{
Name: "HOST",
Value: *v1.NewStructuredValues(selected.Address),
},
{
Name: "USER",
Value: *v1.NewStructuredValues(selected.User),
},
}
err = r.client.Create(ctx, &provision)
return err
}
return nil
}