Skip to content

Commit

Permalink
Add gen-doc tool
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Oct 18, 2023
1 parent c05a31d commit 062b2fa
Show file tree
Hide file tree
Showing 45 changed files with 2,657 additions and 1,087 deletions.
46 changes: 46 additions & 0 deletions cmd/gen-docs/crd_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
$HOME/go/src/github.com/crossplane/crossplane/cluster/crds
$HOME/go/src/github.com/jetstack/cert-manager/deploy/crds
$HOME/go/src/github.com/kubernetes-csi/external-snapshotter/client/config/crd
$HOME/go/src/github.com/open-policy-agent/gatekeeper/config/crd/bases
$HOME/go/src/github.com/prometheus-operator/prometheus-operator/example/prometheus-operator-crd
$HOME/go/src/github.com/upbound/provider-aws/package/crds
$HOME/go/src/github.com/upbound/provider-azure/package/crds
$HOME/go/src/github.com/upbound/provider-gcp/package/crds
$HOME/go/src/go.kubeware.dev/catalog/config/crd/bases
$HOME/go/src/go.openviz.dev/grafana-tools/crds
$HOME/go/src/k8s.io/api/crds
$HOME/go/src/k8s.io/autoscaler/vertical-pod-autoscaler/deploy/vpa-v1-crd-gen.yaml
$HOME/go/src/k8s.io/autoscaler/vertical-pod-autoscaler/deploy/vpa-v1-crd.yaml
$HOME/go/src/k8s.io/kube-aggregator/crds
$HOME/go/src/kmodules.xyz/custom-resources/crds
$HOME/go/src/kmodules.xyz/resource-metadata/crds
$HOME/go/src/kubedb.dev/apimachinery/crds
$HOME/go/src/kubeform.dev/provider-aws/package/crds
$HOME/go/src/kubeform.dev/provider-azure/package/crds
$HOME/go/src/kubeform.dev/provider-gcp/package/crds
$HOME/go/src/kubeops.dev/external-dns-operator/crds
$HOME/go/src/kubeops.dev/falco-ui-server/crds
$HOME/go/src/kubeops.dev/supervisor/crds
$HOME/go/src/kubeops.dev/ui-server/crds
$HOME/go/src/kubevault.dev/apimachinery/crds
$HOME/go/src/open-cluster-management.io/api/addon/v1alpha1
$HOME/go/src/open-cluster-management.io/api/cluster/v1
$HOME/go/src/open-cluster-management.io/api/cluster/v1alpha1
$HOME/go/src/open-cluster-management.io/api/cluster/v1beta1
$HOME/go/src/open-cluster-management.io/api/cluster/v1beta2
$HOME/go/src/open-cluster-management.io/api/crdsv1beta1
$HOME/go/src/open-cluster-management.io/api/operator/v1
$HOME/go/src/open-cluster-management.io/api/work/v1
$HOME/go/src/open-cluster-management.io/api/work/v1alpha1
$HOME/go/src/sigs.k8s.io/cluster-api-provider-aws/config/crd/bases
$HOME/go/src/sigs.k8s.io/cluster-api-provider-azure/config/crd/bases
$HOME/go/src/sigs.k8s.io/cluster-api-provider-gcp/config/crd/bases
$HOME/go/src/sigs.k8s.io/cluster-api/config/crd/bases
$HOME/go/src/sigs.k8s.io/secrets-store-csi-driver/charts/secrets-store-csi-driver/crds
$HOME/go/src/stash.appscode.dev/apimachinery/crds
$HOME/go/src/stash.appscode.dev/ui-server/crds
$HOME/go/src/voyagermesh.dev/apimachinery/crds
$HOME/go/src/voyagermesh.dev/installer/charts/gateway-helm/crds
$HOME/go/src/voyagermesh.dev/installer/charts/gateway-helm/crds/generated
$HOME/go/src/voyagermesh.dev/installer/charts/gateway-helm/crds/voyager
$HOME/go/src/x-helm.dev/apimachinery/crds
287 changes: 287 additions & 0 deletions cmd/gen-docs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"

"kmodules.xyz/client-go/tools/parser"
"kmodules.xyz/resource-metadata/hub/resourcedescriptors"
"kmodules.xyz/resource-metadata/hub/resourceeditors"

"github.com/pkg/errors"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
crdv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

func main() {
dir := "/Users/tamal/go/src/go.crdhub.dev/crdhub/public"
os.MkdirAll(dir, 0o755)

Check failure on line 45 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value of `os.MkdirAll` is not checked (errcheck)

Check failure on line 45 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value of `os.MkdirAll` is not checked (errcheck)
GenDescriptor(dir)

Check failure on line 46 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value is not checked (errcheck)

Check failure on line 46 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value is not checked (errcheck)
GenResourceEditor(dir)

Check failure on line 47 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value is not checked (errcheck)

Check failure on line 47 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value is not checked (errcheck)
ImportCRD(dir)

Check failure on line 48 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value is not checked (errcheck)

Check failure on line 48 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value is not checked (errcheck)
}

func GenDescriptor(dir string) error {
for _, rd := range resourcedescriptors.List() {
g := rd.Spec.Resource.Group
if g == "" {
g = "core"
}
r := rd.Spec.Resource.Name

rdir := filepath.Join(dir, g, r)
os.MkdirAll(rdir, 0o755)

Check failure on line 60 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value of `os.MkdirAll` is not checked (errcheck)

Check failure on line 60 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value of `os.MkdirAll` is not checked (errcheck)

// os.WriteFile(filepath.Join(rdir, "crd.yaml"), []byte("crd.yaml"), 0644)

vdir := filepath.Join(rdir, rd.Spec.Resource.Version)
os.MkdirAll(vdir, 0o755)

Check failure on line 65 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value of `os.MkdirAll` is not checked (errcheck)

Check failure on line 65 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value of `os.MkdirAll` is not checked (errcheck)

data, err := yaml.Marshal(rd)
if err != nil {
return err
}
os.WriteFile(filepath.Join(vdir, "resourcedescriptor.yaml"), data, 0o644)

Check failure on line 71 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value of `os.WriteFile` is not checked (errcheck)

Check failure on line 71 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value of `os.WriteFile` is not checked (errcheck)

if rd.Spec.Validation != nil && rd.Spec.Validation.OpenAPIV3Schema != nil {
data, err := yaml.Marshal(rd.Spec.Validation.OpenAPIV3Schema)
if err != nil {
return err
}
os.WriteFile(filepath.Join(vdir, "openapiv3_schema.yaml"), data, 0o644)

Check failure on line 78 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value of `os.WriteFile` is not checked (errcheck)

Check failure on line 78 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value of `os.WriteFile` is not checked (errcheck)
}
}
return nil
}

func GenResourceEditor(dir string) error {
for _, rd := range resourceeditors.List() {
g := rd.Spec.Resource.Group
if g == "" {
g = "core"
}
r := rd.Spec.Resource.Name

rdir := filepath.Join(dir, g, r)
os.MkdirAll(rdir, 0o755)

// os.WriteFile(filepath.Join(rdir, "crd.yaml"), []byte("crd.yaml"), 0644)

vdir := filepath.Join(rdir, rd.Spec.Resource.Version)
os.MkdirAll(vdir, 0o755)

data, err := yaml.Marshal(rd)
if err != nil {
return err
}
os.WriteFile(filepath.Join(vdir, "resourceeditor.yaml"), data, 0o644)

Check failure on line 104 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.20.15)

Error return value of `os.WriteFile` is not checked (errcheck)

Check failure on line 104 in cmd/gen-docs/main.go

View workflow job for this annotation

GitHub Actions / k8s (v1.26.3)

Error return value of `os.WriteFile` is not checked (errcheck)
}
return nil
}

func ImportUIWizards() {
}

/*
# Add icons
go run cmd/icon-namer/main.go
# Import crds
# FluxCD CRDs
# helm template flux fluxcd-community/flux2 --output-dir=/tmp/fluxcd-manifests
/tmp/fluxcd-manifests
*/
func ImportCRD(dir string) error {
_, f2, _, ok := runtime.Caller(1)
if !ok {
return errors.New("can't detect crd_list.txt file location")
}
fmt.Println(f2)

data, err := os.ReadFile(filepath.Join(filepath.Dir(f2), "crd_list.txt"))
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
line = os.ExpandEnv(line)
processLocation(dir, line)
}
return nil
}

func processLocation(dir, location string) error {
u, err := url.Parse(location)
if err != nil {
return err
}

if u.Scheme != "" {
resp, err := http.Get(u.String())
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
return err
}
return parser.ProcessResources(buf.Bytes(), processObject(dir))
} else {
fi, err := os.Stat(location)
if err != nil {
return err
}
if fi.IsDir() {
ext := crdFileExtension(location)

err = filepath.Walk(location, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ext) {
return nil
}

data, err := os.ReadFile(path)
if err != nil {
return err
}
return parser.ProcessResources(data, processObject(dir))
})
if err != nil {
return err
}
} else {
data, err := os.ReadFile(location)
if err != nil {
return err
}
crd, err := CustomResourceDefinition(data)
if err != nil {
return err
}
err = WriteDescriptor(dir, crd)
if err != nil {
return err
}
}
}
return nil
}

func CustomResourceDefinition(data []byte) (*crdv1.CustomResourceDefinition, error) {
var tm metav1.TypeMeta
err := yaml.Unmarshal(data, &tm)
if err != nil {
return nil, err
}

if tm.APIVersion == crdv1.SchemeGroupVersion.String() {
var out crdv1.CustomResourceDefinition
err := yaml.Unmarshal(data, &out)
if err != nil {
return nil, err
}
return &out, nil
}

var defv1 crdv1beta1.CustomResourceDefinition
err = yaml.Unmarshal(data, &defv1)
if err != nil {
return nil, err
}

var inner apiextensions.CustomResourceDefinition
err = crdv1beta1.Convert_v1beta1_CustomResourceDefinition_To_apiextensions_CustomResourceDefinition(&defv1, &inner, nil)
if err != nil {
return nil, err
}

var out crdv1.CustomResourceDefinition
err = crdv1.Convert_apiextensions_CustomResourceDefinition_To_v1_CustomResourceDefinition(&inner, &out, nil)
if err != nil {
return nil, err
}
return &out, nil
}

func processObject(dir string) func(ri parser.ResourceInfo) error {
return func(ri parser.ResourceInfo) error {
data, err := json.Marshal(ri.Object)
if err != nil {
return err
}
crd, err := CustomResourceDefinition(data)
if err != nil {
return err
}
err = WriteDescriptor(dir, crd)
if err != nil {
return err
}
return nil
}
}

func crdFileExtension(dir string) string {
entries, err := os.ReadDir(dir)
if err != nil {
panic(err)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
if strings.HasSuffix(entry.Name(), ".v1.yaml") {
return ".v1.yaml"
}
}
return ".yaml"
}

func WriteDescriptor(dir string, crd *crdv1.CustomResourceDefinition) error {
rdir := filepath.Join(dir, crd.Spec.Group, crd.Spec.Names.Plural)
os.MkdirAll(rdir, 0o755)
data, err := yaml.Marshal(crd)
if err != nil {
return err
}
os.WriteFile(filepath.Join(rdir, "crd.yaml"), data, 0o644)
return nil
}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
k8s.io/klog/v2 v2.80.1
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
kmodules.xyz/apiversion v0.2.0
kmodules.xyz/client-go v0.25.37
kmodules.xyz/client-go v0.25.39
kmodules.xyz/crd-schema-fuzz v0.25.0
kmodules.xyz/resource-metrics v0.25.5
kmodules.xyz/schema-checker v0.4.1
Expand All @@ -38,7 +38,7 @@ require (

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.12.1 // indirect
Expand Down Expand Up @@ -100,8 +100,8 @@ require (
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand All @@ -111,7 +111,7 @@ require (
gomodules.xyz/pointer v0.1.0 // indirect
gomodules.xyz/sets v0.2.1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.1.0 // indirect
Expand Down
Loading

0 comments on commit 062b2fa

Please sign in to comment.