Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/ctl: add caller ID for pd-ctl #8214

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

const (
pdControlCallerID = "pd-ctl"
PDControlCallerID = "pd-ctl"
clusterPrefix = "pd/api/v1/cluster"
)

Expand Down Expand Up @@ -107,7 +107,7 @@ func initNewPDClient(cmd *cobra.Command, opts ...pd.ClientOption) error {
if PDCli != nil {
PDCli.Close()
}
PDCli = pd.NewClient(pdControlCallerID, getEndpoints(cmd), opts...)
PDCli = pd.NewClient(PDControlCallerID, getEndpoints(cmd), opts...).WithCallerID(PDControlCallerID)
return nil
}

Expand All @@ -122,7 +122,7 @@ func initNewPDClientWithTLS(cmd *cobra.Command, caPath, certPath, keyPath string

// TODO: replace dialClient with the PD HTTP client completely.
var dialClient = &http.Client{
Transport: apiutil.NewCallerIDRoundTripper(http.DefaultTransport, pdControlCallerID),
Transport: apiutil.NewCallerIDRoundTripper(http.DefaultTransport, PDControlCallerID),
}

// RequireHTTPSClient creates a HTTPS client if the related flags are set
Expand Down Expand Up @@ -153,7 +153,7 @@ func initHTTPSClient(caPath, certPath, keyPath string) error {
}
dialClient = &http.Client{
Transport: apiutil.NewCallerIDRoundTripper(
&http.Transport{TLSClientConfig: tlsConfig}, pdControlCallerID),
&http.Transport{TLSClientConfig: tlsConfig}, PDControlCallerID),
}
return nil
}
Expand Down
28 changes: 24 additions & 4 deletions tools/pd-ctl/tests/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,46 @@ package tests

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/assertutil"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/server"
cmd "github.com/tikv/pd/tools/pd-ctl/pdctl"
"github.com/tikv/pd/tools/pd-ctl/pdctl/command"
"go.uber.org/zap"
)

const pdControlCallerID = "pd-ctl"

func TestSendAndGetComponent(t *testing.T) {
re := require.New(t)
handler := func(context.Context, *server.Server) (http.Handler, apiutil.APIServiceGroup, error) {
mux := http.NewServeMux()
mux.HandleFunc("/pd/api/v1/cluster", func(w http.ResponseWriter, r *http.Request) {
Copy link
Member Author

@HuSharp HuSharp May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, only cluster command was replaced by PD HTTP SDK

callerID := apiutil.GetCallerIDOnHTTP(r)
for k := range r.Header {
log.Info("header", zap.String("key", k))
}
log.Info("caller id", zap.String("caller-id", callerID))
HuSharp marked this conversation as resolved.
Show resolved Hide resolved
re.Equal(command.PDControlCallerID, callerID)
cluster := &metapb.Cluster{Id: 1}
clusterBytes, err := json.Marshal(cluster)
re.NoError(err)
w.Write(clusterBytes)
})
mux.HandleFunc("/pd/api/v1/health", func(w http.ResponseWriter, r *http.Request) {
callerID := apiutil.GetCallerIDOnHTTP(r)
for k := range r.Header {
log.Info("header", zap.String("key", k))
}
log.Info("caller id", zap.String("caller-id", callerID))
re.Equal(pdControlCallerID, callerID)
re.Equal(command.PDControlCallerID, callerID)
fmt.Fprint(w, callerID)
})
info := apiutil.APIServiceGroup{
Expand All @@ -67,5 +80,12 @@ func TestSendAndGetComponent(t *testing.T) {
args := []string{"-u", pdAddr, "health"}
output, err := ExecuteCommand(cmd, args...)
re.NoError(err)
re.Equal(fmt.Sprintf("%s\n", pdControlCallerID), string(output))
re.Equal(fmt.Sprintf("%s\n", command.PDControlCallerID), string(output))

args = []string{"-u", pdAddr, "cluster"}
output, err = ExecuteCommand(cmd, args...)
re.NoError(err)
re.Equal(fmt.Sprintf("%s\n", `{
"id": 1
}`), string(output))
}