@@ -56,7 +56,8 @@ type Deployment struct {
5656 fileOverviews []* pb.File
5757 files []File
5858
59- Lock sync.RWMutex
59+ FileLock sync.RWMutex
60+ errLock sync.RWMutex
6061}
6162
6263// newDeployment returns a new Deployment object.
@@ -72,56 +73,94 @@ func (d *Deployment) GetBroadcaster() broadcast.Broadcaster {
7273 return d .broadcaster
7374}
7475
75- // GetFileOverviews returns the current list of fileOverviews and configVersion for the deployment.
76- func (d * Deployment ) GetFileOverviews () ([] * pb. File , string ) {
77- d .Lock . RLock ()
78- defer d .Lock . RUnlock ()
76+ // SetLatestConfigError sets the latest config apply error for the deployment.
77+ func (d * Deployment ) SetLatestConfigError ( err error ) {
78+ d .errLock . Lock ()
79+ defer d .errLock . Unlock ()
7980
80- return d . fileOverviews , d . configVersion
81+ d . latestConfigError = err
8182}
8283
83- // GetNGINXPlusActions returns the current NGINX Plus API Actions for the deployment.
84- func (d * Deployment ) GetNGINXPlusActions () [] * pb. NGINXPlusAction {
85- d .Lock . RLock ()
86- defer d .Lock . RUnlock ()
84+ // SetLatestUpstreamError sets the latest upstream update error for the deployment.
85+ func (d * Deployment ) SetLatestUpstreamError ( err error ) {
86+ d .errLock . Lock ()
87+ defer d .errLock . Unlock ()
8788
88- return d . nginxPlusActions
89+ d . latestUpstreamError = err
8990}
9091
9192// GetLatestConfigError gets the latest config apply error for the deployment.
9293func (d * Deployment ) GetLatestConfigError () error {
93- d .Lock .RLock ()
94- defer d .Lock .RUnlock ()
94+ d .errLock .RLock ()
95+ defer d .errLock .RUnlock ()
9596
9697 return d .latestConfigError
9798}
9899
99100// GetLatestUpstreamError gets the latest upstream update error for the deployment.
100101func (d * Deployment ) GetLatestUpstreamError () error {
101- d .Lock .RLock ()
102- defer d .Lock .RUnlock ()
102+ d .errLock .RLock ()
103+ defer d .errLock .RUnlock ()
103104
104105 return d .latestUpstreamError
105106}
106107
108+ // SetPodErrorStatus sets the error status of a Pod in this Deployment if applying the config failed.
109+ func (d * Deployment ) SetPodErrorStatus (pod string , err error ) {
110+ d .errLock .Lock ()
111+ defer d .errLock .Unlock ()
112+
113+ d .podStatuses [pod ] = err
114+ }
115+
107116// RemovePodStatus deletes a pod from the pod status map.
108117func (d * Deployment ) RemovePodStatus (podName string ) {
109- d .Lock .Lock ()
110- defer d .Lock .Unlock ()
118+ d .errLock .Lock ()
119+ defer d .errLock .Unlock ()
111120
112121 delete (d .podStatuses , podName )
113122}
114123
124+ // GetConfigurationStatus returns the current config status for this Deployment. It combines
125+ // the most recent errors (if they exist) for all Pods in the Deployment into a single error.
126+ func (d * Deployment ) GetConfigurationStatus () error {
127+ d .errLock .RLock ()
128+ defer d .errLock .RUnlock ()
129+
130+ errs := make ([]error , 0 , len (d .podStatuses ))
131+ for _ , err := range d .podStatuses {
132+ errs = append (errs , err )
133+ }
134+
135+ if len (errs ) == 1 {
136+ return errs [0 ]
137+ }
138+
139+ return errors .Join (errs ... )
140+ }
141+
115142/*
116143The following functions for the Deployment object are UNLOCKED, meaning that they are unsafe.
117- Callers of these functions MUST ensure the lock is set before calling.
144+ Callers of these functions MUST ensure the FileLock is set before calling.
118145
119146These functions are called as part of the ConfigApply or APIRequest processes. These entire processes
120147are locked by the caller, hence why the functions themselves do not set the locks.
121148*/
122149
150+ // GetFileOverviews returns the current list of fileOverviews and configVersion for the deployment.
151+ // The deployment FileLock MUST already be locked before calling this function.
152+ func (d * Deployment ) GetFileOverviews () ([]* pb.File , string ) {
153+ return d .fileOverviews , d .configVersion
154+ }
155+
156+ // GetNGINXPlusActions returns the current NGINX Plus API Actions for the deployment.
157+ // The deployment FileLock MUST already be locked before calling this function.
158+ func (d * Deployment ) GetNGINXPlusActions () []* pb.NGINXPlusAction {
159+ return d .nginxPlusActions
160+ }
161+
123162// GetFile gets the requested file for the deployment and returns its contents.
124- // The deployment MUST already be locked before calling this function.
163+ // The deployment FileLock MUST already be locked before calling this function.
125164func (d * Deployment ) GetFile (name , hash string ) []byte {
126165 for _ , file := range d .files {
127166 if name == file .Meta .GetName () && hash == file .Meta .GetHash () {
@@ -133,7 +172,7 @@ func (d *Deployment) GetFile(name, hash string) []byte {
133172}
134173
135174// SetFiles updates the nginx files and fileOverviews for the deployment and returns the message to send.
136- // The deployment MUST already be locked before calling this function.
175+ // The deployment FileLock MUST already be locked before calling this function.
137176func (d * Deployment ) SetFiles (files []File ) broadcast.NginxAgentMessage {
138177 d .files = files
139178
@@ -167,45 +206,11 @@ func (d *Deployment) SetFiles(files []File) broadcast.NginxAgentMessage {
167206
168207// SetNGINXPlusActions updates the deployment's latest NGINX Plus Actions to perform if using NGINX Plus.
169208// Used by a Subscriber when it first connects.
170- // The deployment MUST already be locked before calling this function.
209+ // The deployment FileLock MUST already be locked before calling this function.
171210func (d * Deployment ) SetNGINXPlusActions (actions []* pb.NGINXPlusAction ) {
172211 d .nginxPlusActions = actions
173212}
174213
175- // SetPodErrorStatus sets the error status of a Pod in this Deployment if applying the config failed.
176- // The deployment MUST already be locked before calling this function.
177- func (d * Deployment ) SetPodErrorStatus (pod string , err error ) {
178- d .podStatuses [pod ] = err
179- }
180-
181- // SetLatestConfigError sets the latest config apply error for the deployment.
182- // The deployment MUST already be locked before calling this function.
183- func (d * Deployment ) SetLatestConfigError (err error ) {
184- d .latestConfigError = err
185- }
186-
187- // SetLatestUpstreamError sets the latest upstream update error for the deployment.
188- // The deployment MUST already be locked before calling this function.
189- func (d * Deployment ) SetLatestUpstreamError (err error ) {
190- d .latestUpstreamError = err
191- }
192-
193- // GetConfigurationStatus returns the current config status for this Deployment. It combines
194- // the most recent errors (if they exist) for all Pods in the Deployment into a single error.
195- // The deployment MUST already be locked before calling this function.
196- func (d * Deployment ) GetConfigurationStatus () error {
197- errs := make ([]error , 0 , len (d .podStatuses ))
198- for _ , err := range d .podStatuses {
199- errs = append (errs , err )
200- }
201-
202- if len (errs ) == 1 {
203- return errs [0 ]
204- }
205-
206- return errors .Join (errs ... )
207- }
208-
209214//counterfeiter:generate . DeploymentStorer
210215
211216// DeploymentStorer is an interface to store Deployments.
0 commit comments