forked from knative/serving
/
functional.go
968 lines (843 loc) · 30.8 KB
/
functional.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
/*
Copyright 2018 The Knative Authors
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 testing
import (
"fmt"
"time"
"github.com/knative/pkg/apis"
"github.com/knative/pkg/apis/duck"
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
"github.com/knative/serving/pkg/apis/autoscaling"
autoscalingv1alpha1 "github.com/knative/serving/pkg/apis/autoscaling/v1alpha1"
"github.com/knative/serving/pkg/apis/networking"
netv1alpha1 "github.com/knative/serving/pkg/apis/networking/v1alpha1"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
routenames "github.com/knative/serving/pkg/reconciler/v1alpha1/route/resources/names"
"github.com/knative/serving/pkg/reconciler/v1alpha1/route/traffic"
servicenames "github.com/knative/serving/pkg/reconciler/v1alpha1/service/resources/names"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)
// BuildOption enables further configuration of a Build.
type BuildOption func(*unstructured.Unstructured)
// WithSucceededTrue updates the status of the provided unstructured Build object with the
// expected success condition.
func WithSucceededTrue(orig *unstructured.Unstructured) {
cp := orig.DeepCopy()
cp.Object["status"] = map[string]interface{}{"conditions": []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionTrue,
}}}
duck.FromUnstructured(cp, orig) // prevent panic in b.DeepCopy()
}
// WithSucceededUnknown updates the status of the provided unstructured Build object with the
// expected in-flight condition.
func WithSucceededUnknown(reason, message string) BuildOption {
return func(orig *unstructured.Unstructured) {
cp := orig.DeepCopy()
cp.Object["status"] = map[string]interface{}{"conditions": []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: reason,
Message: message,
}}}
duck.FromUnstructured(cp, orig) // prevent panic in b.DeepCopy()
}
}
// WithSucceededFalse updates the status of the provided unstructured Build object with the
// expected failure condition.
func WithSucceededFalse(reason, message string) BuildOption {
return func(orig *unstructured.Unstructured) {
cp := orig.DeepCopy()
cp.Object["status"] = map[string]interface{}{"conditions": []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
}}}
duck.FromUnstructured(cp, orig) // prevent panic in b.DeepCopy()
}
}
// ServiceOption enables further configuration of a Service.
type ServiceOption func(*v1alpha1.Service)
var (
// configSpec is the spec used for the different styles of Service rollout.
configSpec = v1alpha1.ConfigurationSpec{
RevisionTemplate: v1alpha1.RevisionTemplateSpec{
Spec: v1alpha1.RevisionSpec{
Container: corev1.Container{
Image: "busybox",
},
TimeoutSeconds: 60,
},
},
}
)
// WithServiceDeletionTimestamp will set the DeletionTimestamp on the Service.
func WithServiceDeletionTimestamp(r *v1alpha1.Service) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithRunLatestRollout configures the Service to use a "runLatest" rollout.
func WithRunLatestRollout(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
RunLatest: &v1alpha1.RunLatestType{
Configuration: configSpec,
},
}
}
// WithRunLatestConfigSpec confgures the Service to use a "runLastest" configuration
func WithRunLatestConfigSpec(config v1alpha1.ConfigurationSpec) ServiceOption {
return func(svc *v1alpha1.Service) {
svc.Spec = v1alpha1.ServiceSpec{
RunLatest: &v1alpha1.RunLatestType{
Configuration: config,
},
}
}
}
// WithServiceLabel attaches a particular label to the service.
func WithServiceLabel(key, value string) ServiceOption {
return func(service *v1alpha1.Service) {
if service.Labels == nil {
service.Labels = make(map[string]string)
}
service.Labels[key] = value
}
}
// WithResourceRequirements attaches resource requirements to the service
func WithResourceRequirements(resourceRequirements corev1.ResourceRequirements) ServiceOption {
return func(svc *v1alpha1.Service) {
svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Resources = resourceRequirements
}
}
// WithVolume adds a volume to the service
func WithVolume(name, mountPath string, volumeSource corev1.VolumeSource) ServiceOption {
return func(svc *v1alpha1.Service) {
rt := &svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec
rt.Container.VolumeMounts = []corev1.VolumeMount{{
Name: name,
MountPath: mountPath,
}}
rt.Volumes = []corev1.Volume{{
Name: name,
VolumeSource: volumeSource,
}}
}
}
// WithConfigAnnotations assigns config annotations to a service
func WithConfigAnnotations(annotations map[string]string) ServiceOption {
return func(service *v1alpha1.Service) {
service.Spec.RunLatest.Configuration.RevisionTemplate.ObjectMeta.Annotations = annotations
}
}
// WithRevisionTimeoutSeconds sets revision timeout
func WithRevisionTimeoutSeconds(revisionTimeoutSeconds int64) ServiceOption {
return func(service *v1alpha1.Service) {
service.Spec.RunLatest.Configuration.RevisionTemplate.Spec.TimeoutSeconds = revisionTimeoutSeconds
}
}
// MarkConfigurationNotOwned calls the function of the same name on the Service's status.
func MarkConfigurationNotOwned(service *v1alpha1.Service) {
service.Status.MarkConfigurationNotOwned(servicenames.Configuration(service))
}
// MarkRouteNotOwned calls the function of the same name on the Service's status.
func MarkRouteNotOwned(service *v1alpha1.Service) {
service.Status.MarkRouteNotOwned(servicenames.Route(service))
}
// WithPinnedRollout configures the Service to use a "pinned" rollout,
// which is pinned to the named revision.
// Deprecated, since PinnedType is deprecated.
func WithPinnedRollout(name string) ServiceOption {
return WithPinnedRolloutConfigSpec(name, configSpec)
}
// WithPinnedRolloutConfigSpec WithPinnedRollout2
func WithPinnedRolloutConfigSpec(name string, config v1alpha1.ConfigurationSpec) ServiceOption {
return func(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
DeprecatedPinned: &v1alpha1.PinnedType{
RevisionName: name,
Configuration: config,
},
}
}
}
// WithReleaseRolloutAndPercentage configures the Service to use a "release" rollout,
// which spans the provided revisions.
func WithReleaseRolloutAndPercentage(percentage int, names ...string) ServiceOption {
return WithReleaseRolloutAndPercentageConfigSpec(percentage, configSpec, names...)
}
// WithReleaseRolloutAndPercentageConfigSpec configures the Service to use a "release" rollout,
// which spans the provided revisions.
func WithReleaseRolloutAndPercentageConfigSpec(percentage int, config v1alpha1.ConfigurationSpec, names ...string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
Release: &v1alpha1.ReleaseType{
Revisions: names,
RolloutPercent: percentage,
Configuration: config,
},
}
}
}
// WithReleaseRolloutConfigSpec configures the Service to use a "release" rollout,
// which spans the provided revisions.
func WithReleaseRolloutConfigSpec(config v1alpha1.ConfigurationSpec, names ...string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
Release: &v1alpha1.ReleaseType{
Revisions: names,
Configuration: config,
},
}
}
}
// WithReleaseRollout configures the Service to use a "release" rollout,
// which spans the provided revisions.
func WithReleaseRollout(names ...string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
Release: &v1alpha1.ReleaseType{
Revisions: names,
Configuration: configSpec,
},
}
}
}
// WithManualRollout configures the Service to use a "manual" rollout.
func WithManualRollout(s *v1alpha1.Service) {
s.Spec = v1alpha1.ServiceSpec{
Manual: &v1alpha1.ManualType{},
}
}
// WithInitSvcConditions initializes the Service's conditions.
func WithInitSvcConditions(s *v1alpha1.Service) {
s.Status.InitializeConditions()
}
// WithManualStatus configures the Service to have the appropriate
// status for a "manual" rollout type.
func WithManualStatus(s *v1alpha1.Service) {
s.Status.SetManualStatus()
}
// WithReadyRoute reflects the Route's readiness in the Service resource.
func WithReadyRoute(s *v1alpha1.Service) {
s.Status.PropagateRouteStatus(&v1alpha1.RouteStatus{
Status: duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: "Ready",
Status: "True",
}},
},
})
}
// WithSvcStatusDomain propagates the domain name to the status of the Service.
func WithSvcStatusDomain(s *v1alpha1.Service) {
n, ns := s.GetName(), s.GetNamespace()
s.Status.Domain = fmt.Sprintf("%s.%s.example.com", n, ns)
s.Status.DeprecatedDomainInternal = fmt.Sprintf("%s.%s.svc.cluster.local", n, ns)
}
// WithSvcStatusAddress updates the service's status with the address.
func WithSvcStatusAddress(s *v1alpha1.Service) {
s.Status.Address = &duckv1alpha1.Addressable{
Hostname: fmt.Sprintf("%s.%s.svc.cluster.local", s.Name, s.Namespace),
}
}
// WithSvcStatusTraffic sets the Service's status traffic block to the specified traffic targets.
func WithSvcStatusTraffic(targets ...v1alpha1.TrafficTarget) ServiceOption {
return func(r *v1alpha1.Service) {
// Automatically inject URL into TrafficTarget status
for _, tt := range targets {
if tt.Name != "" {
tt.URL = traffic.SubrouteURL(traffic.HttpScheme,
tt.Name,
"example.com")
}
}
r.Status.Traffic = targets
}
}
// WithFailedRoute reflects a Route's failure in the Service resource.
func WithFailedRoute(reason, message string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Status.PropagateRouteStatus(&v1alpha1.RouteStatus{
Status: duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: "Ready",
Status: "False",
Reason: reason,
Message: message,
}},
},
})
}
}
// WithReadyConfig reflects the Configuration's readiness in the Service
// resource. This must coincide with the setting of Latest{Created,Ready}
// to the provided revision name.
func WithReadyConfig(name string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Status.PropagateConfigurationStatus(&v1alpha1.ConfigurationStatus{
Status: duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: "Ready",
Status: "True",
}},
},
ConfigurationStatusFields: v1alpha1.ConfigurationStatusFields{
LatestCreatedRevisionName: name,
LatestReadyRevisionName: name,
},
})
}
}
// WithFailedConfig reflects the Configuration's failure in the Service
// resource. The failing revision's name is reflected in LatestCreated.
func WithFailedConfig(name, reason, message string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Status.PropagateConfigurationStatus(&v1alpha1.ConfigurationStatus{
Status: duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: "Ready",
Status: "False",
Reason: reason,
Message: fmt.Sprintf("Revision %q failed with message: %s.",
name, message),
}},
},
ConfigurationStatusFields: v1alpha1.ConfigurationStatusFields{
LatestCreatedRevisionName: name,
},
})
}
}
// WithServiceLatestReadyRevision sets the latest ready revision on the Service's status.
func WithServiceLatestReadyRevision(lrr string) ServiceOption {
return func(s *v1alpha1.Service) {
s.Status.LatestReadyRevisionName = lrr
}
}
// WithServiceStatusRouteNotReady sets the `RoutesReady` condition on the service to `Unknown`.
func WithServiceStatusRouteNotReady(s *v1alpha1.Service) {
s.Status.MarkRouteNotYetReady()
}
// RouteOption enables further configuration of a Route.
type RouteOption func(*v1alpha1.Route)
// WithSpecTraffic sets the Route's traffic block to the specified traffic targets.
func WithSpecTraffic(traffic ...v1alpha1.TrafficTarget) RouteOption {
return func(r *v1alpha1.Route) {
r.Spec.Traffic = traffic
}
}
// WithRouteUID sets the Route's UID
func WithRouteUID(uid types.UID) RouteOption {
return func(r *v1alpha1.Route) {
r.ObjectMeta.UID = uid
}
}
// WithRouteDeletionTimestamp will set the DeletionTimestamp on the Route.
func WithRouteDeletionTimestamp(r *v1alpha1.Route) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithRouteFinalizer adds the Route finalizer to the Route.
func WithRouteFinalizer(r *v1alpha1.Route) {
r.ObjectMeta.Finalizers = append(r.ObjectMeta.Finalizers, "routes.serving.knative.dev")
}
// WithAnotherRouteFinalizer adds a non-Route finalizer to the Route.
func WithAnotherRouteFinalizer(r *v1alpha1.Route) {
r.ObjectMeta.Finalizers = append(r.ObjectMeta.Finalizers, "another.serving.knative.dev")
}
// WithConfigTarget sets the Route's traffic block to point at a particular Configuration.
func WithConfigTarget(config string) RouteOption {
return WithSpecTraffic(v1alpha1.TrafficTarget{
ConfigurationName: config,
Percent: 100,
})
}
// WithRevTarget sets the Route's traffic block to point at a particular Revision.
func WithRevTarget(revision string) RouteOption {
return WithSpecTraffic(v1alpha1.TrafficTarget{
RevisionName: revision,
Percent: 100,
})
}
// WithStatusTraffic sets the Route's status traffic block to the specified traffic targets.
func WithStatusTraffic(traffic ...v1alpha1.TrafficTarget) RouteOption {
return func(r *v1alpha1.Route) {
r.Status.Traffic = traffic
}
}
// WithRouteOwnersRemoved clears the owner references of this Route.
func WithRouteOwnersRemoved(r *v1alpha1.Route) {
r.OwnerReferences = nil
}
// MarkServiceNotOwned calls the function of the same name on the Service's status.
func MarkServiceNotOwned(r *v1alpha1.Route) {
r.Status.MarkServiceNotOwned(routenames.K8sService(r))
}
// WithDomain sets the .Status.Domain field to the prototypical domain.
func WithDomain(r *v1alpha1.Route) {
r.Status.Domain = fmt.Sprintf("%s.%s.example.com", r.Name, r.Namespace)
}
// WithDomainInternal sets the .Status.DomainInternal field to the prototypical internal domain.
func WithDomainInternal(r *v1alpha1.Route) {
r.Status.DeprecatedDomainInternal = fmt.Sprintf("%s.%s.svc.cluster.local", r.Name, r.Namespace)
}
// WithAddress sets the .Status.Address field to the prototypical internal hostname.
func WithAddress(r *v1alpha1.Route) {
r.Status.Address = &duckv1alpha1.Addressable{
Hostname: fmt.Sprintf("%s.%s.svc.cluster.local", r.Name, r.Namespace),
}
}
// WithAnotherDomain sets the .Status.Domain field to an atypical domain.
func WithAnotherDomain(r *v1alpha1.Route) {
r.Status.Domain = fmt.Sprintf("%s.%s.another-example.com", r.Name, r.Namespace)
}
// WithLocalDomain sets the .Status.Domain field to use `svc.cluster.local` suffix.
func WithLocalDomain(r *v1alpha1.Route) {
r.Status.Domain = fmt.Sprintf("%s.%s.svc.cluster.local", r.Name, r.Namespace)
}
// WithInitRouteConditions initializes the Service's conditions.
func WithInitRouteConditions(rt *v1alpha1.Route) {
rt.Status.InitializeConditions()
}
// MarkTrafficAssigned calls the method of the same name on .Status
func MarkTrafficAssigned(r *v1alpha1.Route) {
r.Status.MarkTrafficAssigned()
}
// MarkIngressReady propagates a Ready=True ClusterIngress status to the Route.
func MarkIngressReady(r *v1alpha1.Route) {
r.Status.PropagateClusterIngressStatus(netv1alpha1.IngressStatus{
Status: duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: "Ready",
Status: "True",
}},
},
})
}
// MarkMissingTrafficTarget calls the method of the same name on .Status
func MarkMissingTrafficTarget(kind, revision string) RouteOption {
return func(r *v1alpha1.Route) {
r.Status.MarkMissingTrafficTarget(kind, revision)
}
}
// MarkConfigurationNotReady calls the method of the same name on .Status
func MarkConfigurationNotReady(name string) RouteOption {
return func(r *v1alpha1.Route) {
r.Status.MarkConfigurationNotReady(name)
}
}
// MarkConfigurationFailed calls the method of the same name on .Status
func MarkConfigurationFailed(name string) RouteOption {
return func(r *v1alpha1.Route) {
r.Status.MarkConfigurationFailed(name)
}
}
// WithRouteLabel sets the specified label on the Route.
func WithRouteLabel(key, value string) RouteOption {
return func(r *v1alpha1.Route) {
if r.Labels == nil {
r.Labels = make(map[string]string)
}
r.Labels[key] = value
}
}
// WithIngressClass sets the ingress class annotation on the Route.
func WithIngressClass(ingressClass string) RouteOption {
return func(r *v1alpha1.Route) {
if r.Annotations == nil {
r.Annotations = make(map[string]string)
}
r.Annotations[networking.IngressClassAnnotationKey] = ingressClass
}
}
// ConfigOption enables further configuration of a Configuration.
type ConfigOption func(*v1alpha1.Configuration)
// WithConfigDeletionTimestamp will set the DeletionTimestamp on the Config.
func WithConfigDeletionTimestamp(r *v1alpha1.Configuration) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithBuild adds a Build to the provided Configuration.
func WithBuild(cfg *v1alpha1.Configuration) {
cfg.Spec.Build = &v1alpha1.RawExtension{
Object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "testing.build.knative.dev/v1alpha1",
"kind": "Build",
"spec": map[string]interface{}{
"steps": []interface{}{
map[string]interface{}{
"image": "foo",
},
map[string]interface{}{
"image": "bar",
},
},
},
},
},
}
}
// WithConfigOwnersRemoved clears the owner references of this Configuration.
func WithConfigOwnersRemoved(cfg *v1alpha1.Configuration) {
cfg.OwnerReferences = nil
}
// WithConfigConcurrencyModel sets the given Configuration's concurrency model.
func WithConfigConcurrencyModel(ss v1alpha1.RevisionRequestConcurrencyModelType) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Spec.RevisionTemplate.Spec.DeprecatedConcurrencyModel = ss
}
}
// WithGeneration sets the generation of the Configuration.
func WithGeneration(gen int64) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Generation = gen
//TODO(dprotaso) remove this for 0.4 release
cfg.Spec.DeprecatedGeneration = gen
}
}
// WithObservedGen sets the observed generation of the Configuration.
func WithObservedGen(cfg *v1alpha1.Configuration) {
cfg.Status.ObservedGeneration = cfg.Generation
}
// WithCreatedAndReady sets the latest{Created,Ready}RevisionName on the Configuration.
func WithCreatedAndReady(created, ready string) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Status.SetLatestCreatedRevisionName(created)
cfg.Status.SetLatestReadyRevisionName(ready)
}
}
// WithLatestCreated initializes the .status.latestCreatedRevisionName to be the name
// of the latest revision that the Configuration would have created.
func WithLatestCreated(name string) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Status.SetLatestCreatedRevisionName(name)
}
}
// WithLatestReady initializes the .status.latestReadyRevisionName to be the name
// of the latest revision that the Configuration would have created.
func WithLatestReady(name string) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Status.SetLatestReadyRevisionName(name)
}
}
// MarkRevisionCreationFailed calls .Status.MarkRevisionCreationFailed.
func MarkRevisionCreationFailed(msg string) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Status.MarkRevisionCreationFailed(msg)
}
}
// MarkLatestCreatedFailed calls .Status.MarkLatestCreatedFailed.
func MarkLatestCreatedFailed(msg string) ConfigOption {
return func(cfg *v1alpha1.Configuration) {
cfg.Status.MarkLatestCreatedFailed(cfg.Status.LatestCreatedRevisionName, msg)
}
}
// WithConfigLabel attaches a particular label to the configuration.
func WithConfigLabel(key, value string) ConfigOption {
return func(config *v1alpha1.Configuration) {
if config.Labels == nil {
config.Labels = make(map[string]string)
}
config.Labels[key] = value
}
}
// RevisionOption enables further configuration of a Revision.
type RevisionOption func(*v1alpha1.Revision)
// WithRevisionDeletionTimestamp will set the DeletionTimestamp on the Revision.
func WithRevisionDeletionTimestamp(r *v1alpha1.Revision) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithInitRevConditions calls .Status.InitializeConditions() on a Revision.
func WithInitRevConditions(r *v1alpha1.Revision) {
r.Status.InitializeConditions()
}
// WithRevName sets the name of the revision
func WithRevName(name string) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.Name = name
}
}
// WithBuildRef sets the .Spec.BuildRef on the Revision to match what we'd get
// using WithBuild(name).
func WithBuildRef(name string) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.Spec.BuildRef = &corev1.ObjectReference{
APIVersion: "testing.build.knative.dev/v1alpha1",
Kind: "Build",
Name: name,
}
}
}
// MarkResourceNotOwned calls the function of the same name on the Revision's status.
func MarkResourceNotOwned(kind, name string) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.Status.MarkResourceNotOwned(kind, name)
}
}
// WithRevConcurrencyModel sets the concurrency model on the Revision.
func WithRevConcurrencyModel(ss v1alpha1.RevisionRequestConcurrencyModelType) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.Spec.DeprecatedConcurrencyModel = ss
}
}
// WithLogURL sets the .Status.LogURL to the expected value.
func WithLogURL(r *v1alpha1.Revision) {
r.Status.LogURL = "http://logger.io/test-uid"
}
// WithCreationTimestamp sets the Revision's timestamp to the provided time.
// TODO(mattmoor): Ideally this could be a more generic Option and use meta.Accessor,
// but unfortunately Go's type system cannot support that.
func WithCreationTimestamp(t time.Time) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.ObjectMeta.CreationTimestamp = metav1.Time{Time: t}
}
}
// WithNoBuild updates the status conditions to propagate a Build status as-if
// no BuildRef was specified.
func WithNoBuild(r *v1alpha1.Revision) {
r.Status.PropagateBuildStatus(duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "NoBuild",
}},
})
}
// WithOngoingBuild propagates the status of an in-progress Build to the Revision's status.
func WithOngoingBuild(r *v1alpha1.Revision) {
r.Status.PropagateBuildStatus(duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionUnknown,
}},
})
}
// WithSuccessfulBuild propagates the status of a successful Build to the Revision's status.
func WithSuccessfulBuild(r *v1alpha1.Revision) {
r.Status.PropagateBuildStatus(duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionTrue,
}},
})
}
// WithFailedBuild propagates the status of a failed Build to the Revision's status.
func WithFailedBuild(reason, message string) RevisionOption {
return func(r *v1alpha1.Revision) {
r.Status.PropagateBuildStatus(duckv1alpha1.Status{
Conditions: []duckv1alpha1.Condition{{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
}},
})
}
}
// WithEmptyLTTs clears the LastTransitionTime fields on all of the conditions of the
// provided Revision.
func WithEmptyLTTs(r *v1alpha1.Revision) {
conds := r.Status.Conditions
for i, c := range conds {
// The LTT defaults and is long enough ago that we expire waiting
// on the Endpoints to become ready.
c.LastTransitionTime = apis.VolatileTime{}
conds[i] = c
}
r.Status.Conditions = conds
}
// WithLastPinned updates the "last pinned" annotation to the provided timestamp.
func WithLastPinned(t time.Time) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.SetLastPinned(t)
}
}
// WithRevStatus is a generic escape hatch for creating hard-to-craft
// status orientations.
func WithRevStatus(st v1alpha1.RevisionStatus) RevisionOption {
return func(rev *v1alpha1.Revision) {
rev.Status = st
}
}
// MarkActive calls .Status.MarkActive on the Revision.
func MarkActive(r *v1alpha1.Revision) {
r.Status.MarkActive()
}
// MarkInactive calls .Status.MarkInactive on the Revision.
func MarkInactive(reason, message string) RevisionOption {
return func(r *v1alpha1.Revision) {
r.Status.MarkInactive(reason, message)
}
}
// MarkActivating calls .Status.MarkActivating on the Revision.
func MarkActivating(reason, message string) RevisionOption {
return func(r *v1alpha1.Revision) {
r.Status.MarkActivating(reason, message)
}
}
// MarkDeploying calls .Status.MarkDeploying on the Revision.
func MarkDeploying(reason string) RevisionOption {
return func(r *v1alpha1.Revision) {
r.Status.MarkDeploying(reason)
}
}
// MarkProgressDeadlineExceeded calls the method of the same name on the Revision
// with the message we expect the Revision Reconciler to pass.
func MarkProgressDeadlineExceeded(r *v1alpha1.Revision) {
r.Status.MarkProgressDeadlineExceeded("Unable to create pods for more than 120 seconds.")
}
// MarkServiceTimeout calls .Status.MarkServiceTimeout on the Revision.
func MarkServiceTimeout(r *v1alpha1.Revision) {
r.Status.MarkServiceTimeout()
}
// MarkContainerMissing calls .Status.MarkContainerMissing on the Revision.
func MarkContainerMissing(rev *v1alpha1.Revision) {
rev.Status.MarkContainerMissing("It's the end of the world as we know it")
}
// MarkContainerExiting calls .Status.MarkContainerExiting on the Revision.
func MarkContainerExiting(exitCode int32, message string) RevisionOption {
return func(r *v1alpha1.Revision) {
r.Status.MarkContainerExiting(exitCode, message)
}
}
// MarkRevisionReady calls the necessary helpers to make the Revision Ready=True.
func MarkRevisionReady(r *v1alpha1.Revision) {
WithInitRevConditions(r)
WithNoBuild(r)
MarkActive(r)
r.Status.MarkResourcesAvailable()
r.Status.MarkContainerHealthy()
}
type PodAutoscalerOption func(*autoscalingv1alpha1.PodAutoscaler)
// WithPodAutoscalerOwnersRemoved clears the owner references of this PodAutoscaler.
func WithPodAutoscalerOwnersRemoved(r *autoscalingv1alpha1.PodAutoscaler) {
r.OwnerReferences = nil
}
// WithTraffic updates the PA to reflect it receiving traffic.
func WithTraffic(pa *autoscalingv1alpha1.PodAutoscaler) {
pa.Status.MarkActive()
}
// WithBufferedTraffic updates the PA to reflect that it has received
// and buffered traffic while it is being activated.
func WithBufferedTraffic(reason, message string) PodAutoscalerOption {
return func(pa *autoscalingv1alpha1.PodAutoscaler) {
pa.Status.MarkActivating(reason, message)
}
}
// WithNoTraffic updates the PA to reflect the fact that it is not
// receiving traffic.
func WithNoTraffic(reason, message string) PodAutoscalerOption {
return func(pa *autoscalingv1alpha1.PodAutoscaler) {
pa.Status.MarkInactive(reason, message)
}
}
// WithPADeletionTimestamp will set the DeletionTimestamp on the PodAutoscaler.
func WithPADeletionTimestamp(r *autoscalingv1alpha1.PodAutoscaler) {
t := metav1.NewTime(time.Unix(1e9, 0))
r.ObjectMeta.SetDeletionTimestamp(&t)
}
// WithHPAClass updates the PA to add the hpa class annotation.
func WithHPAClass(pa *autoscalingv1alpha1.PodAutoscaler) {
if pa.Annotations == nil {
pa.Annotations = make(map[string]string)
}
pa.Annotations[autoscaling.ClassAnnotationKey] = autoscaling.HPA
}
// WithKPAClass updates the PA to add the kpa class annotation.
func WithKPAClass(pa *autoscalingv1alpha1.PodAutoscaler) {
if pa.Annotations == nil {
pa.Annotations = make(map[string]string)
}
pa.Annotations[autoscaling.ClassAnnotationKey] = autoscaling.KPA
}
// WithContainerConcurrency returns a PodAutoscalerOption which sets
// the PodAutoscaler containerConcurrency to the provided value.
func WithContainerConcurrency(cc int32) PodAutoscalerOption {
return func(pa *autoscalingv1alpha1.PodAutoscaler) {
pa.Spec.ContainerConcurrency = v1alpha1.RevisionContainerConcurrencyType(cc)
}
}
// WithTargetAnnotation returns a PodAutoscalerOption which sets
// the PodAutoscaler autoscaling.knative.dev/target to the provided
// value.
func WithTargetAnnotation(target string) PodAutoscalerOption {
return func(pa *autoscalingv1alpha1.PodAutoscaler) {
if pa.Annotations == nil {
pa.Annotations = make(map[string]string)
}
pa.Annotations[autoscaling.TargetAnnotationKey] = target
}
}
// WithMetricAnnotation adds a metric annotation to the PA.
func WithMetricAnnotation(metric string) PodAutoscalerOption {
return func(pa *autoscalingv1alpha1.PodAutoscaler) {
if pa.Annotations == nil {
pa.Annotations = make(map[string]string)
}
pa.Annotations[autoscaling.MetricAnnotationKey] = metric
}
}
// K8sServiceOption enables further configuration of the Kubernetes Service.
type K8sServiceOption func(*corev1.Service)
// MutateK8sService changes the service in a way that must be reconciled.
func MutateK8sService(svc *corev1.Service) {
// An effective hammer ;-P
svc.Spec = corev1.ServiceSpec{}
}
func WithClusterIP(ip string) K8sServiceOption {
return func(svc *corev1.Service) {
svc.Spec.ClusterIP = ip
}
}
func WithExternalName(name string) K8sServiceOption {
return func(svc *corev1.Service) {
svc.Spec.ExternalName = name
}
}
// WithK8sSvcOwnersRemoved clears the owner references of this Route.
func WithK8sSvcOwnersRemoved(svc *corev1.Service) {
svc.OwnerReferences = nil
}
// EndpointsOption enables further configuration of the Kubernetes Endpoints.
type EndpointsOption func(*corev1.Endpoints)
// WithSubsets adds subsets to the body of a Revision, enabling us to refer readiness.
func WithSubsets(ep *corev1.Endpoints) {
ep.Subsets = []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "127.0.0.1"}},
}}
}
// PodOption enables further configuration of a Pod.
type PodOption func(*corev1.Pod)
// WithFailingContainer sets the .Status.ContainerStatuses on the pod to
// include a container named accordingly to fail with the given state.
func WithFailingContainer(name string, exitCode int, message string) PodOption {
return func(pod *corev1.Pod) {
pod.Status.ContainerStatuses = []corev1.ContainerStatus{
{
Name: name,
LastTerminationState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: int32(exitCode),
Message: message,
},
},
},
}
}
}