sigs.k8s.io/cluster-api-provider-azure@v1.14.3/exp/controllers/azuremachinepool_annotations.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 controllers
    18  
    19  import (
    20  	"encoding/json"
    21  )
    22  
    23  // AnnotationJSON returns a map[string]interface from a JSON annotation.
    24  // This method gets the given `annotation` from an `annotationReaderWriter` and unmarshalls it
    25  // from a JSON string into a `map[string]interface{}`.
    26  func (ampr *AzureMachinePoolReconciler) AnnotationJSON(rw annotationReaderWriter, annotation string) (map[string]interface{}, error) {
    27  	out := map[string]interface{}{}
    28  
    29  	jsonAnnotation := ampr.Annotation(rw, annotation)
    30  	if jsonAnnotation == "" {
    31  		return out, nil
    32  	}
    33  
    34  	err := json.Unmarshal([]byte(jsonAnnotation), &out)
    35  	if err != nil {
    36  		return out, err
    37  	}
    38  
    39  	return out, nil
    40  }
    41  
    42  // Annotation fetches the specific machine annotation.
    43  func (ampr *AzureMachinePoolReconciler) Annotation(rw annotationReaderWriter, annotation string) string {
    44  	return rw.GetAnnotations()[annotation]
    45  }