-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add a resource reference generator #50515
Open
ptgott
wants to merge
1
commit into
master
Choose a base branch
from
paul.gottschling/2024-12-9949-resources
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains 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,81 @@ | ||
# Resource reference generator | ||
|
||
The resource reference generator is a Go program that produces a comprehensive | ||
reference guide for all dynamic Teleport resources and the fields they include. | ||
It uses the Teleport source as the basis for the guide. | ||
|
||
## Usage | ||
|
||
``` | ||
$ cd build.assets/tooling/cmd/resource-ref-generator | ||
$ go run . -config config.yaml | ||
``` | ||
|
||
## How it works | ||
|
||
The resource reference generator works by: | ||
|
||
1. Identifying Go structs that correspond to dynamic Teleport resources. | ||
1. Identifying Go types that represent to the fields of each dynamic resource | ||
struct. | ||
1. Retrieving reference information about dynamic resources and their fields | ||
using a combination of Go comments and type information. | ||
|
||
### Editing source files | ||
|
||
The resource reference indicates which Go source files the reference generator | ||
used for each entry. The generator is only aware of Go source files, not | ||
protobuf message definitions. | ||
|
||
If a source file is based on a protobuf message definition, edit the message | ||
definition first, then run: | ||
|
||
``` | ||
$ make grpc | ||
``` | ||
|
||
After that, you can run the reference generator. | ||
|
||
## Configuration | ||
|
||
The generator uses a YAML configuration file with the following fields. | ||
|
||
### Main config | ||
|
||
- `required_field_types`: a list of type info mappings (see "Type info") | ||
indicating type names of fields that must be present in a dynamic resource | ||
before we include it in the reference. For example, if this is `Metadata` from | ||
package `types`, a struct type must include a field with the a field of | ||
`types.Metadata` before we add it to the reference. | ||
|
||
- `source` (string): the path to the root of a Go project directory. | ||
|
||
- `destination` (string): the directory path in which to place reference pages. | ||
|
||
- `excluded_resource_types`: a list of type info mappings (see "Type info") | ||
indicating names of resources to exclude from the reference. | ||
|
||
- `field_assignment_method`: the name of a method of a resource type that | ||
assigns fields to the resource. Used to identify the kind and version of a | ||
resource. | ||
|
||
### Type info | ||
|
||
- `package`: The path of a Go package | ||
- `name`: The name of a type within the package | ||
|
||
### Example | ||
|
||
```yaml | ||
required_field_types: | ||
- name: Metadata | ||
package: api/types | ||
- name: ResourceHeader | ||
package: api/types | ||
source: "api" | ||
destination: "docs/pages/includes/resource-reference.mdx" | ||
excluded_resource_types: | ||
- package: "types" | ||
name: "ResourceHeader" | ||
field_assignment_method: "setStaticFields" | ||
``` |
20 changes: 20 additions & 0 deletions
20
build.assets/tooling/cmd/resource-ref-generator/config.yaml
This file contains 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,20 @@ | ||
required_field_types: | ||
- package: "types" | ||
name: "ResourceHeader" | ||
- package: "types" | ||
name: "Metadata" | ||
source: "../../../../api" | ||
destination: "../../../../docs/pages/reference/tctl-resources" | ||
excluded_resource_types: | ||
- package: "types" | ||
name: "ResourceHeader" | ||
# PluginV1 is not intended to be user facing, as it configures hosted plugins. | ||
- package: "types" | ||
name: "PluginV1" | ||
- package: "types" | ||
name: "Namespace" | ||
# This is an entire resource with a custom unmarshaler, so we should take care | ||
# to determine how to document it. | ||
- package: "types" | ||
name: "MFADevice" | ||
field_assignment_method: "setStaticFields" |
This file contains 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,91 @@ | ||
// Teleport | ||
// Copyright (C) 2023 Gravitational, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gravitational/teleport/build.assets/tooling/cmd/resource-ref-generator/reference" | ||
"github.com/spf13/afero" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const configHelp string = `The path to a YAML configuration file with the following fields: | ||
|
||
## Main config | ||
|
||
required_field_types: a list of type info mappings (see "Type info") indicating | ||
type names of fields that must be present in a dynamic resource before we | ||
include it in the reference. For example, if this is "Metadata" from package | ||
"types", a struct type must include a field with the a field of "types.Metadata" | ||
before we add it to the reference. | ||
|
||
source (string): the path to the root of a Go project directory. | ||
|
||
destination (string): the path to a directory where the generator writes | ||
reference pages. | ||
|
||
excluded_resource_types: a list of type info mappings (see "Type info") | ||
indicating names of resources to exclude from the reference. | ||
|
||
field_assignment_method: the name of a method of a resource type that assigns | ||
fields to the resource. Used to identify the kind and version of a resource. | ||
|
||
## Type info | ||
|
||
package: The path of a Go package | ||
name: The name of a type within the package | ||
|
||
## Example | ||
|
||
required_types: | ||
- name: Metadata | ||
package: api/types | ||
- name: ResourceHeader | ||
package: api/types | ||
source: "api" | ||
destination: "docs/pages/includes/resource-reference.mdx" | ||
excluded_resource_types: | ||
- package: "types" | ||
name: "ResourceHeader" | ||
field_assignment_method: "setStaticFields" | ||
` | ||
|
||
func main() { | ||
conf := flag.String("config", "./conf.yaml", configHelp) | ||
flag.Parse() | ||
|
||
conffile, err := os.Open(*conf) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not open the configuration file %v: %v\n", *conf, err) | ||
os.Exit(1) | ||
} | ||
genconf := reference.GeneratorConfig{} | ||
if err := yaml.NewDecoder(conffile).Decode(&genconf); err != nil { | ||
fmt.Fprintf(os.Stderr, "Invalid configuration file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
osFS := afero.NewOsFs() | ||
err = reference.Generate(osFS, osFS, genconf) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not generate the resource reference: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.