/
build.go
227 lines (197 loc) · 6.24 KB
/
build.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package kube
import (
"fmt"
"log"
"math/rand"
"strings"
"time"
"github.com/brigadecore/brigade/pkg/brigade"
"github.com/brigadecore/brigade/pkg/storage"
"github.com/oklog/ulid"
v1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const secretTypeBuild = "brigade.sh/build"
const jobFilter = "component in (build, job), heritage = brigade, build = %s"
// GetBuild returns the build.
func (s *store) GetBuild(id string) (*brigade.Build, error) {
build := &brigade.Build{ID: id}
labels := fmt.Sprint("heritage=brigade,component=build,build=", build.ID)
listOption := meta.ListOptions{LabelSelector: labels}
secrets, err := s.client.CoreV1().Secrets(s.namespace).List(listOption)
if err != nil {
return nil, err
}
if len(secrets.Items) < 1 {
return nil, fmt.Errorf("could not find build %s: no secrets exist with labels %s", id, labels)
}
// Select the first secret as the build IDs are unique
b := NewBuildFromSecret(secrets.Items[0])
b.Worker, err = s.GetWorker(build.ID)
return b, err
}
// DeleteBuild deletes a build.
func (s *store) DeleteBuild(bid string, options storage.DeleteBuildOptions) error {
opts := meta.ListOptions{
LabelSelector: fmt.Sprintf(jobFilter, bid),
}
delOpts := meta.NewDeleteOptions(0)
pods, err := s.client.CoreV1().Pods(s.namespace).List(opts)
if err != nil {
return err
}
if options.SkipRunningBuilds {
for _, p := range pods.Items {
if p.Labels["component"] == "build" {
if p.Status.Phase == v1.PodRunning || p.Status.Phase == v1.PodPending {
log.Printf("skipping Build %s because its Status is %s", p.Labels["build"], p.Status.Phase)
return nil
}
}
}
}
for _, p := range pods.Items {
log.Printf("Deleting pod %q", p.Name)
if err := s.client.CoreV1().Pods(s.namespace).Delete(p.Name, delOpts); err != nil {
log.Printf("failed to delete job pod %s (continuing): %s", p.Name, err)
}
}
secrets, err := s.client.CoreV1().Secrets(s.namespace).List(opts)
if err != nil {
return err
}
for _, sec := range secrets.Items {
log.Printf("Deleting secret %q", sec.Name)
if err := s.client.CoreV1().Secrets(s.namespace).Delete(sec.Name, delOpts); err != nil {
log.Printf("failed to delete job secret %s (continuing): %s", sec.Name, err)
}
}
// As a safety condition, we might also consider deleting PVCs.
return nil
}
// CreateBuild creates a new Secret based on the build options and writes it to storage.
func (s *store) CreateBuild(build *brigade.Build) error {
if build.ID == "" {
build.ID = genID()
}
buildName := fmt.Sprintf("brigade-worker-%s", build.ID)
secret := v1.Secret{
ObjectMeta: meta.ObjectMeta{
Name: buildName,
Labels: map[string]string{
"build": build.ID,
"component": "build",
"heritage": "brigade",
"project": build.ProjectID,
},
},
Type: secretTypeBuild,
Data: map[string][]byte{
"script": build.Script,
"config": build.Config,
"payload": build.Payload,
},
StringData: map[string]string{
"build_id": buildName,
"build_name": buildName,
"short_title": build.ShortTitle,
"long_title": build.LongTitle,
"clone_url": build.CloneURL,
"commit_id": build.Revision.Commit,
"commit_ref": build.Revision.Ref,
"event_provider": build.Provider,
"event_type": build.Type,
"project_id": build.ProjectID,
"log_level": build.LogLevel,
},
}
_, err := s.client.CoreV1().Secrets(s.namespace).Create(&secret)
return err
}
// GetBuilds returns all the builds in storage.
func (s *store) GetBuilds() ([]*brigade.Build, error) {
lo := meta.ListOptions{LabelSelector: "heritage=brigade,component=build"}
secretList, err := s.client.CoreV1().Secrets(s.namespace).List(lo)
if err != nil {
return nil, err
}
podList, err := s.client.CoreV1().Pods(s.namespace).List(lo)
if err != nil {
return nil, err
}
buildList := make([]*brigade.Build, len(secretList.Items))
for i := range secretList.Items {
b := NewBuildFromSecret(secretList.Items[i])
// The error is ErrWorkerNotFound, and in that case, we just ignore
// it and assign nil to the worker.
b.Worker, _ = findWorker(b.ID, podList.Items)
buildList[i] = b
}
return buildList, nil
}
// GetProjectBuilds returns all the builds for the given project.
func (s *store) GetProjectBuilds(proj *brigade.Project) ([]*brigade.Build, error) {
// Load the pods that ran as part of this build.
labelSelectorMap := map[string]string{
"heritage": "brigade",
"component": "build",
"project": proj.ID,
}
projectSecrets, err := s.apiCache.GetSecretsFilteredBy(labelSelectorMap)
if err != nil {
return nil, err
}
// The theory here is that the secrets and pods are close to 1:1, so we can
// preload the pods and take a local hit in walking the list rather than take
// a network hit to load each pod per secret.
projectPods, err := s.apiCache.GetPodsFilteredBy(labelSelectorMap)
if err != nil {
return nil, err
}
buildList := make([]*brigade.Build, len(projectSecrets))
for i := range projectSecrets {
b := NewBuildFromSecret(projectSecrets[i])
// The error is ErrWorkerNotFound, and in that case, we just ignore
// it and assign nil to the worker.
b.Worker, _ = findWorker(b.ID, projectPods)
buildList[i] = b
}
return buildList, nil
}
func findWorker(id string, pods []v1.Pod) (*brigade.Worker, bool) {
for _, i := range pods {
buildID, ok := i.Labels["build"]
if !ok {
continue
}
if id == buildID {
return NewWorkerFromPod(i), true
}
}
return nil, false
}
// NewBuildFromSecret creates a Build object from a secret.
func NewBuildFromSecret(secret v1.Secret) *brigade.Build {
lbs := secret.ObjectMeta.Labels
sv := SecretValues(secret.Data)
return &brigade.Build{
ID: lbs["build"],
ProjectID: lbs["project"],
Type: sv.String("event_type"),
Provider: sv.String("event_provider"),
ShortTitle: sv.String("short_title"),
LongTitle: sv.String("long_title"),
CloneURL: sv.String("clone_url"),
Revision: &brigade.Revision{
Commit: sv.String("commit_id"),
Ref: sv.String("commit_ref"),
},
Payload: sv.Bytes("payload"),
Script: sv.Bytes("script"),
}
}
var entropy = rand.New(rand.NewSource(time.Now().UnixNano()))
func genID() string {
id := ulid.MustNew(ulid.Timestamp(time.Now()), entropy)
return strings.ToLower(id.String())
}