sigs.k8s.io/cluster-api@v1.6.3/exp/runtime/topologymutation/variables.go (about) 1 /* 2 Copyright 2022 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 topologymutation 18 19 import ( 20 "strconv" 21 22 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 23 24 patchvariables "sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/variables" 25 ) 26 27 // TODO: Add func for validating received variables are of the expected types 28 // TODO: Add func for converting complex variables into go structs and/or other basic types. 29 30 // GetVariable get the variable value. 31 func GetVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (*apiextensionsv1.JSON, bool, error) { 32 value, err := patchvariables.GetVariableValue(templateVariables, variableName) 33 if err != nil { 34 if patchvariables.IsNotFoundError(err) { 35 return nil, false, nil 36 } 37 return nil, false, err 38 } 39 return value, true, nil 40 } 41 42 // GetStringVariable get the variable value as a string. 43 func GetStringVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (string, bool, error) { 44 value, found, err := GetVariable(templateVariables, variableName) 45 if !found || err != nil { 46 return "", found, err 47 } 48 49 // Unquote the JSON string. 50 stringValue, err := strconv.Unquote(string(value.Raw)) 51 if err != nil { 52 return "", true, err 53 } 54 return stringValue, true, nil 55 }