Execute executes the instructions given with the fluent builder, returns any error encountered
(ctx context.Context, key types.NamespacedName, obj client.Object)
| 178 | |
| 179 | // Execute executes the instructions given with the fluent builder, returns any error encountered |
| 180 | func (fb *FencingMetadataExecutor) Execute(ctx context.Context, key types.NamespacedName, obj client.Object) error { |
| 181 | if len(fb.instanceNames) == 0 { |
| 182 | return errors.New("chose an operation to execute") |
| 183 | } |
| 184 | if len(fb.instanceNames) > 1 && slices.Contains(fb.instanceNames, FenceAllInstances) { |
| 185 | return errors.New("the fence-all-instances token (*) cannot be used along other instances") |
| 186 | } |
| 187 | |
| 188 | if err := fb.cli.Get(ctx, key, obj); err != nil { |
| 189 | return err |
| 190 | } |
| 191 | |
| 192 | var appliedChange bool |
| 193 | fencedObject := obj.DeepCopyObject().(client.Object) |
| 194 | for _, name := range fb.instanceNames { |
| 195 | changed, err := fb.fenceFunc(name, fencedObject) |
| 196 | if err != nil { |
| 197 | return err |
| 198 | } |
| 199 | appliedChange = appliedChange || changed |
| 200 | } |
| 201 | if !appliedChange { |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | return fb.cli.Patch(ctx, fencedObject, client.MergeFrom(obj)) |
| 206 | } |