-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
1147 lines (988 loc) · 31.7 KB
/
controller.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Qubit Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at //
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubeci
// controller.go: the controller (TODO: also currently syncer), updates
// a github check run to match the current state of a workflow created
// in kubernetes.
import (
"context"
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
workflow "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
clientset "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned"
informers "github.com/argoproj/argo-workflows/v3/pkg/client/informers/externalversions"
listers "github.com/argoproj/argo-workflows/v3/pkg/client/listers/workflow/v1alpha1"
"gopkg.in/yaml.v2"
"github.com/google/go-github/v45/github"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
var (
annCommit = "kube-ci.qutics.com/sha"
annBranch = "kube-ci.qutics.com/branch"
annRepo = "kube-ci.qutics.com/repo"
annOrg = "kube-ci.qutics.com/org"
annInstID = "kube-ci.qutics.com/github-install-id"
annCheckRunName = "kube-ci.qutics.com/check-run-name"
annCheckRunID = "kube-ci.qutics.com/check-run-id"
annAnnotationsPublished = "kube-ci.qutics.com/annotations-published"
annDeploymentIDs = "kube-ci.qutics.com/deployment-ids"
annPRURL = "kube-ci.qutics.com/pr-url"
annCacheVolumeName = "kube-ci.qutics.com/cacheName"
annCacheVolumeScope = "kube-ci.qutics.com/cacheScope"
annCacheVolumeStorageSize = "kube-ci.qutics.com/cacheSize"
annCacheVolumeStorageClassName = "kube-ci.qutics.com/cacheStorageClassName"
annRunBranch = "kube-ci.qutics.com/runForBranch"
annRunTag = "kube-ci.qutics.com/runForTag"
annManualTemplates = "kube-ci.qutics.com/manualTemplates"
annEssentialTemplates = "kube-ci.qutics.com/essentialTemplates"
annDeployTemplates = "kube-ci.qutics.com/deployTemplates"
annNonInteractiveBranches = "kube-ci.qutics.com/nonInteractiveBranches"
annFeatures = "kube-ci.qutics.com/features"
annWorkflowLint = "kube-ci.qutics.com/lint"
labelManagedBy = "managedBy"
labelOrg = "org"
labelRepo = "repo"
labelBranch = "branch"
labelScope = "scope"
labelCacheHash = "cacheHash"
)
// CacheSpec lets you choose the default settings for a
// per-job cache volume.
type CacheSpec struct {
Scope string `yaml:"scope"`
Size string `yaml:"size"`
StorageClassName string `yaml:"storageClassName"`
}
// TemplateSpec gives the description, and location, of a set
// of config files for use by the setup slash command
type TemplateSpec struct {
Description string `yaml:"description"`
CI string `yaml:"ci"`
Deploy string `yaml:"deploy"`
}
// TemplateSet describes a set of templates
type TemplateSet map[string]TemplateSpec
func (ts TemplateSet) Help() string {
keys := []string{}
for name := range ts {
keys = append(keys, name)
}
sort.Strings(keys)
body := ""
for _, name := range keys {
t := ts[name]
body += fmt.Sprintf("- *%s*: %s\n", name, t.Description)
}
return body
}
type APIConfig struct {
DefaultInstallID int `yaml:"defaultInstallID"`
}
// Config defines our configuration file format
type Config struct {
CIContextPath string `yaml:"ciContextPath"`
CIYAMLFile string `yaml:"ciYAMLFile"`
CIStarlarkFile string `yaml:"ciStarlarkFile"`
Namespace string `yaml:"namespace"`
ManagedBy string `yaml:"managedBy"`
Tolerations []v1.Toleration `yaml:"tolerations"`
NodeSelector map[string]string `yaml:"nodeSelector"`
TemplateSet TemplateSet `yaml:"templates"`
CacheDefaults CacheSpec `yaml:"cacheDefaults"`
BuildDraftPRs bool `yaml:"buildDraftPRs"`
BuildBranches string `yaml:"buildBranches"`
ManualTemplates string `yaml:"manualTemplates"`
EssentialTemplates string `yaml:"essentialTemplates"`
DeployTemplates string `yaml:"deployTemplates"`
ProductionEnvironments string `yaml:"productionEnvironments"`
EnvironmentParameter string `yaml:"environmentParameter"`
NonInteractiveBranches string `yaml:"nonInteractiveBranches"`
ExtraParameters map[string]string `yaml:"extraParameters"`
API APIConfig `yaml:"api"`
buildBranches *regexp.Regexp
manualTemplates *regexp.Regexp
essentialTemplates *regexp.Regexp
deployTemplates *regexp.Regexp
productionEnvironments *regexp.Regexp
nonInteractiveBranches *regexp.Regexp
}
func ReadConfig(configfile, defaultNamespace string) (*Config, error) {
var err error
wfconfig := Config{
CIYAMLFile: "ci.yaml",
CIStarlarkFile: "ci.star",
CIContextPath: ".kube-ci",
Namespace: defaultNamespace,
ManagedBy: "kube-ci",
BuildDraftPRs: false,
BuildBranches: "master",
ManualTemplates: "^$",
EssentialTemplates: "^$",
DeployTemplates: "^$",
ProductionEnvironments: "^production$",
EnvironmentParameter: "environment",
NonInteractiveBranches: "^$",
}
if configfile != "" {
bs, err := os.ReadFile(configfile)
if err != nil {
log.Fatalf("failed to read config file, %v", err)
}
err = yaml.Unmarshal(bs, &wfconfig)
if err != nil {
return nil, fmt.Errorf("failed to parse config file, %w", err)
}
}
wfconfig.buildBranches, err = regexp.Compile(wfconfig.BuildBranches)
if err != nil {
return nil, fmt.Errorf("failed to compile branches regexp, %w", err)
}
wfconfig.manualTemplates, err = regexp.Compile(wfconfig.ManualTemplates)
if err != nil {
return nil, fmt.Errorf("failed to compile manualTemplates regexp, %w", err)
}
wfconfig.essentialTemplates, err = regexp.Compile(wfconfig.EssentialTemplates)
if err != nil {
return nil, fmt.Errorf("failed to compile essentialTemplates regexp, %w", err)
}
wfconfig.deployTemplates, err = regexp.Compile(wfconfig.DeployTemplates)
if err != nil {
return nil, fmt.Errorf("failed to compile deployTemplates regexp, %w", err)
}
wfconfig.productionEnvironments, err = regexp.Compile(wfconfig.DeployTemplates)
if err != nil {
return nil, fmt.Errorf("failed to compile productionEnvironments regexp, %w", err)
}
wfconfig.nonInteractiveBranches, err = regexp.Compile(wfconfig.NonInteractiveBranches)
if err != nil {
return nil, fmt.Errorf("failed to compile nonInteractiveBranches regexp, %w", err)
}
return &wfconfig, nil
}
type storageManager interface {
ensurePVC(wf *workflow.Workflow, org, repo, branch string, defaults CacheSpec) error
deletePVC(org, repo, branch string, action string) error
}
// workflowSyncer watches argo workflows as they run and publishes updates
// udpates back to github.
type workflowSyncer struct {
appID int64
ghSecret []byte
ghClientSrc githubClientSource
config Config
kubeclient kubernetes.Interface
client clientset.Interface
lister listers.WorkflowLister
synced cache.InformerSynced
workqueue workqueue.RateLimitingInterface
storage storageManager
argoUIBase string
}
var sanitize = regexp.MustCompile(`[^-a-z0-9]`)
var sanitizeToDNS = regexp.MustCompile(`^[-0-9.]*`)
var sanitizeToDNSPref = regexp.MustCompile(`^[-.0-9]+`)
var sanitizeToDNSSuff = regexp.MustCompile(`[-.]+$`)
func escape(str string) string {
str = sanitize.ReplaceAllString(strings.ToLower(str), "-")
str = sanitizeToDNS.ReplaceAllString(str, "")
str = sanitizeToDNSPref.ReplaceAllString(str, "")
str = sanitizeToDNSSuff.ReplaceAllString(str, "")
return str
}
func labelSafeLen(maxLen int, strs ...string) string {
escStrs := make([]string, 0, len(strs))
for i := 0; i < len(strs); i++ {
str := escape(strs[i])
if len(str) == 0 {
continue
}
escStrs = append(escStrs, str)
}
str := strings.Join(escStrs, ".")
if len(str) > maxLen {
strOver := maxLen - len(str)
str = str[strOver*-1:]
}
// Need to tidy up the start and end incase we got unlucky.
str = sanitizeToDNSPref.ReplaceAllString(str, "")
str = sanitizeToDNSSuff.ReplaceAllString(str, "")
return str
}
func labelSafe(strs ...string) string {
return labelSafeLen(50, strs...)
}
func (ws *workflowSyncer) enqueue(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
ws.workqueue.AddRateLimited(key)
}
func (ws *workflowSyncer) doDelete(obj interface{}) {
// we want to update the check run status for any
// workflows that are deleted while still
wf, ok := obj.(*workflow.Workflow)
if !ok {
return
}
if labels := wf.GetLabels(); labels == nil || labels[labelManagedBy] != ws.config.ManagedBy {
return
}
if !wf.Status.FinishedAt.IsZero() {
return
}
ghInfo, err := githubInfoFromWorkflow(wf, ws.ghClientSrc)
if err != nil {
return
}
ctx := context.Background()
// Status: Complete check run, cancelled
status := "completed"
conclusion := "cancelled"
StatusUpdate(
ctx,
ghInfo,
GithubStatus{
Status: status,
Conclusion: conclusion,
},
)
}
func NewWorkflowSyner(
kubeclient kubernetes.Interface,
clientset clientset.Interface,
sinf informers.SharedInformerFactory,
storage storageManager,
ghClientSrc githubClientSource,
appID int64,
ghSecret []byte,
baseURL string,
config Config,
) *workflowSyncer {
informer := sinf.Argoproj().V1alpha1().Workflows()
syncer := &workflowSyncer{
appID: appID,
ghClientSrc: ghClientSrc,
ghSecret: ghSecret,
kubeclient: kubeclient,
storage: storage,
client: clientset,
lister: informer.Lister(),
synced: informer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "workflows"),
argoUIBase: baseURL,
config: config,
}
log.Print("Setting up event handlers")
informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: syncer.enqueue,
UpdateFunc: func(_, new interface{}) {
syncer.enqueue(new)
},
DeleteFunc: syncer.doDelete,
})
return syncer
}
// handleCheckRunResetRun returns true if we should continue
func (ws *workflowSyncer) handleCheckRunResetRun(ctx context.Context, wf *workflow.Workflow) (*workflow.Workflow, bool) {
var err error
if v, ok := wf.Annotations[annAnnotationsPublished]; ok && v == "true" {
switch wf.Status.Phase {
case workflow.WorkflowPending, workflow.WorkflowRunning: // attempt create new checkrun for a resubmitted job
wf, err = ws.resetCheckRun(ctx, wf)
if err != nil {
log.Printf("failed checkrun reset, %v", err)
return nil, false
}
default:
// The workflow is not yet running, ignore it
return nil, false
}
}
return wf, true
}
func (ws *workflowSyncer) nodesText(wf *workflow.Workflow) string {
text := ""
var names []string
namesToNodes := make(map[string]string)
for k, v := range wf.Status.Nodes {
if v.Type != "Pod" {
continue
}
names = append(names, v.Name)
namesToNodes[v.Name] = k
}
sort.Strings(names)
for _, k := range names {
n := namesToNodes[k]
node := wf.Status.Nodes[n]
text += fmt.Sprintf("%s(%s): %s \n", k, node.Phase, node.Message)
}
return text
}
func completionStatus(wf *workflow.Workflow) (string, string) {
completed := "completed"
inProgress := "in_progress"
status := ""
failure := "failure"
success := "success"
cancelled := "cancelled"
var conclusion string
switch wf.Status.Phase {
case workflow.WorkflowPending:
status = *defaultCheckRunStatus
case workflow.WorkflowRunning:
status = inProgress
case workflow.WorkflowFailed:
status = completed
conclusion = failure
if wf.Spec.ActiveDeadlineSeconds != nil && *wf.Spec.ActiveDeadlineSeconds == 0 {
conclusion = cancelled
}
case workflow.WorkflowError:
// TODO: This might want further thought, a workflow errors if something
// went wrong that was not he fault of the workflow, the workflow may be
// retried. so it may not be sensile to mark it as completed.
status = completed
conclusion = failure
case workflow.WorkflowSucceeded:
status = completed
conclusion = success
default:
log.Printf("ignoring %s/%s, unknown node phase %q", wf.Namespace, wf.Name, wf.Status.Phase)
return "", ""
}
return status, conclusion
}
func deployStatusFromPhase(p workflow.NodePhase) string {
switch p {
case workflow.NodeRunning:
return "in_progress"
case workflow.NodeSucceeded:
return "success"
case workflow.NodeFailed:
return "failure"
case workflow.NodeError:
return "error"
default:
return ""
}
}
func (ws *workflowSyncer) isProdEnvironment(env string) bool {
if ws.config.productionEnvironments == nil {
return false
}
return ws.config.productionEnvironments.MatchString(env)
}
func (ws *workflowSyncer) createOnDemandDeployment(ctx context.Context, wf *workflow.Workflow, info *githubInfo, n workflow.NodeStatus, env string) (int64, *workflow.Workflow, error) {
desc := fmt.Sprintf("deploying %s/%s (%s) to %s", info.orgName, info.repoName, n.TemplateName, env)
deployID := info.deploymentIDs[n.ID]
params := wf.Spec.Arguments.Parameters
refType := ""
refPrefix := ""
refName := ""
refSHA := ""
for _, p := range params {
switch p.Name {
case "refType":
refType = p.GetValue()
switch refType {
case "branch":
refPrefix = "heads"
case "tag":
refPrefix = "tags"
}
case "refName":
refName = p.GetValue()
case "revision":
refSHA = p.GetValue()
}
}
if refPrefix == "" || refName == "" {
return deployID, nil, fmt.Errorf("can't create deployment, could not determine ref type or name from workflow")
}
ref := fmt.Sprintf("%s/%s", refPrefix, refName)
if deployID == 0 {
// we need to create a new deployment and record it here
requiredContexts := []string{}
opts := &github.DeploymentRequest{
Environment: github.String(env),
Task: github.String(n.TemplateName),
ProductionEnvironment: github.Bool(ws.isProdEnvironment(env)),
Ref: github.String(ref),
Payload: DeploymentPayload{
KubeCI: KubeCIPayload{
Run: false, // don't need to run a workflow, we already have one
RefType: refType,
RefName: refName,
SHA: refSHA,
},
},
Description: github.String(desc),
TransientEnvironment: nil, // TODO(tcm): support transient environments
AutoMerge: github.Bool(false), // TODO(tcm): support auto-merge
RequiredContexts: &requiredContexts, // TODO(tcm): at the moment this trips up because the check we have created has finished
}
dep, err := info.ghClient.CreateDeployment(ctx, opts)
if err != nil {
return 0, nil, fmt.Errorf("could not create on-demand deployment, %w", err)
}
deployID = dep.GetID()
info.deploymentIDs[n.ID] = deployID
deploymentIDsJSON, err := json.Marshal(info.deploymentIDs)
if err == nil {
wf, err = ws.updateWorkflow(ctx, wf, func(wf *workflow.Workflow) {
if wf.Annotations == nil {
wf.Annotations = map[string]string{}
}
wf.Annotations[annDeploymentIDs] = string(deploymentIDsJSON)
})
}
if err != nil {
// TODO(tcm): We should probably scrap the deployment we just created
return 0, nil, fmt.Errorf("could not update with on-demand deployment, %w", err)
}
}
return deployID, wf, nil
}
func (ws *workflowSyncer) getDeployNodeEnv(n workflow.NodeStatus) string {
env := ""
if n.Inputs != nil {
for _, p := range n.Inputs.Parameters {
if p.Name == ws.config.EnvironmentParameter && p.Value != nil {
env = p.Value.String()
break
}
}
}
return env
}
func (ws *workflowSyncer) syncDeployments(ctx context.Context, wf *workflow.Workflow, info *githubInfo) (*workflow.Workflow, error) {
templRegex := ws.config.deployTemplates
if str := wf.Annotations[annDeployTemplates]; str != "" {
templRegex, _ = regexp.Compile(str)
}
for _, n := range wf.Status.Nodes {
if n.TemplateName == "" || !templRegex.MatchString(n.TemplateName) {
continue
}
state := deployStatusFromPhase(n.Phase)
if state == "" {
// not a phase we care about
continue
}
env := ws.getDeployNodeEnv(n)
if env == "" {
// TODO(tcm): need to report to the user that the deploy failed as we
// couldn't work out which env was being deployed to
log.Printf("couldn't determine env for %s/%s", wf.Namespace, wf.Name)
continue
}
deployID := info.deploymentIDs[n.ID]
if deployID == 0 {
id, _, err := ws.createOnDemandDeployment(ctx, wf, info, n, env)
if err != nil {
// TODO(tcm): need to report to the user that the deploy failed as we
// couldn't work out which env was being deployed to
log.Printf("couldn't create deployment for %s/%s,%v", wf.Namespace, wf.Name, err)
continue
}
deployID = id
}
desc := fmt.Sprintf("deploying %s/%s (%s) to %s: %s", info.orgName, info.repoName, n.TemplateName, env, state)
opts := &github.DeploymentStatusRequest{
State: github.String(state),
LogURL: github.String(ws.nodeURL(wf, n)),
Description: github.String(desc),
Environment: github.String(env),
AutoInactive: github.Bool(true), // TODO(tcm): we should probably make auto-inactive controllable
}
_, err := info.ghClient.CreateDeploymentStatus(ctx, deployID, opts)
if err != nil {
return nil, fmt.Errorf("couldn't set workflow status, %w", err)
}
}
return wf, nil
}
func (ws *workflowSyncer) wfURL(wf *workflow.Workflow) string {
return fmt.Sprintf(
"%s/workflows/%s/%s",
ws.argoUIBase,
wf.Namespace,
wf.Name)
}
func (ws *workflowSyncer) nodeURL(wf *workflow.Workflow, n workflow.NodeStatus) string {
base := ws.wfURL(wf)
u, _ := url.Parse(base)
vs := u.Query()
vs.Set("tab", "workflow")
vs.Set("nodeId", n.ID)
vs.Set("sidePanel", fmt.Sprintf("logs:%s:main", n.ID))
u.RawQuery = vs.Encode()
return u.String()
}
func (ws *workflowSyncer) sync(wf *workflow.Workflow) (*workflow.Workflow, error) {
var err error
ctx := context.Background()
if labels := wf.GetLabels(); labels == nil || labels[labelManagedBy] != ws.config.ManagedBy {
return wf, nil
}
// we may modify this, so we'll just assume we will
wf = wf.DeepCopy()
log.Printf("got workflow phase: %v/%v %v", wf.Namespace, wf.Name, wf.Status.Phase)
wf, cont := ws.handleCheckRunResetRun(ctx, wf)
if !cont {
return wf, nil
}
info, err := githubInfoFromWorkflow(wf, ws.ghClientSrc)
if err != nil {
log.Printf("ignoring %s/%s, %v", wf.Namespace, wf.Name, err)
return wf, nil
}
wf, err = ws.syncDeployments(ctx, wf, info)
if err != nil {
return wf, err
}
status, conclusion := completionStatus(wf)
if status == "" {
return wf, nil
}
summary := wf.Status.Message
title := fmt.Sprintf("Workflow Run (%s/%s))", wf.Namespace, wf.Name)
text := ws.nodesText(wf)
ghStatus := GithubStatus{
Title: title,
Summary: summary,
Status: status,
Conclusion: conclusion,
DetailsURL: ws.wfURL(wf),
Text: text,
}
// Status: Progress Update
StatusUpdate(
ctx,
info,
ghStatus,
)
if status == "completed" {
_, err = ws.completeCheckRun(ctx, wf, info, ghStatus)
}
return wf, err
}
func (ws *workflowSyncer) process() bool {
obj, shutdown := ws.workqueue.Get()
if shutdown {
return false
}
defer ws.workqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
ws.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return true
}
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
ws.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("couldn't split workflow cache key %q, %v", key, err))
return true
}
wf, err := ws.lister.Workflows(namespace).Get(name)
if err != nil {
ws.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("couldn't get workflow %q, %v ", key, err))
return true
}
_, err = ws.sync(wf)
if err != nil {
log.Printf("processing event for %s/%s failed, %v", wf.Namespace, wf.Name, err)
runtime.HandleError(err)
return true
}
ws.workqueue.Forget(obj)
return true
}
func (ws *workflowSyncer) runWorker() {
for ws.process() {
}
log.Print("worker stopped")
}
type githubInfo struct {
orgName string
repoName string
instID int
headSHA string
headBranch string
checkRunName string
checkRunID int64
deploymentIDs map[string]int64
ghClient GithubClientInterface
}
type deployIDs map[string]int64
func githubInfoFromWorkflow(wf *workflow.Workflow, ghClientSrc githubClientSource) (*githubInfo, error) {
var err error
instIDStr, ok := wf.Annotations[annInstID]
if !ok {
return nil, fmt.Errorf("could not get github installation id for %s/%s", wf.Namespace, wf.Name)
}
instID, err := strconv.Atoi(instIDStr)
if err != nil {
return nil, fmt.Errorf("could not convert installation id for %s/%s to int", wf.Namespace, wf.Name)
}
headSHA, ok := wf.Annotations[annCommit]
if !ok {
return nil, fmt.Errorf("could not get commit sha for %s/%s", wf.Namespace, wf.Name)
}
headBranch, ok := wf.Annotations[annBranch]
if !ok {
return nil, fmt.Errorf("could not get commit branch for %s/%s", wf.Namespace, wf.Name)
}
orgName, ok := wf.Annotations[annOrg]
if !ok {
return nil, fmt.Errorf("could not get github org for %s/%s", wf.Namespace, wf.Name)
}
repoName, ok := wf.Annotations[annRepo]
if !ok {
return nil, fmt.Errorf("could not get github repo name for %s/%s", wf.Namespace, wf.Name)
}
checkRunName, ok := wf.Annotations[annCheckRunName]
if !ok {
return nil, fmt.Errorf("could not get check run name for %s/%s", wf.Namespace, wf.Name)
}
checkRunIDStr, ok := wf.Annotations[annCheckRunID]
if !ok {
return nil, fmt.Errorf("could not get check run id for %s/%s", wf.Namespace, wf.Name)
}
checkRunID, err := strconv.Atoi(checkRunIDStr)
if err != nil {
return nil, fmt.Errorf("could not convert check run id for %s/%s to int", wf.Namespace, wf.Name)
}
deploymentIDs := deployIDs{}
deploymentIDsJSON := wf.Annotations[annDeploymentIDs]
json.Unmarshal([]byte(deploymentIDsJSON), &deploymentIDs)
ghClient, err := ghClientSrc.getClient(orgName, int(instID), repoName)
if err != nil {
return nil, fmt.Errorf("could not get github client, %w", err)
}
return &githubInfo{
headSHA: headSHA,
instID: instID,
headBranch: headBranch,
orgName: orgName,
repoName: repoName,
checkRunName: checkRunName,
checkRunID: int64(checkRunID),
deploymentIDs: deploymentIDs,
ghClient: ghClient,
}, nil
}
func (ws *workflowSyncer) resetCheckRun(ctx context.Context, wf *workflow.Workflow) (*workflow.Workflow, error) {
ghInfo, err := githubInfoFromWorkflow(wf, ws.ghClientSrc)
if err != nil {
return nil, fmt.Errorf("no check-run info found in restarted workflow (%s/%s)", wf.Namespace, wf.Name)
}
newCR, err := ghInfo.ghClient.CreateCheckRun(context.Background(),
github.CreateCheckRunOptions{
Name: ghInfo.checkRunName,
HeadSHA: ghInfo.headSHA,
ExternalID: github.String(wf.Spec.Entrypoint),
Status: defaultCheckRunStatus,
Output: &github.CheckRunOutput{
Title: github.String("Workflow Setup"),
Summary: github.String("Creating workflow"),
},
},
)
if err != nil {
return nil, fmt.Errorf("failed creating new check run, %w", err)
}
ghInfo.checkRunID = newCR.GetID()
for k := range wf.Annotations {
switch k {
case annAnnotationsPublished:
wf.Annotations[k] = "false"
case annCheckRunName:
wf.Annotations[k] = newCR.GetName()
case annCheckRunID:
wf.Annotations[k] = strconv.Itoa(int(newCR.GetID()))
case annDeploymentIDs:
wf.Annotations[k] = `{}`
}
}
return ws.client.ArgoprojV1alpha1().Workflows(wf.GetNamespace()).Update(ctx, wf, metav1.UpdateOptions{})
}
func getWFVolumeScope(wf *workflow.Workflow) string {
if wf.Annotations == nil {
return scopeNone
}
switch wf.Annotations[annCacheVolumeScope] {
case scopeBranch:
return scopeBranch
case scopeProject:
return scopeProject
default:
return scopeNone
}
}
var ()
func usesCacheVolume(wf *workflow.Workflow) bool {
if wf.Annotations == nil {
return false
}
scope := wf.Annotations[annCacheVolumeScope]
if scope == scopeNone || scope == "" {
return false
}
return true
}
func clearCacheAction(wf *workflow.Workflow) *github.CheckRunAction {
if !usesCacheVolume(wf) {
return nil
}
clearCacheAction := "clearCache"
if getWFVolumeScope(wf) == scopeBranch {
clearCacheAction = "clearCacheBranch"
}
return &github.CheckRunAction{
Label: "Clear Cache",
Description: "delete the cache volume for this build",
Identifier: clearCacheAction,
}
}
func (ws *workflowSyncer) buttonsForWorkflow(wf *workflow.Workflow) ([]*github.CheckRunAction, []string) {
var warnings []string
var actions []*github.CheckRunAction
if action := clearCacheAction(wf); action != nil {
actions = append(actions, action)
}
return actions, warnings
}
func (ws *workflowSyncer) isInteractiveBranch(ctx context.Context, wf *workflow.Workflow, ghInfo *githubInfo) (bool, error) {
r, err := ghInfo.ghClient.GetRepo(ctx)
if err != nil {
return false, err
}
if r.GetDefaultBranch() == ghInfo.headBranch {
return false, nil
}
templRegex := ws.config.nonInteractiveBranches
if str := wf.Annotations[annNonInteractiveBranches]; str != "" {
templRegex, err = regexp.Compile(str)
if err != nil {
templRegex = ws.config.nonInteractiveBranches
}
}
if templRegex.MatchString(ghInfo.headBranch) {
return false, nil
}
return true, nil
}
func (ws *workflowSyncer) nextCheckRunsForWorkflow(ctx context.Context, wf *workflow.Workflow, ghInfo *githubInfo) []string {
var warnings []string
ok, err := ws.isInteractiveBranch(ctx, wf, ghInfo)
if err != nil {
warnings = append(warnings, fmt.Sprintf("error when check for active PRs, %v", err))
}
templRegex := ws.config.manualTemplates
if str := wf.Annotations[annManualTemplates]; str != "" {
templRegex, err = regexp.Compile(str)
if err != nil {
templRegex = ws.config.manualTemplates
warnings = append(warnings, fmt.Sprintf("ignoring bad manual template regexp, %v", err))
}
}
essentialRegex := ws.config.essentialTemplates
if str := wf.Annotations[annEssentialTemplates]; str != "" {
templRegex, err = regexp.Compile(str)
if err != nil {
templRegex = ws.config.essentialTemplates
warnings = append(warnings, fmt.Sprintf("ignoring bad essential template regexp, %v", err))
}
}
var nextTasks []string
if ok {
for _, t := range wf.Spec.Templates {
if t.Name != "" && templRegex.MatchString(t.Name) {
if len(t.Name) > 20 {
warnings = append(warnings, fmt.Sprintf("Action button %s dropped, only 20 chars permitted", t.Name))
continue
}
nextTasks = append(nextTasks, t.Name)
}
}
}
// if we are already one of the valid next tasks, don't create next check runs.
for _, task := range nextTasks {
if task == wf.Spec.Entrypoint {
return nil
}
}
for _, task := range nextTasks {
conclusion := "neutral"
if essentialRegex.MatchString(task) {
conclusion = "action_required"
}
_, err := ghInfo.ghClient.CreateCheckRun(ctx, github.CreateCheckRunOptions{
Name: fmt.Sprintf("Workflow - %s", task),
HeadSHA: ghInfo.headSHA,
Conclusion: github.String(conclusion),
ExternalID: github.String(task),
Actions: []*github.CheckRunAction{
{
Label: "Run",
Description: "run this workflow template manually",
Identifier: "run",
},