extract.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. //
  31. // The provided object MUST bo a root resource object since subresource objects
  32. // do not contain their own managed fields. For example, an autoscaling.Scale
  33. // object read from a "scale" subresource does not have any managed fields and so
  34. // cannot be used as the object.
  35. //
  36. // If the fields of a subresource are a subset of the fields of the root object,
  37. // and their field paths and types are exactly the same, then ExtractInto can be
  38. // called with the root resource as the object and the subresource as the
  39. // applyConfiguration. This works for "status", obviously, because status is
  40. // represented by the exact same object as the root resource. This does NOT
  41. // work, for example, with the "scale" subresources of Deployment, ReplicaSet and
  42. // StatefulSet. While the spec.replicas, status.replicas fields are in the same
  43. // exact field path locations as they are in autoscaling.Scale, the selector
  44. // fields are in different locations, and are a different type.
  45. func ExtractInto(object runtime.Object, objectType typed.ParseableType, fieldManager string, applyConfiguration interface{}, subresource string) error {
  46. typedObj, err := toTyped(object, objectType)
  47. if err != nil {
  48. return fmt.Errorf("error converting obj to typed: %w", err)
  49. }
  50. accessor, err := meta.Accessor(object)
  51. if err != nil {
  52. return fmt.Errorf("error accessing metadata: %w", err)
  53. }
  54. fieldsEntry, ok := findManagedFields(accessor, fieldManager, subresource)
  55. if !ok {
  56. return nil
  57. }
  58. fieldset := &fieldpath.Set{}
  59. err = fieldset.FromJSON(bytes.NewReader(fieldsEntry.FieldsV1.Raw))
  60. if err != nil {
  61. return fmt.Errorf("error marshalling FieldsV1 to JSON: %w", err)
  62. }
  63. u := typedObj.ExtractItems(fieldset.Leaves()).AsValue().Unstructured()
  64. m, ok := u.(map[string]interface{})
  65. if !ok {
  66. return fmt.Errorf("unable to convert managed fields for %s to unstructured, expected map, got %T", fieldManager, u)
  67. }
  68. // set the type meta manually if it doesn't exist to avoid missing kind errors
  69. // when decoding from unstructured JSON
  70. if _, ok := m["kind"]; !ok && object.GetObjectKind().GroupVersionKind().Kind != "" {
  71. m["kind"] = object.GetObjectKind().GroupVersionKind().Kind
  72. m["apiVersion"] = object.GetObjectKind().GroupVersionKind().GroupVersion().String()
  73. }
  74. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(m, applyConfiguration); err != nil {
  75. return fmt.Errorf("error extracting into obj from unstructured: %w", err)
  76. }
  77. return nil
  78. }
  79. func findManagedFields(accessor metav1.Object, fieldManager string, subresource string) (metav1.ManagedFieldsEntry, bool) {
  80. objManagedFields := accessor.GetManagedFields()
  81. for _, mf := range objManagedFields {
  82. if mf.Manager == fieldManager && mf.Operation == metav1.ManagedFieldsOperationApply && mf.Subresource == subresource {
  83. return mf, true
  84. }
  85. }
  86. return metav1.ManagedFieldsEntry{}, false
  87. }
  88. func toTyped(obj runtime.Object, objectType typed.ParseableType) (*typed.TypedValue, error) {
  89. switch o := obj.(type) {
  90. case *unstructured.Unstructured:
  91. return objectType.FromUnstructured(o.Object)
  92. default:
  93. return objectType.FromStructured(o)
  94. }
  95. }