@@ -56,9 +56,7 @@ const (
5656//counterfeiter:generate . processOperator
5757
5858type resourceServiceInterface interface {
59- AddInstances (instanceList []* mpi.Instance ) * mpi.Resource
6059 UpdateInstances (ctx context.Context , instanceList []* mpi.Instance ) * mpi.Resource
61- DeleteInstances (ctx context.Context , instanceList []* mpi.Instance ) * mpi.Resource
6260 ApplyConfig (ctx context.Context , instanceID string ) (* model.NginxConfigContext , error )
6361 Instance (instanceID string ) * mpi.Instance
6462 GetHTTPUpstreamServers (ctx context.Context , instance * mpi.Instance , upstreams string ) ([]client.UpstreamServer ,
@@ -91,9 +89,10 @@ type (
9189
9290type ResourceService struct {
9391 resource * mpi.Resource
92+ napInstance * mpi.Instance
9493 nginxConfigParser parser.ConfigParser
9594 agentConfig * config.Config
96- instanceOperators map [ string ] instanceOperator // key is instance ID
95+ instanceOperator instanceOperator
9796 info host.InfoInterface
9897 manifestFilePath string
9998 resourceMutex sync.Mutex
@@ -106,7 +105,7 @@ func NewResourceService(ctx context.Context, agentConfig *config.Config) *Resour
106105 resourceMutex : sync.Mutex {},
107106 info : host .NewInfo (),
108107 operatorsMutex : sync.Mutex {},
109- instanceOperators : make ( map [ string ] instanceOperator ),
108+ instanceOperator : NewInstanceOperator ( agentConfig ),
110109 nginxConfigParser : parser .NewNginxConfigParser (agentConfig ),
111110 agentConfig : agentConfig ,
112111 manifestFilePath : agentConfig .LibDir + "/manifest.json" ,
@@ -117,15 +116,6 @@ func NewResourceService(ctx context.Context, agentConfig *config.Config) *Resour
117116 return resourceService
118117}
119118
120- func (r * ResourceService ) AddInstances (instanceList []* mpi.Instance ) * mpi.Resource {
121- r .resourceMutex .Lock ()
122- defer r .resourceMutex .Unlock ()
123- r .resource .Instances = append (r .resource .GetInstances (), instanceList ... )
124- r .AddOperator (instanceList )
125-
126- return r .resource
127- }
128-
129119func (r * ResourceService ) Instance (instanceID string ) * mpi.Instance {
130120 for _ , instance := range r .resource .GetInstances () {
131121 if instance .GetInstanceMeta ().GetInstanceId () == instanceID {
@@ -136,73 +126,25 @@ func (r *ResourceService) Instance(instanceID string) *mpi.Instance {
136126 return nil
137127}
138128
139- func (r * ResourceService ) AddOperator (instanceList []* mpi.Instance ) {
140- r .operatorsMutex .Lock ()
141- defer r .operatorsMutex .Unlock ()
142- for _ , instance := range instanceList {
143- r .instanceOperators [instance .GetInstanceMeta ().GetInstanceId ()] = NewInstanceOperator (r .agentConfig )
144- }
145- }
146-
147- func (r * ResourceService ) RemoveOperator (instanceList []* mpi.Instance ) {
148- r .operatorsMutex .Lock ()
149- defer r .operatorsMutex .Unlock ()
150- for _ , instance := range instanceList {
151- delete (r .instanceOperators , instance .GetInstanceMeta ().GetInstanceId ())
152- }
153- }
154-
155129func (r * ResourceService ) UpdateInstances (ctx context.Context , instanceList []* mpi.Instance ) * mpi.Resource {
156130 r .resourceMutex .Lock ()
157131 defer r .resourceMutex .Unlock ()
158-
159- for _ , updatedInstance := range instanceList {
160- resourceCopy , ok := proto .Clone (r .resource ).(* mpi.Resource )
161- if ok {
162- for _ , instance := range resourceCopy .GetInstances () {
163- if updatedInstance .GetInstanceMeta ().GetInstanceId () == instance .GetInstanceMeta ().GetInstanceId () {
164- instance .InstanceMeta = updatedInstance .GetInstanceMeta ()
165- instance .InstanceRuntime = updatedInstance .GetInstanceRuntime ()
166- instance .InstanceConfig = updatedInstance .GetInstanceConfig ()
167- }
168- }
169- r .resource = resourceCopy
170- } else {
171- slog .WarnContext (ctx , "Unable to clone resource while updating instances" , "resource" ,
172- r .resource , "instances" , instanceList )
173- }
174- }
175-
176- return r .resource
177- }
178-
179- func (r * ResourceService ) DeleteInstances (ctx context.Context , instanceList []* mpi.Instance ) * mpi.Resource {
180- r .resourceMutex .Lock ()
181- defer r .resourceMutex .Unlock ()
182-
183- for _ , deletedInstance := range instanceList {
184- resourceCopy , ok := proto .Clone (r .resource ).(* mpi.Resource )
185- if ok {
186- for index , instance := range resourceCopy .GetInstances () {
187- if deletedInstance .GetInstanceMeta ().GetInstanceId () == instance .GetInstanceMeta ().GetInstanceId () {
188- r .resource .Instances = append (r .resource .Instances [:index ], r .resource .GetInstances ()[index + 1 :]... )
189- }
190- }
191- } else {
192- slog .WarnContext (ctx , "Unable to clone resource while deleting instances" , "resource" ,
193- r .resource , "instances" , instanceList )
194- }
132+ resourceCopy , ok := proto .Clone (r .resource ).(* mpi.Resource )
133+ if ok {
134+ resourceCopy .Instances = instanceList
135+ r .resource = resourceCopy
136+ } else {
137+ slog .WarnContext (ctx , "Unable to clone resource while updating instances" , "resource" ,
138+ r .resource , "instances" , instanceList )
195139 }
196- r .RemoveOperator (instanceList )
197140
198141 return r .resource
199142}
200143
201144func (r * ResourceService ) ApplyConfig (ctx context.Context , instanceID string ) (* model.NginxConfigContext , error ) {
202145 var instance * mpi.Instance
203- operator := r .instanceOperators [instanceID ]
204146
205- if operator == nil {
147+ if r . instanceOperator == nil {
206148 return nil , fmt .Errorf ("instance %s not found" , instanceID )
207149 }
208150
@@ -224,12 +166,12 @@ func (r *ResourceService) ApplyConfig(ctx context.Context, instanceID string) (*
224166
225167 slog .DebugContext (ctx , "Updated Instance Runtime after parsing config" , "instance" , instance .GetInstanceRuntime ())
226168
227- valErr := operator .Validate (ctx , instance )
169+ valErr := r . instanceOperator .Validate (ctx , instance )
228170 if valErr != nil {
229171 return nil , fmt .Errorf ("failed validating config %w" , valErr )
230172 }
231173
232- reloadErr := operator .Reload (ctx , instance )
174+ reloadErr := r . instanceOperator .Reload (ctx , instance )
233175 if reloadErr != nil {
234176 return nil , fmt .Errorf ("failed to reload NGINX %w" , reloadErr )
235177 }
0 commit comments