@@ -16,6 +16,8 @@ import (
1616 "strings"
1717 "sync"
1818
19+ "google.golang.org/protobuf/proto"
20+
1921 "github.com/nginxinc/nginx-plus-go-client/v2/client"
2022 "google.golang.org/protobuf/types/known/structpb"
2123
@@ -42,8 +44,8 @@ const (
4244
4345type resourceServiceInterface interface {
4446 AddInstances (instanceList []* mpi.Instance ) * mpi.Resource
45- UpdateInstances (instanceList []* mpi.Instance ) * mpi.Resource
46- DeleteInstances (instanceList []* mpi.Instance ) * mpi.Resource
47+ UpdateInstances (ctx context. Context , instanceList []* mpi.Instance ) * mpi.Resource
48+ DeleteInstances (ctx context. Context , instanceList []* mpi.Instance ) * mpi.Resource
4749 ApplyConfig (ctx context.Context , instanceID string ) error
4850 Instance (instanceID string ) * mpi.Instance
4951 GetHTTPUpstreamServers (ctx context.Context , instance * mpi.Instance , upstreams string ) ([]client.UpstreamServer ,
@@ -126,32 +128,45 @@ func (r *ResourceService) RemoveOperator(instanceList []*mpi.Instance) {
126128 }
127129}
128130
129- func (r * ResourceService ) UpdateInstances (instanceList []* mpi.Instance ) * mpi.Resource {
131+ func (r * ResourceService ) UpdateInstances (ctx context. Context , instanceList []* mpi.Instance ) * mpi.Resource {
130132 r .resourceMutex .Lock ()
131133 defer r .resourceMutex .Unlock ()
132134
133135 for _ , updatedInstance := range instanceList {
134- for _ , instance := range r .resource .GetInstances () {
135- if updatedInstance .GetInstanceMeta ().GetInstanceId () == instance .GetInstanceMeta ().GetInstanceId () {
136- instance .InstanceMeta = updatedInstance .GetInstanceMeta ()
137- instance .InstanceRuntime = updatedInstance .GetInstanceRuntime ()
138- instance .InstanceConfig = updatedInstance .GetInstanceConfig ()
136+ resourceCopy , ok := proto .Clone (r .resource ).(* mpi.Resource )
137+ if ok {
138+ for _ , instance := range resourceCopy .GetInstances () {
139+ if updatedInstance .GetInstanceMeta ().GetInstanceId () == instance .GetInstanceMeta ().GetInstanceId () {
140+ instance .InstanceMeta = updatedInstance .GetInstanceMeta ()
141+ instance .InstanceRuntime = updatedInstance .GetInstanceRuntime ()
142+ instance .InstanceConfig = updatedInstance .GetInstanceConfig ()
143+ }
139144 }
145+ r .resource = resourceCopy
146+ } else {
147+ slog .WarnContext (ctx , "Unable to clone resource while updating instances" , "resource" ,
148+ r .resource , "instances" , instanceList )
140149 }
141150 }
142151
143152 return r .resource
144153}
145154
146- func (r * ResourceService ) DeleteInstances (instanceList []* mpi.Instance ) * mpi.Resource {
155+ func (r * ResourceService ) DeleteInstances (ctx context. Context , instanceList []* mpi.Instance ) * mpi.Resource {
147156 r .resourceMutex .Lock ()
148157 defer r .resourceMutex .Unlock ()
149158
150159 for _ , deletedInstance := range instanceList {
151- for index , instance := range r .resource .GetInstances () {
152- if deletedInstance .GetInstanceMeta ().GetInstanceId () == instance .GetInstanceMeta ().GetInstanceId () {
153- r .resource .Instances = append (r .resource .Instances [:index ], r .resource .GetInstances ()[index + 1 :]... )
160+ resourceCopy , ok := proto .Clone (r .resource ).(* mpi.Resource )
161+ if ok {
162+ for index , instance := range resourceCopy .GetInstances () {
163+ if deletedInstance .GetInstanceMeta ().GetInstanceId () == instance .GetInstanceMeta ().GetInstanceId () {
164+ r .resource .Instances = append (r .resource .Instances [:index ], r .resource .GetInstances ()[index + 1 :]... )
165+ }
154166 }
167+ } else {
168+ slog .WarnContext (ctx , "Unable to clone resource while deleting instances" , "resource" ,
169+ r .resource , "instances" , instanceList )
155170 }
156171 }
157172 r .RemoveOperator (instanceList )
0 commit comments