forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
triggers.go
1029 lines (944 loc) · 33.8 KB
/
triggers.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
package set
import (
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strings"
"text/tabwriter"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
kapps "k8s.io/kubernetes/pkg/apis/apps"
kbatch "k8s.io/kubernetes/pkg/apis/batch"
kapi "k8s.io/kubernetes/pkg/apis/core"
kextensions "k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
ometa "github.com/openshift/origin/pkg/api/meta"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/generate/app"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
triggerapi "github.com/openshift/origin/pkg/image/apis/image/v1/trigger"
"github.com/openshift/origin/pkg/image/trigger/annotations"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
)
var (
triggersLong = templates.LongDesc(`
Set or remove triggers
Build configs, deployment configs, and most Kubernetes workload objects may have a set of triggers
that result in a new deployment or build being created when an image changes. This command enables
you to alter those triggers - making them automatic or manual, adding new entries, or changing
existing entries.
Deployments support triggering off of image changes and on config changes. Config changes are any
alterations to the pod template, while image changes will result in the container image value being
updated whenever an image stream tag is updated. You may also trigger Kubernetes stateful sets,
daemon sets, deployments, and cron jobs from images. Disabling the config change trigger is equivalent
to pausing most objects. Deployment configs will not perform their first deployment until all image
change triggers have been submitted.
Build configs support triggering off of image changes, config changes, and webhooks. The config change
trigger for a build config will only trigger the first build.`)
triggersExample = templates.Examples(`
# Print the triggers on the registry
%[1]s triggers dc/registry
# Set all triggers to manual
%[1]s triggers dc/registry --manual
# Enable all automatic triggers
%[1]s triggers dc/registry --auto
# Reset the GitHub webhook on a build to a new, generated secret
%[1]s triggers bc/webapp --from-github
%[1]s triggers bc/webapp --from-webhook
# Remove all triggers
%[1]s triggers bc/webapp --remove-all
# Stop triggering on config change
%[1]s triggers dc/registry --from-config --remove
# Add an image trigger to a build config
%[1]s triggers bc/webapp --from-image=namespace1/image:latest
# Add an image trigger to a stateful set on the main container
%[1]s triggers statefulset/db --from-image=namespace1/image:latest -c main`)
)
type TriggersOptions struct {
Out io.Writer
Err io.Writer
Filenames []string
Selector string
All bool
Output string
Builder *resource.Builder
Infos []*resource.Info
Encoder runtime.Encoder
Cmd *cobra.Command
Local bool
ShortOutput bool
Mapper meta.RESTMapper
PrintTable bool
PrintObject func([]*resource.Info) error
Remove bool
RemoveAll bool
Auto bool
Manual bool
Reset bool
ContainerNames string
FromConfig bool
FromGitHub *bool
FromWebHook *bool
FromWebHookAllowEnv *bool
FromGitLab *bool
FromBitbucket *bool
FromImage string
// FromImageNamespace is the namespace for the FromImage
FromImageNamespace string
}
// NewCmdTriggers implements the set triggers command
func NewCmdTriggers(fullName string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command {
options := &TriggersOptions{
Out: out,
Err: errOut,
}
cmd := &cobra.Command{
Use: "triggers RESOURCE/NAME [--from-config|--from-image|--from-github|--from-webhook] [--auto|--manual]",
Short: "Update the triggers on one or more objects",
Long: triggersLong,
Example: fmt.Sprintf(triggersExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(options.Complete(f, cmd, args))
kcmdutil.CheckErr(options.Validate())
if err := options.Run(); err != nil {
// TODO: move me to kcmdutil
if err == kcmdutil.ErrExit {
os.Exit(1)
}
kcmdutil.CheckErr(err)
}
},
}
kcmdutil.AddPrinterFlags(cmd)
cmd.Flags().StringVarP(&options.Selector, "selector", "l", options.Selector, "Selector (label query) to filter on")
cmd.Flags().BoolVar(&options.All, "all", options.All, "If true, select all resources in the namespace of the specified resource types")
cmd.Flags().StringSliceVarP(&options.Filenames, "filename", "f", options.Filenames, "Filename, directory, or URL to file to use to edit the resource.")
cmd.Flags().BoolVar(&options.Remove, "remove", options.Remove, "If true, remove the specified trigger(s).")
cmd.Flags().BoolVar(&options.RemoveAll, "remove-all", options.RemoveAll, "If true, remove all triggers.")
cmd.Flags().BoolVar(&options.Auto, "auto", options.Auto, "If true, enable all triggers, or just the specified trigger")
cmd.Flags().BoolVar(&options.Manual, "manual", options.Manual, "If true, set all triggers to manual, or just the specified trigger")
cmd.Flags().BoolVar(&options.Local, "local", false, "If true, set image will NOT contact api-server but run locally.")
cmd.Flags().BoolVar(&options.FromConfig, "from-config", options.FromConfig, "If set, configuration changes will result in a change")
cmd.Flags().StringVarP(&options.ContainerNames, "containers", "c", options.ContainerNames, "Comma delimited list of container names this trigger applies to on deployments; defaults to the name of the only container")
cmd.Flags().StringVar(&options.FromImage, "from-image", options.FromImage, "An image stream tag to trigger off of")
options.FromGitHub = cmd.Flags().Bool("from-github", false, "If true, a GitHub webhook - a secret value will be generated automatically")
options.FromWebHook = cmd.Flags().Bool("from-webhook", false, "If true, a generic webhook - a secret value will be generated automatically")
options.FromWebHookAllowEnv = cmd.Flags().Bool("from-webhook-allow-env", false, "If true, a generic webhook which can provide environment variables - a secret value will be generated automatically")
options.FromGitLab = cmd.Flags().Bool("from-gitlab", false, "If true, a GitLab webhook - a secret value will be generated automatically")
options.FromBitbucket = cmd.Flags().Bool("from-bitbucket", false, "If true, a Bitbucket webhook - a secret value will be generated automatically")
kcmdutil.AddDryRunFlag(cmd)
cmd.MarkFlagFilename("filename", "yaml", "yml", "json")
return cmd
}
func (o *TriggersOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
cmdNamespace, explicit, err := f.DefaultNamespace()
if err != nil {
return err
}
if !cmd.Flags().Lookup("from-github").Changed {
o.FromGitHub = nil
}
if !cmd.Flags().Lookup("from-webhook").Changed {
o.FromWebHook = nil
}
if !cmd.Flags().Lookup("from-webhook-allow-env").Changed {
o.FromWebHookAllowEnv = nil
}
if !cmd.Flags().Lookup("from-gitlab").Changed {
o.FromGitLab = nil
}
if !cmd.Flags().Lookup("from-bitbucket").Changed {
o.FromBitbucket = nil
}
o.Cmd = cmd
if len(o.FromImage) > 0 {
ref, err := imageapi.ParseDockerImageReference(o.FromImage)
if err != nil {
return fmt.Errorf("the value of --from-image does not appear to be a valid reference to an image: %v", err)
}
if len(ref.Registry) > 0 || len(ref.ID) > 0 {
return fmt.Errorf("the value of --from-image must point to an image stream tag on this server")
}
if len(ref.Tag) == 0 {
return fmt.Errorf("the value of --from-image must include the tag you wish to pull from")
}
o.FromImage = ref.NameString()
o.FromImageNamespace = defaultNamespace(ref.Namespace, cmdNamespace)
}
count := o.count()
o.Reset = count == 0 && (o.Auto || o.Manual)
switch {
case count == 0 && !o.Remove && !o.RemoveAll && !o.Auto && !o.Manual:
o.PrintTable = true
case !o.RemoveAll && !o.Auto && !o.Manual:
o.Auto = true
}
mapper, _ := f.Object()
o.Builder = f.NewBuilder().
Internal().
LocalParam(o.Local).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(explicit, &resource.FilenameOptions{Recursive: false, Filenames: o.Filenames}).
Flatten()
if !o.Local {
o.Builder = o.Builder.
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(o.All, args...)
}
o.Output = kcmdutil.GetFlagString(cmd, "output")
o.PrintObject = func(infos []*resource.Info) error {
return f.PrintResourceInfos(cmd, o.Local, infos, o.Out)
}
o.Encoder = f.JSONEncoder()
o.ShortOutput = kcmdutil.GetFlagString(cmd, "output") == "name"
o.Mapper = mapper
return nil
}
func (o *TriggersOptions) count() int {
count := 0
if o.FromConfig {
count++
}
if o.FromGitHub != nil {
count++
}
if o.FromWebHook != nil {
count++
}
if o.FromWebHookAllowEnv != nil {
count++
}
if o.FromGitLab != nil {
count++
}
if o.FromBitbucket != nil {
count++
}
if len(o.FromImage) > 0 {
count++
}
return count
}
func (o *TriggersOptions) Validate() error {
count := o.count()
switch {
case o.Auto && o.Manual:
return fmt.Errorf("you must specify at most one of --auto or --manual")
case o.Remove && o.RemoveAll:
return fmt.Errorf("you must specify either --remove or --remove-all")
case o.RemoveAll && (count != 0 || o.Auto || o.Manual):
return fmt.Errorf("--remove-all may not be used with any other flag")
case o.Remove && count < 1:
return fmt.Errorf("--remove requires a flag defining a trigger type to be specified")
case count > 1:
return fmt.Errorf("you may only set one trigger type at a time")
case count == 0 && !o.Remove && !o.RemoveAll && !o.Auto && !o.Manual && !o.PrintTable:
return fmt.Errorf("specify one of the --from-* flags to add a trigger, --remove to remove, or --auto|--manual to control existing triggers")
}
return nil
}
func (o *TriggersOptions) Run() error {
infos := o.Infos
singleItemImplied := len(o.Infos) <= 1
if o.Builder != nil {
loaded, err := o.Builder.Do().IntoSingleItemImplied(&singleItemImplied).Infos()
if err != nil {
return err
}
infos = loaded
}
if o.PrintTable && len(o.Output) == 0 {
return o.printTriggers(infos)
}
updateTriggerFn := func(triggers *TriggerDefinition) error {
o.updateTriggers(triggers)
return nil
}
patches := CalculatePatches(infos, o.Encoder, func(info *resource.Info) (bool, error) {
return UpdateTriggersForObject(info.Object, updateTriggerFn)
})
if singleItemImplied && len(patches) == 0 {
return fmt.Errorf("%s/%s does not support triggers", infos[0].Mapping.Resource, infos[0].Name)
}
if len(o.Output) > 0 || o.Local || kcmdutil.GetDryRunFlag(o.Cmd) {
return o.PrintObject(infos)
}
failed := false
for _, patch := range patches {
info := patch.Info
if patch.Err != nil {
failed = true
fmt.Fprintf(o.Err, "error: %s/%s %v\n", info.Mapping.Resource, info.Name, patch.Err)
continue
}
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
fmt.Fprintf(o.Err, "info: %s %q was not changed\n", info.Mapping.Resource, info.Name)
continue
}
glog.V(4).Infof("Calculated patch %s", patch.Patch)
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
if err != nil {
handlePodUpdateError(o.Err, err, "triggered")
failed = true
continue
}
info.Refresh(obj, true)
kcmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "updated")
}
if failed {
return kcmdutil.ErrExit
}
return nil
}
// printTriggers displays a tabular output of the triggers for each object.
func (o *TriggersOptions) printTriggers(infos []*resource.Info) error {
w := tabwriter.NewWriter(o.Out, 0, 2, 2, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, "NAME\tTYPE\tVALUE\tAUTO\n")
for _, info := range infos {
_, err := UpdateTriggersForObject(info.Object, func(triggers *TriggerDefinition) error {
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%t\n", info.Mapping.Resource, info.Name, "config", "", triggers.ConfigChange)
for _, image := range triggers.ImageChange {
var details string
switch {
case len(image.Names) > 0:
if len(image.Namespace) > 0 {
details = fmt.Sprintf("%s/%s (%s)", image.Namespace, image.From, strings.Join(image.Names, ", "))
} else {
details = fmt.Sprintf("%s (%s)", image.From, strings.Join(image.Names, ", "))
}
case len(image.Namespace) > 0:
details = fmt.Sprintf("%s/%s", image.Namespace, image.From)
default:
details = image.From
}
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%t\n", info.Mapping.Resource, info.Name, "image", details, image.Auto)
}
for _, s := range triggers.GenericWebHooks {
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%s\n", info.Mapping.Resource, info.Name, "webhook", s, "")
}
for _, s := range triggers.GitHubWebHooks {
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%s\n", info.Mapping.Resource, info.Name, "github", s, "")
}
for _, s := range triggers.GitLabWebHooks {
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%s\n", info.Mapping.Resource, info.Name, "gitlab", s, "")
}
for _, s := range triggers.BitbucketWebHooks {
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%s\n", info.Mapping.Resource, info.Name, "bitbucket", s, "")
}
return nil
})
if err != nil {
glog.V(2).Infof("Unable to calculate trigger for %s: %v", info.Name, err)
fmt.Fprintf(w, "%s/%s\t%s\t%s\t%t\n", info.Mapping.Resource, info.Name, "<error>", "", false)
}
}
return nil
}
// updateTriggers updates only those fields with flags set by the user
func (o *TriggersOptions) updateTriggers(triggers *TriggerDefinition) {
// clear everything
if o.RemoveAll {
*triggers = TriggerDefinition{}
return
}
// clear a specific field
if o.Remove {
if o.FromConfig {
triggers.ConfigChange = false
}
if len(o.FromImage) > 0 {
var newTriggers []ImageChangeTrigger
for _, trigger := range triggers.ImageChange {
if trigger.From != o.FromImage {
newTriggers = append(newTriggers, trigger)
}
}
triggers.ImageChange = newTriggers
}
if o.FromWebHook != nil && *o.FromWebHook {
triggers.GenericWebHooks = nil
}
if o.FromWebHookAllowEnv != nil && *o.FromWebHookAllowEnv {
triggers.GenericWebHooks = nil
}
if o.FromGitHub != nil && *o.FromGitHub {
triggers.GitHubWebHooks = nil
}
if o.FromGitLab != nil && *o.FromGitLab {
triggers.GitLabWebHooks = nil
}
if o.FromBitbucket != nil && *o.FromBitbucket {
triggers.BitbucketWebHooks = nil
}
return
}
// change the automated status
if o.Reset {
triggers.ConfigChange = o.Auto
for i := range triggers.ImageChange {
triggers.ImageChange[i].Auto = o.Auto
}
return
}
// change individual elements
if o.FromConfig {
triggers.ConfigChange = true
}
if len(o.FromImage) > 0 {
names := strings.Split(o.ContainerNames, ",")
if len(o.ContainerNames) == 0 {
names = nil
}
found := false
for i, trigger := range triggers.ImageChange {
if trigger.From == o.FromImage && trigger.Namespace == o.FromImageNamespace {
found = true
triggers.ImageChange[i].Auto = !o.Manual
triggers.ImageChange[i].Names = names
break
}
}
if !found {
triggers.ImageChange = append(triggers.ImageChange, ImageChangeTrigger{
From: o.FromImage,
Namespace: o.FromImageNamespace,
Auto: !o.Manual,
Names: names,
})
}
}
if o.FromWebHook != nil && *o.FromWebHook {
secret := app.GenerateSecret(20)
triggers.GenericWebHooks = append(triggers.GenericWebHooks,
buildapi.WebHookTrigger{
Secret: secret,
AllowEnv: false,
},
)
}
if o.FromWebHookAllowEnv != nil && *o.FromWebHookAllowEnv {
secret := app.GenerateSecret(20)
triggers.GenericWebHooks = append(triggers.GenericWebHooks,
buildapi.WebHookTrigger{
Secret: secret,
AllowEnv: true,
},
)
}
if o.FromGitHub != nil && *o.FromGitHub {
secret := app.GenerateSecret(20)
triggers.GitHubWebHooks = append(triggers.GitHubWebHooks,
buildapi.WebHookTrigger{
Secret: secret,
},
)
}
if o.FromGitLab != nil && *o.FromGitLab {
secret := app.GenerateSecret(20)
triggers.GitLabWebHooks = append(triggers.GitLabWebHooks,
buildapi.WebHookTrigger{
Secret: secret,
},
)
}
if o.FromBitbucket != nil && *o.FromBitbucket {
secret := app.GenerateSecret(20)
triggers.BitbucketWebHooks = append(triggers.BitbucketWebHooks,
buildapi.WebHookTrigger{
Secret: secret,
},
)
}
}
// ImageChangeTrigger represents the capabilities present in deployment config and build
// config objects in a consistent way.
type ImageChangeTrigger struct {
// If this trigger is automatically applied
Auto bool
// An ImageStreamTag name to target
From string
// The target namespace, normalized if set
Namespace string
// A list of names this trigger targets
Names []string
}
// TriggerDefinition is the abstract representation of triggers for builds and deployment configs.
type TriggerDefinition struct {
ConfigChange bool
ImageChange []ImageChangeTrigger
GenericWebHooks []buildapi.WebHookTrigger
GitHubWebHooks []buildapi.WebHookTrigger
GitLabWebHooks []buildapi.WebHookTrigger
BitbucketWebHooks []buildapi.WebHookTrigger
}
// defaultNamespace returns an empty string if the provided namespace matches the default namespace, or
// returns the namespace.
func defaultNamespace(namespace, defaultNamespace string) string {
if namespace == defaultNamespace {
return ""
}
return namespace
}
// NewAnnotationTriggers creates a trigger definition from an object that can be triggered by the image
// annotation.
func NewAnnotationTriggers(obj runtime.Object) (*TriggerDefinition, error) {
m, err := meta.Accessor(obj)
if err != nil {
return nil, err
}
t := &TriggerDefinition{ConfigChange: true}
switch typed := obj.(type) {
case *kextensions.Deployment:
t.ConfigChange = !typed.Spec.Paused
}
out, ok := m.GetAnnotations()[triggerapi.TriggerAnnotationKey]
if !ok {
return t, nil
}
triggers := []triggerapi.ObjectFieldTrigger{}
if err := json.Unmarshal([]byte(out), &triggers); err != nil {
return nil, err
}
for _, trigger := range triggers {
container, remainder, err := annotations.ContainerForObjectFieldPath(obj, trigger.FieldPath)
if err != nil || remainder != "image" {
continue
}
t.ImageChange = append(t.ImageChange, ImageChangeTrigger{
Auto: !trigger.Paused,
Names: []string{container.GetName()},
From: trigger.From.Name,
Namespace: defaultNamespace(trigger.From.Namespace, m.GetNamespace()),
})
}
return t, nil
}
// NewDeploymentConfigTriggers creates a trigger definition from a deployment config.
func NewDeploymentConfigTriggers(config *appsapi.DeploymentConfig) *TriggerDefinition {
t := &TriggerDefinition{}
for _, trigger := range config.Spec.Triggers {
switch trigger.Type {
case appsapi.DeploymentTriggerOnConfigChange:
t.ConfigChange = true
case appsapi.DeploymentTriggerOnImageChange:
t.ImageChange = append(t.ImageChange, ImageChangeTrigger{
Auto: trigger.ImageChangeParams.Automatic,
Names: trigger.ImageChangeParams.ContainerNames,
From: trigger.ImageChangeParams.From.Name,
Namespace: defaultNamespace(trigger.ImageChangeParams.From.Namespace, config.Namespace),
})
}
}
return t
}
// NewBuildConfigTriggers creates a trigger definition from a build config.
func NewBuildConfigTriggers(config *buildapi.BuildConfig) *TriggerDefinition {
t := &TriggerDefinition{}
setStrategy := false
for _, trigger := range config.Spec.Triggers {
switch trigger.Type {
case buildapi.ConfigChangeBuildTriggerType:
t.ConfigChange = true
case buildapi.GenericWebHookBuildTriggerType:
t.GenericWebHooks = append(t.GenericWebHooks,
buildapi.WebHookTrigger{
Secret: trigger.GenericWebHook.Secret,
SecretReference: trigger.GenericWebHook.SecretReference,
AllowEnv: trigger.GenericWebHook.AllowEnv,
},
)
case buildapi.GitHubWebHookBuildTriggerType:
t.GitHubWebHooks = append(t.GitHubWebHooks,
buildapi.WebHookTrigger{
Secret: trigger.GitHubWebHook.Secret,
SecretReference: trigger.GitHubWebHook.SecretReference,
},
)
case buildapi.GitLabWebHookBuildTriggerType:
t.GitLabWebHooks = append(t.GitLabWebHooks,
buildapi.WebHookTrigger{
Secret: trigger.GitLabWebHook.Secret,
SecretReference: trigger.GitLabWebHook.SecretReference,
},
)
case buildapi.BitbucketWebHookBuildTriggerType:
t.BitbucketWebHooks = append(t.BitbucketWebHooks,
buildapi.WebHookTrigger{
Secret: trigger.BitbucketWebHook.Secret,
SecretReference: trigger.BitbucketWebHook.SecretReference,
},
)
case buildapi.ImageChangeBuildTriggerType:
if trigger.ImageChange.From == nil {
if strategyTrigger := strategyTrigger(config); strategyTrigger != nil {
setStrategy = true
strategyTrigger.Auto = true
t.ImageChange = append(t.ImageChange, *strategyTrigger)
}
continue
}
// normalize the trigger
trigger.ImageChange.From.Namespace = defaultNamespace(trigger.ImageChange.From.Namespace, config.Namespace)
t.ImageChange = append(t.ImageChange, ImageChangeTrigger{
Auto: true,
From: trigger.ImageChange.From.Name,
Namespace: trigger.ImageChange.From.Namespace,
})
}
}
if !setStrategy {
if strategyTrigger := strategyTrigger(config); strategyTrigger != nil {
t.ImageChange = append(t.ImageChange, *strategyTrigger)
}
}
return t
}
// Apply writes a trigger definition back to an object.
func (t *TriggerDefinition) Apply(obj runtime.Object) error {
switch c := obj.(type) {
case *appsapi.DeploymentConfig:
if len(t.GitHubWebHooks) > 0 {
return fmt.Errorf("deployment configs do not support GitHub web hooks")
}
if len(t.GenericWebHooks) > 0 {
return fmt.Errorf("deployment configs do not support web hooks")
}
if len(t.GitLabWebHooks) > 0 {
return fmt.Errorf("deployment configs do not support GitLab web hooks")
}
if len(t.BitbucketWebHooks) > 0 {
return fmt.Errorf("deployment configs do not support Bitbucket web hooks")
}
existingTriggers := filterDeploymentTriggers(c.Spec.Triggers, appsapi.DeploymentTriggerOnConfigChange)
var triggers []appsapi.DeploymentTriggerPolicy
if t.ConfigChange {
triggers = append(triggers, appsapi.DeploymentTriggerPolicy{Type: appsapi.DeploymentTriggerOnConfigChange})
}
allNames := sets.NewString()
for _, container := range c.Spec.Template.Spec.Containers {
allNames.Insert(container.Name)
}
for _, container := range c.Spec.Template.Spec.InitContainers {
allNames.Insert(container.Name)
}
for _, trigger := range t.ImageChange {
if len(trigger.Names) == 0 {
return fmt.Errorf("you must specify --containers when setting --from-image")
}
if !allNames.HasAll(trigger.Names...) {
return fmt.Errorf(
"not all container names exist: %s (accepts: %s)",
strings.Join(sets.NewString(trigger.Names...).Difference(allNames).List(), ", "),
strings.Join(allNames.List(), ", "),
)
}
triggers = append(triggers, appsapi.DeploymentTriggerPolicy{
Type: appsapi.DeploymentTriggerOnImageChange,
ImageChangeParams: &appsapi.DeploymentTriggerImageChangeParams{
Automatic: trigger.Auto,
From: kapi.ObjectReference{
Kind: "ImageStreamTag",
Name: trigger.From,
},
ContainerNames: trigger.Names,
},
})
}
c.Spec.Triggers = mergeDeployTriggers(existingTriggers, triggers)
return nil
case *buildapi.BuildConfig:
var triggers []buildapi.BuildTriggerPolicy
if t.ConfigChange {
triggers = append(triggers, buildapi.BuildTriggerPolicy{Type: buildapi.ConfigChangeBuildTriggerType})
}
for i := range t.GenericWebHooks {
triggers = append(triggers, buildapi.BuildTriggerPolicy{
Type: buildapi.GenericWebHookBuildTriggerType,
GenericWebHook: &t.GenericWebHooks[i],
})
}
for i := range t.GitHubWebHooks {
triggers = append(triggers, buildapi.BuildTriggerPolicy{
Type: buildapi.GitHubWebHookBuildTriggerType,
GitHubWebHook: &t.GitHubWebHooks[i],
})
}
for i := range t.GitLabWebHooks {
triggers = append(triggers, buildapi.BuildTriggerPolicy{
Type: buildapi.GitLabWebHookBuildTriggerType,
GitLabWebHook: &t.GitLabWebHooks[i],
})
}
for i := range t.BitbucketWebHooks {
triggers = append(triggers, buildapi.BuildTriggerPolicy{
Type: buildapi.BitbucketWebHookBuildTriggerType,
BitbucketWebHook: &t.BitbucketWebHooks[i],
})
}
// add new triggers, filter out any old triggers that match (if moving from automatic to manual),
// and then merge the old triggers and the new triggers to preserve fields like lastTriggeredImageID
existingTriggers := c.Spec.Triggers
strategyTrigger := strategyTrigger(c)
for _, trigger := range t.ImageChange {
change := &buildapi.ImageChangeTrigger{
From: &kapi.ObjectReference{
Kind: "ImageStreamTag",
Name: trigger.From,
Namespace: trigger.Namespace,
},
}
// use the canonical ImageChangeTrigger with nil From
if strategyTrigger != nil {
strategyTrigger.Auto = trigger.Auto
}
if reflect.DeepEqual(strategyTrigger, &trigger) {
change.From = nil
}
// if this trigger is not automatic, then we need to remove it from the list of triggers
if !trigger.Auto {
existingTriggers = filterBuildImageTriggers(existingTriggers, trigger, strategyTrigger)
continue
}
triggers = append(triggers, buildapi.BuildTriggerPolicy{
Type: buildapi.ImageChangeBuildTriggerType,
ImageChange: change,
})
}
c.Spec.Triggers = mergeBuildTriggers(existingTriggers, triggers)
return nil
case *kextensions.DaemonSet, *kapps.StatefulSet, *kbatch.CronJob, *kextensions.Deployment:
if len(t.GitHubWebHooks) > 0 {
return fmt.Errorf("does not support GitHub web hooks")
}
if len(t.GenericWebHooks) > 0 {
return fmt.Errorf("does not support web hooks")
}
if len(t.GitLabWebHooks) > 0 {
return fmt.Errorf("does not support GitLab web hooks")
}
if len(t.BitbucketWebHooks) > 0 {
return fmt.Errorf("does not support Bitbucket web hooks")
}
m, err := meta.Accessor(obj)
if err != nil {
return err
}
spec, path, err := ometa.GetPodSpec(obj)
if err != nil {
return err
}
allNames := sets.NewString()
for _, container := range spec.Containers {
allNames.Insert(container.Name)
}
for _, container := range spec.InitContainers {
allNames.Insert(container.Name)
}
alreadyTriggered := sets.NewString()
var triggers []triggerapi.ObjectFieldTrigger
glog.V(4).Infof("calculated triggers: %#v", t.ImageChange)
for _, trigger := range t.ImageChange {
if len(trigger.Names) == 0 {
return fmt.Errorf("you must specify --containers when setting --from-image")
}
if !allNames.HasAll(trigger.Names...) {
return fmt.Errorf(
"not all container names exist: %s (accepts: %s)",
strings.Join(sets.NewString(trigger.Names...).Difference(allNames).List(), ", "),
strings.Join(allNames.List(), ", "),
)
}
if alreadyTriggered.HasAny(trigger.Names...) {
return fmt.Errorf("only one trigger may reference each container: %s", strings.Join(alreadyTriggered.Intersection(sets.NewString(trigger.Names...)).List(), ", "))
}
alreadyTriggered.Insert(trigger.Names...)
ns := trigger.Namespace
if ns == m.GetNamespace() {
ns = ""
}
for _, name := range trigger.Names {
triggers = append(triggers, triggerapi.ObjectFieldTrigger{
From: triggerapi.ObjectReference{
Kind: "ImageStreamTag",
Name: trigger.From,
Namespace: ns,
},
FieldPath: fmt.Sprintf(path.Child("containers").String()+"[?(@.name==\"%s\")].image", name),
Paused: !trigger.Auto,
})
}
}
out, err := json.Marshal(triggers)
if err != nil {
return err
}
a := m.GetAnnotations()
if a == nil {
a = make(map[string]string)
}
a[triggerapi.TriggerAnnotationKey] = string(out)
m.SetAnnotations(a)
switch typed := obj.(type) {
case *kextensions.Deployment:
typed.Spec.Paused = !t.ConfigChange
}
glog.V(4).Infof("Updated annotated object: %#v", obj)
return nil
default:
return fmt.Errorf("the object is not a deployment config or build config")
}
}
// triggerMatchesBuildImageChange identifies whether the image change is equivalent to the trigger
func triggerMatchesBuildImageChange(trigger ImageChangeTrigger, strategyTrigger *ImageChangeTrigger, imageChange *buildapi.ImageChangeTrigger) bool {
if imageChange == nil {
return false
}
if imageChange.From == nil {
return strategyTrigger != nil && strategyTrigger.From == trigger.From && strategyTrigger.Namespace == trigger.Namespace
}
namespace := imageChange.From.Namespace
if strategyTrigger != nil {
namespace = defaultNamespace(namespace, strategyTrigger.Namespace)
}
return imageChange.From.Name == trigger.From && namespace == trigger.Namespace
}
// filterBuildImageTriggers return only triggers that do not match the provided ImageChangeTrigger. strategyTrigger may be provided
// if set to remove a BuildTriggerPolicy without a From (which points to the strategy)
func filterBuildImageTriggers(src []buildapi.BuildTriggerPolicy, trigger ImageChangeTrigger, strategyTrigger *ImageChangeTrigger) []buildapi.BuildTriggerPolicy {
var dst []buildapi.BuildTriggerPolicy
for i := range src {
if triggerMatchesBuildImageChange(trigger, strategyTrigger, src[i].ImageChange) {
continue
}
dst = append(dst, src[i])
}
return dst
}
// filterDeploymentTriggers returns only triggers that do not have one of the provided types.
func filterDeploymentTriggers(src []appsapi.DeploymentTriggerPolicy, types ...appsapi.DeploymentTriggerType) []appsapi.DeploymentTriggerPolicy {
var dst []appsapi.DeploymentTriggerPolicy
Outer:
for i := range src {
for _, t := range types {
if t == src[i].Type {
continue Outer
}
}
dst = append(dst, src[i])
}
return dst
}
// strategyTrigger returns a synthetic ImageChangeTrigger that represents the image stream tag the build strategy
// points to, or nil if no such strategy trigger is possible (if the build doesn't point to an ImageStreamTag).
func strategyTrigger(config *buildapi.BuildConfig) *ImageChangeTrigger {
if from := buildapi.GetInputReference(config.Spec.Strategy); from != nil {
if from.Kind == "ImageStreamTag" {
// normalize the strategy object reference
from.Namespace = defaultNamespace(from.Namespace, config.Namespace)
return &ImageChangeTrigger{From: from.Name, Namespace: from.Namespace}
}
}
return nil
}
// mergeDeployTriggers returns an array of DeploymentTriggerPolicies that have no duplicates.
func mergeDeployTriggers(dst, src []appsapi.DeploymentTriggerPolicy) []appsapi.DeploymentTriggerPolicy {
// never return an empty map, because the triggers on a deployment config default when the map is empty
result := []appsapi.DeploymentTriggerPolicy{}
for _, current := range dst {
if findDeployTrigger(src, current) != -1 {
result = append(result, current)
}
}
for _, current := range src {
if findDeployTrigger(result, current) == -1 {
result = append(result, current)
}
}
return result
}
// findDeployTrigger finds the position of a deployment trigger in the provided array, or -1 if no such
// matching trigger is found.
func findDeployTrigger(dst []appsapi.DeploymentTriggerPolicy, trigger appsapi.DeploymentTriggerPolicy) int {
for i := range dst {
if reflect.DeepEqual(dst[i], trigger) {
return i
}
}
return -1
}
// mergeBuildTriggers returns an array of BuildTriggerPolicies that have no duplicates, in the same order
// as they exist in their original arrays (a zip-merge).
func mergeBuildTriggers(dst, src []buildapi.BuildTriggerPolicy) []buildapi.BuildTriggerPolicy {
var result []buildapi.BuildTriggerPolicy
for _, current := range dst {
if findBuildTrigger(src, current) != -1 {
result = append(result, current)
}
}
for _, current := range src {
if findBuildTrigger(result, current) == -1 {
result = append(result, current)
}
}
return result
}
// findBuildTrigger finds the equivalent build trigger position in the provided array, or -1 if
// no such build trigger exists. Equality only cares about the value of the From field.
func findBuildTrigger(dst []buildapi.BuildTriggerPolicy, trigger buildapi.BuildTriggerPolicy) int {
// make a copy for semantic equality
if trigger.ImageChange != nil {
trigger.ImageChange = &buildapi.ImageChangeTrigger{From: trigger.ImageChange.From}
}
for i, copied := range dst {
// make a copy for semantic equality
if copied.ImageChange != nil {
copied.ImageChange = &buildapi.ImageChangeTrigger{From: copied.ImageChange.From}
}
if reflect.DeepEqual(copied, trigger) {
return i
}
}
return -1
}
// UpdateTriggersForObject extracts a trigger definition from the provided object, passes it to fn, and
// then applies the trigger definition back on the object. It returns true if the object was mutated