sigs.k8s.io/cluster-api@v1.6.3/internal/controllers/topology/cluster/patches/variables/merge.go (about)

     1  /*
     2  Copyright 2021 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 variables
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"github.com/pkg/errors"
    23  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    24  )
    25  
    26  // MergeVariableMaps merges variables.
    27  // This func is useful when merging global and template-specific variables.
    28  // NOTE: In case a variable exists in multiple maps, the variable from the latter map is preserved.
    29  // NOTE: The builtin variable object is merged instead of simply overwritten.
    30  func MergeVariableMaps(variableMaps ...map[string]apiextensionsv1.JSON) (map[string]apiextensionsv1.JSON, error) {
    31  	res := make(map[string]apiextensionsv1.JSON)
    32  
    33  	for _, variableMap := range variableMaps {
    34  		for variableName, variableValue := range variableMap {
    35  			// If the variable already exists and is the builtin variable, merge it.
    36  			if _, ok := res[variableName]; ok && variableName == BuiltinsName {
    37  				mergedV, err := mergeBuiltinVariables(res[variableName], variableValue)
    38  				if err != nil {
    39  					return nil, errors.Wrapf(err, "failed to merge builtin variables")
    40  				}
    41  				res[variableName] = *mergedV
    42  				continue
    43  			}
    44  			res[variableName] = variableValue
    45  		}
    46  	}
    47  
    48  	return res, nil
    49  }
    50  
    51  // mergeBuiltinVariables merges builtin variable objects.
    52  // NOTE: In case a variable exists in multiple builtin variables, the variable from the latter map is preserved.
    53  func mergeBuiltinVariables(variableList ...apiextensionsv1.JSON) (*apiextensionsv1.JSON, error) {
    54  	builtins := &Builtins{}
    55  
    56  	// Unmarshal all variables into builtins.
    57  	// NOTE: This accumulates the fields on the builtins.
    58  	// Fields will be overwritten by later Unmarshals if fields are
    59  	// set on multiple variables.
    60  	for _, variable := range variableList {
    61  		if err := json.Unmarshal(variable.Raw, builtins); err != nil {
    62  			return nil, errors.Wrapf(err, "failed to unmarshal builtin variable")
    63  		}
    64  	}
    65  
    66  	// Marshal builtins to JSON.
    67  	builtinVariableJSON, err := json.Marshal(builtins)
    68  	if err != nil {
    69  		return nil, errors.Wrapf(err, "failed to marshal builtin variable")
    70  	}
    71  
    72  	return &apiextensionsv1.JSON{
    73  		Raw: builtinVariableJSON,
    74  	}, nil
    75  }