sigs.k8s.io/cluster-api@v1.7.1/util/conditions/unstructured.go (about) 1 /* 2 Copyright 2020 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 conditions 18 19 import ( 20 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 21 "k8s.io/apimachinery/pkg/runtime" 22 "sigs.k8s.io/controller-runtime/pkg/log" 23 24 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 25 "sigs.k8s.io/cluster-api/util" 26 ) 27 28 // UnstructuredGetter return a Getter object that can read conditions from an Unstructured object. 29 // Important. This method should be used only with types implementing Cluster API conditions. 30 func UnstructuredGetter(u *unstructured.Unstructured) Getter { 31 return &unstructuredWrapper{Unstructured: u} 32 } 33 34 // UnstructuredSetter return a Setter object that can set conditions from an Unstructured object. 35 // Important. This method should be used only with types implementing Cluster API conditions. 36 func UnstructuredSetter(u *unstructured.Unstructured) Setter { 37 return &unstructuredWrapper{Unstructured: u} 38 } 39 40 type unstructuredWrapper struct { 41 *unstructured.Unstructured 42 } 43 44 // GetConditions returns the list of conditions from an Unstructured object. 45 // 46 // NOTE: Due to the constraints of JSON-unmarshal, this operation is to be considered best effort. 47 // In more details: 48 // - Errors during JSON-unmarshal are ignored and a empty collection list is returned. 49 // - It's not possible to detect if the object has an empty condition list or if it does not implement conditions; 50 // in both cases the operation returns an empty slice is returned. 51 // - If the object doesn't implement conditions on under status as defined in Cluster API, 52 // JSON-unmarshal matches incoming object keys to the keys; this can lead to conditions values partially set. 53 func (c *unstructuredWrapper) GetConditions() clusterv1.Conditions { 54 conditions := clusterv1.Conditions{} 55 if err := util.UnstructuredUnmarshalField(c.Unstructured, &conditions, "status", "conditions"); err != nil { 56 return nil 57 } 58 return conditions 59 } 60 61 // SetConditions set the conditions into an Unstructured object. 62 // 63 // NOTE: Due to the constraints of JSON-unmarshal, this operation is to be considered best effort. 64 // In more details: 65 // - Errors during JSON-unmarshal are ignored and a empty collection list is returned. 66 // - It's not possible to detect if the object has an empty condition list or if it does not implement conditions; 67 // in both cases the operation returns an empty slice is returned. 68 func (c *unstructuredWrapper) SetConditions(conditions clusterv1.Conditions) { 69 v := make([]interface{}, 0, len(conditions)) 70 for i := range conditions { 71 m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&conditions[i]) 72 if err != nil { 73 log.Log.Error(err, "Failed to convert Condition to unstructured map. This error shouldn't have occurred, please file an issue.", "groupVersionKind", c.GroupVersionKind(), "name", c.GetName(), "namespace", c.GetNamespace()) 74 continue 75 } 76 v = append(v, m) 77 } 78 // unstructured.SetNestedField returns an error only if value cannot be set because one of 79 // the nesting levels is not a map[string]interface{}; this is not the case so the error should never happen here. 80 err := unstructured.SetNestedField(c.Unstructured.Object, v, "status", "conditions") 81 if err != nil { 82 log.Log.Error(err, "Failed to set Conditions on unstructured object. This error shouldn't have occurred, please file an issue.", "groupVersionKind", c.GroupVersionKind(), "name", c.GetName(), "namespace", c.GetNamespace()) 83 } 84 }