@@ -87,7 +87,14 @@ func (m *ComputeDomainManager) Start(ctx context.Context) (rerr error) {
8787 }
8888 }()
8989
90- _ , err := m .informer .AddEventHandler (cache.ResourceEventHandlerFuncs {
90+ err := m .informer .AddIndexers (cache.Indexers {
91+ "uid" : uidIndexer [* nvapi .ComputeDomain ],
92+ })
93+ if err != nil {
94+ return fmt .Errorf ("error adding indexer for ComputeDomain UID: %w" , err )
95+ }
96+
97+ _ , err = m .informer .AddEventHandler (cache.ResourceEventHandlerFuncs {
9198 AddFunc : func (obj any ) {
9299 m .config .workQueue .Enqueue (obj , m .onAddOrUpdate )
93100 },
@@ -133,13 +140,43 @@ func (m *ComputeDomainManager) Stop() error {
133140 return nil
134141}
135142
143+ // Get gets the ComputeDomain by UID from the informer cache.
144+ func (m * ComputeDomainManager ) Get (uid string ) (* nvapi.ComputeDomain , error ) {
145+ objs , err := m .informer .GetIndexer ().ByIndex ("uid" , uid )
146+ if err != nil {
147+ return nil , fmt .Errorf ("error retrieving ComputeDomain by UID: %w" , err )
148+ }
149+ if len (objs ) == 0 {
150+ return nil , nil
151+ }
152+ if len (objs ) != 1 {
153+ return nil , fmt .Errorf ("multiple ComputeDomains with the same UID" )
154+ }
155+ cd , ok := objs [0 ].(* nvapi.ComputeDomain )
156+ if ! ok {
157+ return nil , fmt .Errorf ("error casting to ComputeDomain" )
158+ }
159+ return cd , nil
160+ }
161+
136162// onAddOrUpdate handles the addition or update of a ComputeDomain.
137163func (m * ComputeDomainManager ) onAddOrUpdate (ctx context.Context , obj any ) error {
138- cd , ok := obj .(* nvapi.ComputeDomain )
164+ // Cast the object to a ComputeDomain object
165+ o , ok := obj .(* nvapi.ComputeDomain )
139166 if ! ok {
140167 return fmt .Errorf ("failed to cast to ComputeDomain" )
141168 }
142169
170+ // Get the latest ComputeDomain object from the informer cache since we
171+ // plan to update it later and always *must* have the latest version.
172+ cd , err := m .Get (string (o .GetUID ()))
173+ if err != nil {
174+ return fmt .Errorf ("error getting latest ComputeDomain: %w" , err )
175+ }
176+ if cd == nil {
177+ return nil
178+ }
179+
143180 // Skip ComputeDomains that don't match on UUID
144181 if string (cd .UID ) != m .config .computeDomainUUID {
145182 klog .Errorf ("ComputeDomain processed with non-matching UID (%v, %v)" , cd .UID , m .config .computeDomainUUID )
0 commit comments