mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-27 16:17:34 +00:00
Feat: support vela show for workflow step definition
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
This commit is contained in:
+22
-5
@@ -22,17 +22,34 @@ import (
|
||||
"strings"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/build"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
)
|
||||
|
||||
// GetParameters get parameter from cue template
|
||||
func GetParameters(templateStr string) ([]types.Parameter, error) {
|
||||
r := cue.Runtime{}
|
||||
template, err := r.Compile("", templateStr+BaseTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func GetParameters(templateStr string, pd *packages.PackageDiscover) ([]types.Parameter, error) {
|
||||
var template *cue.Instance
|
||||
var err error
|
||||
if pd != nil {
|
||||
bi := build.NewContext().NewInstance("", nil)
|
||||
err := bi.AddFile("-", templateStr+BaseTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
template, err = pd.ImportPackagesAndBuildInstance(bi)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
r := cue.Runtime{}
|
||||
template, err = r.Compile("", templateStr+BaseTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
tempStruct, err := template.Value().Struct()
|
||||
if err != nil {
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
|
||||
func TestGetParameter(t *testing.T) {
|
||||
data, _ := os.ReadFile("testdata/workloads/metrics.cue")
|
||||
params, err := GetParameters(string(data))
|
||||
params, err := GetParameters(string(data), nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, params, []types.Parameter{
|
||||
{Name: "format", Required: false, Default: "prometheus", Usage: "format of the metrics, " +
|
||||
@@ -38,7 +38,7 @@ func TestGetParameter(t *testing.T) {
|
||||
{Name: "selector", Required: false, Usage: "the label selector for the pods, default is the workload labels", Type: cue.StructKind},
|
||||
})
|
||||
data, _ = os.ReadFile("testdata/workloads/deployment.cue")
|
||||
params, err = GetParameters(string(data))
|
||||
params, err = GetParameters(string(data), nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []types.Parameter{
|
||||
{Name: "name", Required: true, Default: "", Type: cue.StringKind},
|
||||
@@ -50,7 +50,7 @@ func TestGetParameter(t *testing.T) {
|
||||
params)
|
||||
|
||||
data, _ = os.ReadFile("testdata/workloads/test-param.cue")
|
||||
params, err = GetParameters(string(data))
|
||||
params, err = GetParameters(string(data), nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []types.Parameter{
|
||||
{Name: "name", Required: true, Default: "", Type: cue.StringKind},
|
||||
@@ -61,13 +61,13 @@ func TestGetParameter(t *testing.T) {
|
||||
{Name: "fval", Default: 64.3, Type: cue.FloatKind},
|
||||
{Name: "nval", Default: float64(0), Required: true, Type: cue.NumberKind}}, params)
|
||||
data, _ = os.ReadFile("testdata/workloads/empty.cue")
|
||||
params, err = GetParameters(string(data))
|
||||
params, err = GetParameters(string(data), nil)
|
||||
assert.NoError(t, err)
|
||||
var exp []types.Parameter
|
||||
assert.Equal(t, exp, params)
|
||||
|
||||
data, _ = os.ReadFile("testdata/workloads/webservice.cue") // test cue parameter with "// +ignore" annotation
|
||||
params, err = GetParameters(string(data)) // Only test for func RetrieveComments
|
||||
params, err = GetParameters(string(data), nil) // Only test for func RetrieveComments
|
||||
assert.NoError(t, err)
|
||||
var flag bool
|
||||
for _, para := range params {
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/ast"
|
||||
"cuelang.org/go/cue/build"
|
||||
"cuelang.org/go/cue/format"
|
||||
"cuelang.org/go/encoding/openapi"
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
@@ -62,6 +63,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
velacue "github.com/oam-dev/kubevela/pkg/cue"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
)
|
||||
|
||||
@@ -146,11 +148,26 @@ func HTTPGet(ctx context.Context, url string) ([]byte, error) {
|
||||
}
|
||||
|
||||
// GetCUEParameterValue converts definitions to cue format
|
||||
func GetCUEParameterValue(cueStr string) (cue.Value, error) {
|
||||
r := cue.Runtime{}
|
||||
template, err := r.Compile("", cueStr+velacue.BaseTemplate)
|
||||
if err != nil {
|
||||
return cue.Value{}, err
|
||||
func GetCUEParameterValue(cueStr string, pd *packages.PackageDiscover) (cue.Value, error) {
|
||||
var template *cue.Instance
|
||||
var err error
|
||||
if pd != nil {
|
||||
bi := build.NewContext().NewInstance("", nil)
|
||||
err := bi.AddFile("-", cueStr+velacue.BaseTemplate)
|
||||
if err != nil {
|
||||
return cue.Value{}, err
|
||||
}
|
||||
|
||||
template, err = pd.ImportPackagesAndBuildInstance(bi)
|
||||
if err != nil {
|
||||
return cue.Value{}, err
|
||||
}
|
||||
} else {
|
||||
r := cue.Runtime{}
|
||||
template, err = r.Compile("", cueStr+velacue.BaseTemplate)
|
||||
if err != nil {
|
||||
return cue.Value{}, err
|
||||
}
|
||||
}
|
||||
tempStruct, err := template.Value().Struct()
|
||||
if err != nil {
|
||||
|
||||
@@ -127,7 +127,7 @@ output: {
|
||||
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
_, err := GetCUEParameterValue(tc.cueStr)
|
||||
_, err := GetCUEParameterValue(tc.cueStr, nil)
|
||||
if tc.want.err != nil {
|
||||
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
|
||||
t.Errorf("\n%s\nGenOpenAPIFromFile(...): -want error, +got error:\n%s", tc.reason, diff)
|
||||
@@ -162,7 +162,7 @@ name
|
||||
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
_, err := GetCUEParameterValue(tc.cueStr)
|
||||
_, err := GetCUEParameterValue(tc.cueStr, nil)
|
||||
if diff := cmp.Diff(tc.want.errMsg, err.Error(), test.EquateConditions()); diff != "" {
|
||||
t.Errorf("\n%s\nGenOpenAPIFromFile(...): -want error, +got error:\n%s", tc.reason, diff)
|
||||
}
|
||||
|
||||
@@ -762,14 +762,14 @@ func ParseCapability(mapper discoverymapper.DiscoveryMapper, data []byte) (types
|
||||
}
|
||||
workloadDefinitionRef = ref.Name
|
||||
}
|
||||
return plugins.HandleDefinition(cd.Name, workloadDefinitionRef, cd.Annotations, cd.Labels, cd.Spec.Extension, types.TypeComponentDefinition, nil, cd.Spec.Schematic)
|
||||
return plugins.HandleDefinition(cd.Name, workloadDefinitionRef, cd.Annotations, cd.Labels, cd.Spec.Extension, types.TypeComponentDefinition, nil, cd.Spec.Schematic, nil)
|
||||
case "TraitDefinition":
|
||||
var td v1beta1.TraitDefinition
|
||||
err = yaml.Unmarshal(data, &td)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
return plugins.HandleDefinition(td.Name, td.Spec.Reference.Name, td.Annotations, td.Labels, td.Spec.Extension, types.TypeTrait, td.Spec.AppliesToWorkloads, td.Spec.Schematic)
|
||||
return plugins.HandleDefinition(td.Name, td.Spec.Reference.Name, td.Annotations, td.Labels, td.Spec.Extension, types.TypeTrait, td.Spec.AppliesToWorkloads, td.Spec.Schematic, nil)
|
||||
case "ScopeDefinition":
|
||||
// TODO(wonderflow): support scope definition here.
|
||||
}
|
||||
|
||||
+51
-13
@@ -32,6 +32,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/common"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/system"
|
||||
cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
|
||||
@@ -62,8 +63,8 @@ var webSite bool
|
||||
func NewCapabilityShowCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "show",
|
||||
Short: "Show the reference doc for a component type or trait.",
|
||||
Long: "Show the reference doc for component or trait types.",
|
||||
Short: "Show the reference doc for a component, trait or workflow.",
|
||||
Long: "Show the reference doc for component, trait or workflow types.",
|
||||
Example: `show webservice`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
@@ -126,7 +127,7 @@ func startReferenceDocsSite(ctx context.Context, ns string, c common.Args, ioStr
|
||||
}
|
||||
}
|
||||
if !capabilityIsValid {
|
||||
return fmt.Errorf("%s is not a valid component type or trait", capabilityName)
|
||||
return fmt.Errorf("%s is not a valid component, trait or workflow", capabilityName)
|
||||
}
|
||||
|
||||
cli, err := c.GetClient()
|
||||
@@ -139,7 +140,15 @@ func startReferenceDocsSite(ctx context.Context, ns string, c common.Args, ioStr
|
||||
},
|
||||
}
|
||||
|
||||
if err := ref.CreateMarkdown(ctx, capabilities, docsPath, plugins.ReferenceSourcePath); err != nil {
|
||||
config, err := c.GetConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pd, err := packages.NewPackageDiscover(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ref.CreateMarkdown(ctx, capabilities, docsPath, plugins.ReferenceSourcePath, pd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -171,6 +180,8 @@ func startReferenceDocsSite(ctx context.Context, ns string, c common.Args, ioStr
|
||||
case types.TypeScope:
|
||||
case types.TypeComponentDefinition:
|
||||
capabilityPath = plugins.ComponentDefinitionTypePath
|
||||
case types.TypeWorkflowStep:
|
||||
capabilityPath = plugins.WorkflowStepPath
|
||||
default:
|
||||
return fmt.Errorf("unsupported type: %v", capabilityType)
|
||||
}
|
||||
@@ -216,7 +227,7 @@ func launch(server *http.Server, errChan chan<- error) {
|
||||
|
||||
func generateSideBar(capabilities []types.Capability, docsPath string) error {
|
||||
sideBar := filepath.Join(docsPath, SideBar)
|
||||
components, traits := getComponentsAndTraits(capabilities)
|
||||
components, traits, workflowsteps := getDefinitions(capabilities)
|
||||
f, err := os.Create(sideBar)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -237,6 +248,14 @@ func generateSideBar(capabilities []types.Capability, docsPath string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := f.WriteString("- Workflow Steps\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range workflowsteps {
|
||||
if _, err := f.WriteString(fmt.Sprintf(" - [%s](%s/%s.md)\n", t, plugins.WorkflowStepPath, t)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -303,12 +322,12 @@ func generateREADME(capabilities []types.Capability, docsPath string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := f.WriteString("# KubeVela Reference Docs for Component Types and Traits\n" +
|
||||
"Click the navigation bar on the left or the links below to look into the detailed referennce of a Workload type or a Trait.\n"); err != nil {
|
||||
if _, err := f.WriteString("# KubeVela Reference Docs for Component Types, Traits and WorkflowSteps\n" +
|
||||
"Click the navigation bar on the left or the links below to look into the detailed reference of a Workload type, Trait or Workflow Step.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
workloads, traits := getComponentsAndTraits(capabilities)
|
||||
workloads, traits, workflowsteps := getDefinitions(capabilities)
|
||||
|
||||
if _, err := f.WriteString("## Component Types\n"); err != nil {
|
||||
return err
|
||||
@@ -328,28 +347,47 @@ func generateREADME(capabilities []types.Capability, docsPath string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := f.WriteString("## Workflow Steps\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range workflowsteps {
|
||||
if _, err := f.WriteString(fmt.Sprintf(" - [%s](%s/%s.md)\n", t, plugins.WorkflowStepPath, t)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getComponentsAndTraits(capabilities []types.Capability) ([]string, []string) {
|
||||
var components, traits []string
|
||||
func getDefinitions(capabilities []types.Capability) ([]string, []string, []string) {
|
||||
var components, traits, workflowSteps []string
|
||||
for _, c := range capabilities {
|
||||
switch c.Type {
|
||||
case types.TypeComponentDefinition:
|
||||
components = append(components, c.Name)
|
||||
case types.TypeTrait:
|
||||
traits = append(traits, c.Name)
|
||||
case types.TypeWorkflowStep:
|
||||
workflowSteps = append(workflowSteps, c.Name)
|
||||
case types.TypeScope:
|
||||
case types.TypeWorkload:
|
||||
default:
|
||||
}
|
||||
}
|
||||
return components, traits
|
||||
return components, traits, workflowSteps
|
||||
}
|
||||
|
||||
// ShowReferenceConsole will show capability reference in console
|
||||
func ShowReferenceConsole(ctx context.Context, c common.Args, ioStreams cmdutil.IOStreams, capabilityName string, ns string) error {
|
||||
capability, err := plugins.GetCapabilityByName(ctx, c, capabilityName, ns)
|
||||
config, err := c.GetConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pd, err := packages.NewPackageDiscover(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
capability, err := plugins.GetCapabilityByName(ctx, c, capabilityName, ns, pd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -377,7 +415,7 @@ func ShowReferenceConsole(ctx context.Context, c common.Args, ioStreams cmdutil.
|
||||
return err
|
||||
}
|
||||
case types.CUECategory:
|
||||
propertyConsole, err = ref.GenerateCUETemplateProperties(capability)
|
||||
propertyConsole, err = ref.GenerateCUETemplateProperties(capability, pd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ func TestGetWorkloadAndTraits(t *testing.T) {
|
||||
}
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
gotWorkloads, gotTraits := getComponentsAndTraits(tc.capabilities)
|
||||
gotWorkloads, gotTraits, _ := getDefinitions(tc.capabilities)
|
||||
assert.Equal(t, tc.want, want{workloads: gotWorkloads, traits: gotTraits})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/appfile"
|
||||
"github.com/oam-dev/kubevela/pkg/cue"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/common"
|
||||
@@ -212,9 +213,9 @@ func validateCapabilities(tmp *types.Capability, dm discoverymapper.DiscoveryMap
|
||||
|
||||
// HandleDefinition will handle definition to capability
|
||||
func HandleDefinition(name, crdName string, annotation, labels map[string]string, extension *runtime.RawExtension, tp types.CapType,
|
||||
applyTo []string, schematic *commontypes.Schematic) (types.Capability, error) {
|
||||
applyTo []string, schematic *commontypes.Schematic, pd *packages.PackageDiscover) (types.Capability, error) {
|
||||
var tmp types.Capability
|
||||
tmp, err := HandleTemplate(extension, schematic, name)
|
||||
tmp, err := HandleTemplate(extension, schematic, name, pd)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
@@ -242,7 +243,7 @@ func GetDescription(annotation map[string]string) string {
|
||||
}
|
||||
|
||||
// HandleTemplate will handle definition template to capability
|
||||
func HandleTemplate(in *runtime.RawExtension, schematic *commontypes.Schematic, name string) (types.Capability, error) {
|
||||
func HandleTemplate(in *runtime.RawExtension, schematic *commontypes.Schematic, name string, pd *packages.PackageDiscover) (types.Capability, error) {
|
||||
tmp, err := appfile.ConvertTemplateJSON2Object(name, in, schematic)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
@@ -282,7 +283,7 @@ func HandleTemplate(in *runtime.RawExtension, schematic *commontypes.Schematic,
|
||||
}
|
||||
return types.Capability{}, errors.New("template not exist in definition")
|
||||
}
|
||||
tmp.Parameters, err = cue.GetParameters(tmp.CueTemplate)
|
||||
tmp.Parameters, err = cue.GetParameters(tmp.CueTemplate, pd)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
@@ -291,7 +292,7 @@ func HandleTemplate(in *runtime.RawExtension, schematic *commontypes.Schematic,
|
||||
}
|
||||
|
||||
// GetCapabilityByName gets capability by definition name
|
||||
func GetCapabilityByName(ctx context.Context, c common.Args, capabilityName string, ns string) (*types.Capability, error) {
|
||||
func GetCapabilityByName(ctx context.Context, c common.Args, capabilityName string, ns string, pd *packages.PackageDiscover) (*types.Capability, error) {
|
||||
var (
|
||||
foundCapability bool
|
||||
capability *types.Capability
|
||||
@@ -357,6 +358,25 @@ func GetCapabilityByName(ctx context.Context, c common.Args, capabilityName stri
|
||||
}
|
||||
return capability, nil
|
||||
}
|
||||
|
||||
var wfStepDef v1beta1.WorkflowStepDefinition
|
||||
err = newClient.Get(ctx, client.ObjectKey{Namespace: ns, Name: capabilityName}, &wfStepDef)
|
||||
if err == nil {
|
||||
foundCapability = true
|
||||
} else if kerrors.IsNotFound(err) {
|
||||
err = newClient.Get(ctx, client.ObjectKey{Namespace: types.DefaultKubeVelaNS, Name: capabilityName}, &wfStepDef)
|
||||
if err == nil {
|
||||
foundCapability = true
|
||||
}
|
||||
}
|
||||
if foundCapability {
|
||||
capability, err = GetCapabilityByWorkflowStepDefinitionObject(wfStepDef, pd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return capability, nil
|
||||
}
|
||||
|
||||
if ns == types.DefaultKubeVelaNS {
|
||||
return nil, fmt.Errorf("could not find %s in namespace %s", capabilityName, ns)
|
||||
}
|
||||
@@ -366,7 +386,7 @@ func GetCapabilityByName(ctx context.Context, c common.Args, capabilityName stri
|
||||
// GetCapabilityByComponentDefinitionObject gets capability by ComponentDefinition object
|
||||
func GetCapabilityByComponentDefinitionObject(componentDef v1beta1.ComponentDefinition, referenceName string) (*types.Capability, error) {
|
||||
capability, err := HandleDefinition(componentDef.Name, referenceName, componentDef.Annotations, componentDef.Labels,
|
||||
componentDef.Spec.Extension, types.TypeComponentDefinition, nil, componentDef.Spec.Schematic)
|
||||
componentDef.Spec.Extension, types.TypeComponentDefinition, nil, componentDef.Spec.Schematic, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to handle ComponentDefinition")
|
||||
}
|
||||
@@ -381,10 +401,25 @@ func GetCapabilityByTraitDefinitionObject(traitDef v1beta1.TraitDefinition) (*ty
|
||||
err error
|
||||
)
|
||||
capability, err = HandleDefinition(traitDef.Name, traitDef.Spec.Reference.Name, traitDef.Annotations, traitDef.Labels,
|
||||
traitDef.Spec.Extension, types.TypeTrait, nil, traitDef.Spec.Schematic)
|
||||
traitDef.Spec.Extension, types.TypeTrait, nil, traitDef.Spec.Schematic, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to handle TraitDefinition")
|
||||
}
|
||||
capability.Namespace = traitDef.Namespace
|
||||
return &capability, nil
|
||||
}
|
||||
|
||||
// GetCapabilityByWorkflowStepDefinitionObject gets capability by WorkflowStepDefinition object
|
||||
func GetCapabilityByWorkflowStepDefinitionObject(wfStepDef v1beta1.WorkflowStepDefinition, pd *packages.PackageDiscover) (*types.Capability, error) {
|
||||
var (
|
||||
capability types.Capability
|
||||
err error
|
||||
)
|
||||
capability, err = HandleDefinition(wfStepDef.Name, wfStepDef.Spec.Reference.Name, wfStepDef.Annotations, wfStepDef.Labels,
|
||||
nil, types.TypeWorkflowStep, nil, wfStepDef.Spec.Schematic, pd)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to handle WorkflowStepDefinition")
|
||||
}
|
||||
capability.Namespace = wfStepDef.Namespace
|
||||
return &capability, nil
|
||||
}
|
||||
|
||||
@@ -227,15 +227,15 @@ var _ = Describe("test GetCapabilityByName", func() {
|
||||
|
||||
It("get capability", func() {
|
||||
Context("ComponentDefinition is in the current namespace", func() {
|
||||
_, err := GetCapabilityByName(ctx, c, component1, ns)
|
||||
_, err := GetCapabilityByName(ctx, c, component1, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
})
|
||||
Context("ComponentDefinition is in the default namespace", func() {
|
||||
_, err := GetCapabilityByName(ctx, c, component2, ns)
|
||||
_, err := GetCapabilityByName(ctx, c, component2, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
})
|
||||
Context("ComponentDefinition is in the default namespace", func() {
|
||||
cap, err := GetCapabilityByName(ctx, c, component3, ns)
|
||||
cap, err := GetCapabilityByName(ctx, c, component3, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
jsontmp, err := json.Marshal(cap.KubeParameter)
|
||||
Expect(err).Should(BeNil())
|
||||
@@ -245,20 +245,20 @@ var _ = Describe("test GetCapabilityByName", func() {
|
||||
Expect(string(jsontmp)).Should(ContainSubstring("the specific container port num which can accept external request."))
|
||||
})
|
||||
Context("ComponentDefinition's workload type is AutoDetectWorkloadDefinition", func() {
|
||||
_, err := GetCapabilityByName(ctx, c, component4, ns)
|
||||
_, err := GetCapabilityByName(ctx, c, component4, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
})
|
||||
|
||||
Context("TraitDefinition is in the current namespace", func() {
|
||||
_, err := GetCapabilityByName(ctx, c, trait1, ns)
|
||||
_, err := GetCapabilityByName(ctx, c, trait1, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
})
|
||||
Context("TraitDefinition is in the default namespace", func() {
|
||||
_, err := GetCapabilityByName(ctx, c, trait2, ns)
|
||||
_, err := GetCapabilityByName(ctx, c, trait2, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
})
|
||||
Context("TraitDefinition is in the default namespace", func() {
|
||||
cap, err := GetCapabilityByName(ctx, c, trait3, ns)
|
||||
cap, err := GetCapabilityByName(ctx, c, trait3, ns, nil)
|
||||
Expect(err).Should(BeNil())
|
||||
jsontmp, err := json.Marshal(cap.KubeParameter)
|
||||
Expect(err).Should(BeNil())
|
||||
@@ -267,7 +267,7 @@ var _ = Describe("test GetCapabilityByName", func() {
|
||||
})
|
||||
|
||||
Context("capability cloud not be found", func() {
|
||||
_, err := GetCapabilityByName(ctx, c, "a-component-definition-not-existed", ns)
|
||||
_, err := GetCapabilityByName(ctx, c, "a-component-definition-not-existed", ns, nil)
|
||||
Expect(err).Should(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -149,7 +149,7 @@ variable "acl" {
|
||||
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := tc.ref.CreateMarkdown(ctx, tc.capabilities, RefTestDir, ReferenceSourcePath)
|
||||
got := tc.ref.CreateMarkdown(ctx, tc.capabilities, RefTestDir, ReferenceSourcePath, nil)
|
||||
if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" {
|
||||
t.Errorf("\n%s\nCreateMakrdown(...): -want error, +got error:\n%s", tc.reason, diff)
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ 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/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/common"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/terraform"
|
||||
)
|
||||
@@ -55,6 +56,8 @@ const (
|
||||
WorkloadTypePath = "workload-types"
|
||||
// TraitPath is the URL path for trait typed capability
|
||||
TraitPath = "traits"
|
||||
// WorkflowStepPath is the URL path for workflow step typed capability
|
||||
WorkflowStepPath = "workflowsteps"
|
||||
)
|
||||
|
||||
// Int64Type is int64 type
|
||||
@@ -556,6 +559,14 @@ func (ref *MarkdownReference) GenerateReferenceDocs(ctx context.Context, c commo
|
||||
caps []types.Capability
|
||||
err error
|
||||
)
|
||||
config, err := c.GetConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pd, err := packages.NewPackageDiscover(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ref.DefinitionName == "" {
|
||||
caps, err = LoadAllInstalledCapability("default", c)
|
||||
@@ -563,18 +574,18 @@ func (ref *MarkdownReference) GenerateReferenceDocs(ctx context.Context, c commo
|
||||
return fmt.Errorf("failed to get all capabilityes: %w", err)
|
||||
}
|
||||
} else {
|
||||
cap, err := GetCapabilityByName(ctx, c, ref.DefinitionName, namespace)
|
||||
cap, err := GetCapabilityByName(ctx, c, ref.DefinitionName, namespace, pd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get capability capability %s: %w", ref.DefinitionName, err)
|
||||
}
|
||||
caps = []types.Capability{*cap}
|
||||
}
|
||||
|
||||
return ref.CreateMarkdown(ctx, caps, baseRefPath, ReferenceSourcePath)
|
||||
return ref.CreateMarkdown(ctx, caps, baseRefPath, ReferenceSourcePath, pd)
|
||||
}
|
||||
|
||||
// CreateMarkdown creates markdown based on capabilities
|
||||
func (ref *MarkdownReference) CreateMarkdown(ctx context.Context, caps []types.Capability, baseRefPath, referenceSourcePath string) error {
|
||||
func (ref *MarkdownReference) CreateMarkdown(ctx context.Context, caps []types.Capability, baseRefPath, referenceSourcePath string, pd *packages.PackageDiscover) error {
|
||||
setDisplayFormat("markdown")
|
||||
for i, c := range caps {
|
||||
var (
|
||||
@@ -610,7 +621,7 @@ func (ref *MarkdownReference) CreateMarkdown(ctx context.Context, caps []types.C
|
||||
capNameInTitle := ref.makeReadableTitle(capName)
|
||||
switch c.Category {
|
||||
case types.CUECategory:
|
||||
cueValue, err := common.GetCUEParameterValue(c.CueTemplate)
|
||||
cueValue, err := common.GetCUEParameterValue(c.CueTemplate, pd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to retrieve `parameters` value from %s with err: %w", c.Name, err)
|
||||
}
|
||||
@@ -821,7 +832,7 @@ func (ref *ParseReference) parseParameters(paraValue cue.Value, paramKey string,
|
||||
param.Type = val.IncompleteKind()
|
||||
switch val.IncompleteKind() {
|
||||
case cue.StructKind:
|
||||
if subField, _ := val.Struct(); subField.Len() == 0 { // err cannot be not nil,so ignore it
|
||||
if subField, err := val.Struct(); err == nil && subField.Len() == 0 { // err cannot be not nil,so ignore it
|
||||
if mapValue, ok := val.Elem(); ok {
|
||||
// In the future we could recursive call to surpport complex map-value(struct or list)
|
||||
param.PrintableType = fmt.Sprintf("map[string]%s", mapValue.IncompleteKind().String())
|
||||
@@ -932,11 +943,11 @@ func (ref *MarkdownReference) generateConflictWithAndMore(capabilityName string,
|
||||
}
|
||||
|
||||
// GenerateCUETemplateProperties get all properties of a capability
|
||||
func (ref *ConsoleReference) GenerateCUETemplateProperties(capability *types.Capability) ([]ConsoleReference, error) {
|
||||
func (ref *ConsoleReference) GenerateCUETemplateProperties(capability *types.Capability, pd *packages.PackageDiscover) ([]ConsoleReference, error) {
|
||||
setDisplayFormat("console")
|
||||
capName := capability.Name
|
||||
|
||||
cueValue, err := common.GetCUEParameterValue(capability.CueTemplate)
|
||||
cueValue, err := common.GetCUEParameterValue(capability.CueTemplate, pd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to retrieve `parameters` value from %s with err: %w", capName, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user