@@ -8,11 +8,13 @@ import (
8
8
"testing"
9
9
"time"
10
10
11
+ "k8s.io/apimachinery/pkg/util/intstr"
12
+
13
+ "github.com/stretchr/testify/assert"
11
14
"github.com/undefinedlabs/go-mpatch"
12
15
13
16
"github.com/argoproj/argo-rollouts/utils/queue"
14
17
15
- "github.com/stretchr/testify/assert"
16
18
appsv1 "k8s.io/api/apps/v1"
17
19
corev1 "k8s.io/api/core/v1"
18
20
"k8s.io/apimachinery/pkg/api/equality"
@@ -70,6 +72,7 @@ type fixture struct {
70
72
analysisRunLister []* v1alpha1.AnalysisRun
71
73
analysisTemplateLister []* v1alpha1.AnalysisTemplate
72
74
clusterAnalysisTemplateLister []* v1alpha1.ClusterAnalysisTemplate
75
+ serviceLister []* corev1.Service
73
76
// Actions expected to happen on the client.
74
77
kubeactions []core.Action
75
78
actions []core.Action
@@ -102,6 +105,9 @@ func newFixture(t *testing.T, objects ...runtime.Object) *fixture {
102
105
case * appsv1.ReplicaSet :
103
106
f .kubeobjects = append (f .kubeobjects , obj )
104
107
f .replicaSetLister = append (f .replicaSetLister , obj .(* appsv1.ReplicaSet ))
108
+ case * corev1.Service :
109
+ f .kubeobjects = append (f .kubeobjects , obj )
110
+ f .serviceLister = append (f .serviceLister , obj .(* corev1.Service ))
105
111
}
106
112
}
107
113
f .client = fake .NewSimpleClientset (f .objects ... )
@@ -234,6 +240,32 @@ func newCondition(reason string, experiment *v1alpha1.Experiment) *v1alpha1.Expe
234
240
return nil
235
241
}
236
242
243
+ func templateToService (ex * v1alpha1.Experiment , template v1alpha1.TemplateSpec , replicaSet appsv1.ReplicaSet ) * corev1.Service {
244
+ if template .Service != nil {
245
+ service := & corev1.Service {
246
+ ObjectMeta : metav1.ObjectMeta {
247
+ Name : replicaSet .Name ,
248
+ Namespace : replicaSet .Namespace ,
249
+ Annotations : map [string ]string {
250
+ v1alpha1 .ExperimentNameAnnotationKey : ex .Name ,
251
+ v1alpha1 .ExperimentTemplateNameAnnotationKey : template .Name ,
252
+ },
253
+ OwnerReferences : []metav1.OwnerReference {* metav1 .NewControllerRef (ex , experimentKind )},
254
+ },
255
+ Spec : corev1.ServiceSpec {
256
+ Selector : replicaSet .Spec .Selector .MatchLabels ,
257
+ Ports : []corev1.ServicePort {{
258
+ Protocol : "TCP" ,
259
+ Port : int32 (80 ),
260
+ TargetPort : intstr .FromInt (8080 ),
261
+ }},
262
+ },
263
+ }
264
+ return service
265
+ }
266
+ return nil
267
+ }
268
+
237
269
func templateToRS (ex * v1alpha1.Experiment , template v1alpha1.TemplateSpec , availableReplicas int32 ) * appsv1.ReplicaSet {
238
270
rsLabels := map [string ]string {}
239
271
for k , v := range template .Selector .MatchLabels {
@@ -335,6 +367,7 @@ func (f *fixture) newController(resync resyncFunc) (*Controller, informers.Share
335
367
AnalysisRunInformer : i .Argoproj ().V1alpha1 ().AnalysisRuns (),
336
368
AnalysisTemplateInformer : i .Argoproj ().V1alpha1 ().AnalysisTemplates (),
337
369
ClusterAnalysisTemplateInformer : i .Argoproj ().V1alpha1 ().ClusterAnalysisTemplates (),
370
+ ServiceInformer : k8sI .Core ().V1 ().Services (),
338
371
ResyncPeriod : resync (),
339
372
RolloutWorkQueue : rolloutWorkqueue ,
340
373
ExperimentWorkQueue : experimentWorkqueue ,
@@ -371,6 +404,10 @@ func (f *fixture) newController(resync resyncFunc) (*Controller, informers.Share
371
404
k8sI .Apps ().V1 ().ReplicaSets ().Informer ().GetIndexer ().Add (r )
372
405
}
373
406
407
+ for _ , r := range f .serviceLister {
408
+ k8sI .Core ().V1 ().Services ().Informer ().GetIndexer ().Add (r )
409
+ }
410
+
374
411
for _ , r := range f .analysisRunLister {
375
412
i .Argoproj ().V1alpha1 ().AnalysisRuns ().Informer ().GetIndexer ().Add (r )
376
413
}
@@ -484,7 +521,9 @@ func filterInformerActions(actions []core.Action) []core.Action {
484
521
action .Matches ("list" , "clusteranalysistemplates" ) ||
485
522
action .Matches ("watch" , "clusteranalysistemplates" ) ||
486
523
action .Matches ("list" , "analysisruns" ) ||
487
- action .Matches ("watch" , "analysisruns" ) {
524
+ action .Matches ("watch" , "analysisruns" ) ||
525
+ action .Matches ("watch" , "services" ) ||
526
+ action .Matches ("list" , "services" ) {
488
527
continue
489
528
}
490
529
ret = append (ret , action )
@@ -493,6 +532,18 @@ func filterInformerActions(actions []core.Action) []core.Action {
493
532
return ret
494
533
}
495
534
535
+ func (f * fixture ) expectCreateServiceAction (service * corev1.Service ) int {
536
+ len := len (f .kubeactions )
537
+ f .kubeactions = append (f .kubeactions , core .NewCreateAction (schema.GroupVersionResource {Resource : "services" }, service .Namespace , service ))
538
+ return len
539
+ }
540
+
541
+ func (f * fixture ) expectDeleteServiceAction (service * corev1.Service ) int {
542
+ len := len (f .kubeactions )
543
+ f .kubeactions = append (f .kubeactions , core .NewDeleteAction (schema.GroupVersionResource {Resource : "services" }, service .Namespace , service .Name ))
544
+ return len
545
+ }
546
+
496
547
func (f * fixture ) expectCreateReplicaSetAction (r * appsv1.ReplicaSet ) int {
497
548
len := len (f .kubeactions )
498
549
f .kubeactions = append (f .kubeactions , core .NewCreateAction (schema.GroupVersionResource {Resource : "replicasets" }, r .Namespace , r ))
@@ -586,6 +637,27 @@ func (f *fixture) getCreatedReplicaSet(index int) *appsv1.ReplicaSet {
586
637
return rs
587
638
}
588
639
640
+ func (f * fixture ) verifyPatchedReplicaSetAddScaleDownDelay (index int , scaleDownDelaySeconds int32 ) {
641
+ action := filterInformerActions (f .kubeclient .Actions ())[index ]
642
+ patchAction , ok := action .(core.PatchAction )
643
+ if ! ok {
644
+ assert .Fail (f .t , "Expected Patch action, not %s" , action .GetVerb ())
645
+ }
646
+ now := metav1 .Now ().Add (time .Duration (scaleDownDelaySeconds ) * time .Second ).UTC ().Format (time .RFC3339 )
647
+ patch := fmt .Sprintf (addScaleDownAtAnnotationsPatch , v1alpha1 .DefaultReplicaSetScaleDownDeadlineAnnotationKey , now )
648
+ assert .Equal (f .t , string (patchAction .GetPatch ()), patch )
649
+ }
650
+
651
+ func (f * fixture ) verifyPatchedReplicaSetRemoveScaleDownDelayAnnotation (index int ) {
652
+ action := filterInformerActions (f .kubeclient .Actions ())[index ]
653
+ patchAction , ok := action .(core.PatchAction )
654
+ if ! ok {
655
+ assert .Fail (f .t , "Expected Patch action, not %s" , action .GetVerb ())
656
+ }
657
+ patch := fmt .Sprintf (removeScaleDownAtAnnotationsPatch , v1alpha1 .DefaultReplicaSetScaleDownDeadlineAnnotationKey )
658
+ assert .Equal (f .t , string (patchAction .GetPatch ()), patch )
659
+ }
660
+
589
661
func (f * fixture ) getUpdatedReplicaSet (index int ) * appsv1.ReplicaSet {
590
662
action := filterInformerActions (f .kubeclient .Actions ())[index ]
591
663
updateAction , ok := action .(core.UpdateAction )
0 commit comments