-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Stop using apiserver for generating docs #126
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Don't use apiserver-builder to generate reference documentation.
- Loading branch information
commit 5594571fdda89cdaf414842030754f2e0bf51bf4
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ import ( | |
| "path/filepath" | ||
| "strings" | ||
|
|
||
| generatecmd "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/generate" | ||
| "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
@@ -59,12 +60,16 @@ kubebuilder docs | |
| var generateConfig bool | ||
| var cleanup, verbose bool | ||
| var outputDir string | ||
| var copyright string | ||
|
|
||
| func AddDocs(cmd *cobra.Command) { | ||
| docsCmd.Flags().BoolVar(&cleanup, "cleanup", true, "If true, cleanup intermediary files") | ||
| docsCmd.Flags().BoolVar(&verbose, "verbose", true, "If true, use verbose output") | ||
| docsCmd.Flags().BoolVar(&generateConfig, "generate-config", true, "If true, generate the docs/reference/config.yaml.") | ||
| docsCmd.Flags().StringVar(&outputDir, "output-dir", filepath.Join("docs", "reference"), "Build docs into this directory") | ||
| docsCmd.Flags().StringVar(©right, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.") | ||
| docsCmd.Flags().StringVar(&generatecmd.Docscopyright, "docs-copyright", "<a href=\"https://github.com/kubernetes/kubernetes\">Copyright 2018 The Kubernetes Authors.</a>", "html for the copyright text on the docs") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason of using two different copyright here? Can we set |
||
| docsCmd.Flags().StringVar(&generatecmd.Docstitle, "title", "API Reference", "title of the docs page") | ||
| cmd.AddCommand(docsCmd) | ||
| docsCmd.AddCommand(docsCleanCmd) | ||
| } | ||
|
|
@@ -87,8 +92,14 @@ func RunCleanDocs(cmd *cobra.Command, args []string) { | |
| os.Remove(filepath.Join(outputDir, "manifest.json")) | ||
| } | ||
|
|
||
| var openapipkg = filepath.Join("pkg", "generated", "openapi") | ||
|
|
||
| func RunDocs(cmd *cobra.Command, args []string) { | ||
| // Delete old build artifacts | ||
| os.RemoveAll(filepath.Join(outputDir, "includes")) | ||
| os.RemoveAll(filepath.Join(outputDir, "build")) | ||
| os.Remove(filepath.Join(outputDir, "manifest.json")) | ||
|
|
||
| os.MkdirAll(filepath.Join(outputDir, "openapi-spec"), 0700) | ||
| os.MkdirAll(filepath.Join(outputDir, "static_includes"), 0700) | ||
| os.MkdirAll(filepath.Join(outputDir, "examples"), 0700) | ||
|
|
@@ -99,17 +110,35 @@ func RunDocs(cmd *cobra.Command, args []string) { | |
| } | ||
|
|
||
| if generateConfig { | ||
| // Regenerate the config.yaml with the table of contents | ||
| os.Remove(filepath.Join(outputDir, "config.yaml")) | ||
| CodeGenerator{}.Execute(wd) | ||
| } | ||
|
|
||
| // Run the docker command to build the docs | ||
| c := exec.Command("docker", "run", | ||
| "-v", fmt.Sprintf("%s:%s", filepath.Join(wd), "/host/repo"), | ||
| "-e", "DOMAIN="+util.GetDomain(), | ||
| "-e", "DIR="+filepath.Join("src", util.Repo), | ||
| "-e", "OUTPUT="+outputDir, | ||
| "gcr.io/kubebuilder/gendocs:alpha4", | ||
| ) | ||
| // Make sure to generate the openapi | ||
| generatecmd.Codegenerators = []string{"openapi"} | ||
| generatecmd.RunGenerate(cmd, args) | ||
|
|
||
| // Create the go program to create the swagger.json by serializing the openapi go struct | ||
| cr := util.GetCopyright(copyright) | ||
| doSwaggerGen(wd, swaggerGenTemplateArgs{ | ||
| cr, | ||
| util.Repo, | ||
| }) | ||
| defer func() { | ||
| if cleanup { | ||
| os.RemoveAll(filepath.Join(wd, filepath.Join("pkg", "generated"))) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "includes")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "manifest.json")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "runbrodocs.sh")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "node_modules", "marked", "Makefile")) | ||
| } | ||
| }() | ||
|
|
||
| // Run the go program to write the swagger.json output to a file | ||
| c := exec.Command("go", "run", filepath.Join(openapipkg, "cmd", "main.go")) | ||
| if verbose { | ||
| log.Println(strings.Join(c.Args, " ")) | ||
| c.Stderr = os.Stderr | ||
|
|
@@ -120,6 +149,11 @@ func RunDocs(cmd *cobra.Command, args []string) { | |
| log.Fatalf("error: %v\n", err) | ||
| } | ||
|
|
||
| // Call the apidocs code generator to create the markdown files that will be converted into | ||
| // html | ||
| generatecmd.Codegenerators = []string{"apidocs"} | ||
| generatecmd.RunGenerate(cmd, args) | ||
|
|
||
| // Run the docker command to build the docs | ||
| c = exec.Command("docker", "run", | ||
| "-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "includes"), "/source"), | ||
|
|
@@ -138,16 +172,32 @@ func RunDocs(cmd *cobra.Command, args []string) { | |
| log.Fatalf("error: %v\n", err) | ||
| } | ||
|
|
||
| // Cleanup intermediate files | ||
| if cleanup { | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "includes")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "manifest.json")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "openapi-spec")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "runbrodocs.sh")) | ||
| os.RemoveAll(filepath.Join(wd, outputDir, "build", "node_modules", "marked", "Makefile")) | ||
| } | ||
|
|
||
| fmt.Printf("Reference docs written to %s\n", filepath.Join(outputDir, "build", "index.html")) | ||
| } | ||
|
|
||
| // Scaffolding file for writing the openapi generated structs to a swagger.json file | ||
| type swaggerGenTemplateArgs struct { | ||
| BoilerPlate string | ||
| Repo string | ||
| } | ||
|
|
||
| // Create a go file that will take the generated openapi.go file and serialize the go structs into a swagger.json. | ||
| func doSwaggerGen(dir string, args swaggerGenTemplateArgs) bool { | ||
| path := filepath.Join(dir, openapipkg, "cmd", "main.go") | ||
| return util.WriteIfNotFound(path, "swagger-template", swaggerGenTemplate, args) | ||
| } | ||
|
|
||
| var swaggerGenTemplate = ` | ||
| {{.BoilerPlate}} | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "github.com/kubernetes-sigs/kubebuilder/pkg/docs" | ||
| "{{ .Repo }}/pkg/generated/openapi" | ||
| ) | ||
|
|
||
| func main() { | ||
| docs.WriteOpenAPI(openapi.GetOpenAPIDefinitions) | ||
| } | ||
| ` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| Copyright 2018 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package docs | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "log" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/go-openapi/spec" | ||
| "k8s.io/kube-openapi/pkg/common" | ||
| ) | ||
|
|
||
| // WriteOpenAPI writes the openapi json to docs/reference/openapi-spec/swagger.json | ||
| func WriteOpenAPI(openapi func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition) { | ||
| defs := openapi(func(name string) spec.Ref { | ||
| parts := strings.Split(name, "/") | ||
| return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s.%s", | ||
| common.EscapeJsonPointer(parts[len(parts)-2]), | ||
| common.EscapeJsonPointer(parts[len(parts)-1]))) | ||
| }) | ||
|
|
||
| o, err := json.MarshalIndent(defs, "", " ") | ||
| if err != nil { | ||
| log.Fatalf("Could not Marshal JSON %v\n%v", err, defs) | ||
| } | ||
| err = ioutil.WriteFile(filepath.Join("docs", "reference", "openapi-spec", "swagger.json"), o, 0700) | ||
| if err != nil { | ||
| log.Fatalf("%v", err) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the changes in kubebuilder branch will ever be merged in master for that repo ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not for a long time if ever. The branch moves away from using the endpoints to find resources, and instead uses the paths. Reference-docs hasn't had much active development AFAIK.