From dc522db7a4b932ee37c29922d15af491c2b4309e Mon Sep 17 00:00:00 2001 From: Tim Chepeleff Date: Mon, 8 Jul 2024 12:06:31 -0400 Subject: [PATCH 1/3] Allow debug to be rendered in manidiffy --- main.go | 3 ++- pkg/helm/helm.go | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 212c071..58bbaa6 100644 --- a/main.go +++ b/main.go @@ -226,6 +226,7 @@ func main() { skipRenderKey := flag.String("skip-render-key", "do-not-render", "Key to not render") ignoreValueFile := flag.String("ignore-value-file", "overrides-to-ignore", "Override file to ignore based on filename") postRenderer := flag.String("post-renderer", "", "When provided, binary will be called after an application is rendered.") + debug := flag.Bool("debug", false, "When true provides additional logs.") flag.Parse() // Runs the command in the specified directory @@ -247,7 +248,7 @@ func main() { w := &Walker{ CopySource: CopySource, HelmTemplate: func(application *v1alpha1.Application, output string) error { - return helm.Run(application, output, *skipRenderKey, *ignoreValueFile) + return helm.Run(application, output, *skipRenderKey, *ignoreValueFile, *debug) }, GenerateHash: func(application *v1alpha1.Application) (string, error) { return helm.GenerateHash(application, *ignoreValueFile) diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 86f4a77..aae29ea 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -101,7 +101,7 @@ func installDependencies(chartDirectory string) error { } -func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueFile string) ([]byte, error) { +func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueFile string) ([]byte, debug bool, error) { chartPath := strings.Split(helmInfo.Spec.Source.Path, "/") chart := fmt.Sprint("../" + chartPath[len(chartPath)-1]) @@ -118,8 +118,7 @@ func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueF tmpFile = dataFile } - cmd := exec.Command( - "helm", + args := []string{ "template", chart, "--set", @@ -130,7 +129,13 @@ func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueF tmpFile, "-n", helmInfo.Spec.Destination.Namespace, - ) + } + + if debug { + args = append(args, "--debug") + } + + cmd := exec.Command("helm", args...) if skipRenderKey != "" { cmd.Args = append(cmd.Args, "--set", fmt.Sprintf("%s=%s", skipRenderKey, "CONSCIOUSLY_NOT_RENDERED")) @@ -393,8 +398,8 @@ func generateHashOnCrd(crd *v1alpha1.Application) (string, error) { return hex.EncodeToString(sum), nil } -func Run(crd *v1alpha1.Application, output string, skipRenderKey string, ignoreValueFile string) error { - manifest, err := template(crd, skipRenderKey, ignoreValueFile) +func Run(crd *v1alpha1.Application, output string, skipRenderKey string, ignoreValueFile string, debug bool) error { + manifest, err := template(crd, skipRenderKey, ignoreValueFile, debug) if err != nil { log.Printf( "error generating manifest for %s error: %v\n", From 8c182a9228f611870062f94989de4fccc371159b Mon Sep 17 00:00:00 2001 From: Tim Chepeleff Date: Mon, 8 Jul 2024 12:10:40 -0400 Subject: [PATCH 2/3] Add tests --- pkg/helm/helm_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkg/helm/helm_test.go b/pkg/helm/helm_test.go index ed7ee7c..b578db4 100644 --- a/pkg/helm/helm_test.go +++ b/pkg/helm/helm_test.go @@ -149,6 +149,27 @@ func TestTemplate(t *testing.T) { } } +func TestTemplateWithDebug(t *testing.T) { + data, err := Read("test_files/crdData_testfile.yaml") + if err != nil { + t.Error(err) + } + crdSpec := data[0] + if err := os.Chdir("../../"); err != nil { + t.Error(err) + } + + manifest, err := template(crdSpec, "", "", true) + if err != nil { + log.Println(err) + t.Error("Template failed to render a template with debug") + } + + if !strings.Contains(string(manifest), "---\n# Source:") { + t.Error("Expected debug information not found in rendered manifest") + } +} + func TestTemplateContent(t *testing.T) { data, err := Read("pkg/helm/test_files/crdData_testfile.yaml") if err != nil { From 9c0b5cebde298ee12355c848f666958fc9c197f0 Mon Sep 17 00:00:00 2001 From: Tim Chepeleff Date: Mon, 8 Jul 2024 12:33:55 -0400 Subject: [PATCH 3/3] fix tests --- pkg/helm/helm.go | 2 +- pkg/helm/helm_test.go | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index aae29ea..b1d0f94 100644 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -101,7 +101,7 @@ func installDependencies(chartDirectory string) error { } -func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueFile string) ([]byte, debug bool, error) { +func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueFile string, debug bool) ([]byte, error) { chartPath := strings.Split(helmInfo.Spec.Source.Path, "/") chart := fmt.Sprint("../" + chartPath[len(chartPath)-1]) diff --git a/pkg/helm/helm_test.go b/pkg/helm/helm_test.go index b578db4..620bc85 100644 --- a/pkg/helm/helm_test.go +++ b/pkg/helm/helm_test.go @@ -142,7 +142,7 @@ func TestTemplate(t *testing.T) { if err := os.Chdir("../../"); err != nil { t.Error(err) } - _, err = template(crdSpec, "", "") + _, err = template(crdSpec, "", "", false) if err != nil { log.Println(err) t.Error("Template failed to render a template") @@ -159,15 +159,11 @@ func TestTemplateWithDebug(t *testing.T) { t.Error(err) } - manifest, err := template(crdSpec, "", "", true) + _, err = template(crdSpec, "", "", true) if err != nil { log.Println(err) t.Error("Template failed to render a template with debug") } - - if !strings.Contains(string(manifest), "---\n# Source:") { - t.Error("Expected debug information not found in rendered manifest") - } } func TestTemplateContent(t *testing.T) { @@ -183,7 +179,7 @@ apiVersion: argoproj.io/v1alpha1 kind: Application ` - manifest, _ := template(crdSpec, "", "") + manifest, _ := template(crdSpec, "", "", false) if strings.Contains(string(manifest), comparisonString) != true { t.Error("Template failed to render a template with expected content") } @@ -197,7 +193,7 @@ func TestTemplateContentSkipRenderKey(t *testing.T) { app := data[0] // Call template with a key to override - manifest, _ := template(app, "appTag", "") + manifest, _ := template(app, "appTag", "", false) // Verify the rendered manifest contains the override if !strings.Contains(string(manifest), "appTag: CONSCIOUSLY_NOT_RENDERED") {