Skip to content

Commit

Permalink
Remove dots from operator reference filenames
Browse files Browse the repository at this point in the history
The dots cause issues with our hosting infrastructure, which returns
404s when users make requests for Kubernetes operator reference pages
with no trailing slash.

This change returns a filename from the format function in the CRD/CRD
docs generator so we can return different file names for CRDs and their
docs. It also renames CRD docs pages to include hyphens instead of dots
and underscores, and adds redirects.
  • Loading branch information
ptgott committed Jan 3, 2025
1 parent 35a0440 commit 8e68065
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 12 deletions.
68 changes: 67 additions & 1 deletion docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,71 @@
}
}
},
"redirects": []
"redirects": [
{
"source": "/reference/operator-resources/resources.teleport.dev_accesslists/",
"destination": "/reference/operator-resources/resources-teleport-dev-accesslists/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_githubconnectors/",
"destination": "/reference/operator-resources/resources-teleport-dev-githubconnectors/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_loginrules/",
"destination": "/reference/operator-resources/resources-teleport-dev-loginrules/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_oidcconnectors/",
"destination": "/reference/operator-resources/resources-teleport-dev-oidcconnectors/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_oktaimportrules/",
"destination": "/reference/operator-resources/resources-teleport-dev-oktaimportrules/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_openssheiceserversv2/",
"destination": "/reference/operator-resources/resources-teleport-dev-openssheiceserversv2/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_opensshserversv2/",
"destination": "/reference/operator-resources/resources-teleport-dev-opensshserversv2/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_provisiontokens/",
"destination": "/reference/operator-resources/resources-teleport-dev-provisiontokens/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_roles/",
"destination": "/reference/operator-resources/resources-teleport-dev-roles/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_rolesv6/",
"destination": "/reference/operator-resources/resources-teleport-dev-rolesv6/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_rolesv7/",
"destination": "/reference/operator-resources/resources-teleport-dev-rolesv7/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_samlconnectors/",
"destination": "/reference/operator-resources/resources-teleport-dev-samlconnectors/",
"permanent": true
},
{
"source": "/reference/operator-resources/resources.teleport.dev_users/",
"destination": "/reference/operator-resources/resources-teleport-dev-users/",
"permanent": true
}
]
}
20 changes: 14 additions & 6 deletions integrations/operator/crdgen/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ import (
)

// crdFormatFunc formats the given CRD into a document. It returns the document
// as a byte slice, plus the file extension for the document.
type crdFormatFunc func(apiextv1.CustomResourceDefinition) ([]byte, string, error)
// as a byte slice, plus the file name for the document. The file name is based
// on the CRD's API group name and plural name.
type crdFormatFunc func(crd apiextv1.CustomResourceDefinition, groupName, pluralName string) ([]byte, string, error)

func formatAsYAML(crd apiextv1.CustomResourceDefinition) ([]byte, string, error) {
func formatAsCRD(crd apiextv1.CustomResourceDefinition, groupName, pluralName string) ([]byte, string, error) {
doc, err := yaml.Marshal(crd)
if err != nil {
return nil, "", err
}
return doc, "yaml", nil
filename := fmt.Sprintf("%s_%s.%v", groupName, pluralName, "yaml")
return doc, filename, nil
}

var crdDocTmpl string = `---
Expand Down Expand Up @@ -226,7 +228,7 @@ func propertyTable(currentFieldName string, props *apiextv1.JSONSchemaProps) ([]
return tables, nil
}

func formatAsDocsPage(crd apiextv1.CustomResourceDefinition) ([]byte, string, error) {
func formatAsDocsPage(crd apiextv1.CustomResourceDefinition, groupName, pluralName string) ([]byte, string, error) {
var buf bytes.Buffer
rp := ResourcePage{
Title: crd.Spec.Names.Kind,
Expand Down Expand Up @@ -263,5 +265,11 @@ resource, which you can apply after installing the Teleport Kubernetes operator.
return nil, "", trace.Wrap(err)
}

return buf.Bytes(), "mdx", nil
filename := fmt.Sprintf(
"%s-%s.%v",
strings.ReplaceAll(groupName, ".", "-"),
pluralName,
"mdx",
)
return buf.Bytes(), filename, nil
}
8 changes: 3 additions & 5 deletions integrations/operator/crdgen/handlerequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package crdgen

import (
"fmt"
"os"

gogodesc "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
Expand All @@ -34,7 +33,7 @@ import (
)

func HandleCRDRequest(req *gogoplugin.CodeGeneratorRequest) error {
return handleRequest(req, formatAsYAML)
return handleRequest(req, formatAsCRD)
}

func HandleDocsRequest(req *gogoplugin.CodeGeneratorRequest) error {
Expand Down Expand Up @@ -232,13 +231,12 @@ func generateSchema(file *File, groupName string, format crdFormatFunc, resp *go
if err != nil {
return trace.Wrap(err, "generating CRD")
}
data, ext, err := format(crd)
data, filename, err := format(crd, groupName, root.pluralName)
if err != nil {
return trace.Wrap(err)
}
name := fmt.Sprintf("%s_%s.%v", groupName, root.pluralName, ext)
content := string(data)
resp.File = append(resp.File, &gogoplugin.CodeGeneratorResponse_File{Name: &name, Content: &content})
resp.File = append(resp.File, &gogoplugin.CodeGeneratorResponse_File{Name: &filename, Content: &content})
}

return nil
Expand Down

0 comments on commit 8e68065

Please sign in to comment.