Skip to content

Commit 0c35f20

Browse files
author
Philippe Martin
committed
New resourcesdocs generator used to generate API Reference pages in Markdown format
1 parent 8eec3cc commit 0c35f20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+221026
-0
lines changed

gen-resourcesdocs/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
clean:
2+
rm -rf kwebsite/content/en/docs/* kwebsite/public
3+
4+
kwebsite: clean
5+
mkdir -p kwebsite/content/en/docs
6+
go run cmd/main.go kwebsite --config-dir config/v1.20/ --file api/v1.20/swagger.json --output-dir kwebsite/content/en/docs --templates ./templates
7+
8+
copy:
9+
cp -R kwebsite/content/en/docs/* ../website/content/en/docs/reference/kubernetes-api/

gen-resourcesdocs/api/v1.19/swagger.json

+105,020
Large diffs are not rendered by default.

gen-resourcesdocs/api/v1.20/swagger.json

+108,928
Large diffs are not rendered by default.

gen-resourcesdocs/cmd/cli/cli.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
)
11+
12+
const (
13+
fileOption = "file"
14+
configDirOption = "config-dir"
15+
outputDirOption = "output-dir"
16+
templatesDirOption = "templates"
17+
showDefinitionsOption = "show-definitions"
18+
)
19+
20+
// RootCmd defines the root cli command
21+
func RootCmd() *cobra.Command {
22+
cmd := &cobra.Command{
23+
Use: "kubernetes-api-reference",
24+
Short: "K8s API documentation tools",
25+
Long: `Tool to build documentation from OpenAPI specification of the Kubernetes API`,
26+
SilenceErrors: true,
27+
SilenceUsage: true,
28+
PreRun: func(cmd *cobra.Command, args []string) {
29+
viper.BindPFlags(cmd.Flags())
30+
},
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
return nil
33+
},
34+
}
35+
36+
cmd.PersistentFlags().StringP(fileOption, "f", "", "OpenAPI spec file")
37+
cmd.MarkFlagRequired(fileOption)
38+
39+
subcommands := []func() *cobra.Command{
40+
ResourceslistCmd, ShowTOCCmd, GVKeysMap, KWebsite,
41+
}
42+
for _, subcommand := range subcommands {
43+
cmd.AddCommand(subcommand())
44+
}
45+
46+
cobra.OnInitialize(initConfig)
47+
48+
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
49+
return cmd
50+
}
51+
52+
// Run the cli
53+
func Run() {
54+
if err := RootCmd().Execute(); err != nil {
55+
fmt.Println(err)
56+
os.Exit(1)
57+
}
58+
}
59+
60+
func initConfig() {
61+
viper.AutomaticEnv()
62+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/feloy/kubernetes-api-reference/pkg/kubernetes"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// GVKeysMap defines the `gvkeysmap` subcommand
12+
func GVKeysMap() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "gvkeysmap",
15+
Short: "show the map between group/version and definition keys",
16+
Long: "show the map between group/version and definition keys",
17+
SilenceErrors: true,
18+
SilenceUsage: true,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
file := cmd.Flag(fileOption).Value.String()
21+
spec, err := kubernetes.NewSpec(file)
22+
if err != nil {
23+
return err
24+
}
25+
gvs := make([]string, len(spec.GVToKey))
26+
i := 0
27+
for gv := range spec.GVToKey {
28+
gvs[i] = gv
29+
i++
30+
}
31+
sort.Strings(gvs)
32+
for _, gv := range gvs {
33+
keys := spec.GVToKey[gv]
34+
fmt.Printf("%s\n", gv)
35+
for _, key := range keys {
36+
fmt.Printf("\t%s\n", key)
37+
}
38+
}
39+
return nil
40+
},
41+
}
42+
return cmd
43+
}

gen-resourcesdocs/cmd/cli/helpers.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cli
2+
3+
import (
4+
"path"
5+
6+
"github.com/feloy/kubernetes-api-reference/pkg/config"
7+
"github.com/feloy/kubernetes-api-reference/pkg/kubernetes"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// prepareTOC loads Spec and Toc config, and completes TOC
12+
// by adding associates resources and not specifed resources in TOC
13+
func prepareTOC(cmd *cobra.Command) (*config.TOC, error) {
14+
file := cmd.Flag(fileOption).Value.String()
15+
spec, err := kubernetes.NewSpec(file)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
configDir := cmd.Flag(configDirOption).Value.String()
21+
toc, err := config.LoadTOC(path.Join(configDir, "toc.yaml"))
22+
err = toc.PopulateAssociates(spec)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
toc.AddOtherResources(spec)
28+
toc.Definitions = &spec.Swagger.Definitions
29+
toc.Actions = spec.Actions
30+
toc.Actions.Sort()
31+
32+
// TODO browse directory
33+
categories, err := config.LoadCategories([]string{path.Join(configDir, "fields.yaml")})
34+
if err != nil {
35+
return nil, err
36+
}
37+
toc.Categories = categories
38+
39+
return toc, nil
40+
}

gen-resourcesdocs/cmd/cli/kwebsite.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Hugo defines the `kwebsite` subcommand
10+
func KWebsite() *cobra.Command {
11+
cmd := &cobra.Command{
12+
Use: "kwebsite",
13+
Short: "output specification for k/website",
14+
Long: "output the specification in a format usable for the Kubernetes website",
15+
SilenceErrors: true,
16+
SilenceUsage: true,
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
toc, err := prepareTOC(cmd)
19+
if err != nil {
20+
return fmt.Errorf("Unable to load specs and/or toc config: %v", err)
21+
}
22+
23+
outputDir := cmd.Flag(outputDirOption).Value.String()
24+
templatesDir := cmd.Flag(templatesDirOption).Value.String()
25+
err = toc.ToKWebsite(outputDir, templatesDir)
26+
if err != nil {
27+
return err
28+
}
29+
30+
show, err := cmd.Flags().GetBool(showDefinitionsOption)
31+
if err != nil {
32+
return err
33+
}
34+
if show {
35+
toc.OutputDocumentedDefinitions()
36+
}
37+
return nil
38+
},
39+
}
40+
cmd.Flags().StringP(configDirOption, "c", "", "Directory containing documentation configuration")
41+
cmd.MarkFlagRequired(configDirOption)
42+
cmd.Flags().StringP(outputDirOption, "o", "", "Directory to write markdown files")
43+
cmd.MarkFlagRequired(outputDirOption)
44+
cmd.Flags().StringP(templatesDirOption, "t", "", "Directory containing go templates for output")
45+
cmd.MarkFlagRequired(templatesDirOption)
46+
cmd.Flags().Bool(showDefinitionsOption, false, "Show where definitions are defined on output")
47+
return cmd
48+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/feloy/kubernetes-api-reference/pkg/kubernetes"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// ResourceslistCmd defines the `resourceslist` subcommand
12+
func ResourceslistCmd() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "resourceslist",
15+
Short: "list k8s resources",
16+
Long: "list Kubernetes resources in the specification",
17+
SilenceErrors: true,
18+
SilenceUsage: true,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
file := cmd.Flag(fileOption).Value.String()
21+
spec, err := kubernetes.NewSpec(file)
22+
if err != nil {
23+
return err
24+
}
25+
26+
resources := spec.Resources
27+
i := 0
28+
keys := make([]string, len(*resources))
29+
for k := range *resources {
30+
keys[i] = k.String()
31+
i++
32+
}
33+
sort.Strings(keys)
34+
for _, k := range keys {
35+
rs := (*resources)[kubernetes.APIKind(k)]
36+
fmt.Println(k)
37+
for _, r := range rs {
38+
fmt.Println("\t" + r.GetGV())
39+
}
40+
}
41+
return nil
42+
},
43+
}
44+
return cmd
45+
}

gen-resourcesdocs/cmd/cli/showtoc.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// ShowTOCCmd defines the `showtoc` subcommand
11+
func ShowTOCCmd() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "showtoc",
14+
Short: "show the table of contents",
15+
Long: "list the parts and chapter of the documentation",
16+
SilenceErrors: true,
17+
SilenceUsage: true,
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
toc, err := prepareTOC(cmd)
20+
if err != nil {
21+
return fmt.Errorf("Unable to load specs and/or toc config: %v", err)
22+
}
23+
toc.ToMarkdown(os.Stdout)
24+
return nil
25+
},
26+
}
27+
cmd.Flags().StringP(configDirOption, "c", "", "Directory containing documentation configuration")
28+
cmd.MarkFlagRequired(configDirOption)
29+
30+
return cmd
31+
}

gen-resourcesdocs/cmd/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"github.com/feloy/kubernetes-api-reference/cmd/cli"
5+
)
6+
7+
func main() {
8+
cli.Run()
9+
}

0 commit comments

Comments
 (0)