From dfa49caa53428ba72adc1e532365d74bc8ab2a81 Mon Sep 17 00:00:00 2001 From: zirain Date: Tue, 23 Jul 2024 21:32:16 +0800 Subject: [PATCH] Add apiextensions types to default Kubernetes known types (#82) This adds types of the apiextensions.k8s.io group to the Kubernetes default known types. --------- Signed-off-by: zirain --- renderer/functions.go | 11 ++++++++--- renderer/functions_test.go | 24 +++++++++++++++++++++--- test.sh | 5 +++++ test/api/v1/guestbook_types.go | 3 +++ test/expected.asciidoc | 3 +++ test/expected.md | 3 +++ test/hide.md | 3 +++ 7 files changed, 46 insertions(+), 6 deletions(-) diff --git a/renderer/functions.go b/renderer/functions.go index bbd8c72..ceb16a3 100644 --- a/renderer/functions.go +++ b/renderer/functions.go @@ -14,6 +14,7 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. + package renderer import ( @@ -29,7 +30,7 @@ import ( ) const ( - kubePackagesRegex = `^k8s\.io/(?:api|apimachinery/pkg/apis)/` + kubePackagesRegex = `^k8s\.io/(?:api|apimachinery|apiextensions-apiserver/pkg/apis)/` kubeDocLinkTemplate = `https://kubernetes.io/docs/reference/generated/kubernetes-api/v{{ .kubeVersion }}/#{{ .type }}-{{ .version }}-{{ .group }}` ) @@ -168,10 +169,14 @@ func (k *kubernetesHelper) LinkForKubeType(t *types.Type) string { if len(parts) < 2 { zap.S().Fatalw("Unexpected Kubernetes package name", "type", t) } - + group := strings.ToLower(parts[len(parts)-2]) + // this is alias handling + if group == "apiextensions" { + group = "apiextensions-k8s-io" + } args := map[string]string{ "kubeVersion": k.kubeVersion, - "group": strings.ToLower(parts[len(parts)-2]), + "group": group, "version": strings.ToLower(parts[len(parts)-1]), "type": strings.ToLower(t.Name), } diff --git a/renderer/functions_test.go b/renderer/functions_test.go index 0ad7316..c312c1c 100644 --- a/renderer/functions_test.go +++ b/renderer/functions_test.go @@ -27,13 +27,31 @@ import ( func TestKubernetesHelper(t *testing.T) { conf := config.Config{ Render: config.RenderConfig{ - KubernetesVersion: "1.15", + KubernetesVersion: "1.29", }, } kh, err := newKubernetesHelper(&conf) require.NoError(t, err) - link := kh.LinkForKubeType(&types.Type{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ObjectMeta"}) - require.Equal(t, "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#objectmeta-v1-meta", link) + cases := []struct { + input *types.Type + excepted string + }{ + { + input: &types.Type{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ObjectMeta"}, + excepted: "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#objectmeta-v1-meta", + }, + { + input: &types.Type{Package: "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1", Name: "JSON"}, + excepted: "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#json-v1-apiextensions-k8s-io", + }, + } + + for _, tc := range cases { + t.Run("", func(t *testing.T) { + link := kh.LinkForKubeType(tc.input) + require.Equal(t, tc.excepted, link) + }) + } } diff --git a/test.sh b/test.sh index cc5c24f..73fb6a7 100755 --- a/test.sh +++ b/test.sh @@ -24,6 +24,7 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TEMP_DIR=$(mktemp -d -t crd-ref-docs-XXXXX) DEFAULT_ARGS=(--log-level=ERROR --source-path="${SCRIPT_DIR}/test" --output-path="${TEMP_DIR}/out" --config="${SCRIPT_DIR}/test/config.yaml") +AUTO_FIX=${AUTO_FIX:-} trap '[[ $TEMP_DIR ]] && rm -rf "$TEMP_DIR"' EXIT @@ -88,6 +89,10 @@ run_test() { if diff=$(diff -a -y --suppress-common-lines "${SCRIPT_DIR}/test/${expected}" "$actual"); then echo "OK" else + if [[ -n "$AUTO_FIX" ]]; then + echo "INFO: auto-fixing the output" + cp "$actual" "${SCRIPT_DIR}/test/${expected}" + fi echo "ERROR: outputs differ with ${expected}" echo "" echo "$diff" diff --git a/test/api/v1/guestbook_types.go b/test/api/v1/guestbook_types.go index 1f3f124..e1c7e6b 100644 --- a/test/api/v1/guestbook_types.go +++ b/test/api/v1/guestbook_types.go @@ -20,6 +20,7 @@ package v1 import ( "github.com/elastic/crd-ref-docs/api/common" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" gwapiv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1" ) @@ -52,6 +53,8 @@ type Embedded4 struct { } type EmbeddedX struct { X string `json:"x,omitempty"` + + Value apiextensionsv1.JSON `json:"value,omitempty"` } // Underlying tests that Underlying1's underlying type is Underlying2 instead of string. diff --git a/test/expected.asciidoc b/test/expected.asciidoc index 4717fb4..de0ec21 100644 --- a/test/expected.asciidoc +++ b/test/expected.asciidoc @@ -68,6 +68,7 @@ Package v1 contains API Schema definitions for the webapp v1 API group | *`a`* __string__ | | | | *`e`* __string__ | | | | *`x`* __string__ | | | +| *`value`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io[$$JSON$$]__ | | | |=== @@ -90,6 +91,7 @@ Package v1 contains API Schema definitions for the webapp v1 API group | Field | Description | Default | Validation | *`e`* __string__ | | | | *`x`* __string__ | | | +| *`value`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io[$$JSON$$]__ | | | |=== @@ -112,6 +114,7 @@ Package v1 contains API Schema definitions for the webapp v1 API group |=== | Field | Description | Default | Validation | *`x`* __string__ | | | +| *`value`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io[$$JSON$$]__ | | | |=== diff --git a/test/expected.md b/test/expected.md index 652a777..6f628f3 100644 --- a/test/expected.md +++ b/test/expected.md @@ -56,6 +56,7 @@ Package v1 contains API Schema definitions for the webapp v1 API group | `a` _string_ | | | | | `e` _string_ | | | | | `x` _string_ | | | | +| `value` _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io)_ | | | | #### Embedded1 @@ -73,6 +74,7 @@ _Appears in:_ | --- | --- | --- | --- | | `e` _string_ | | | | | `x` _string_ | | | | +| `value` _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io)_ | | | | #### EmbeddedX @@ -90,6 +92,7 @@ _Appears in:_ | Field | Description | Default | Validation | | --- | --- | --- | --- | | `x` _string_ | | | | +| `value` _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io)_ | | | | #### Guestbook diff --git a/test/hide.md b/test/hide.md index 2737558..45e5618 100644 --- a/test/hide.md +++ b/test/hide.md @@ -55,6 +55,7 @@ Package v1 contains API Schema definitions for the webapp v1 API group | `metadata` _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#objectmeta-v1-meta)_ | Refer to Kubernetes API documentation for fields of `metadata`. | | | | `a` _string_ | | | | | `x` _string_ | | | | +| `value` _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io)_ | | | | #### Embedded1 @@ -71,6 +72,7 @@ _Appears in:_ | Field | Description | Default | Validation | | --- | --- | --- | --- | | `x` _string_ | | | | +| `value` _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io)_ | | | | #### EmbeddedX @@ -88,6 +90,7 @@ _Appears in:_ | Field | Description | Default | Validation | | --- | --- | --- | --- | | `x` _string_ | | | | +| `value` _[JSON](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#json-v1-apiextensions-k8s-io)_ | | | | #### Guestbook