From cda59d3be1e489c8aa0ca937cd4bf764cf068995 Mon Sep 17 00:00:00 2001 From: Maxime Santerre Date: Mon, 26 Feb 2024 21:10:46 -0800 Subject: [PATCH 1/6] Skip customize files + set workdir --- main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 80a998a..12951a2 100644 --- a/main.go +++ b/main.go @@ -157,9 +157,12 @@ func (w *Walker) Render(application *v1alpha1.Application, output string) error var render Renderer - // Figure out what renderer to use + // Figure out which renderer to use if application.Spec.Source.Helm != nil { render = w.HelmTemplate + } else if application.Spec.Source.Kustomize != nil { + log.Println("WARNING: kustomize not supported") + return nil } else { render = w.CopySource } @@ -213,6 +216,12 @@ func main() { postRenderer := flag.String("post-renderer", "", "When provided, binary will be called after an application is rendered.") flag.Parse() + // Runs the command in the specified directory + if flag.NArg() == 1 { + rootPath := os.Args[len(os.Args)-1] + os.Chdir(rootPath) + } + start := time.Now() if err := helm.VerifyRenderDir(*renderDir); err != nil { log.Fatal(err) From 030e98956853c112a16628be3f3fd2654e88b26a Mon Sep 17 00:00:00 2001 From: Maxime Santerre Date: Tue, 27 Feb 2024 09:42:13 -0800 Subject: [PATCH 2/6] Fix err check --- main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 12951a2..4400564 100644 --- a/main.go +++ b/main.go @@ -158,12 +158,13 @@ func (w *Walker) Render(application *v1alpha1.Application, output string) error var render Renderer // Figure out which renderer to use - if application.Spec.Source.Helm != nil { + switch { + case application.Spec.Source.Helm != nil: render = w.HelmTemplate - } else if application.Spec.Source.Kustomize != nil { + case application.Spec.Source.Kustomize != nil: log.Println("WARNING: kustomize not supported") return nil - } else { + default: render = w.CopySource } @@ -219,7 +220,10 @@ func main() { // Runs the command in the specified directory if flag.NArg() == 1 { rootPath := os.Args[len(os.Args)-1] - os.Chdir(rootPath) + err := os.Chdir(rootPath) + if err != nil { + log.Fatal(err) + } } start := time.Now() From a160f62e846a06189e31ec6dc45d316b6a87cffe Mon Sep 17 00:00:00 2001 From: Maxime Santerre Date: Tue, 27 Feb 2024 10:42:38 -0800 Subject: [PATCH 3/6] Handle error --- main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 4400564..e3f95e8 100644 --- a/main.go +++ b/main.go @@ -136,6 +136,9 @@ func (w *Walker) walk(inputPath, outputPath string, depth, maxDepth int, visited if hashGenerated != hash || emptyManifest { log.Printf("No match detected. Render: %s\n", crd.ObjectMeta.Name) if err := w.Render(crd, path); err != nil { + if strings.Contains(err.Error(), "not supported") { + continue + } return err } @@ -163,7 +166,7 @@ func (w *Walker) Render(application *v1alpha1.Application, output string) error render = w.HelmTemplate case application.Spec.Source.Kustomize != nil: log.Println("WARNING: kustomize not supported") - return nil + return fmt.Errorf("kustomize not supported") default: render = w.CopySource } From 7c14a3554ba1e1cab6033dd43e21ad7eeb281248 Mon Sep 17 00:00:00 2001 From: Maxime Santerre Date: Tue, 27 Feb 2024 11:09:25 -0800 Subject: [PATCH 4/6] Put the workdir option in a flag --- main.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index e3f95e8..c513201 100644 --- a/main.go +++ b/main.go @@ -210,6 +210,7 @@ func PostRender(command string) PostRenderer { func main() { root := flag.String("root", "bootstrap", "Directory to initially look for k8s manifests containing Argo applications. The root of the tree.") + workdir := flag.String("workdir", ".", "Directory to run the command in.") renderDir := flag.String("output", ".zz.auto-generated", "Path to store the compiled Argo applications.") maxDepth := flag.Int("max-depth", InfiniteDepth, "Maximum depth for the depth first walk.") hashStore := flag.String("hash-store", "sumfile", "The hashing backend to use. Can be `sumfile` or `json`.") @@ -221,12 +222,9 @@ func main() { flag.Parse() // Runs the command in the specified directory - if flag.NArg() == 1 { - rootPath := os.Args[len(os.Args)-1] - err := os.Chdir(rootPath) - if err != nil { - log.Fatal(err) - } + err := os.Chdir(*workdir) + if err != nil { + log.Fatal(err) } start := time.Now() From 43d4f619a29e3a9a3dfcf43ad28bcc52d35f42a3 Mon Sep 17 00:00:00 2001 From: Maxime Santerre Date: Tue, 27 Feb 2024 14:03:49 -0800 Subject: [PATCH 5/6] Use native errors --- main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c513201..1cb792d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "flag" "fmt" "log" @@ -136,7 +137,7 @@ func (w *Walker) walk(inputPath, outputPath string, depth, maxDepth int, visited if hashGenerated != hash || emptyManifest { log.Printf("No match detected. Render: %s\n", crd.ObjectMeta.Name) if err := w.Render(crd, path); err != nil { - if strings.Contains(err.Error(), "not supported") { + if errors.Is(err, errors.ErrUnsupported) { continue } return err @@ -166,7 +167,7 @@ func (w *Walker) Render(application *v1alpha1.Application, output string) error render = w.HelmTemplate case application.Spec.Source.Kustomize != nil: log.Println("WARNING: kustomize not supported") - return fmt.Errorf("kustomize not supported") + return errors.ErrUnsupported default: render = w.CopySource } @@ -224,7 +225,7 @@ func main() { // Runs the command in the specified directory err := os.Chdir(*workdir) if err != nil { - log.Fatal(err) + log.Fatal("Could not set workdir: ", err) } start := time.Now() From b7e06ba08338b95b58a2768a280c8e174a12f5de Mon Sep 17 00:00:00 2001 From: Maxime Santerre Date: Tue, 27 Feb 2024 14:09:18 -0800 Subject: [PATCH 6/6] Go version compatibility --- main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 1cb792d..16bb007 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,8 @@ import ( const InfiniteDepth = -1 +var ErrKustomizeNotSupported = errors.New("kustomize not supported") + // Renderer is a function that can render an Argo application. type Renderer func(*v1alpha1.Application, string) error @@ -137,7 +139,7 @@ func (w *Walker) walk(inputPath, outputPath string, depth, maxDepth int, visited if hashGenerated != hash || emptyManifest { log.Printf("No match detected. Render: %s\n", crd.ObjectMeta.Name) if err := w.Render(crd, path); err != nil { - if errors.Is(err, errors.ErrUnsupported) { + if errors.Is(err, ErrKustomizeNotSupported) { continue } return err @@ -167,7 +169,7 @@ func (w *Walker) Render(application *v1alpha1.Application, output string) error render = w.HelmTemplate case application.Spec.Source.Kustomize != nil: log.Println("WARNING: kustomize not supported") - return errors.ErrUnsupported + return ErrKustomizeNotSupported default: render = w.CopySource }