executePatchOperation applies a patch to the specified object using API call Patch. There 2 types of patches: - Merge — use Patch API call with MergePatchType. - JSON — use Patch API call with JSONPatchType. Other options: - WithSubresource — a subresource argument for Patch or Update API call. -
(op *patchOperation)
| 160 | // - IgnoreMissingObject — do not return error if the specified object is missing. |
| 161 | // - IgnoreHookError — allows applying patches for a Status subresource even if the hook fails |
| 162 | func (o *ObjectPatcher) executePatchOperation(op *patchOperation) error { |
| 163 | if op.patchType == types.MergePatchType { |
| 164 | log.Debug("Started MergePatchObject") |
| 165 | defer log.Debug("Finished MergePatchObject") |
| 166 | } |
| 167 | if op.patchType == types.JSONPatchType { |
| 168 | log.Debug("Started JSONPatchObject") |
| 169 | defer log.Debug("Finished JSONPatchObject") |
| 170 | } |
| 171 | |
| 172 | patchBytes, err := convertPatchToBytes(op.patch) |
| 173 | if err != nil { |
| 174 | return fmt.Errorf("encode %s patch for %s/%s/%s/%s: %v", op.patchType, op.apiVersion, op.kind, op.namespace, op.name, err) |
| 175 | } |
| 176 | if patchBytes == nil { |
| 177 | return fmt.Errorf("%s patch is nil for %s/%s/%s/%s", op.patchType, op.apiVersion, op.kind, op.namespace, op.name) |
| 178 | } |
| 179 | |
| 180 | gvk, err := o.kubeClient.GroupVersionResource(op.apiVersion, op.kind) |
| 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | |
| 185 | log.Debug("Started Patch API call") |
| 186 | _, err = o.kubeClient.Dynamic(). |
| 187 | Resource(gvk). |
| 188 | Namespace(op.namespace). |
| 189 | Patch(context.TODO(), op.name, op.patchType, patchBytes, pkg.DefaultPatchOptions(), generateSubresources(op.subresource)...) |
| 190 | log.Debug("Finished Patch API call") |
| 191 | |
| 192 | if op.ignoreMissingObject && errors.IsNotFound(err) { |
| 193 | return nil |
| 194 | } |
| 195 | return err |
| 196 | } |
| 197 | |
| 198 | // executeFilterOperation retrieves a specified object, modified it with |
| 199 | // filterFunc and calls update. |
no test coverage detected