Skip to content

Commit

Permalink
refactor nodetemplates extension to enable top-level nodetemplate set…
Browse files Browse the repository at this point in the history
…tings
  • Loading branch information
git-ival authored and nickwsuse committed Apr 9, 2024
1 parent 2105cdb commit af2c4e7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions extensions/rke1/nodetemplates/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ func CreateAWSNodeTemplate(rancherClient *rancher.Client) (*nodetemplates.NodeTe
nodeTemplateConfig := &nodetemplates.NodeTemplate{}
config.LoadConfig(nodetemplates.NodeTemplateConfigurationFileKey, nodeTemplateConfig)

<<<<<<< HEAD
err := mergo.Merge(&nodeTemplate, nodeTemplateConfig, mergo.WithOverride)
=======
nodeTemplateFinal, err := nodeTemplate.MergeOverride(nodeTemplateConfig, nodetemplates.AmazonEC2NodeTemplateConfigurationFileKey)
>>>>>>> 1e4e99e (refactor nodetemplates extension to enable top-level nodetemplate settings)
if err != nil {
return nil, err
}

resp := &nodetemplates.NodeTemplate{}
<<<<<<< HEAD
err = rancherClient.Management.APIBaseClient.Ops.DoCreate(management.NodeTemplateType, nodeTemplate, resp)
=======
err = rancherClient.Management.APIBaseClient.Ops.DoCreate(management.NodeTemplateType, *nodeTemplateFinal, resp)
>>>>>>> 1e4e99e (refactor nodetemplates extension to enable top-level nodetemplate settings)
if err != nil {
return nil, err
}
Expand Down
75 changes: 75 additions & 0 deletions extensions/rke1/nodetemplates/nodetemplates.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package nodetemplates

import (
"maps"

"github.com/pkg/errors"
"github.com/rancher/norman/types"
"gopkg.in/yaml.v2"
"k8s.io/utils/strings/slices"
)

// The json/yaml config key for the node template config
Expand Down Expand Up @@ -42,3 +47,73 @@ type NodeTemplate struct {
UUID string `json:"uuid,omitempty" yaml:"uuid,omitempty"`
UseInternalIPAddress *bool `json:"useInternalIpAddress,omitempty" yaml:"useInternalIpAddress,omitempty"`
}

func providerTemplateConfigKeys() []string {
return []string{
AmazonEC2NodeTemplateConfigurationFileKey,
AzureNodeTemplateConfigurationFileKey,
HarvesterNodeTemplateConfigurationFileKey,
LinodeNodeTemplateConfigurationFileKey,
VmwareVsphereNodeTemplateConfigurationFileKey,
}
}

// MergeOverride merges two NodeTemplate objects by overriding fields from n1 with fields from n2
// - preserves fields not present in n2
// - deletes all provider keys except the specified providerTemplateConfigKey from both NodeTemplate objects before merging
//
// providerTemplateConfigKey: The key representing the provider template configuration to preserve during merging
//
// returns a pointer to the merged NodeTemplate and an error if any
func (n1 *NodeTemplate) MergeOverride(n2 *NodeTemplate, providerTemplateConfigKey string) (*NodeTemplate, error) {
var n1Data map[string]any
n1YAML, err := yaml.Marshal(&n1)
if err != nil {
return nil, errors.Wrap(err, "MergeOverride: ")
}
err = yaml.Unmarshal(n1YAML, &n1Data)
if err != nil {
return nil, errors.Wrap(err, "MergeOverride: ")
}

var n2Data map[string]any
n2YAML, err := yaml.Marshal(&n2)
if err != nil {
return nil, errors.Wrap(err, "MergeOverride: ")
}
err = yaml.Unmarshal(n2YAML, &n2Data)
if err != nil {
return nil, errors.Wrap(err, "MergeOverride: ")
}

configKeys := providerTemplateConfigKeys()
var keyPosition int
for pos, configKey := range configKeys {
if configKey == providerTemplateConfigKey {
keyPosition = pos
}
}

// Delete all other provider keys from both nodetemplates
keysToDelete := append(configKeys[:keyPosition], configKeys[keyPosition+1:]...)
maps.DeleteFunc(n1Data, func(k string, v any) bool {
return slices.Contains(keysToDelete, k) && k != providerTemplateConfigKey
})
maps.DeleteFunc(n2Data, func(k string, v any) bool {
return slices.Contains(keysToDelete, k) && k != providerTemplateConfigKey
})

maps.Copy(n1Data, n2Data)

tempData, err := yaml.Marshal(n1Data)
if err != nil {
return nil, errors.Wrap(err, "MergeOverride: ")
}

var mergedNodeTemplate = NodeTemplate{}
err = yaml.Unmarshal(tempData, &mergedNodeTemplate)
if err != nil {
return nil, errors.Wrap(err, "MergeOverride: ")
}
return &mergedNodeTemplate, nil
}

0 comments on commit af2c4e7

Please sign in to comment.