extract.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package managedfields
  14. import (
  15. "bytes"
  16. "fmt"
  17. "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
  18. "sigs.k8s.io/structured-merge-diff/v4/typed"
  19. "k8s.io/apimachinery/pkg/api/meta"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. )
  24. // ExtractInto extracts the applied configuration state from object for fieldManager
  25. // into applyConfiguration. If no managed fields are found for the given fieldManager,
  26. // no error is returned, but applyConfiguration is left unpopulated. It is possible
  27. // that no managed fields were found for the fieldManager because other field managers
  28. // have taken ownership of all the fields previously owned by the fieldManager. It is
  29. // also possible the fieldManager never owned fields.
  30. func ExtractInto(object runtime.Object, objectType typed.ParseableType, fieldManager string, applyConfiguration interface{}) error {
  31. typedObj, err := toTyped(object, objectType)
  32. if err != nil {
  33. return fmt.Errorf("error converting obj to typed: %w", err)
  34. }
  35. accessor, err := meta.Accessor(object)
  36. if err != nil {
  37. return fmt.Errorf("error accessing metadata: %w", err)
  38. }
  39. fieldsEntry, ok := findManagedFields(accessor, fieldManager)
  40. if !ok {
  41. return nil
  42. }
  43. fieldset := &fieldpath.Set{}
  44. err = fieldset.FromJSON(bytes.NewReader(fieldsEntry.FieldsV1.Raw))
  45. if err != nil {
  46. return fmt.Errorf("error marshalling FieldsV1 to JSON: %w", err)
  47. }
  48. u := typedObj.ExtractItems(fieldset.Leaves()).AsValue().Unstructured()
  49. m, ok := u.(map[string]interface{})
  50. if !ok {
  51. return fmt.Errorf("unable to convert managed fields for %s to unstructured, expected map, got %T", fieldManager, u)
  52. }
  53. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(m, applyConfiguration); err != nil {
  54. return fmt.Errorf("error extracting into obj from unstructured: %w", err)
  55. }
  56. return nil
  57. }
  58. func findManagedFields(accessor metav1.Object, fieldManager string) (metav1.ManagedFieldsEntry, bool) {
  59. objManagedFields := accessor.GetManagedFields()
  60. for _, mf := range objManagedFields {
  61. if mf.Manager == fieldManager && mf.Operation == metav1.ManagedFieldsOperationApply {
  62. return mf, true
  63. }
  64. }
  65. return metav1.ManagedFieldsEntry{}, false
  66. }
  67. func toTyped(obj runtime.Object, objectType typed.ParseableType) (*typed.TypedValue, error) {
  68. switch o := obj.(type) {
  69. case *unstructured.Unstructured:
  70. return objectType.FromUnstructured(o.Object)
  71. default:
  72. return objectType.FromStructured(o)
  73. }
  74. }