github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/managedfields/extract.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package managedfields 18 19 import ( 20 "bytes" 21 "fmt" 22 23 "sigs.k8s.io/structured-merge-diff/v4/fieldpath" 24 "sigs.k8s.io/structured-merge-diff/v4/typed" 25 26 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/meta" 27 metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1" 28 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1/unstructured" 29 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime" 30 ) 31 32 // ExtractInto extracts the applied configuration state from object for fieldManager 33 // into applyConfiguration. If no managed fields are found for the given fieldManager, 34 // no error is returned, but applyConfiguration is left unpopulated. It is possible 35 // that no managed fields were found for the fieldManager because other field managers 36 // have taken ownership of all the fields previously owned by the fieldManager. It is 37 // also possible the fieldManager never owned fields. 38 // 39 // The provided object MUST bo a root resource object since subresource objects 40 // do not contain their own managed fields. For example, an autoscaling.Scale 41 // object read from a "scale" subresource does not have any managed fields and so 42 // cannot be used as the object. 43 // 44 // If the fields of a subresource are a subset of the fields of the root object, 45 // and their field paths and types are exactly the same, then ExtractInto can be 46 // called with the root resource as the object and the subresource as the 47 // applyConfiguration. This works for "status", obviously, because status is 48 // represented by the exact same object as the root resource. This does NOT 49 // work, for example, with the "scale" subresources of Deployment, ReplicaSet and 50 // StatefulSet. While the spec.replicas, status.replicas fields are in the same 51 // exact field path locations as they are in autoscaling.Scale, the selector 52 // fields are in different locations, and are a different type. 53 func ExtractInto(object runtime.Object, objectType typed.ParseableType, fieldManager string, applyConfiguration interface{}, subresource string) error { 54 typedObj, err := toTyped(object, objectType) 55 if err != nil { 56 return fmt.Errorf("error converting obj to typed: %w", err) 57 } 58 59 accessor, err := meta.Accessor(object) 60 if err != nil { 61 return fmt.Errorf("error accessing metadata: %w", err) 62 } 63 fieldsEntry, ok := findManagedFields(accessor, fieldManager, subresource) 64 if !ok { 65 return nil 66 } 67 fieldset := &fieldpath.Set{} 68 err = fieldset.FromJSON(bytes.NewReader(fieldsEntry.FieldsV1.Raw)) 69 if err != nil { 70 return fmt.Errorf("error marshalling FieldsV1 to JSON: %w", err) 71 } 72 73 u := typedObj.ExtractItems(fieldset.Leaves()).AsValue().Unstructured() 74 m, ok := u.(map[string]interface{}) 75 if !ok { 76 return fmt.Errorf("unable to convert managed fields for %s to unstructured, expected map, got %T", fieldManager, u) 77 } 78 79 // set the type meta manually if it doesn't exist to avoid missing kind errors 80 // when decoding from unstructured JSON 81 if _, ok := m["kind"]; !ok && object.GetObjectKind().GroupVersionKind().Kind != "" { 82 m["kind"] = object.GetObjectKind().GroupVersionKind().Kind 83 m["apiVersion"] = object.GetObjectKind().GroupVersionKind().GroupVersion().String() 84 } 85 if err := runtime.DefaultUnstructuredConverter.FromUnstructured(m, applyConfiguration); err != nil { 86 return fmt.Errorf("error extracting into obj from unstructured: %w", err) 87 } 88 return nil 89 } 90 91 func findManagedFields(accessor metav1.Object, fieldManager string, subresource string) (metav1.ManagedFieldsEntry, bool) { 92 objManagedFields := accessor.GetManagedFields() 93 for _, mf := range objManagedFields { 94 if mf.Manager == fieldManager && mf.Operation == metav1.ManagedFieldsOperationApply && mf.Subresource == subresource { 95 return mf, true 96 } 97 } 98 return metav1.ManagedFieldsEntry{}, false 99 } 100 101 func toTyped(obj runtime.Object, objectType typed.ParseableType) (*typed.TypedValue, error) { 102 switch o := obj.(type) { 103 case *unstructured.Unstructured: 104 return objectType.FromUnstructured(o.Object) 105 default: 106 return objectType.FromStructured(o) 107 } 108 }