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

Method patchSimple

pkg/cmd/apply/patcher.go:118–226  ·  view source on GitHub ↗
(obj runtime.Object, modified []byte, namespace, name string, errOut io.Writer, localApply bool)

Source from the content-addressed store, hash-verified

116}
117
118func (p *Patcher) patchSimple(obj runtime.Object, modified []byte, namespace, name string, errOut io.Writer, localApply bool) ([]byte, runtime.Object, error) {
119 // Serialize the current configuration of the object from the server.
120 current, err := runtime.Encode(unstructured.UnstructuredJSONScheme, obj)
121 if err != nil {
122 return nil, nil, fmt.Errorf("serializing current configuration from:\n%v\nfor: %w", obj, err)
123 }
124
125 // Retrieve the original configuration of the object from the annotation.
126 original, err := util.GetOriginalConfiguration(obj)
127 if err != nil {
128 return nil, nil, fmt.Errorf("retrieving original configuration from:\n%v\nfor: %w", obj, err)
129 }
130
131 var patchType types.PatchType
132 var patch []byte
133
134 if p.OpenAPIV3Root != nil {
135 gvkSupported, err := p.gvkSupportsPatchOpenAPIV3(p.Mapping.GroupVersionKind)
136 if err != nil {
137 // Realistically this error logging is not needed (not present in V2),
138 // but would help us in debugging if users encounter a problem
139 // with OpenAPI V3 not present in V2.
140 klog.V(5).Infof("warning: OpenAPI V3 path does not exist - group: %s, version %s, kind %s\n",
141 p.Mapping.GroupVersionKind.Group, p.Mapping.GroupVersionKind.Version, p.Mapping.GroupVersionKind.Kind)
142 } else if gvkSupported {
143 patch, err = p.buildStrategicMergePatchFromOpenAPIV3(original, modified, current)
144 if err != nil {
145 // Fall back to OpenAPI V2 if there is a problem
146 // We should remove the fallback in the future,
147 // but for the first release it might be beneficial
148 // to fall back to OpenAPI V2 while logging the error
149 // and seeing if we get any bug reports.
150 fmt.Fprintf(errOut, "warning: error calculating patch from openapi v3 spec: %v\n", err)
151 } else {
152 patchType = types.StrategicMergePatchType
153 }
154 } else {
155 klog.V(5).Infof("warning: OpenAPI V3 path does not support strategic merge patch - group: %s, version %s, kind %s\n",
156 p.Mapping.GroupVersionKind.Group, p.Mapping.GroupVersionKind.Version, p.Mapping.GroupVersionKind.Kind)
157 }
158 }
159
160 if patch == nil && p.OpenAPIGetter != nil {
161 if openAPISchema, err := p.OpenAPIGetter.OpenAPISchema(); err == nil && openAPISchema != nil {
162 // if openapischema is used, we'll try to get required patch type for this GVK from Open API.
163 // if it fails or could not find any patch type, fall back to baked-in patch type determination.
164 if patchType, err = p.getPatchTypeFromOpenAPI(openAPISchema, p.Mapping.GroupVersionKind); err == nil && patchType == types.StrategicMergePatchType {
165 patch, err = p.buildStrategicMergeFromOpenAPI(openAPISchema, original, modified, current)
166 if err != nil {
167 // Warn user about problem and continue strategic merge patching using builtin types.
168 fmt.Fprintf(errOut, "warning: error calculating patch from openapi spec: %v\n", err)
169 }
170 }
171 }
172 }
173
174 if patch == nil {
175 versionedObj, err := scheme.Scheme.New(p.Mapping.GroupVersionKind)

Callers 2

PatchMethod · 0.95
PatchLocalMethod · 0.95

Calls 10

buildMergePatchMethod · 0.95
GetOriginalConfigurationFunction · 0.92
addResourceVersionFunction · 0.85
OpenAPISchemaMethod · 0.65
PatchMethod · 0.65

Tested by

no test coverage detected