Skip to content

Commit 45a887e

Browse files
committed
Update scaler
Signed-off-by: Jorge Turrado <[email protected]>
1 parent 79b1816 commit 45a887e

File tree

5 files changed

+70
-40
lines changed

5 files changed

+70
-40
lines changed

pkg/k8s/endpoints_cache.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package k8s
2+
3+
import (
4+
"encoding/json"
5+
6+
v1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/watch"
8+
)
9+
10+
// EndpointsCache is a simple cache of endpoints.
11+
// It allows callers to quickly get given endpoints in a given
12+
// namespace, or watch for changes to specific endpoints, all
13+
// without incurring the cost of issuing a network request
14+
// to the Kubernetes API
15+
type EndpointsCache interface {
16+
json.Marshaler
17+
// Get gets the endpoints with the given name
18+
// in the given namespace from the cache.
19+
//
20+
// If the endpoints doesn't exist in the cache, it
21+
// will be requested from the backing store (most commonly
22+
// the Kubernetes API server)
23+
Get(namespace, name string) (v1.Endpoints, error)
24+
// Watch opens a watch stream for the endpoints with
25+
// the given name in the given namespace from the cache.
26+
//
27+
// If the endpoints don't exist in the cache, it
28+
// will be requested from the backing store (most commonly
29+
// the Kubernetes API server)
30+
Watch(namespace, name string) (watch.Interface, error)
31+
}

scaler/handlers_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ func TestGetMetricSpecTable(t *testing.T) {
346346
},
347347
Spec: httpv1alpha1.HTTPScaledObjectSpec{
348348
ScaleTargetRef: httpv1alpha1.ScaleTargetRef{
349-
Deployment: "testdepl",
350-
Service: "testsrv",
351-
Port: 8080,
349+
Name: "testdepl",
350+
Service: "testsrv",
351+
Port: 8080,
352352
},
353353
TargetPendingRequests: ptr.To[int32](123),
354354
},
@@ -387,9 +387,9 @@ func TestGetMetricSpecTable(t *testing.T) {
387387
"validHost2",
388388
},
389389
ScaleTargetRef: httpv1alpha1.ScaleTargetRef{
390-
Deployment: "testdepl",
391-
Service: "testsrv",
392-
Port: 8080,
390+
Name: "testdepl",
391+
Service: "testsrv",
392+
Port: 8080,
393393
},
394394
TargetPendingRequests: ptr.To[int32](123),
395395
},

scaler/main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
)
3434

3535
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;list;watch
36-
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch
3736
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects,verbs=get;list;watch
3837

3938
func main() {
@@ -74,8 +73,8 @@ func main() {
7473
os.Exit(1)
7574
}
7675

77-
// create the deployment informer
78-
deployInformer := k8s.NewInformerBackedDeploymentCache(
76+
// create the endpoints informer
77+
endpInformer := k8s.NewInformerBackedEndpointsCache(
7978
lggr,
8079
k8sCl,
8180
cfg.DeploymentCacheRsyncPeriod,
@@ -94,11 +93,11 @@ func main() {
9493

9594
eg, ctx := errgroup.WithContext(ctx)
9695

97-
// start the deployment informer
96+
// start the endpoints informer
9897
eg.Go(func() error {
99-
lggr.Info("starting the deployment informer")
98+
lggr.Info("starting the endpoints informer")
10099

101-
deployInformer.Start(ctx)
100+
endpInformer.Start(ctx)
102101
return nil
103102
})
104103

@@ -113,7 +112,7 @@ func main() {
113112
eg.Go(func() error {
114113
lggr.Info("starting the queue pinger")
115114

116-
if err := pinger.start(ctx, time.NewTicker(cfg.QueueTickDuration), deployInformer); !util.IsIgnoredErr(err) {
115+
if err := pinger.start(ctx, time.NewTicker(cfg.QueueTickDuration), endpInformer); !util.IsIgnoredErr(err) {
117116
lggr.Error(err, "queue pinger failed")
118117
return err
119118
}

scaler/queue_pinger.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ import (
3434
// // context
3535
// go pinger.start(ctx, ticker)
3636
type queuePinger struct {
37-
getEndpointsFn k8s.GetEndpointsFunc
38-
interceptorNS string
39-
interceptorSvcName string
40-
interceptorDeplName string
41-
adminPort string
42-
pingMut *sync.RWMutex
43-
lastPingTime time.Time
44-
allCounts map[string]int
45-
aggregateCount int
46-
lggr logr.Logger
37+
getEndpointsFn k8s.GetEndpointsFunc
38+
interceptorNS string
39+
interceptorSvcName string
40+
interceptorServiceName string
41+
adminPort string
42+
pingMut *sync.RWMutex
43+
lastPingTime time.Time
44+
allCounts map[string]int
45+
aggregateCount int
46+
lggr logr.Logger
4747
}
4848

4949
func newQueuePinger(
@@ -57,15 +57,15 @@ func newQueuePinger(
5757
) (*queuePinger, error) {
5858
pingMut := new(sync.RWMutex)
5959
pinger := &queuePinger{
60-
getEndpointsFn: getEndpointsFn,
61-
interceptorNS: ns,
62-
interceptorSvcName: svcName,
63-
interceptorDeplName: deplName,
64-
adminPort: adminPort,
65-
pingMut: pingMut,
66-
lggr: lggr,
67-
allCounts: map[string]int{},
68-
aggregateCount: 0,
60+
getEndpointsFn: getEndpointsFn,
61+
interceptorNS: ns,
62+
interceptorSvcName: svcName,
63+
interceptorServiceName: deplName,
64+
adminPort: adminPort,
65+
pingMut: pingMut,
66+
lggr: lggr,
67+
allCounts: map[string]int{},
68+
aggregateCount: 0,
6969
}
7070
return pinger, pinger.fetchAndSaveCounts(ctx)
7171
}
@@ -74,14 +74,14 @@ func newQueuePinger(
7474
func (q *queuePinger) start(
7575
ctx context.Context,
7676
ticker *time.Ticker,
77-
deplCache k8s.DeploymentCache,
77+
endpCache k8s.EndpointsCache,
7878
) error {
79-
deployWatchIface, err := deplCache.Watch(q.interceptorNS, q.interceptorDeplName)
79+
endpoWatchIface, err := endpCache.Watch(q.interceptorNS, q.interceptorServiceName)
8080
if err != nil {
8181
return err
8282
}
83-
deployEvtChan := deployWatchIface.ResultChan()
84-
defer deployWatchIface.Stop()
83+
endpEvtChan := endpoWatchIface.ResultChan()
84+
defer endpoWatchIface.Stop()
8585

8686
lggr := q.lggr.WithName("scaler.queuePinger.start")
8787
defer ticker.Stop()
@@ -102,13 +102,13 @@ func (q *queuePinger) start(
102102
return fmt.Errorf("error getting request counts: %w", err)
103103
}
104104
// handle changes to the interceptor fleet
105-
// Deployment
106-
case <-deployEvtChan:
105+
// Endpoints
106+
case <-endpEvtChan:
107107
err := q.fetchAndSaveCounts(ctx)
108108
if err != nil {
109109
lggr.Error(
110110
err,
111-
"getting request counts after interceptor deployment event",
111+
"getting request counts after interceptor endpoints event",
112112
)
113113
}
114114
}

scaler/queue_pinger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestCounts(t *testing.T) {
7373
r.NoError(q.Resize("host3", 3))
7474
r.NoError(q.Resize("host4", 4))
7575
ticker := time.NewTicker(tickDur)
76-
fakeCache := k8s.NewFakeDeploymentCache()
76+
fakeCache := k8s.NewFakeEndpointsCache()
7777
go func() {
7878
_ = pinger.start(ctx, ticker, fakeCache)
7979
}()

0 commit comments

Comments
 (0)