forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Running `make generate-crd-docs` in the root project or `make update-crdlist` in Documentation will run the crdlistgen tool. This tool rebuilds the Documentation/crdlist.rst file by looking at the pkg/k8s/apis/cilium.io/client package's CRD List. It will also scan the rst files of Documentation to see if the CRD is being used as a header anywhere to generate a link to it. Signed-off-by: Daniel Hawton <[email protected]>
- Loading branch information
Showing
9 changed files
with
254 additions
and
41 deletions.
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
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
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
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,14 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
target_file="${script_dir}/crdlist.rst" | ||
|
||
if ! git diff --quiet -- "$target_file" ; then | ||
git --no-pager diff -- "$target_file" | ||
echo "HINT: to fix this, run 'make -C Documentation update-crdlist'" | ||
exit 1 | ||
fi |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,124 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
operatorServer "github.com/cilium/cilium/api/v1/operator/server" | ||
"github.com/cilium/cilium/pkg/hive" | ||
"github.com/cilium/cilium/pkg/hive/cell" | ||
"github.com/cilium/cilium/pkg/k8s/apis/cilium.io/client" | ||
) | ||
|
||
var ( | ||
MainCell = cell.Module( | ||
"main", | ||
"Main module for generating CRD Lists", | ||
cell.Invoke(printCRDList), | ||
) | ||
|
||
Hive = hive.New( | ||
operatorServer.SpecCell, | ||
MainCell, | ||
) | ||
) | ||
|
||
var ErrorBreakEarly = fmt.Errorf("break early") | ||
|
||
func printCRDList( | ||
opSpec *operatorServer.Spec, | ||
shutdown hive.Shutdowner, | ||
) error { | ||
list := client.CustomResourceDefinitionList() | ||
|
||
crdlist := []string{} | ||
|
||
for _, crd := range list { | ||
crdlist = append(crdlist, cleanupCRDName(crd.Name)) | ||
} | ||
|
||
// Sort the list | ||
sort.Strings(crdlist) | ||
|
||
for idx, name := range crdlist { | ||
// We need to walk ../../Documentation rst files to look and see if the CRD name is a header in the format of `.. _ <name>:`, if so | ||
// add `ref:` to the name so it will link to the CRD in the docs. | ||
err := filepath.WalkDir("./Documentation", func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if filepath.Ext(path) == ".rst" { | ||
match, err := grepFile(path, ".. _"+name+":") | ||
if err != nil { | ||
return err | ||
} | ||
// We can stop walking the documentation as we already know there is a match, so we send an ErrorBreakEarly and ignore that on the other side | ||
// as WalkDir will keep running until it returns an error or there are no more files to walk. | ||
if match { | ||
// Change the name to a ref also specifically override the link text, as a couple headers add " CRD" to the text which causes the link to not be uniform. | ||
crdlist[idx] = ":ref:`" + name + "<" + name + ">`" | ||
return ErrorBreakEarly | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil && !errors.Is(err, ErrorBreakEarly) { | ||
return err | ||
} | ||
} | ||
|
||
f, err := os.Create("Documentation/crdlist.rst") | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
for _, name := range crdlist { | ||
_, err := f.WriteString(fmt.Sprintf("- %s\n", name)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
shutdown.Shutdown() | ||
return nil | ||
} | ||
|
||
// Scan file for string | ||
func grepFile(path, search string) (bool, error) { | ||
//fmt.Printf("searching %s for %s\n", path, search) | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
if strings.Contains(scanner.Text(), search) { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, scanner.Err() | ||
} | ||
|
||
// Remove the /(version) portion from the CRD Name | ||
func cleanupCRDName(name string) string { | ||
return strings.Split(name, "/")[0] | ||
} | ||
|
||
func main() { | ||
Hive.Run() | ||
} |