Skip to content

Commit 5594571

Browse files
committed
Don't use apiserver-builder to generate reference documentation.
1 parent 5e70386 commit 5594571

File tree

7 files changed

+145
-116
lines changed

7 files changed

+145
-116
lines changed

build/apiserverbuilder/Dockerfile

Lines changed: 0 additions & 34 deletions
This file was deleted.

build/apiserverbuilder/docs.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

build/thirdparty/darwin/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ENV GOOS darwin
8585
ENV GOARCH amd64
8686
ENV DEST /usr/local/kubebuilder/bin/
8787
RUN mkdir -p $DEST || echo ""
88-
RUN git clone https://github.com/kubernetes-incubator/reference-docs $GOPATH/src/github.com/kubernetes-incubator/reference-docs --depth=1
88+
RUN git clone https://github.com/kubernetes-incubator/reference-docs $GOPATH/src/github.com/kubernetes-incubator/reference-docs --branch kubebuilder --depth=1
8989
RUN go build -o $DEST/gen-apidocs github.com/kubernetes-incubator/reference-docs/gen-apidocs
9090

9191
# Copy all binaries into a single tar.gz file

build/thirdparty/linux/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ENV GOOS linux
6868
ENV GOARCH amd64
6969
ENV DEST /usr/local/kubebuilder/bin/
7070
RUN mkdir -p $DEST || echo ""
71-
RUN git clone https://github.com/kubernetes-incubator/reference-docs $GOPATH/src/github.com/kubernetes-incubator/reference-docs --depth=1
71+
RUN git clone https://github.com/kubernetes-incubator/reference-docs $GOPATH/src/github.com/kubernetes-incubator/reference-docs --branch kubebuilder --depth=1
7272
RUN go build -o $DEST/gen-apidocs github.com/kubernetes-incubator/reference-docs/gen-apidocs
7373

7474
FROM golang:1.10-stretch as linux

cmd/kubebuilder/docs/docs.go

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path/filepath"
2525
"strings"
2626

27+
generatecmd "github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/generate"
2728
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2829
"github.com/spf13/cobra"
2930
)
@@ -59,12 +60,16 @@ kubebuilder docs
5960
var generateConfig bool
6061
var cleanup, verbose bool
6162
var outputDir string
63+
var copyright string
6264

6365
func AddDocs(cmd *cobra.Command) {
6466
docsCmd.Flags().BoolVar(&cleanup, "cleanup", true, "If true, cleanup intermediary files")
6567
docsCmd.Flags().BoolVar(&verbose, "verbose", true, "If true, use verbose output")
6668
docsCmd.Flags().BoolVar(&generateConfig, "generate-config", true, "If true, generate the docs/reference/config.yaml.")
6769
docsCmd.Flags().StringVar(&outputDir, "output-dir", filepath.Join("docs", "reference"), "Build docs into this directory")
70+
docsCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
71+
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")
72+
docsCmd.Flags().StringVar(&generatecmd.Docstitle, "title", "API Reference", "title of the docs page")
6873
cmd.AddCommand(docsCmd)
6974
docsCmd.AddCommand(docsCleanCmd)
7075
}
@@ -87,8 +92,14 @@ func RunCleanDocs(cmd *cobra.Command, args []string) {
8792
os.Remove(filepath.Join(outputDir, "manifest.json"))
8893
}
8994

95+
var openapipkg = filepath.Join("pkg", "generated", "openapi")
96+
9097
func RunDocs(cmd *cobra.Command, args []string) {
98+
// Delete old build artifacts
9199
os.RemoveAll(filepath.Join(outputDir, "includes"))
100+
os.RemoveAll(filepath.Join(outputDir, "build"))
101+
os.Remove(filepath.Join(outputDir, "manifest.json"))
102+
92103
os.MkdirAll(filepath.Join(outputDir, "openapi-spec"), 0700)
93104
os.MkdirAll(filepath.Join(outputDir, "static_includes"), 0700)
94105
os.MkdirAll(filepath.Join(outputDir, "examples"), 0700)
@@ -99,17 +110,35 @@ func RunDocs(cmd *cobra.Command, args []string) {
99110
}
100111

101112
if generateConfig {
113+
// Regenerate the config.yaml with the table of contents
114+
os.Remove(filepath.Join(outputDir, "config.yaml"))
102115
CodeGenerator{}.Execute(wd)
103116
}
104117

105-
// Run the docker command to build the docs
106-
c := exec.Command("docker", "run",
107-
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd), "/host/repo"),
108-
"-e", "DOMAIN="+util.GetDomain(),
109-
"-e", "DIR="+filepath.Join("src", util.Repo),
110-
"-e", "OUTPUT="+outputDir,
111-
"gcr.io/kubebuilder/gendocs:alpha4",
112-
)
118+
// Make sure to generate the openapi
119+
generatecmd.Codegenerators = []string{"openapi"}
120+
generatecmd.RunGenerate(cmd, args)
121+
122+
// Create the go program to create the swagger.json by serializing the openapi go struct
123+
cr := util.GetCopyright(copyright)
124+
doSwaggerGen(wd, swaggerGenTemplateArgs{
125+
cr,
126+
util.Repo,
127+
})
128+
defer func() {
129+
if cleanup {
130+
os.RemoveAll(filepath.Join(wd, filepath.Join("pkg", "generated")))
131+
os.RemoveAll(filepath.Join(wd, outputDir, "includes"))
132+
os.RemoveAll(filepath.Join(wd, outputDir, "manifest.json"))
133+
os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents"))
134+
os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents"))
135+
os.RemoveAll(filepath.Join(wd, outputDir, "build", "runbrodocs.sh"))
136+
os.RemoveAll(filepath.Join(wd, outputDir, "build", "node_modules", "marked", "Makefile"))
137+
}
138+
}()
139+
140+
// Run the go program to write the swagger.json output to a file
141+
c := exec.Command("go", "run", filepath.Join(openapipkg, "cmd", "main.go"))
113142
if verbose {
114143
log.Println(strings.Join(c.Args, " "))
115144
c.Stderr = os.Stderr
@@ -120,6 +149,11 @@ func RunDocs(cmd *cobra.Command, args []string) {
120149
log.Fatalf("error: %v\n", err)
121150
}
122151

152+
// Call the apidocs code generator to create the markdown files that will be converted into
153+
// html
154+
generatecmd.Codegenerators = []string{"apidocs"}
155+
generatecmd.RunGenerate(cmd, args)
156+
123157
// Run the docker command to build the docs
124158
c = exec.Command("docker", "run",
125159
"-v", fmt.Sprintf("%s:%s", filepath.Join(wd, outputDir, "includes"), "/source"),
@@ -138,16 +172,32 @@ func RunDocs(cmd *cobra.Command, args []string) {
138172
log.Fatalf("error: %v\n", err)
139173
}
140174

141-
// Cleanup intermediate files
142-
if cleanup {
143-
os.RemoveAll(filepath.Join(wd, outputDir, "includes"))
144-
os.RemoveAll(filepath.Join(wd, outputDir, "manifest.json"))
145-
os.RemoveAll(filepath.Join(wd, outputDir, "openapi-spec"))
146-
os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents"))
147-
os.RemoveAll(filepath.Join(wd, outputDir, "build", "documents"))
148-
os.RemoveAll(filepath.Join(wd, outputDir, "build", "runbrodocs.sh"))
149-
os.RemoveAll(filepath.Join(wd, outputDir, "build", "node_modules", "marked", "Makefile"))
150-
}
151-
152175
fmt.Printf("Reference docs written to %s\n", filepath.Join(outputDir, "build", "index.html"))
153176
}
177+
178+
// Scaffolding file for writing the openapi generated structs to a swagger.json file
179+
type swaggerGenTemplateArgs struct {
180+
BoilerPlate string
181+
Repo string
182+
}
183+
184+
// Create a go file that will take the generated openapi.go file and serialize the go structs into a swagger.json.
185+
func doSwaggerGen(dir string, args swaggerGenTemplateArgs) bool {
186+
path := filepath.Join(dir, openapipkg, "cmd", "main.go")
187+
return util.WriteIfNotFound(path, "swagger-template", swaggerGenTemplate, args)
188+
}
189+
190+
var swaggerGenTemplate = `
191+
{{.BoilerPlate}}
192+
193+
package main
194+
195+
import (
196+
"github.com/kubernetes-sigs/kubebuilder/pkg/docs"
197+
"{{ .Repo }}/pkg/generated/openapi"
198+
)
199+
200+
func main() {
201+
docs.WriteOpenAPI(openapi.GetOpenAPIDefinitions)
202+
}
203+
`

cmd/kubebuilder/generate/generate.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ import (
3333

3434
var versionedAPIs []string
3535
var unversionedAPIs []string
36-
var codegenerators []string
36+
var Codegenerators []string
3737
var copyright string
3838
var generators = sets.String{}
3939
var vendorDir string
40+
var Docscopyright string
41+
var Docstitle string
4042

4143
var generateCmd = &cobra.Command{
42-
Use: "generate",
44+
Use: "generate",
4345
Aliases: []string{"generated"},
44-
Short: "Run code generators.",
45-
Long: `Run code generators`,
46+
Short: "Run code generators.",
47+
Long: `Run code generators`,
4648
Example: `# Run code generators.
4749
kubebuilder generate`,
4850
Run: RunGenerate,
@@ -58,7 +60,9 @@ func AddGenerate(cmd *cobra.Command) {
5860
generateCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
5961
generateCmd.Flags().StringVar(&vendorDir, "vendor-dir", "", "Location of directory containing vendor files.")
6062
generateCmd.Flags().StringArrayVar(&versionedAPIs, "api-versions", []string{}, "API version to generate code for. Can be specified multiple times. e.g. --api-versions foo/v1beta1 --api-versions bar/v1 defaults to all versions found under directories pkg/apis/<group>/<version>")
61-
generateCmd.Flags().StringArrayVar(&codegenerators, "generator", []string{}, "list of generators to run. e.g. --generator kubebuilder --generator conversion Valid values: [kubebuilder,client]")
63+
generateCmd.Flags().StringArrayVar(&Codegenerators, "generator", []string{}, "list of generators to run. e.g. --generator kubebuilder --generator conversion Valid values: [kubebuilder,client,openapi]")
64+
generateCmd.Flags().StringVar(&Docscopyright, "docs-copyright", "<a href=\"https://github.com/kubernetes/kubernetes\">Copyright 2018 The Kubernetes Authors.</a>", "html for the copyright text on the docs")
65+
generateCmd.Flags().StringVar(&Docstitle, "docs-title", "API Reference", "title of the docs page")
6266
generateCmd.AddCommand(generateCleanCmd)
6367
}
6468

@@ -94,7 +98,7 @@ func doGen(g string) bool {
9498
func RunGenerate(cmd *cobra.Command, args []string) {
9599
initApis()
96100

97-
for _, g := range codegenerators {
101+
for _, g := range Codegenerators {
98102
generators.Insert(strings.Replace(g, "-gen", "", -1))
99103
}
100104

@@ -164,7 +168,7 @@ func RunGenerate(cmd *cobra.Command, args []string) {
164168
}
165169
}
166170

167-
if generators.Has("openapi-gen") {
171+
if generators.Has("openapi") {
168172
apis := []string{
169173
"k8s.io/apimachinery/pkg/apis/meta/v1",
170174
"k8s.io/apimachinery/pkg/api/resource",
@@ -191,7 +195,7 @@ func RunGenerate(cmd *cobra.Command, args []string) {
191195
"-o", util.GoSrc,
192196
"--go-header-file", copyright,
193197
"-i", strings.Join(apis, ","),
194-
"--output-package", filepath.Join(util.Repo, "pkg", "openapi"))...,
198+
"--output-package", filepath.Join(util.Repo, "pkg", "generated", "openapi"))...,
195199
)
196200
glog.V(4).Infof("%s\n", strings.Join(c.Args, " "))
197201
out, err := c.CombinedOutput()
@@ -200,6 +204,20 @@ func RunGenerate(cmd *cobra.Command, args []string) {
200204
}
201205
}
202206

207+
// Generate reference documentation
208+
if generators.Has("apidocs") {
209+
c := exec.Command(filepath.Join(root, "gen-apidocs"),
210+
"--copyright", Docscopyright,
211+
"--title", Docstitle,
212+
"--config-dir", "docs/reference/",
213+
)
214+
glog.V(4).Infof("%s\n", strings.Join(c.Args, " "))
215+
out, err := c.CombinedOutput()
216+
if err != nil {
217+
glog.Fatalf("failed to run gen-apidocs %s %v", out, err)
218+
}
219+
}
220+
203221
if generators.Has("defaulter-gen") {
204222
c := exec.Command(filepath.Join(root, "defaulter-gen"),
205223
append(all,

pkg/docs/gendocs.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package docs
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"io/ioutil"
23+
"log"
24+
"path/filepath"
25+
"strings"
26+
27+
"github.com/go-openapi/spec"
28+
"k8s.io/kube-openapi/pkg/common"
29+
)
30+
31+
// WriteOpenAPI writes the openapi json to docs/reference/openapi-spec/swagger.json
32+
func WriteOpenAPI(openapi func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition) {
33+
defs := openapi(func(name string) spec.Ref {
34+
parts := strings.Split(name, "/")
35+
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s.%s",
36+
common.EscapeJsonPointer(parts[len(parts)-2]),
37+
common.EscapeJsonPointer(parts[len(parts)-1])))
38+
})
39+
40+
o, err := json.MarshalIndent(defs, "", " ")
41+
if err != nil {
42+
log.Fatalf("Could not Marshal JSON %v\n%v", err, defs)
43+
}
44+
err = ioutil.WriteFile(filepath.Join("docs", "reference", "openapi-spec", "swagger.json"), o, 0700)
45+
if err != nil {
46+
log.Fatalf("%v", err)
47+
}
48+
}

0 commit comments

Comments
 (0)