|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fake |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | +) |
| 26 | + |
| 27 | +// convertUnstructuredIntoObject wraps Scheme.Convert to add support for |
| 28 | +// PartialObjectMetadata as an optional target type. |
| 29 | +// |
| 30 | +// Warning: This code converts through JSON and should only be used for testing. |
| 31 | +func convertUnstructuredIntoObject(from *unstructured.Unstructured, to client.Object, scheme *runtime.Scheme) error { |
| 32 | + // Convert to the requested type and version (DeepCopyInto the input object). |
| 33 | + switch tObj := to.(type) { |
| 34 | + case *metav1.PartialObjectMetadata: |
| 35 | + return convertFromUnstructuredIntoV1MetadataObject(from, tObj, scheme) |
| 36 | + // TODO: add support for v1beta1.PartialObjectMetadata, as needed |
| 37 | + default: |
| 38 | + return scheme.Convert(from, to, nil) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// convertUnstructuredListIntoObjectList wraps Scheme.Convert to add support for |
| 43 | +// PartialObjectMetadataList as an optional target type. |
| 44 | +// |
| 45 | +// Warning: This code converts through JSON and should only be used for testing. |
| 46 | +func convertUnstructuredListIntoObjectList(from *unstructured.UnstructuredList, to client.ObjectList, scheme *runtime.Scheme) error { |
| 47 | + // Convert to the requested type and version (DeepCopyInto the input object). |
| 48 | + switch tObj := to.(type) { |
| 49 | + case *metav1.PartialObjectMetadataList: |
| 50 | + return convertFromUnstructuredListIntoV1MetadataList(from, tObj, scheme) |
| 51 | + // TODO: add support for v1beta1.PartialObjectMetadataList, as needed |
| 52 | + default: |
| 53 | + return scheme.Convert(from, to, nil) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// convertFromUnstructuredIntoV1MetadataObject converts from UnstructuredList |
| 58 | +// to v1.PartialObjectMetadata. |
| 59 | +// |
| 60 | +// PartialObjectMetadata isn't registered with the Scheme and has no generated |
| 61 | +// conversion code, so it has to be converted through JSON. |
| 62 | +// This is effectively Convert + DeepCopyInto. |
| 63 | +// |
| 64 | +// Warning: This code converts through JSON and should only be used for testing. |
| 65 | +func convertFromUnstructuredIntoV1MetadataObject(uObj *unstructured.Unstructured, mObj *metav1.PartialObjectMetadata, scheme *runtime.Scheme) error { |
| 66 | + // Validate matching GroupKinds |
| 67 | + if uObj.GroupVersionKind().GroupKind() != mObj.GroupVersionKind().GroupKind() { |
| 68 | + return fmt.Errorf("converting Unstructured (%s) to PartialObjectMetadata (%s): GroupKind mismatch", |
| 69 | + uObj.GroupVersionKind().GroupKind(), mObj.GroupVersionKind().GroupKind()) |
| 70 | + } |
| 71 | + // Convert to desired version |
| 72 | + if uObj.GroupVersionKind().Version != mObj.GroupVersionKind().Version { |
| 73 | + vObj := &unstructured.Unstructured{} |
| 74 | + vObj.SetGroupVersionKind(mObj.GroupVersionKind()) |
| 75 | + if err := scheme.Convert(uObj, vObj, nil); err != nil { |
| 76 | + return fmt.Errorf("converting Unstructured (%s) to Unstructured (%s): %w", |
| 77 | + uObj.GroupVersionKind(), vObj.GroupVersionKind(), err) |
| 78 | + } |
| 79 | + uObj = vObj |
| 80 | + } |
| 81 | + // Convert from Unstructured to PartialObjectMetadata the hard way |
| 82 | + jsonBytes, err := uObj.MarshalJSON() |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("encoding Unstructured as json: %w", err) |
| 85 | + } |
| 86 | + if err := json.Unmarshal(jsonBytes, mObj); err != nil { |
| 87 | + return fmt.Errorf("decoding json as PartialObjectMetadata: %w", err) |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// convertFromUnstructuredListIntoV1MetadataList converts from UnstructuredList |
| 93 | +// to v1.PartialObjectMetadataList. |
| 94 | +// |
| 95 | +// PartialObjectMetadataList isn't registered with the Scheme and has no |
| 96 | +// generated conversion code, so it has to be converted through JSON. |
| 97 | +// This is effectively Convert + DeepCopyInto. |
| 98 | +// |
| 99 | +// Warning: This code converts through JSON and should only be used for testing. |
| 100 | +func convertFromUnstructuredListIntoV1MetadataList(uList *unstructured.UnstructuredList, mList *metav1.PartialObjectMetadataList, scheme *runtime.Scheme) error { |
| 101 | + // Validate matching GroupKinds |
| 102 | + if uList.GroupVersionKind().GroupKind() != mList.GroupVersionKind().GroupKind() { |
| 103 | + return fmt.Errorf("converting UnstructuredList (%s) to PartialObjectMetadataList (%s): GroupKind mismatch", |
| 104 | + uList.GroupVersionKind().GroupKind(), mList.GroupVersionKind().GroupKind()) |
| 105 | + } |
| 106 | + // Convert to desired version |
| 107 | + if uList.GroupVersionKind().Version != mList.GroupVersionKind().Version { |
| 108 | + vList := &unstructured.UnstructuredList{} |
| 109 | + vList.SetGroupVersionKind(mList.GroupVersionKind()) |
| 110 | + if err := scheme.Convert(uList, vList, nil); err != nil { |
| 111 | + return fmt.Errorf("converting UnstructuredList (%s) to UnstructuredList (%s): %w", |
| 112 | + uList.GroupVersionKind(), vList.GroupVersionKind(), err) |
| 113 | + } |
| 114 | + uList = vList |
| 115 | + } |
| 116 | + // Convert from UnstructuredList to PartialObjectMetadataList the hard way |
| 117 | + jsonBytes, err := uList.MarshalJSON() |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("encoding UnstructuredList as json: %w", err) |
| 120 | + } |
| 121 | + if err := json.Unmarshal(jsonBytes, mList); err != nil { |
| 122 | + return fmt.Errorf("decoding json as PartialObjectMetadataList: %w", err) |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
0 commit comments