Skip to content

Commit 7877368

Browse files
committed
CR comment - reuse existing ingressValue struct
1 parent 9f812b6 commit 7877368

File tree

3 files changed

+172
-177
lines changed

3 files changed

+172
-177
lines changed

pkg/dlx/handler_test.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/v3io/scaler/pkg/ingresscache"
13+
"github.com/v3io/scaler/pkg/kube"
1314
resourcescalerMock "github.com/v3io/scaler/pkg/resourcescaler/mock"
1415
"github.com/v3io/scaler/pkg/scalertypes"
1516

@@ -29,12 +30,6 @@ type HandlerTestSuite struct {
2930
backendPort int
3031
}
3132

32-
type ingressValue struct {
33-
host string
34-
path string
35-
targets []string
36-
}
37-
3833
func (suite *HandlerTestSuite) SetupSuite() {
3934
var err error
4035
suite.logger, err = nucliozap.NewNuclioZapTest("test")
@@ -83,7 +78,7 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
8378
for _, tc := range []struct {
8479
name string
8580
resolveServiceNameErr error
86-
initialCachedData *ingressValue
81+
initialCachedData *kube.IngressValue
8782
reqHeaders map[string]string
8883
reqHost string
8984
reqPath string
@@ -92,21 +87,21 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
9287
{
9388
name: "No ingress headers, host and path found in ingress cache",
9489
resolveServiceNameErr: nil,
95-
initialCachedData: &ingressValue{
96-
host: "www.example.com",
97-
path: "/test/path",
98-
targets: []string{"test-targets-name-1"},
90+
initialCachedData: &kube.IngressValue{
91+
Host: "www.example.com",
92+
Path: "/test/path",
93+
Targets: []string{"test-targets-name-1"},
9994
},
10095
reqHost: "www.example.com",
10196
reqPath: "/test/path",
10297
expectedStatus: http.StatusOK,
10398
}, {
10499
name: "No ingress headers,multiple targets found in ingress cache",
105100
resolveServiceNameErr: nil,
106-
initialCachedData: &ingressValue{
107-
host: "www.example.com",
108-
path: "/test/path/to/multiple",
109-
targets: []string{"test-targets-name-1", "test-targets-name-2"},
101+
initialCachedData: &kube.IngressValue{
102+
Host: "www.example.com",
103+
Path: "/test/path/to/multiple",
104+
Targets: []string{"test-targets-name-1", "test-targets-name-2"},
110105
},
111106
reqHost: "www.example.com",
112107
reqPath: "/test/path/to/multiple",
@@ -123,10 +118,10 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
123118
{
124119
name: "No ingress headers, scaler fails",
125120
resolveServiceNameErr: errors.New("fail"),
126-
initialCachedData: &ingressValue{
127-
host: "www.example.com",
128-
path: "/test/path",
129-
targets: []string{"test-targets-name-1"},
121+
initialCachedData: &kube.IngressValue{
122+
Host: "www.example.com",
123+
Path: "/test/path",
124+
Targets: []string{"test-targets-name-1"},
130125
},
131126
reqHost: "www.example.com",
132127
reqPath: "/test/path",
@@ -140,7 +135,7 @@ func (suite *HandlerTestSuite) TestHandleRequest() {
140135
suite.scaler.On("SetScaleCtx", mock.Anything, mock.Anything, mock.Anything).Return(nil)
141136
testIngressCache := ingresscache.NewIngressCache(suite.logger)
142137
if tc.initialCachedData != nil {
143-
err := testIngressCache.Set(tc.initialCachedData.host, tc.initialCachedData.path, tc.initialCachedData.targets)
138+
err := testIngressCache.Set(tc.initialCachedData.Host, tc.initialCachedData.Path, tc.initialCachedData.Targets)
144139
suite.Require().NoError(err)
145140
}
146141

@@ -162,7 +157,7 @@ func (suite *HandlerTestSuite) TestGetResourceNameAndPath() {
162157
for _, tc := range []struct {
163158
name string
164159
errMsg string
165-
initialCachedData *ingressValue
160+
initialCachedData *kube.IngressValue
166161
reqHeaders map[string]string
167162
reqHost string
168163
reqPath string
@@ -172,10 +167,10 @@ func (suite *HandlerTestSuite) TestGetResourceNameAndPath() {
172167
}{
173168
{
174169
name: "No ingress headers, host and path found in ingress cache",
175-
initialCachedData: &ingressValue{
176-
host: "www.example.com",
177-
path: "/test/path",
178-
targets: []string{"test-targets-name-1"},
170+
initialCachedData: &kube.IngressValue{
171+
Host: "www.example.com",
172+
Path: "/test/path",
173+
Targets: []string{"test-targets-name-1"},
179174
},
180175
reqHost: "www.example.com",
181176
reqPath: "/test/path",
@@ -203,7 +198,7 @@ func (suite *HandlerTestSuite) TestGetResourceNameAndPath() {
203198
// test case setup
204199
testIngressCache := ingresscache.NewIngressCache(suite.logger)
205200
if tc.initialCachedData != nil {
206-
err := testIngressCache.Set(tc.initialCachedData.host, tc.initialCachedData.path, tc.initialCachedData.targets)
201+
err := testIngressCache.Set(tc.initialCachedData.Host, tc.initialCachedData.Path, tc.initialCachedData.Targets)
207202
suite.Require().NoError(err)
208203
}
209204

pkg/kube/ingress.go

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import (
3535
"k8s.io/client-go/tools/cache"
3636
)
3737

38-
type ingressValue struct {
39-
name string
40-
host string
41-
path string
42-
targets []string
38+
type IngressValue struct {
39+
Name string
40+
Host string
41+
Path string
42+
Targets []string
4343
}
4444

4545
// IngressWatcher watches for changes in Kubernetes Ingress resources and updates the ingress cache accordingly
@@ -133,22 +133,22 @@ func (iw *IngressWatcher) AddHandler(obj interface{}) {
133133
return
134134
}
135135

136-
if err := iw.cache.Set(ingress.host, ingress.path, ingress.targets); err != nil {
136+
if err := iw.cache.Set(ingress.Host, ingress.Path, ingress.Targets); err != nil {
137137
iw.logger.WarnWith("Add ingress handler failure - failed to add the new value to ingress cache",
138138
"error", err.Error(),
139139
"object", obj,
140-
"ingressName", ingress.name,
141-
"host", ingress.host,
142-
"path", ingress.path,
143-
"targets", ingress.targets)
140+
"ingressName", ingress.Name,
141+
"host", ingress.Host,
142+
"path", ingress.Path,
143+
"targets", ingress.Targets)
144144
return
145145
}
146146

147147
iw.logger.DebugWith("Add ingress handler - successfully added ingress to cache",
148-
"ingressName", ingress.name,
149-
"host", ingress.host,
150-
"path", ingress.path,
151-
"targets", ingress.targets)
148+
"ingressName", ingress.Name,
149+
"host", ingress.Host,
150+
"path", ingress.Path,
151+
"targets", ingress.Targets)
152152
}
153153

154154
func (iw *IngressWatcher) UpdateHandler(oldObj, newObj interface{}) {
@@ -187,34 +187,34 @@ func (iw *IngressWatcher) UpdateHandler(oldObj, newObj interface{}) {
187187
}
188188

189189
// if the host or path has changed, we need to delete the old entry
190-
if oldIngress.host != newIngress.host || oldIngress.path != newIngress.path {
191-
if err := iw.cache.Delete(oldIngress.host, oldIngress.path, oldIngress.targets); err != nil {
190+
if oldIngress.Host != newIngress.Host || oldIngress.Path != newIngress.Path {
191+
if err := iw.cache.Delete(oldIngress.Host, oldIngress.Path, oldIngress.Targets); err != nil {
192192
iw.logger.WarnWith("Update ingress handler failure - failed to delete old ingress",
193193
"error", err.Error(),
194-
"ingressName", oldIngress.name,
194+
"ingressName", oldIngress.Name,
195195
"object", oldObj,
196-
"host", oldIngress.host,
197-
"path", oldIngress.path,
198-
"targets", oldIngress.targets)
196+
"host", oldIngress.Host,
197+
"path", oldIngress.Path,
198+
"targets", oldIngress.Targets)
199199
}
200200
}
201201

202-
if err := iw.cache.Set(newIngress.host, newIngress.path, newIngress.targets); err != nil {
202+
if err := iw.cache.Set(newIngress.Host, newIngress.Path, newIngress.Targets); err != nil {
203203
iw.logger.WarnWith("Update ingress handler failure - failed to add the new value",
204204
"error", err.Error(),
205205
"object", newObj,
206-
"ingressName", newIngress.name,
207-
"host", newIngress.host,
208-
"path", newIngress.path,
209-
"targets", newIngress.targets)
206+
"ingressName", newIngress.Name,
207+
"host", newIngress.Host,
208+
"path", newIngress.Path,
209+
"targets", newIngress.Targets)
210210
return
211211
}
212212

213213
iw.logger.DebugWith("Update ingress handler - successfully updated ingress in cache",
214-
"ingressName", newIngress.name,
215-
"host", newIngress.host,
216-
"path", newIngress.path,
217-
"targets", newIngress.targets)
214+
"ingressName", newIngress.Name,
215+
"host", newIngress.Host,
216+
"path", newIngress.Path,
217+
"targets", newIngress.Targets)
218218
}
219219

220220
func (iw *IngressWatcher) DeleteHandler(obj interface{}) {
@@ -225,28 +225,28 @@ func (iw *IngressWatcher) DeleteHandler(obj interface{}) {
225225
return
226226
}
227227

228-
if err := iw.cache.Delete(ingress.host, ingress.path, ingress.targets); err != nil {
228+
if err := iw.cache.Delete(ingress.Host, ingress.Path, ingress.Targets); err != nil {
229229
iw.logger.WarnWith("Delete ingress handler failure - failed delete from cache",
230230
"error", err.Error(),
231231
"object", obj,
232-
"ingressName", ingress.name,
233-
"host", ingress.host,
234-
"path", ingress.path,
235-
"targets", ingress.targets)
232+
"ingressName", ingress.Name,
233+
"host", ingress.Host,
234+
"path", ingress.Path,
235+
"targets", ingress.Targets)
236236
return
237237
}
238238

239239
iw.logger.DebugWith("Delete ingress handler - successfully deleted ingress from cache",
240-
"ingressName", ingress.name,
241-
"host", ingress.host,
242-
"path", ingress.path,
243-
"targets", ingress.targets)
240+
"ingressName", ingress.Name,
241+
"host", ingress.Host,
242+
"path", ingress.Path,
243+
"targets", ingress.Targets)
244244
}
245245

246246
// --- internal methods ---
247247

248248
// extractValuesFromIngressResource extracts the relevant values from a Kubernetes Ingress resource
249-
func (iw *IngressWatcher) extractValuesFromIngressResource(obj interface{}) (*ingressValue, error) {
249+
func (iw *IngressWatcher) extractValuesFromIngressResource(obj interface{}) (*IngressValue, error) {
250250
ingress, ok := obj.(*networkingv1.Ingress)
251251
if !ok {
252252
return nil, errors.New("Failed to cast object to Ingress")
@@ -271,11 +271,11 @@ func (iw *IngressWatcher) extractValuesFromIngressResource(obj interface{}) (*in
271271
return nil, errors.Wrap(err, "Failed to extract path from ingress")
272272
}
273273

274-
return &ingressValue{
275-
host: host,
276-
path: path,
277-
targets: targets,
278-
name: ingress.Name,
274+
return &IngressValue{
275+
Host: host,
276+
Path: path,
277+
Targets: targets,
278+
Name: ingress.Name,
279279
}, nil
280280
}
281281

0 commit comments

Comments
 (0)