Skip to content

pkg/generator: Create generic file rendering function#310

Merged
theishshah merged 13 commits intooperator-framework:masterfrom
theishshah:generic-render-func
Jun 13, 2018
Merged

pkg/generator: Create generic file rendering function#310
theishshah merged 13 commits intooperator-framework:masterfrom
theishshah:generic-render-func

Conversation

@theishshah
Copy link
Copy Markdown
Contributor

Issue #303

@theishshah theishshah requested a review from rithujohn191 June 8, 2018 18:25
@theishshah
Copy link
Copy Markdown
Contributor Author

This had been tested against the memcached user guide example and appears to be in working order

@theishshah
Copy link
Copy Markdown
Contributor Author

So generator.go still needs to be cleaned up, but this offers a single render function instead of the 9 separate files and functions which did functionally the same work of loading a template and replacing values from a struct

@theishshah theishshah changed the title pkg/generator: Create generic file rendering function [WIP] pkg/generator: Create generic file rendering function Jun 8, 2018
@theishshah theishshah changed the title [WIP] pkg/generator: Create generic file rendering function pkg/generator: Create generic file rendering function Jun 11, 2018
@theishshah
Copy link
Copy Markdown
Contributor Author

Fixed issue with creating olm directory

@rithujohn191 rithujohn191 requested a review from hasbro17 June 11, 2018 17:39
Copy link
Copy Markdown
Contributor

@rithujohn191 rithujohn191 left a comment

Choose a reason for hiding this comment

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

Overall looks good. Just a few nits

Comment thread pkg/generator/generator.go Outdated
return nil
}

func renderGenericFile(w io.Writer, fileLoc string, fileTmpl string, info tmplData) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you rename this to renderFile and add a comment above it describing what it does?

CatalogVersion string
}

func (g *Generator) generateDirStructure() error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please add a small comment describing what the function does.

return writeFileAndPrint(filepath.Join(stubDir, handler), buf.Bytes(), defaultFileMode)
}

type tmplData struct {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Most of these fields are pretty self-explanatory. But for fields like KindSingular and KindPlural could you add a comment please

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@hasbro17 I wasn't here when the interfaces were designed, can you provide some insight as to why we have 3 versions of Kind?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's just the Kubernetes API convention for all types. For e.g a pod has the Kind Pod which defines the scheme it uses. The singular for pods is pod which is an alias for the Kind. And the plural is pods which is used in the REST pattern for the resource.

See https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/#create-a-customresourcedefinition
and https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md

You can just use the comments from the CRD doc if needed:

// plural name to be used in the URL: /apis/<group>/<version>/<plural>
KindSingular string
// singular name to be used as an alias on the CLI and for display
KindPlural   string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Cool, just updated, should be ready to merge after Travis finishes

Comment thread pkg/generator/generator.go Outdated
buf := &bytes.Buffer{}
if err := renderMainFile(buf, repoPath, apiVersion, kind); err != nil {

if err := renderFile(buf, "cmd/<projectName>/main.go", mainTmpl, tmplData{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you define the tmplData argument before the function call. It helps to improve readability:

	td := tmplData{
		OperatorSDKImport: sdkImport,
		StubImport:        filepath.Join(repoPath, stubDir),
		K8sutilImport:     k8sutilImport,
		SDKVersionImport:  versionImport,
		APIVersion:        apiVersion,
		Kind:              kind,
	}
	if err := renderFile(buf, "cmd/<projectName>/main.go", mainTmpl, td); err != nil {
		return err
	}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here and in all other places.

Copy link
Copy Markdown
Contributor

@rithujohn191 rithujohn191 left a comment

Choose a reason for hiding this comment

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

👍 lgtm

Comment thread pkg/generator/generator.go Outdated
CatalogVersion string
}

//Creates all the necesary directories for the generated files
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing space in comment: //Creates ==> // Creates

Comment thread pkg/generator/generator.go Outdated
return nil
}

//Renders a file given a template, and fills in template fields according to values passed in the tmplData struct
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing space in comment: //Renders ==> // Renders

Copy link
Copy Markdown
Contributor

@hasbro17 hasbro17 left a comment

Choose a reason for hiding this comment

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

LGTM after nits

@theishshah theishshah merged commit 5db5df0 into operator-framework:master Jun 13, 2018
m1kola pushed a commit to m1kola/operator-sdk that referenced this pull request Jun 7, 2024
…0-rebase-master

Merge upstream tag v1.28.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants