From 1de43ec43425173d01eeac082e6c747cadcec214 Mon Sep 17 00:00:00 2001 From: crossoverJie <crossoverJie@gmail.com> Date: Thu, 28 Mar 2024 11:13:14 +0800 Subject: [PATCH 1/5] add HealthCheckWithTopicVersion api --- pulsaradmin/pkg/admin/brokers.go | 12 +++++++-- pulsaradmin/pkg/admin/brokers_test.go | 39 +++++++++++++++++++++++++++ pulsaradmin/pkg/utils/data.go | 11 ++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 pulsaradmin/pkg/admin/brokers_test.go diff --git a/pulsaradmin/pkg/admin/brokers.go b/pulsaradmin/pkg/admin/brokers.go index 79fcb092ef..84b1e9928a 100644 --- a/pulsaradmin/pkg/admin/brokers.go +++ b/pulsaradmin/pkg/admin/brokers.go @@ -53,8 +53,11 @@ type Brokers interface { // GetAllDynamicConfigurations returns values of all overridden dynamic-configs GetAllDynamicConfigurations() (map[string]string, error) - // HealthCheck run a health check on the broker + // Deprecated: Use HealthCheckWithTopicVersion instead HealthCheck() error + + // HealthCheckWithTopicVersion run a health check on the broker + HealthCheckWithTopicVersion(utils.TopicVersion) error } type broker struct { @@ -142,9 +145,14 @@ func (b *broker) GetAllDynamicConfigurations() (map[string]string, error) { } func (b *broker) HealthCheck() error { + return b.HealthCheckWithTopicVersion(utils.V1) +} +func (b *broker) HealthCheckWithTopicVersion(topicVersion utils.TopicVersion) error { endpoint := b.pulsar.endpoint(b.basePath, "/health") - buf, err := b.pulsar.Client.GetWithQueryParams(endpoint, nil, nil, false) + buf, err := b.pulsar.Client.GetWithQueryParams(endpoint, nil, map[string]string{ + "topicVersion": topicVersion.String(), + }, false) if err != nil { return err } diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go new file mode 100644 index 0000000000..56b6c83eb5 --- /dev/null +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package admin + +import ( + "testing" + + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" + "github.com/stretchr/testify/assert" +) + +func Test_broker_HealthCheckWithTopicVersion(t *testing.T) { + cfg := &config.Config{} + admin, err := New(cfg) + assert.NoError(t, err) + assert.NotNil(t, admin) + err = admin.Brokers().HealthCheck() + assert.NoError(t, err) + err = admin.Brokers().HealthCheckWithTopicVersion(utils.V1) + assert.NoError(t, err) + err = admin.Brokers().HealthCheckWithTopicVersion(utils.V2) + assert.NoError(t, err) +} diff --git a/pulsaradmin/pkg/utils/data.go b/pulsaradmin/pkg/utils/data.go index cc797d1892..408a35ecde 100644 --- a/pulsaradmin/pkg/utils/data.go +++ b/pulsaradmin/pkg/utils/data.go @@ -473,3 +473,14 @@ type GetStatsOptions struct { ExcludePublishers bool `json:"exclude_publishers"` ExcludeConsumers bool `json:"exclude_consumers"` } + +type TopicVersion string + +const ( + V1 TopicVersion = "V1" + V2 TopicVersion = "V2" +) + +func (t TopicVersion) String() string { + return string(t) +} From a551c669cf764becbf5169696e92dd5e027d8b51 Mon Sep 17 00:00:00 2001 From: crossoverJie <crossoverJie@gmail.com> Date: Thu, 28 Mar 2024 12:40:47 +0800 Subject: [PATCH 2/5] add HealthCheckWithTopicVersion api --- pulsaradmin/pkg/admin/brokers_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 56b6c83eb5..8b655e44de 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -20,6 +20,8 @@ package admin import ( "testing" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/auth" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/stretchr/testify/assert" @@ -27,7 +29,10 @@ import ( func Test_broker_HealthCheckWithTopicVersion(t *testing.T) { cfg := &config.Config{} - admin, err := New(cfg) + tokenAuth, err := auth.NewAuthenticationToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9."+ + "MKSR5Mb2wu_FQlMYACv2i4ubMCn4h4Dj_aIDo1dPsDk", nil) + assert.NoError(t, err) + admin, err := NewPulsarClientWithAuthProvider(cfg, tokenAuth) assert.NoError(t, err) assert.NotNil(t, admin) err = admin.Brokers().HealthCheck() From e67b3c55a8701c1eab0204e8a68acce6eee91b22 Mon Sep 17 00:00:00 2001 From: crossoverJie <crossoverJie@gmail.com> Date: Thu, 28 Mar 2024 13:01:26 +0800 Subject: [PATCH 3/5] add HealthCheckWithTopicVersion api --- pulsaradmin/pkg/admin/brokers_test.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 8b655e44de..329b5b04cf 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -20,19 +20,16 @@ package admin import ( "testing" - "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/auth" - "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/stretchr/testify/assert" ) func Test_broker_HealthCheckWithTopicVersion(t *testing.T) { - cfg := &config.Config{} - tokenAuth, err := auth.NewAuthenticationToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9."+ - "MKSR5Mb2wu_FQlMYACv2i4ubMCn4h4Dj_aIDo1dPsDk", nil) - assert.NoError(t, err) - admin, err := NewPulsarClientWithAuthProvider(cfg, tokenAuth) + cfg := &config.Config{ + Token: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.MKSR5Mb2wu_FQlMYACv2i4ubMCn4h4Dj_aIDo1dPsDk", + } + admin, err := New(cfg) assert.NoError(t, err) assert.NotNil(t, admin) err = admin.Brokers().HealthCheck() From 62b978b51ff72b11b159939a4e6f50d2c001fae5 Mon Sep 17 00:00:00 2001 From: crossoverJie <crossoverJie@gmail.com> Date: Thu, 28 Mar 2024 13:16:11 +0800 Subject: [PATCH 4/5] read from admin-token --- integration-tests/tokens/admin-token | 1 + pulsaradmin/pkg/admin/brokers_test.go | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 integration-tests/tokens/admin-token diff --git a/integration-tests/tokens/admin-token b/integration-tests/tokens/admin-token new file mode 100644 index 0000000000..67acef15fd --- /dev/null +++ b/integration-tests/tokens/admin-token @@ -0,0 +1 @@ +eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.MKSR5Mb2wu_FQlMYACv2i4ubMCn4h4Dj_aIDo1dPsDk \ No newline at end of file diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 329b5b04cf..0afc1b9586 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -18,6 +18,7 @@ package admin import ( + "os" "testing" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" @@ -25,9 +26,11 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_broker_HealthCheckWithTopicVersion(t *testing.T) { +func TestBrokerHealthCheckWithTopicVersion(t *testing.T) { + readFile, err := os.ReadFile("../../../integration-tests/tokens/admin-token") + assert.NoError(t, err) cfg := &config.Config{ - Token: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiJ9.MKSR5Mb2wu_FQlMYACv2i4ubMCn4h4Dj_aIDo1dPsDk", + Token: string(readFile), } admin, err := New(cfg) assert.NoError(t, err) From 996e400919adbff98f0b49636071093fa602182f Mon Sep 17 00:00:00 2001 From: crossoverJie <crossoverJie@gmail.com> Date: Fri, 29 Mar 2024 23:44:52 +0800 Subject: [PATCH 5/5] fix with cr --- pulsaradmin/pkg/admin/brokers.go | 2 +- pulsaradmin/pkg/admin/brokers_test.go | 4 ++-- pulsaradmin/pkg/utils/data.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pulsaradmin/pkg/admin/brokers.go b/pulsaradmin/pkg/admin/brokers.go index 84b1e9928a..e178610c30 100644 --- a/pulsaradmin/pkg/admin/brokers.go +++ b/pulsaradmin/pkg/admin/brokers.go @@ -145,7 +145,7 @@ func (b *broker) GetAllDynamicConfigurations() (map[string]string, error) { } func (b *broker) HealthCheck() error { - return b.HealthCheckWithTopicVersion(utils.V1) + return b.HealthCheckWithTopicVersion(utils.TopicVersionV1) } func (b *broker) HealthCheckWithTopicVersion(topicVersion utils.TopicVersion) error { endpoint := b.pulsar.endpoint(b.basePath, "/health") diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 0afc1b9586..d48ce7cbd4 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -37,8 +37,8 @@ func TestBrokerHealthCheckWithTopicVersion(t *testing.T) { assert.NotNil(t, admin) err = admin.Brokers().HealthCheck() assert.NoError(t, err) - err = admin.Brokers().HealthCheckWithTopicVersion(utils.V1) + err = admin.Brokers().HealthCheckWithTopicVersion(utils.TopicVersionV1) assert.NoError(t, err) - err = admin.Brokers().HealthCheckWithTopicVersion(utils.V2) + err = admin.Brokers().HealthCheckWithTopicVersion(utils.TopicVersionV2) assert.NoError(t, err) } diff --git a/pulsaradmin/pkg/utils/data.go b/pulsaradmin/pkg/utils/data.go index 408a35ecde..4363dd349e 100644 --- a/pulsaradmin/pkg/utils/data.go +++ b/pulsaradmin/pkg/utils/data.go @@ -477,8 +477,8 @@ type GetStatsOptions struct { type TopicVersion string const ( - V1 TopicVersion = "V1" - V2 TopicVersion = "V2" + TopicVersionV1 TopicVersion = "V1" + TopicVersionV2 TopicVersion = "V2" ) func (t TopicVersion) String() string {