diff --git a/hack/docgen/cli/gen.go b/hack/docgen/cli/gen.go index 021083445..b076d16b1 100644 --- a/hack/docgen/cli/gen.go +++ b/hack/docgen/cli/gen.go @@ -61,7 +61,7 @@ func PrintCLIByTag(cmd *cobra.Command, all []*cobra.Command, tag string) string return result } -// GenMarkdownTreeForIndex will generate the markdown doc for vela index +// GenMarkdownTreeForIndex will generate the Markdown doc for vela index func GenMarkdownTreeForIndex(cmd *cobra.Command, dir string) error { basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md" filename := filepath.Join(dir, basename) diff --git a/hack/docgen/def/mods/component.go b/hack/docgen/def/mods/component.go index b143c0fa3..fb29879af 100644 --- a/hack/docgen/def/mods/component.go +++ b/hack/docgen/def/mods/component.go @@ -24,6 +24,8 @@ import ( "strings" "time" + "k8s.io/klog/v2" + "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/references/docgen" @@ -86,8 +88,14 @@ func ComponentDef(ctx context.Context, c common.Args, path, location *string, de }, CustomDocHeader: CustomComponentHeaderEN, } - ref.Remote = &docgen.FromCluster{Namespace: types.DefaultKubeVelaNS} + ref.Local = &docgen.FromLocal{Path: ComponentDefDir} + dm, err := c.GetDiscoveryMapper() + if err != nil { + klog.ErrorS(err, "failed to get discovery mapper") + return + } + ref.DiscoveryMapper = dm if *path != "" { ref.I18N = &docgen.En if strings.Contains(*location, "zh") || strings.Contains(*location, "chinese") { diff --git a/references/docgen/markdown.go b/references/docgen/markdown.go index 15f803d83..a4246136f 100644 --- a/references/docgen/markdown.go +++ b/references/docgen/markdown.go @@ -25,6 +25,8 @@ import ( "sort" "strings" + "github.com/oam-dev/kubevela/pkg/oam/discoverymapper" + "k8s.io/klog/v2" "github.com/pkg/errors" @@ -43,6 +45,7 @@ type MarkdownReference struct { Filter func(types.Capability) bool AllInOne bool CustomDocHeader string + DiscoveryMapper discoverymapper.DiscoveryMapper ParseReference } @@ -140,13 +143,16 @@ func (ref *MarkdownReference) CreateMarkdown(ctx context.Context, caps []types.C func (ref *MarkdownReference) GenerateMarkdownForCap(ctx context.Context, c types.Capability, pd *packages.PackageDiscover, containSuffix bool) (string, error) { var ( description string + base string sample string specification string generatedDoc string + baseDoc string err error ) - if c.Type != types.TypeWorkload && c.Type != types.TypeComponentDefinition && c.Type != types.TypeTrait && - c.Type != types.TypeWorkflowStep && c.Type != types.TypePolicy { + switch c.Type { + case types.TypeWorkload, types.TypeComponentDefinition, types.TypeTrait, types.TypeWorkflowStep, types.TypePolicy: + default: return "", fmt.Errorf("type(%s) of the capability(%s) is not supported for now", c.Type, c.Name) } @@ -164,6 +170,10 @@ func (ref *MarkdownReference) GenerateMarkdownForCap(ctx context.Context, c type if err != nil { return "", err } + baseDoc, err = GetBaseResourceKinds(c.CueTemplate, pd, ref.DiscoveryMapper) + if err != nil { + klog.Warningf("failed to get base resource kinds for %s: %v", c.Name, err) + } case types.HelmCategory, types.KubeCategory: properties, _, err := ref.GenerateHelmAndKubeProperties(ctx, &c) if err != nil { @@ -203,11 +213,13 @@ func (ref *MarkdownReference) GenerateMarkdownForCap(ctx context.Context, c type var sharp = "##" exampleTitle := lang.Get(Examples) + baseTitle := lang.Get(Base) specificationTitle := lang.Get(Specification) if ref.AllInOne { sharp = "###" exampleTitle += " (" + capName + ")" specificationTitle += " (" + capName + ")" + baseTitle += " (" + capName + ")" } description = fmt.Sprintf("\n\n%s %s\n\n%s", sharp, lang.Get(Description), strings.TrimSpace(lang.Get(descriptionI18N))) if !strings.HasSuffix(description, lang.Get(".")) { @@ -238,9 +250,12 @@ func (ref *MarkdownReference) GenerateMarkdownForCap(ctx context.Context, c type if sampleContent != "" { sample = fmt.Sprintf("\n\n%s %s\n\n%s", sharp, exampleTitle, sampleContent) } + if c.Category == types.CUECategory && baseDoc != "" { + base = fmt.Sprintf("\n\n%s %s\n\n%s", sharp, baseTitle, baseDoc) + } specification = fmt.Sprintf("\n\n%s %s\n%s", sharp, specificationTitle, parameterDoc) - return title + description + sample + specification, nil + return title + description + base + sample + specification, nil } func (ref *MarkdownReference) makeReadableTitle(title string) string { diff --git a/references/docgen/parser.go b/references/docgen/parser.go index 9964e20e1..107d2adc3 100644 --- a/references/docgen/parser.go +++ b/references/docgen/parser.go @@ -35,6 +35,7 @@ import ( "github.com/rogpeppe/go-internal/modfile" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" @@ -44,8 +45,10 @@ import ( "github.com/oam-dev/kubevela/pkg/controller/utils" velacue "github.com/oam-dev/kubevela/pkg/cue" "github.com/oam-dev/kubevela/pkg/cue/model" + "github.com/oam-dev/kubevela/pkg/cue/model/value" "github.com/oam-dev/kubevela/pkg/cue/packages" pkgdef "github.com/oam-dev/kubevela/pkg/definition" + "github.com/oam-dev/kubevela/pkg/oam/discoverymapper" pkgUtils "github.com/oam-dev/kubevela/pkg/utils" "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/pkg/utils/terraform" @@ -250,8 +253,8 @@ func (ref *ParseReference) parseParameters(capName string, paraValue cue.Value, } } case cue.ListKind: - elem, success := val.Elem() - if !success { + elem := val.LookupPath(cue.MakePath(cue.AnyIndex)) + if !elem.Exists() { // fail to get elements, use the value of ListKind to be the type param.Type = val.Kind() param.PrintableType = val.IncompleteKind().String() @@ -637,3 +640,36 @@ func WalkParameterSchema(parameters *openapi3.Schema, name string, depth int) { WalkParameterSchema(schema.Schemas, schema.Name, depth+1) } } + +// GetBaseResourceKinds helps get resource.group string of components' base resource +func GetBaseResourceKinds(cueStr string, pd *packages.PackageDiscover, dm discoverymapper.DiscoveryMapper) (string, error) { + t, err := value.NewValue(cueStr+velacue.BaseTemplate, pd, "") + if err != nil { + return "", errors.Wrap(err, "failed to parse base template") + } + tmpl := t.CueValue() + + kindValue := tmpl.LookupPath(cue.ParsePath("output.kind")) + kind, err := kindValue.String() + if err != nil { + return "", err + } + apiVersionValue := tmpl.LookupPath(cue.ParsePath("output.apiVersion")) + apiVersion, err := apiVersionValue.String() + if err != nil { + return "", err + } + GroupAndVersion := strings.Split(apiVersion, "/") + if len(GroupAndVersion) == 1 { + GroupAndVersion = append([]string{""}, GroupAndVersion...) + } + gvr, err := dm.ResourcesFor(schema.GroupVersionKind{ + Group: GroupAndVersion[0], + Version: GroupAndVersion[1], + Kind: kind, + }) + if err != nil { + return "", err + } + return fmt.Sprintf("- %s.%s", gvr.Resource, gvr.Group), nil +} diff --git a/references/docgen/vars.go b/references/docgen/vars.go index 7a0e7fddc..63eb0526b 100644 --- a/references/docgen/vars.go +++ b/references/docgen/vars.go @@ -30,4 +30,6 @@ const ( Description = "Description" // Examples marks the title of example in reference doc Examples = "Examples" + // Base is the title of base in reference doc + Base = "Underlying Kubernetes Resources" )