@@ -25,8 +25,6 @@ import (
2525 "github.com/apache/pulsar-client-go/pulsar"
2626 pulsaradminconfig "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config"
2727 "github.com/streamnative/pulsarctl/pkg/cmdutils"
28- "github.com/streamnative/streamnative-mcp-server/pkg/auth"
29- "github.com/streamnative/streamnative-mcp-server/pkg/auth/store"
3028)
3129
3230const (
@@ -49,51 +47,39 @@ type PulsarContext struct {
4947
5048// Session represents a Pulsar session
5149type Session struct {
52- Ctx PulsarContext
53- Client pulsar.Client
54- AdminClient cmdutils.Client
55- AdminV3Client cmdutils.Client
56- ClientOptions pulsar.ClientOptions
57- mutex sync.RWMutex
58- }
59-
60- func init () {
61- cmdutils .PulsarCtlConfig = & cmdutils.ClusterConfig {}
50+ Ctx PulsarContext
51+ Client pulsar.Client
52+ AdminClient cmdutils.Client
53+ AdminV3Client cmdutils.Client
54+ ClientOptions pulsar.ClientOptions
55+ PulsarCtlConfig * cmdutils.ClusterConfig
56+ mutex sync.RWMutex
6257}
6358
6459// NewSession creates a new Pulsar session with the given context
6560// This function dynamically constructs clients without relying on global state
66- func NewSession (ctx PulsarContext , issuer * auth. Issuer , tokenStore * store. Store ) (* Session , error ) {
61+ func NewSession (ctx PulsarContext ) (* Session , error ) {
6762 session := & Session {
6863 Ctx : ctx ,
6964 }
7065
71- if err := session .SetPulsarContext (); err != nil {
66+ if err := session .SetPulsarContext (ctx ); err != nil {
7267 return nil , fmt .Errorf ("failed to set pulsar context: %w" , err )
7368 }
7469
7570 return session , nil
7671}
7772
78- func (s * Session ) ChangeContext (ctx PulsarContext , issuer * auth. Issuer , tokenStore * store. Store ) error {
73+ func (s * Session ) SetPulsarContext (ctx PulsarContext ) error {
7974 s .mutex .Lock ()
8075 defer s .mutex .Unlock ()
81-
8276 s .Ctx = ctx
83-
84- return s .SetPulsarContext ()
85- }
86-
87- func (s * Session ) SetPulsarContext () error {
88- s .mutex .Lock ()
89- defer s .mutex .Unlock ()
90-
9177 pc := & s .Ctx
9278 var err error
9379 // Configure pulsarctl with the token
9480 switch {
9581 case pc .Token != "" :
96- cmdutils .PulsarCtlConfig = & cmdutils.ClusterConfig {
82+ s .PulsarCtlConfig = & cmdutils.ClusterConfig {
9783 WebServiceURL : pc .WebServiceURL ,
9884 AuthPlugin : "org.apache.pulsar.client.impl.auth.AuthenticationToken" ,
9985 AuthParams : fmt .Sprintf ("token:%s" , pc .Token ),
@@ -117,7 +103,7 @@ func (s *Session) SetPulsarContext() error {
117103 TLSKeyFilePath : pc .TLSKeyFile ,
118104 }
119105 case pc .AuthPlugin != "" && pc .AuthParams != "" :
120- cmdutils .PulsarCtlConfig = & cmdutils.ClusterConfig {
106+ s .PulsarCtlConfig = & cmdutils.ClusterConfig {
121107 WebServiceURL : pc .WebServiceURL ,
122108 AuthPlugin : pc .AuthPlugin ,
123109 AuthParams : pc .AuthParams ,
@@ -145,7 +131,7 @@ func (s *Session) SetPulsarContext() error {
145131 }
146132 default :
147133 // No authentication provided
148- cmdutils .PulsarCtlConfig = & cmdutils.ClusterConfig {
134+ s .PulsarCtlConfig = & cmdutils.ClusterConfig {
149135 WebServiceURL : pc .WebServiceURL ,
150136 TLSAllowInsecureConnection : pc .TLSAllowInsecureConnection ,
151137 TLSEnableHostnameVerification : pc .TLSEnableHostnameVerification ,
@@ -167,8 +153,8 @@ func (s *Session) SetPulsarContext() error {
167153 }
168154 }
169155
170- s .AdminClient = cmdutils . NewPulsarClient ( )
171- s .AdminV3Client = cmdutils . NewPulsarClientWithAPIVersion (pulsaradminconfig .V3 )
156+ s .AdminClient = s . PulsarCtlConfig . Client ( pulsaradminconfig . V2 )
157+ s .AdminV3Client = s . PulsarCtlConfig . Client (pulsaradminconfig .V3 )
172158
173159 s .Client , err = pulsar .NewClient (s .ClientOptions )
174160 if err != nil {
@@ -182,7 +168,7 @@ func (s *Session) GetAdminClient() (cmdutils.Client, error) {
182168 s .mutex .RLock ()
183169 defer s .mutex .RUnlock ()
184170
185- if cmdutils .PulsarCtlConfig .WebServiceURL == "" {
171+ if s .PulsarCtlConfig .WebServiceURL == "" {
186172 return nil , fmt .Errorf ("err: ContextNotSetErr: Please set the cluster context first" )
187173 }
188174 return s .AdminClient , nil
@@ -192,7 +178,7 @@ func (s *Session) GetAdminV3Client() (cmdutils.Client, error) {
192178 s .mutex .RLock ()
193179 defer s .mutex .RUnlock ()
194180
195- if cmdutils .PulsarCtlConfig .WebServiceURL == "" {
181+ if s .PulsarCtlConfig .WebServiceURL == "" {
196182 return nil , fmt .Errorf ("err: ContextNotSetErr: Please set the cluster context first" )
197183 }
198184 return s .AdminV3Client , nil
0 commit comments