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

Allow helm --debug to be conditionally rendered #26

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could add a way (this or other PR) to pass this and any other additional arguments to Helm, something like additionalArgs="--debug --other=x" 🤷

BTW, you could also set HELM_DEBUG to enable debugging 🤷

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, two great points!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@d1egoaz - I'll work on getting additionalHelmArgs added 👍

flag.Parse()

// Runs the command in the specified directory
Expand All @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, debug bool) ([]byte, error) {

chartPath := strings.Split(helmInfo.Spec.Source.Path, "/")
chart := fmt.Sprint("../" + chartPath[len(chartPath)-1])
Expand All @@ -118,8 +118,7 @@ func template(helmInfo *v1alpha1.Application, skipRenderKey string, ignoreValueF
tmpFile = dataFile
}

cmd := exec.Command(
"helm",
args := []string{
"template",
chart,
"--set",
Expand All @@ -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"))
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 20 additions & 3 deletions pkg/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,30 @@ 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")
}
}

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)
}

_, err = template(crdSpec, "", "", true)
if err != nil {
log.Println(err)
t.Error("Template failed to render a template with debug")
}
}

func TestTemplateContent(t *testing.T) {
data, err := Read("pkg/helm/test_files/crdData_testfile.yaml")
if err != nil {
Expand All @@ -162,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")
}
Expand All @@ -176,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") {
Expand Down