From 0dfc2e572bcf672de5de7f75409064f3a459185b Mon Sep 17 00:00:00 2001 From: Camille Dela Rosa Date: Wed, 11 Dec 2024 14:55:56 +1100 Subject: [PATCH] Fix nats-jetstream scaler to use account ID instead of name Signed-off-by: Camille Dela Rosa --- CHANGELOG.md | 1 + pkg/scalers/nats_jetstream_scaler.go | 5 +++-- pkg/scalers/nats_jetstream_scaler_test.go | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e85235b4e1..43521d4e50c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -139,6 +139,7 @@ New deprecation(s): - **AWS SQS Scaler**: Improve error handling for SQS queue metrics ([#6178](https://github.com/kedacore/keda/issues/6178)) - **Azure Event Hub Scaler**: Checkpointer errors are correctly handled ([#6084](https://github.com/kedacore/keda/issues/6084)) - **Metrics API Scaler**: Prometheus metrics can have multiple labels ([#6077](https://github.com/kedacore/keda/issues/6077)) +- **JetStream**: NATS API uses account ID and not account name to filter by account ### Deprecations diff --git a/pkg/scalers/nats_jetstream_scaler.go b/pkg/scalers/nats_jetstream_scaler.go index 47a0bbe1f36..d4fa6943ef2 100644 --- a/pkg/scalers/nats_jetstream_scaler.go +++ b/pkg/scalers/nats_jetstream_scaler.go @@ -64,6 +64,7 @@ type jetStreamCluster struct { } type accountDetail struct { + Id string `json:id` Name string `json:"name"` Streams []*streamDetail `json:"stream_detail"` } @@ -278,7 +279,7 @@ func (s *natsJetStreamScaler) getNATSJetstreamMonitoringData(ctx context.Context } for _, jetStreamAccount := range jetStreamAccountResp.Accounts { - if jetStreamAccount.Name == s.metadata.account { + if jetStreamAccount.Id == s.metadata.account { for _, stream := range jetStreamAccount.Streams { if stream.Name == s.metadata.stream { for _, consumer := range stream.Consumers { @@ -305,7 +306,7 @@ func (s *natsJetStreamScaler) setNATSJetStreamMonitoringData(jetStreamAccountRes // find and assign the stream that we are looking for. for _, jsAccount := range jetStreamAccountResp.Accounts { - if jsAccount.Name == s.metadata.account { + if jsAccount.Id == s.metadata.account { for _, stream := range jsAccount.Streams { if stream.Name == s.metadata.stream { s.stream = stream diff --git a/pkg/scalers/nats_jetstream_scaler_test.go b/pkg/scalers/nats_jetstream_scaler_test.go index c77811011f7..3ba765896e2 100644 --- a/pkg/scalers/nats_jetstream_scaler_test.go +++ b/pkg/scalers/nats_jetstream_scaler_test.go @@ -131,7 +131,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ 0, "s0-nats-jetstream-mystream", }, &jetStreamEndpointResponse{ - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystream", Consumers: []consumerDetail{{Name: "pull_consumer"}}, }}, @@ -145,7 +145,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ 0, "s0-nats-jetstream-mystream", }, &jetStreamEndpointResponse{ - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystream", Consumers: []consumerDetail{{Name: "pull_consumer", NumPending: 100}}, }}, @@ -159,7 +159,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ 0, "s0-nats-jetstream-mystream", }, &jetStreamEndpointResponse{ - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystream", State: streamState{LastSequence: 1}, Consumers: []consumerDetail{{Name: "pull_consumer_bad", NumPending: 100}}, }}, @@ -173,7 +173,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ 0, "s0-nats-jetstream-mystream", }, &jetStreamEndpointResponse{ - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystreamBad", State: streamState{LastSequence: 1}, Consumers: []consumerDetail{{Name: "pull_consumer", NumPending: 100}}, }}, @@ -187,7 +187,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ 0, "s0-nats-jetstream-mystream", }, &jetStreamEndpointResponse{ - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystream", Consumers: []consumerDetail{{Name: "pull_consumer", NumPending: 100}}, }}, @@ -202,7 +202,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ }, &jetStreamEndpointResponse{ MetaCluster: metaCluster{ClusterSize: 3}, - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystream", Consumers: []consumerDetail{{Name: "pull_consumer", NumPending: 100, Cluster: consumerCluster{Leader: "leader"}}}, }}, @@ -217,7 +217,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ }, &jetStreamEndpointResponse{ MetaCluster: metaCluster{ClusterSize: 3}, - Accounts: []accountDetail{{Name: "$G", + Accounts: []accountDetail{{Name: "$G", Id: "$G", Streams: []*streamDetail{{Name: "mystream"}}, }}, }, false, true},