Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) add more template functions #851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 86 additions & 6 deletions controllers/template_instantiation.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func fecthClusterObjects(ctx context.Context, config *rest.Config, c client.Clie
return result, nil
}

func instantiateTemplateValues(ctx context.Context, config *rest.Config, c client.Client,
func instantiateTemplateValues(ctx context.Context, config *rest.Config, c client.Client, //nolint: funlen // adding few closures
clusterType libsveltosv1beta1.ClusterType, clusterNamespace, clusterName, requestorName, values string,
mgmtResources map[string]*unstructured.Unstructured, logger logr.Logger) (string, error) {

Expand Down Expand Up @@ -182,20 +182,90 @@ func instantiateTemplateValues(ctx context.Context, config *rest.Config, c clien
return ""
}

var uObject unstructured.Unstructured
uObject.SetUnstructuredContent(u)
uObject.SetManagedFields(nil)
uObject.SetResourceVersion("")
uObject.SetUID("")
uObject := resetFields(u)

data, err := yaml.Marshal(uObject.UnstructuredContent())
if err != nil {
// Swallow errors inside of a template.
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed with err %v", err))
return ""
}

return strings.TrimSuffix(string(data), "\n")
}
funcMap["getField"] = func(id string, fields string) interface{} {
u, ok := objects.MgmtResources[id]
if !ok {
// Swallow errors inside of a template.
return ""
}

v, isPresent, err := unstructured.NestedFieldCopy(u, strings.Split(fields, ".")...)
if err != nil || !isPresent {
return ""
}

return v
}
funcMap["removeField"] = func(id, fields string) string {
u, ok := objects.MgmtResources[id]
if !ok {
// Swallow errors inside of a template.
return ""
}

unstructured.RemoveNestedField(u, strings.Split(fields, ".")...)
uObject := resetFields(u)

data, err := yaml.Marshal(uObject)
if err != nil {
// Swallow errors inside of a template.
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed with err %v", err))
return ""
}

return strings.TrimSuffix(string(data), "\n")
}
funcMap["setField"] = func(id, fields string, value any) string {
u, ok := objects.MgmtResources[id]
if !ok {
// Swallow errors inside of a template.
return ""
}

err := unstructured.SetNestedField(u, value, strings.Split(fields, ".")...)
if err != nil {
// Swallow errors inside of a template.
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed with err %v", err))
return ""
}

uObject := resetFields(u)

data, err := yaml.Marshal(uObject.UnstructuredContent())
if err != nil {
// Swallow errors inside of a template.
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed with err %v", err))
return ""
}

return strings.TrimSuffix(string(data), "\n")
}
funcMap["chainRemoveField"] = func(u map[string]interface{}, fields string) map[string]interface{} {
unstructured.RemoveNestedField(u, strings.Split(fields, ".")...)

return u
}
funcMap["chainSetField"] = func(u map[string]interface{}, fields string, value any) map[string]interface{} {
err := unstructured.SetNestedField(u, value, strings.Split(fields, ".")...)
if err != nil {
// Swallow errors inside of a template.
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed with err %v", err))
return nil
}

return u
}

templateName := getTemplateName(clusterNamespace, clusterName, requestorName)
tmpl, err := template.New(templateName).Option("missingkey=error").Funcs(funcMap).Parse(values)
Expand All @@ -217,3 +287,13 @@ func instantiateTemplateValues(ctx context.Context, config *rest.Config, c clien
func getTemplateName(clusterNamespace, clusterName, requestorName string) string {
return fmt.Sprintf("%s-%s-%s", clusterNamespace, clusterName, requestorName)
}

func resetFields(u map[string]interface{}) unstructured.Unstructured {
var uObject unstructured.Unstructured
uObject.SetUnstructuredContent(u)
uObject.SetManagedFields(nil)
uObject.SetResourceVersion("")
uObject.SetUID("")

return uObject
}
Loading