@@ -8,79 +8,63 @@ import (
8
8
"github.com/go-logr/logr"
9
9
"github.com/stretchr/testify/require"
10
10
"golang.org/x/sync/errgroup"
11
- appsv1 "k8s.io/api/apps/v1"
12
- corev1 "k8s.io/api/core/v1"
11
+ v1 "k8s.io/api/core/v1"
13
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
13
"k8s.io/apimachinery/pkg/watch"
15
14
16
15
"github.com/kedacore/http-add-on/pkg/k8s"
17
16
)
18
17
19
18
// Test to make sure the wait function returns a nil error if there is immediately
20
- // one replica on the target deployment
19
+ // one active endpoint on the target deployment
21
20
func TestForwardWaitFuncOneReplica (t * testing.T ) {
22
21
ctx := context .Background ()
23
22
24
23
const waitFuncWait = 1 * time .Second
25
24
r := require .New (t )
26
25
const ns = "testNS"
27
- const deployName = "TestForwardingHandlerDeploy"
28
- cache := k8s .NewFakeDeploymentCache ()
29
- cache .AddDeployment (* newDeployment (
30
- ns ,
31
- deployName ,
32
- "myimage" ,
33
- []int32 {123 },
34
- nil ,
35
- map [string ]string {},
36
- corev1 .PullAlways ,
37
- ))
26
+ const endpointsName = "TestForwardingHandler"
27
+ endpoints := * newEndpoint (ns , endpointsName )
28
+ cache := k8s .NewFakeEndpointsCache ()
29
+ cache .Set (endpoints )
30
+ cache .SetSubsets (ns , endpointsName , 1 )
38
31
39
32
ctx , done := context .WithTimeout (ctx , waitFuncWait )
40
33
defer done ()
41
34
group , ctx := errgroup .WithContext (ctx )
42
35
43
- waitFunc := newDeployReplicasForwardWaitFunc (
36
+ waitFunc := newWorkloadReplicasForwardWaitFunc (
44
37
logr .Discard (),
45
38
cache ,
46
39
)
47
40
48
41
group .Go (func () error {
49
- _ , err := waitFunc (ctx , ns , deployName )
42
+ _ , err := waitFunc (ctx , ns , endpointsName )
50
43
return err
51
44
})
52
45
r .NoError (group .Wait (), "wait function failed, but it shouldn't have" )
53
46
}
54
47
55
- // Test to make sure the wait function returns an error if there are no replicas , and that doesn't change
48
+ // Test to make sure the wait function returns an error if there are active endpoints , and that doesn't change
56
49
// within a timeout
57
50
func TestForwardWaitFuncNoReplicas (t * testing.T ) {
58
51
ctx := context .Background ()
59
52
const waitFuncWait = 1 * time .Second
60
53
r := require .New (t )
61
54
const ns = "testNS"
62
- const deployName = "TestForwardingHandlerHoldsDeployment"
63
- deployment := newDeployment (
64
- ns ,
65
- deployName ,
66
- "myimage" ,
67
- []int32 {123 },
68
- nil ,
69
- map [string ]string {},
70
- corev1 .PullAlways ,
71
- )
72
- deployment .Status .ReadyReplicas = 0
73
- cache := k8s .NewFakeDeploymentCache ()
74
- cache .AddDeployment (* deployment )
55
+ const endpointsName = "TestForwardWaitFuncNoReplicas"
56
+ endpoints := * newEndpoint (ns , endpointsName )
57
+ cache := k8s .NewFakeEndpointsCache ()
58
+ cache .Set (endpoints )
75
59
76
60
ctx , done := context .WithTimeout (ctx , waitFuncWait )
77
61
defer done ()
78
- waitFunc := newDeployReplicasForwardWaitFunc (
62
+ waitFunc := newWorkloadReplicasForwardWaitFunc (
79
63
logr .Discard (),
80
64
cache ,
81
65
)
82
66
83
- _ , err := waitFunc (ctx , ns , deployName )
67
+ _ , err := waitFunc (ctx , ns , endpointsName )
84
68
r .Error (err )
85
69
}
86
70
@@ -90,100 +74,58 @@ func TestWaitFuncWaitsUntilReplicas(t *testing.T) {
90
74
totalWaitDur := 500 * time .Millisecond
91
75
92
76
const ns = "testNS"
93
- const deployName = "TestForwardingHandlerHoldsDeployment"
94
- deployment := newDeployment (
95
- ns ,
96
- deployName ,
97
- "myimage" ,
98
- []int32 {123 },
99
- nil ,
100
- map [string ]string {},
101
- corev1 .PullAlways ,
102
- )
103
- deployment .Spec .Replicas = k8s .Int32P (0 )
104
- cache := k8s .NewFakeDeploymentCache ()
105
- cache .AddDeployment (* deployment )
77
+ const endpointsName = "TestForwardingHandlerHolds"
78
+
79
+ endpoints := * newEndpoint (ns , endpointsName )
80
+ cache := k8s .NewFakeEndpointsCache ()
81
+ cache .Set (endpoints )
106
82
// create a watcher first so that the goroutine
107
83
// can later fetch it and send a message on it
108
- _ , err := cache .Watch (ns , deployName )
84
+ _ , err := cache .Watch (ns , endpointsName )
109
85
r .NoError (err )
110
86
111
87
ctx , done := context .WithTimeout (ctx , totalWaitDur )
112
- waitFunc := newDeployReplicasForwardWaitFunc (
88
+ waitFunc := newWorkloadReplicasForwardWaitFunc (
113
89
logr .Discard (),
114
90
cache ,
115
91
)
116
92
117
- // this channel will be closed immediately after the replicas were increased
118
- replicasIncreasedCh := make (chan struct {})
93
+ // this channel will be closed immediately after the active endpoints were increased
94
+ activeEndpointsIncreasedCh := make (chan struct {})
119
95
go func () {
120
96
time .Sleep (totalWaitDur / 2 )
121
- watcher := cache .GetWatcher (ns , deployName )
97
+ watcher := cache .GetWatcher (ns , endpointsName )
122
98
r .NotNil (watcher , "watcher was not found" )
123
- modifiedDeployment := deployment .DeepCopy ()
124
- modifiedDeployment .Spec .Replicas = k8s .Int32P (1 )
125
- watcher .Action (watch .Modified , modifiedDeployment )
126
- close (replicasIncreasedCh )
99
+ modifiedEndpoints := endpoints .DeepCopy ()
100
+ modifiedEndpoints .Subsets = []v1.EndpointSubset {
101
+ {
102
+ Addresses : []v1.EndpointAddress {
103
+ {IP : "1.2.3.4" },
104
+ },
105
+ },
106
+ }
107
+ watcher .Action (watch .Modified , modifiedEndpoints )
108
+ close (activeEndpointsIncreasedCh )
127
109
}()
128
- _ , err = waitFunc (ctx , ns , deployName )
110
+ _ , err = waitFunc (ctx , ns , endpointsName )
129
111
r .NoError (err )
130
112
done ()
131
113
}
132
114
133
- // newDeployment creates a new deployment object
115
+ // newEndpoint creates a new endpoints object
134
116
// with the given name and the given image. This does not actually create
135
- // the deployment in the cluster, it just creates the deployment object
117
+ // the endpoints in the cluster, it just creates the endpoints object
136
118
// in memory
137
- func newDeployment (
119
+ func newEndpoint (
138
120
namespace ,
139
- name ,
140
- image string ,
141
- ports []int32 ,
142
- env []corev1.EnvVar ,
143
- labels map [string ]string ,
144
- pullPolicy corev1.PullPolicy ,
145
- ) * appsv1.Deployment {
146
- containerPorts := make ([]corev1.ContainerPort , len (ports ))
147
- for i , port := range ports {
148
- containerPorts [i ] = corev1.ContainerPort {
149
- ContainerPort : port ,
150
- }
151
- }
152
- deployment := & appsv1.Deployment {
153
- TypeMeta : metav1.TypeMeta {
154
- Kind : "Deployment" ,
155
- },
121
+ name string ,
122
+ ) * v1.Endpoints {
123
+ endpoints := & v1.Endpoints {
156
124
ObjectMeta : metav1.ObjectMeta {
157
125
Name : name ,
158
126
Namespace : namespace ,
159
- Labels : labels ,
160
- },
161
- Spec : appsv1.DeploymentSpec {
162
- Selector : & metav1.LabelSelector {
163
- MatchLabels : labels ,
164
- },
165
- Replicas : k8s .Int32P (1 ),
166
- Template : corev1.PodTemplateSpec {
167
- ObjectMeta : metav1.ObjectMeta {
168
- Labels : labels ,
169
- },
170
- Spec : corev1.PodSpec {
171
- Containers : []corev1.Container {
172
- {
173
- Image : image ,
174
- Name : name ,
175
- ImagePullPolicy : pullPolicy ,
176
- Ports : containerPorts ,
177
- Env : env ,
178
- },
179
- },
180
- },
181
- },
182
- },
183
- Status : appsv1.DeploymentStatus {
184
- ReadyReplicas : 1 ,
185
127
},
186
128
}
187
129
188
- return deployment
130
+ return endpoints
189
131
}
0 commit comments