Skip to content

Conversation

ranatrk
Copy link
Contributor

@ranatrk ranatrk commented Dec 12, 2022

Closes #2016

What changed?

  • Listing templates can now list all templates with the kinds supported
  • template-kind flag added to get templates cmd to filter templates based on the template kind

Why was this change made?
The template kind was previously hard coded to only use CAPITemplates, and so when listing templates, any GitOpsTemplate was not retrieved in the results

How was this change implemented?
Add template-kind flag to get templates with cli
Fix listing templates to include gitopstemplates
Add defaults to template kind when listing templates

How did you validate the change?

Release notes

Documentation Changes

@ranatrk ranatrk added bug Something isn't working bug_acceptance bugs found during qa / acceptance testing labels Dec 12, 2022
@ranatrk ranatrk force-pushed the 2016-fix-listing-gitops-templates branch from 840db45 to 21cdf64 Compare December 13, 2022 09:48
@ranatrk ranatrk marked this pull request as ready for review December 13, 2022 11:00
@ranatrk ranatrk requested a review from a team December 13, 2022 11:01
@ranatrk ranatrk force-pushed the 2016-fix-listing-gitops-templates branch from 21cdf64 to 9e16bf9 Compare December 13, 2022 11:03
if flags.ListTemplateParameters {
if len(args) == 0 {
return errors.New("template name is required")
}

return templates.GetTemplateParameters(templates.CAPITemplateKind, args[0], flags.TemplateNamespace, client, w)
//Default template kind for GetTemplateParameters is CAPITemplate
if templateKind == "" {
Copy link
Contributor

@bigkevmcd bigkevmcd Dec 13, 2022

Choose a reason for hiding this comment

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

Under what circumstances is templateKind == "" ?

https://github.com/weaveworks/weave-gitops-enterprise/blob/9e16bf9e47fffad096000e078a269dc3ba92aa65/cmd/gitops/app/get/templates/cmd.go#L93 should prevent this?

Shouldn't it set it to the CAPITemplate kind?

Copy link
Contributor Author

@ranatrk ranatrk Dec 13, 2022

Choose a reason for hiding this comment

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

the DefaultTemplateKind is an empty string, more of a "all" indicator
If its set to CAPITemplate, then when using get templates only CAPITemplates will be retrieved, where if it was empty all types are retrieved (handeled here)

There are 3 cases I tried to handle for geting templates without parameters, [GitOpsTemplate,CAPITemplate, all-supported-templates]

When getting parameters, If trying to get it for all templates then it would fail for some template kinds as the template with the name provided is of only one type, so I defaulted to CAPITemplates
(This was mainly to default to all for get templates, and default to CAPITemplates when getting get templates TEMP_NAME --list-parameters

@ranatrk ranatrk force-pushed the 2016-fix-listing-gitops-templates branch from 10019c1 to fca2a05 Compare December 15, 2022 12:33
@ranatrk ranatrk requested a review from bigkevmcd December 15, 2022 12:33
Copy link
Contributor

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Not necessarily for this branch, but perhaps we could look at using https://pkg.go.dev/k8s.io/cli-runtime/pkg/printers ?

(specifically the ResourcePrinter implementations)

And also not for this branch, but what if someone wants a YAML or JSON format version?

Maybe we should open an issue so that we can support

-o, --output='':
	Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath,
	jsonpath-as-json, jsonpath-file, custom-columns, custom-columns-file, wide). See custom columns
	[https://kubernetes.io/docs/reference/kubectl/#custom-columns], golang template
	[http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template
	[https://kubernetes.io/docs/reference/kubectl/jsonpath/].

*t = TemplateKind(v)
return nil

} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simpify this code a bit and remove the else if bits because each clause is returning?

}

// Return a string representation of all supported template Kinds
func TemplateKindsString() string {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't need to be exported, it's not used outside the package?

return nil

} else {
return errors.New(`Template kind not found. Supported templates: ` + TemplateKindsString())
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, but you could also do the more common:

fmt.Errorf("template kind not found, supported templates: %s", templateKindsString())

Generally Go errors don't start with initial caps, as they tend to get concatenated together see https://github.com/golang/go/wiki/CodeReviewComments#error-strings for a bit more on this.

@ranatrk ranatrk force-pushed the 2016-fix-listing-gitops-templates branch from d1d0bf7 to 3a3176f Compare December 18, 2022 11:47
@ranatrk ranatrk merged commit 6f83a21 into main Dec 18, 2022
@ranatrk ranatrk deleted the 2016-fix-listing-gitops-templates branch December 18, 2022 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug_acceptance bugs found during qa / acceptance testing bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLI] gitops can not list GitOpsTemplates
2 participants