MCPcopy
hub / github.com/kubernetes/kubectl / getPatchedJSON

Function getPatchedJSON

pkg/cmd/patch/patch.go:318–350  ·  view source on GitHub ↗
(patchType types.PatchType, originalJS, patchJS []byte, gvk schema.GroupVersionKind, creater runtime.ObjectCreater)

Source from the content-addressed store, hash-verified

316}
317
318func getPatchedJSON(patchType types.PatchType, originalJS, patchJS []byte, gvk schema.GroupVersionKind, creater runtime.ObjectCreater) ([]byte, error) {
319 switch patchType {
320 case types.JSONPatchType:
321 patchObj, err := jsonpatch.DecodePatch(patchJS)
322 if err != nil {
323 return nil, err
324 }
325 bytes, err := patchObj.Apply(originalJS)
326 // TODO: This is pretty hacky, we need a better structured error from the json-patch
327 if err != nil && strings.Contains(err.Error(), "doc is missing key") {
328 msg := err.Error()
329 ix := strings.Index(msg, "key:")
330 key := msg[ix+5:]
331 return bytes, fmt.Errorf("Object to be patched is missing field (%s)", key)
332 }
333 return bytes, err
334
335 case types.MergePatchType:
336 return jsonpatch.MergePatch(originalJS, patchJS)
337
338 case types.StrategicMergePatchType:
339 // get a typed object for this GVK if we need to apply a strategic merge patch
340 obj, err := creater.New(gvk)
341 if err != nil {
342 return nil, fmt.Errorf("strategic merge patch is not supported for %s locally, try --type merge", gvk.String())
343 }
344 return strategicpatch.StrategicMergePatch(originalJS, patchJS, obj)
345
346 default:
347 // only here as a safety net - go-restful filters content-type
348 return nil, fmt.Errorf("unknown Content-Type header for patch: %v", patchType)
349 }
350}
351
352func patchOperation(didPatch bool) string {
353 if didPatch {

Callers 1

RunPatchMethod · 0.85

Calls 3

ApplyMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…