@@ -11,9 +11,9 @@ import (
1111 "slices"
1212 "sync"
1313
14- "github.com/nginx/agent/v3/internal/model "
14+ "github.com/nginx/agent/v3/internal/command "
1515
16- "github.com/nginx/agent/v3/internal/grpc "
16+ "github.com/nginx/agent/v3/internal/model "
1717
1818 "github.com/nginx/agent/v3/internal/watcher/credentials"
1919
@@ -40,12 +40,14 @@ type (
4040 instanceWatcherService instanceWatcherServiceInterface
4141 healthWatcherService * health.HealthWatcherService
4242 fileWatcherService * file.FileWatcherService
43- credentialWatcherService credentialWatcherServiceInterface
43+ commandCredentialWatcherService credentialWatcherServiceInterface
44+ auxiliaryCredentialWatcherService credentialWatcherServiceInterface
4445 instanceUpdatesChannel chan instance.InstanceUpdatesMessage
4546 nginxConfigContextChannel chan instance.NginxConfigContextMessage
4647 instanceHealthChannel chan health.InstanceHealthMessage
4748 fileUpdatesChannel chan file.FileUpdateMessage
48- credentialUpdatesChannel chan credentials.CredentialUpdateMessage
49+ commandCredentialUpdatesChannel chan credentials.CredentialUpdateMessage
50+ auxiliaryCredentialUpdatesChannel chan credentials.CredentialUpdateMessage
4951 cancel context.CancelFunc
5052 instancesWithConfigApplyInProgress []string
5153 watcherMutex sync.Mutex
@@ -78,12 +80,14 @@ func NewWatcher(agentConfig *config.Config) *Watcher {
7880 instanceWatcherService : instance .NewInstanceWatcherService (agentConfig ),
7981 healthWatcherService : health .NewHealthWatcherService (agentConfig ),
8082 fileWatcherService : file .NewFileWatcherService (agentConfig ),
81- credentialWatcherService : credentials .NewCredentialWatcherService (agentConfig ),
83+ commandCredentialWatcherService : credentials .NewCredentialWatcherService (agentConfig , command .Command ),
84+ auxiliaryCredentialWatcherService : credentials .NewCredentialWatcherService (agentConfig , command .Auxiliary ),
8285 instanceUpdatesChannel : make (chan instance.InstanceUpdatesMessage ),
8386 nginxConfigContextChannel : make (chan instance.NginxConfigContextMessage ),
8487 instanceHealthChannel : make (chan health.InstanceHealthMessage ),
8588 fileUpdatesChannel : make (chan file.FileUpdateMessage ),
86- credentialUpdatesChannel : make (chan credentials.CredentialUpdateMessage ),
89+ commandCredentialUpdatesChannel : make (chan credentials.CredentialUpdateMessage ),
90+ auxiliaryCredentialUpdatesChannel : make (chan credentials.CredentialUpdateMessage ),
8791 instancesWithConfigApplyInProgress : []string {},
8892 watcherMutex : sync.Mutex {},
8993 }
@@ -100,7 +104,11 @@ func (w *Watcher) Init(ctx context.Context, messagePipe bus.MessagePipeInterface
100104
101105 go w .instanceWatcherService .Watch (watcherContext , w .instanceUpdatesChannel , w .nginxConfigContextChannel )
102106 go w .healthWatcherService .Watch (watcherContext , w .instanceHealthChannel )
103- go w .credentialWatcherService .Watch (watcherContext , w .credentialUpdatesChannel )
107+ go w .commandCredentialWatcherService .Watch (watcherContext , w .commandCredentialUpdatesChannel )
108+
109+ if w .agentConfig .AuxiliaryCommand != nil {
110+ go w .auxiliaryCredentialWatcherService .Watch (watcherContext , w .auxiliaryCredentialUpdatesChannel )
111+ }
104112
105113 if w .agentConfig .IsFeatureEnabled (pkgConfig .FeatureFileWatcher ) {
106114 go w .fileWatcherService .Watch (watcherContext , w .fileUpdatesChannel )
@@ -132,8 +140,6 @@ func (*Watcher) Info() *bus.Info {
132140
133141func (w * Watcher ) Process (ctx context.Context , msg * bus.Message ) {
134142 switch msg .Topic {
135- case bus .CredentialUpdatedTopic :
136- w .handleCredentialUpdate (ctx )
137143 case bus .ConfigApplyRequestTopic :
138144 w .handleConfigApplyRequest (ctx , msg )
139145 case bus .ConfigApplySuccessfulTopic :
@@ -149,7 +155,6 @@ func (w *Watcher) Process(ctx context.Context, msg *bus.Message) {
149155
150156func (* Watcher ) Subscriptions () []string {
151157 return []string {
152- bus .CredentialUpdatedTopic ,
153158 bus .ConfigApplyRequestTopic ,
154159 bus .ConfigApplySuccessfulTopic ,
155160 bus .ConfigApplyCompleteTopic ,
@@ -248,34 +253,29 @@ func (w *Watcher) handleConfigApplyComplete(ctx context.Context, msg *bus.Messag
248253 w .fileWatcherService .SetEnabled (true )
249254}
250255
251- func (w * Watcher ) handleCredentialUpdate (ctx context.Context ) {
252- slog .DebugContext (ctx , "Watcher plugin received credential update message" )
253-
254- w .watcherMutex .Lock ()
255- // This will be changed/moved during the credential watcher PR
256- conn , err := grpc .NewGrpcConnection (ctx , w .agentConfig , w .agentConfig .Command )
257- if err != nil {
258- slog .ErrorContext (ctx , "Unable to create new grpc connection" , "error" , err )
259- w .watcherMutex .Unlock ()
260-
261- return
262- }
263- w .watcherMutex .Unlock ()
264- w .messagePipe .Process (ctx , & bus.Message {
265- Topic : bus .ConnectionResetTopic , Data : conn ,
266- })
267- }
268-
269256func (w * Watcher ) monitorWatchers (ctx context.Context ) {
270257 for {
271258 select {
272259 case <- ctx .Done ():
273260 return
274- case message := <- w .credentialUpdatesChannel :
275- slog .DebugContext (ctx , "Received credential update event" )
276- newCtx := context .WithValue (ctx , logger .CorrelationIDContextKey , message .CorrelationID )
261+ case message := <- w .commandCredentialUpdatesChannel :
262+ slog .DebugContext (ctx , "Received credential update event for command server" )
263+ newCtx := context .WithValue (context .WithValue (ctx , logger .CorrelationIDContextKey , message .CorrelationID ),
264+ logger .ServerTypeContextKey , slog .Any (logger .ServerTypeKey ,
265+ message .SeverType .String ()))
266+
267+ w .messagePipe .Process (newCtx , & bus.Message {
268+ Topic : bus .ConnectionResetTopic , Data : message .Conn ,
269+ })
270+
271+ case message := <- w .auxiliaryCredentialUpdatesChannel :
272+ slog .DebugContext (ctx , "Received credential update event for auxiliary command server" )
273+ newCtx := context .WithValue (context .WithValue (ctx , logger .CorrelationIDContextKey , message .CorrelationID ),
274+ logger .ServerTypeContextKey , slog .Any (logger .ServerTypeKey ,
275+ message .SeverType .String ()))
276+
277277 w .messagePipe .Process (newCtx , & bus.Message {
278- Topic : bus .CredentialUpdatedTopic , Data : nil ,
278+ Topic : bus .ConnectionResetTopic , Data : message . Conn ,
279279 })
280280 case message := <- w .instanceUpdatesChannel :
281281 newCtx := context .WithValue (ctx , logger .CorrelationIDContextKey , message .CorrelationID )
0 commit comments