Skip to content

Add skip-validation flag; Remove default save path #194

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

Merged
merged 3 commits into from
Apr 18, 2025
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
41 changes: 26 additions & 15 deletions cmd/template-resolver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/pmezard/go-difflib/difflib"
"gopkg.in/yaml.v3"

"github.com/stolostron/go-template-utils/v6/cmd/template-resolver/utils"
)
Expand Down Expand Up @@ -67,27 +68,37 @@ func cliTest(testName string) func(t *testing.T) {
}
}

kcPath := ""
clusterName := ""
hubNS := ""
saveHubResources := ""
configBytes, readErr := readFile(filePrefix + "config.yaml")
if readErr != nil {
if !os.IsNotExist(readErr) {
t.Fatal("Failed to read config file:", readErr)
}
}

var tmplResolver utils.TemplateResolver

tmpDir := t.TempDir()

if strings.HasSuffix(testName, "_hub") {
kcPath = kubeconfigPath
clusterName = "local-cluster"
hubNS = "policies"
saveHubResources = filepath.Join(tmpDir, "save_hub_resources.yaml")
tmplResolver.HubKubeConfigPath = kubeconfigPath
tmplResolver.ClusterName = "local-cluster"
tmplResolver.HubNamespace = "policies"
tmplResolver.SaveHubResources = filepath.Join(tmpDir, "save_hub_resources.yaml")
}

objNamespace := "my-obj-namespace"
objName := "my-obj-name"
tmplResolver.SaveResources = filepath.Join(tmpDir, "save_resources.yaml")
tmplResolver.ObjNamespace = "my-obj-namespace"
tmplResolver.ObjName = "my-obj-name"

saveResources := filepath.Join(tmpDir, "save_resources.yaml")
// Overwrite configuration with test configuration
if len(configBytes) > 0 {
err = yaml.Unmarshal(configBytes, &tmplResolver)
if err != nil {
t.Fatal("Failed to parse config file:", err)
}
}

resolvedYAML, err := utils.ProcessTemplate(inputBytes, kcPath, clusterName,
hubNS, objNamespace, objName, saveResources, saveHubResources)
resolvedYAML, err := tmplResolver.ProcessTemplate(inputBytes)
if err != nil {
if len(errorBytes) == 0 {
t.Fatal(err)
Expand All @@ -104,10 +115,10 @@ func cliTest(testName string) func(t *testing.T) {
// if testName ends with "_hub" as well.
// otherwise test only managedCluster resources.
if strings.HasSuffix(testName, "_hub") {
compareSaveResources(t, testName, saveHubResources, true)
compareSaveResources(t, testName, tmplResolver.SaveHubResources, true)
}

compareSaveResources(t, testName, saveResources, false)
compareSaveResources(t, testName, tmplResolver.SaveResources, false)

if !bytes.Equal(expectedBytes, resolvedYAML) {
//nolint: forbidigo
Expand Down
11 changes: 11 additions & 0 deletions cmd/template-resolver/testdata/setup/data-yaml.configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: data-yaml
namespace: policies
data:
config.yml: |
colors:
- fuchsia
- magenta
- cyan
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
skipPolicyValidation: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test: |
{{ toYAML (set (fromYAML (fromConfigMap "policies" "data-yaml" "config.yml")) "greeting" "hello") | autoindent }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test: |
colors:
- fuchsia
- magenta
- cyan
greeting: hello
51 changes: 31 additions & 20 deletions cmd/template-resolver/utils/resolver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

// Struct representing the template-resolver command
type TemplateResolver struct {
hubKubeConfigPath string
clusterName string
hubNamespace string
objNamespace string
objName string
saveResources string
// saveHubResources Output doesn't include "ManagedClusters" resources,
saveHubResources string
HubKubeConfigPath string `yaml:"hubKubeConfigPath"`
ClusterName string `yaml:"clusterName"`
HubNamespace string `yaml:"hubNamespace"`
ObjNamespace string `yaml:"objNamespace"`
ObjName string `yaml:"objName"`
SaveResources string `yaml:"saveResources"`
// saveHubResources Output doesn't include "ManagedClusters" resources
SaveHubResources string `yaml:"saveHubResources"`
SkipPolicyValidation bool `yaml:"skipPolicyValidation"`
}

func (t *TemplateResolver) GetCmd() *cobra.Command {
Expand All @@ -36,53 +37,64 @@ func (t *TemplateResolver) GetCmd() *cobra.Command {
// Initialize variables used to collect user input from CLI flags
// Add template-resolver command and parse flags
templateResolverCmd.Flags().StringVar(
&t.hubKubeConfigPath,
&t.HubKubeConfigPath,
"hub-kubeconfig",
"",
"the input kubeconfig to also resolve hub templates",
)

templateResolverCmd.Flags().StringVar(
&t.clusterName,
&t.ClusterName,
"cluster-name",
"",
"the cluster name to use for the .ManagedClusterName template variable when resolving hub cluster templates",
)

templateResolverCmd.Flags().StringVar(
&t.hubNamespace,
&t.HubNamespace,
"hub-namespace",
"",
"the namespace on the hub to restrict namespaced lookups to when resolving hub templates",
)

templateResolverCmd.Flags().StringVar(
&t.objNamespace,
&t.ObjNamespace,
"object-namespace",
"",
"the object namespace to use for the .ObjectNamespace template variable when policy uses namespaceSelector",
)

templateResolverCmd.Flags().StringVar(
&t.objName,
&t.ObjName,
"object-name",
"",
"the object namespace to use for the .ObjectName template variable "+
"when policy uses namespaceSelector or objectSelector",
)

templateResolverCmd.Flags().StringVar(
&t.saveResources,
&t.SaveResources,
"save-resources",
"s",
"",
"the path to save the output containing resources used. "+
"This output can be used as input resources for the dry-run CLI or for local environment testing.",
)

templateResolverCmd.Flags().StringVar(
&t.saveHubResources,
&t.SaveHubResources,
"save-hub-resources",
"s",
"",
"the path to save the output containing resources used in the hub cluster. "+
"This output can be used as input resources for the dry-run CLI or for local environment testing.",
)

templateResolverCmd.Flags().BoolVar(
&t.SkipPolicyValidation,
"skip-policy-validation",
false,
"Handle the input directly as a Go template, skipping any surrounding policy field validations.",
)

return templateResolverCmd
}

Expand All @@ -108,7 +120,7 @@ func (t *TemplateResolver) resolveTemplates(cmd *cobra.Command, args []string) e
}

// Validate flag args
if t.hubKubeConfigPath != "" && t.clusterName == "" {
if t.HubKubeConfigPath != "" && t.ClusterName == "" {
return errors.New(
"when a hub kubeconfig is provided, you must provide a managed cluster name for hub templates to resolve " +
"using the cluster-name argument",
Expand All @@ -120,8 +132,7 @@ func (t *TemplateResolver) resolveTemplates(cmd *cobra.Command, args []string) e
return fmt.Errorf("error handling YAML file input: %w", err)
}

resolvedYAML, err := ProcessTemplate(yamlBytes, t.hubKubeConfigPath,
t.clusterName, t.hubNamespace, t.objNamespace, t.objName, t.saveResources, t.saveHubResources)
resolvedYAML, err := t.ProcessTemplate(yamlBytes)
if err != nil {
cmd.Printf("error processing templates: %s\n", err.Error())

Expand Down
Loading