mirror of
https://github.com/kubevela/kubevela.git
synced 2026-02-14 18:10:21 +00:00
Fix: Support cuex package imports in vela show/def show commands (#7017)
Some checks failed
Webhook Upgrade Validation / webhook-upgrade-check (push) Failing after 2m0s
Some checks failed
Webhook Upgrade Validation / webhook-upgrade-check (push) Failing after 2m0s
- Added GetCUExParameterValue()
function that uses cuex.DefaultCompiler instead
of standard CUE compiler
- Added GetParametersWithCuex() function with cuex support
- Updated GetBaseResourceKinds() to use cuex compiler
- Updated all callers to use cuex-aware functions
Fixes #7012
Signed-off-by: GoGstickGo <janilution@gmail.com>
This commit is contained in:
@@ -371,7 +371,9 @@ func HandleTemplate(in *runtime.RawExtension, schematic *commontypes.Schematic,
|
||||
if tmp.CueTemplate == "" {
|
||||
return types.Capability{}, errors.New("template not exist in definition")
|
||||
}
|
||||
tmp.Parameters, err = cue.GetParameters(tmp.CueTemplate)
|
||||
// TODO: Accept context parameter for proper cancellation/timeout support
|
||||
// Currently using Background() to avoid breaking changes to function
|
||||
tmp.Parameters, err = cue.GetParametersWithCuex(context.Background(), tmp.CueTemplate)
|
||||
if err != nil && !errors.Is(err, cue.ErrParameterNotExist) {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
|
||||
@@ -95,7 +95,9 @@ func (ref *ConsoleReference) GenerateCUETemplateProperties(capability *types.Cap
|
||||
ref.DisplayFormat = "console"
|
||||
capName := capability.Name
|
||||
|
||||
cueValue, err := common.GetCUEParameterValue(capability.CueTemplate)
|
||||
// TODO: Accept context parameter for proper cancellation/timeout support
|
||||
// Currently using Background() to avoid breaking changes to function
|
||||
cueValue, err := common.GetCUExParameterValue(context.Background(), capability.CueTemplate)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("failed to retrieve `parameters` value from %s with err: %w", capName, err)
|
||||
}
|
||||
|
||||
@@ -145,7 +145,10 @@ func (ref *MarkdownReference) GenerateMarkdownForCap(_ context.Context, c types.
|
||||
capNameInTitle := ref.makeReadableTitle(capName)
|
||||
switch c.Category {
|
||||
case types.CUECategory:
|
||||
cueValue, err := common.GetCUEParameterValue(c.CueTemplate)
|
||||
// TODO: Use context from caller for proper cancellation/timeout support
|
||||
// Currently using Background() to avoid breaking changes to function
|
||||
ctx := context.Background()
|
||||
cueValue, err := common.GetCUExParameterValue(ctx, c.CueTemplate)
|
||||
if err != nil && !errors.Is(err, cue.ErrParameterNotExist) {
|
||||
return "", fmt.Errorf("failed to retrieve `parameters` value from %s with err: %w", c.Name, err)
|
||||
}
|
||||
@@ -156,7 +159,7 @@ func (ref *MarkdownReference) GenerateMarkdownForCap(_ context.Context, c types.
|
||||
}
|
||||
if c.Type == types.TypeComponentDefinition {
|
||||
var warnErr error
|
||||
baseDoc, warnErr = GetBaseResourceKinds(c.CueTemplate, ref.Client.RESTMapper())
|
||||
baseDoc, warnErr = GetBaseResourceKinds(ctx, c.CueTemplate, ref.Client.RESTMapper())
|
||||
if warnErr != nil {
|
||||
klog.Warningf("failed to get base resource kinds for %s: %v", c.Name, warnErr)
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ import (
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/ast"
|
||||
"cuelang.org/go/cue/cuecontext"
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
"github.com/kubevela/pkg/cue/cuex"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/mod/modfile"
|
||||
@@ -699,8 +699,15 @@ func WalkParameterSchema(parameters *openapi3.Schema, name string, depth int) er
|
||||
}
|
||||
|
||||
// GetBaseResourceKinds helps get resource.group string of components' base resource
|
||||
func GetBaseResourceKinds(cueStr string, mapper meta.RESTMapper) (string, error) {
|
||||
tmpl := cuecontext.New().CompileString(cueStr + velacue.BaseTemplate)
|
||||
func GetBaseResourceKinds(ctx context.Context, cueStr string, mapper meta.RESTMapper) (string, error) {
|
||||
tmpl, err := cuex.DefaultCompiler.Get().CompileStringWithOptions(
|
||||
ctx,
|
||||
cueStr+velacue.BaseTemplate,
|
||||
cuex.DisableResolveProviderFunctions{},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
kindValue := tmpl.LookupPath(cue.ParsePath("output.kind"))
|
||||
kind, err := kindValue.String()
|
||||
if err != nil {
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package docgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -622,7 +623,7 @@ func TestExtractParameter(t *testing.T) {
|
||||
|
||||
ref := &MarkdownReference{}
|
||||
for key, ca := range testcases {
|
||||
cueValue, _ := common.GetCUEParameterValue(ca.cueTemplate)
|
||||
cueValue, _ := common.GetCUExParameterValue(context.Background(), ca.cueTemplate)
|
||||
out, _, err := ref.parseParameters("", cueValue, key, 0, false)
|
||||
assert.NoError(t, err, key)
|
||||
assert.Contains(t, out, ca.contains, key)
|
||||
@@ -728,7 +729,7 @@ func TestExtractParameterFromFiles(t *testing.T) {
|
||||
for key, ca := range testcases {
|
||||
content, err := os.ReadFile(ca.path)
|
||||
assert.NoError(t, err, ca.path)
|
||||
cueValue, _ := common.GetCUEParameterValue(string(content))
|
||||
cueValue, _ := common.GetCUExParameterValue(context.Background(), string(content))
|
||||
out, _, err := ref.parseParameters("", cueValue, key, 0, false)
|
||||
assert.NoError(t, err, key)
|
||||
assert.Contains(t, out, ca.contains, key)
|
||||
|
||||
Reference in New Issue
Block a user