Skip to content

Commit

Permalink
Add apiextensions types to default Kubernetes known types (#82)
Browse files Browse the repository at this point in the history
This adds types of the apiextensions.k8s.io group to the Kubernetes default known types.

---------
Signed-off-by: zirain <zirain2009@gmail.com>
  • Loading branch information
zirain authored Jul 23, 2024
1 parent 1c93a29 commit dfa49ca
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
11 changes: 8 additions & 3 deletions renderer/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 }}`
)

Expand Down Expand Up @@ -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),
}
Expand Down
24 changes: 21 additions & 3 deletions renderer/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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$$]__ | | |
|===


Expand All @@ -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$$]__ | | |
|===


Expand All @@ -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$$]__ | | |
|===


Expand Down
3 changes: 3 additions & 0 deletions test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/hide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dfa49ca

Please sign in to comment.