1/*
2Copyright 2021 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package managedfields
18
19import (
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	"k8s.io/apimachinery/pkg/api/meta"
27	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29	"k8s.io/apimachinery/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 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.
53func 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	if err := runtime.DefaultUnstructuredConverter.FromUnstructured(m, applyConfiguration); err != nil {
79		return fmt.Errorf("error extracting into obj from unstructured: %w", err)
80	}
81	return nil
82}
83
84func findManagedFields(accessor metav1.Object, fieldManager string, subresource string) (metav1.ManagedFieldsEntry, bool) {
85	objManagedFields := accessor.GetManagedFields()
86	for _, mf := range objManagedFields {
87		if mf.Manager == fieldManager && mf.Operation == metav1.ManagedFieldsOperationApply && mf.Subresource == subresource {
88			return mf, true
89		}
90	}
91	return metav1.ManagedFieldsEntry{}, false
92}
93
94func toTyped(obj runtime.Object, objectType typed.ParseableType) (*typed.TypedValue, error) {
95	switch o := obj.(type) {
96	case *unstructured.Unstructured:
97		return objectType.FromUnstructured(o.Object)
98	default:
99		return objectType.FromStructured(o)
100	}
101}
102