sigs.k8s.io/cluster-api-provider-aws@v1.5.5/controllers/awsmachine_annotations.go (about) 1 /* 2 Copyright 2018 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 controllers 18 19 import ( 20 "encoding/json" 21 22 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 23 ) 24 25 // updateMachineAnnotationJSON updates the `annotation` on `machine` with 26 // `content`. `content` in this case should be a `map[string]interface{}` 27 // suitable for turning into JSON. This `content` map will be marshalled into a 28 // JSON string before being set as the given `annotation`. 29 func (r *AWSMachineReconciler) updateMachineAnnotationJSON(machine *infrav1.AWSMachine, annotation string, content map[string]interface{}) error { 30 b, err := json.Marshal(content) 31 if err != nil { 32 return err 33 } 34 35 r.updateMachineAnnotation(machine, annotation, string(b)) 36 return nil 37 } 38 39 // updateMachineAnnotation updates the `annotation` on the given `machine` with 40 // `content`. 41 func (r *AWSMachineReconciler) updateMachineAnnotation(machine *infrav1.AWSMachine, annotation, content string) { 42 // Get the annotations 43 annotations := machine.GetAnnotations() 44 45 // Set our annotation to the given content. 46 if annotations != nil { 47 annotations[annotation] = content 48 } 49 50 // Update the machine object with these annotations 51 machine.SetAnnotations(annotations) 52 } 53 54 // Returns a map[string]interface from a JSON annotation. 55 // This method gets the given `annotation` from the `machine` and unmarshalls it 56 // from a JSON string into a `map[string]interface{}`. 57 func (r *AWSMachineReconciler) machineAnnotationJSON(machine *infrav1.AWSMachine, annotation string) (map[string]interface{}, error) { 58 out := map[string]interface{}{} 59 60 jsonAnnotation := r.machineAnnotation(machine, annotation) 61 if len(jsonAnnotation) == 0 { 62 return out, nil 63 } 64 65 err := json.Unmarshal([]byte(jsonAnnotation), &out) 66 if err != nil { 67 return out, err 68 } 69 70 return out, nil 71 } 72 73 // Fetches the specific machine annotation. 74 func (r *AWSMachineReconciler) machineAnnotation(machine *infrav1.AWSMachine, annotation string) string { 75 return machine.GetAnnotations()[annotation] 76 }