Skip to content

Commit

Permalink
Merge pull request #9670 from lubronzhan/topic/add_func_to_topologymu…
Browse files Browse the repository at this point in the history
…ltation_variable_extractor

🌱Add more helper functions in topologymutation varaible.go to help unmarshal variables
  • Loading branch information
k8s-ci-robot committed Jan 31, 2024
2 parents caa378c + ec9d06b commit 2fd1e7d
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 131 deletions.
27 changes: 27 additions & 0 deletions exp/runtime/topologymutation/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package topologymutation

import (
patchvariables "sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/variables"
)

// IsNotFoundError checks if the passed error is a notFoundError.
// It exposes same function in internal package so that user can directly call.
func IsNotFoundError(err error) bool {
return patchvariables.IsNotFoundError(err)
}
62 changes: 48 additions & 14 deletions exp/runtime/topologymutation/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,73 @@ limitations under the License.
package topologymutation

import (
"encoding/json"
"strconv"
"strings"

"github.com/pkg/errors"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

patchvariables "sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/variables"
)

// TODO: Add func for validating received variables are of the expected types
// TODO: Add func for converting complex variables into go structs and/or other basic types.
// TODO: Add func for validating received variables are of the expected types.

// GetVariable get the variable value.
func GetVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (*apiextensionsv1.JSON, bool, error) {
func GetVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (*apiextensionsv1.JSON, error) {
value, err := patchvariables.GetVariableValue(templateVariables, variableName)
if err != nil {
if patchvariables.IsNotFoundError(err) {
return nil, false, nil
}
return nil, false, err
return nil, err
}
return value, true, nil
return value, nil
}

// GetStringVariable get the variable value as a string.
func GetStringVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (string, bool, error) {
value, found, err := GetVariable(templateVariables, variableName)
if !found || err != nil {
return "", found, err
func GetStringVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (string, error) {
value, err := GetVariable(templateVariables, variableName)
if err != nil {
return "", err
}

// Unquote the JSON string.
stringValue, err := strconv.Unquote(string(value.Raw))
if err != nil {
return "", true, err
return "", err
}
return stringValue, nil
}

// GetBoolVariable get the value as a bool.
func GetBoolVariable(templateVariables map[string]apiextensionsv1.JSON, variableName string) (bool, error) {
value, err := GetVariable(templateVariables, variableName)
if err != nil {
return false, err
}
return stringValue, true, nil

// Parse the JSON bool.
boolValue, err := strconv.ParseBool(string(value.Raw))
if err != nil {
return false, err
}
return boolValue, nil
}

// GetObjectVariableInto gets variable's string value then unmarshal it into
// object passed from 'into'.
func GetObjectVariableInto(templateVariables map[string]apiextensionsv1.JSON, variableName string, into interface{}) error {
value, err := GetVariable(templateVariables, variableName)
if err != nil {
return err
}

if err := json.Unmarshal(sanitizeJSON(value.Raw), into); err != nil {
return errors.Wrapf(err, "failed to unmarshal variable json %q into %q", string(value.Raw), into)
}

return nil
}

func sanitizeJSON(input []byte) (output []byte) {
output = []byte(strings.ReplaceAll(string(input), "\\", ""))
return output
}
Loading

0 comments on commit 2fd1e7d

Please sign in to comment.