Fix: corrects validating webhook behaviour with cuex compilers (#6799)
CodeQL / Analyze (go) (push) Has been cancelled Details
Definition-Lint / definition-doc (push) Has been cancelled Details
E2E MultiCluster Test / detect-noop (push) Has been cancelled Details
E2E Test / detect-noop (push) Has been cancelled Details
Go / detect-noop (push) Has been cancelled Details
license / Check for unapproved licenses (push) Has been cancelled Details
Registry / Build and Push Vela Images (push) Has been cancelled Details
Unit-Test / detect-noop (push) Has been cancelled Details
E2E MultiCluster Test / e2e-multi-cluster-tests (v1.29) (push) Has been cancelled Details
E2E Test / e2e-tests (v1.29) (push) Has been cancelled Details
Go / staticcheck (push) Has been cancelled Details
Go / lint (push) Has been cancelled Details
Go / check-diff (push) Has been cancelled Details
Go / check-windows (push) Has been cancelled Details
Go / check-core-image-build (push) Has been cancelled Details
Go / check-cli-image-build (push) Has been cancelled Details
Registry / Generate and Push Provenance to GCHR (${{ needs.publish-vela-images.outputs.vela_cli_digest }}, ${{ needs.publish-vela-images.outputs.vela_cli_image }}, Vela CLI Image) (push) Has been cancelled Details
Registry / Generate and Push Provenance to GCHR (${{ needs.publish-vela-images.outputs.vela_core_digest }}, ${{ needs.publish-vela-images.outputs.vela_core_image }}, Vela Core Image) (push) Has been cancelled Details
Registry / Generate and Push Provenance to DockerHub (${{ needs.publish-vela-images.outputs.vela_cli_digest }}, ${{ needs.publish-vela-images.outputs.vela_cli_dockerhub_image }}, Vela CLI Image) (push) Has been cancelled Details
Registry / Generate and Push Provenance to DockerHub (${{ needs.publish-vela-images.outputs.vela_core_digest }}, ${{ needs.publish-vela-images.outputs.vela_core_dockerhub_image }}, Vela Core Image) (push) Has been cancelled Details
Unit-Test / unit-tests (push) Has been cancelled Details
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled Details

Signed-off-by: Brian Kane <briankane1@gmail.com>
This commit is contained in:
Brian Kane 2025-07-17 23:18:36 +01:00 committed by GitHub
parent fedcca1c7b
commit c79f03fe92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 119 additions and 2 deletions

View File

@ -65,7 +65,7 @@ func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) a
// validate cueTemplate
if obj.Spec.Schematic != nil && obj.Spec.Schematic.CUE != nil {
err = webhookutils.ValidateCueTemplate(obj.Spec.Schematic.CUE.Template)
err = webhookutils.ValidateCuexTemplate(ctx, obj.Spec.Schematic.CUE.Template)
if err != nil {
return admission.Denied(err.Error())
}

View File

@ -91,7 +91,7 @@ func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) a
// validate cueTemplate
if obj.Spec.Schematic != nil && obj.Spec.Schematic.CUE != nil {
err = webhookutils.ValidateCueTemplate(obj.Spec.Schematic.CUE.Template)
err = webhookutils.ValidateCuexTemplate(ctx, obj.Spec.Schematic.CUE.Template)
if err != nil {
return admission.Denied(err.Error())
}

View File

@ -23,6 +23,8 @@ import (
"strconv"
"strings"
"github.com/kubevela/pkg/cue/cuex"
"cuelang.org/go/cue/cuecontext"
cueErrors "cuelang.org/go/cue/errors"
"github.com/pkg/errors"
@ -73,6 +75,19 @@ func ValidateCueTemplate(cueTemplate string) error {
return checkError(err)
}
// ValidateCuexTemplate validate cueTemplate with CueX for types utilising it
func ValidateCuexTemplate(ctx context.Context, cueTemplate string) error {
val, err := cuex.DefaultCompiler.Get().CompileStringWithOptions(ctx, cueTemplate)
if err != nil {
return err
}
if e := checkError(val.Err()); e != nil {
return e
}
err = val.Validate()
return checkError(err)
}
func checkError(err error) error {
re := regexp.MustCompile(ContextRegex)
if err != nil {

View File

@ -17,9 +17,17 @@ limitations under the License.
package utils
import (
"context"
"fmt"
"strings"
"testing"
"github.com/kubevela/pkg/cue/cuex"
"github.com/kubevela/pkg/util/singleton"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
dynamicfake "k8s.io/client-go/dynamic/fake"
"cuelang.org/go/cue/errors"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
@ -70,6 +78,100 @@ func TestValidateCueTemplate(t *testing.T) {
}
}
func TestValidateCuexTemplate(t *testing.T) {
cases := map[string]struct {
cueTemplate string
want error
}{
"normalCueTemp": {
cueTemplate: "name: 'name'",
want: nil,
},
"contextNouFoundCueTemp": {
cueTemplate: `
output: {
metadata: {
name: context.name
label: context.label
annotation: "default"
}
}`,
want: nil,
},
"withCuexPackageImports": {
cueTemplate: `
import "test/ext"
test: ext.#Add & {
a: 1
b: 2
}
output: {
metadata: {
name: context.name + "\(test.result)"
label: context.label
annotation: "default"
}
}
`,
want: nil,
},
"inValidCueTemp": {
cueTemplate: `
output: {
metadata: {
name: context.name
label: context.label
annotation: "default"
},
hello: world
}`,
want: errors.New("output.hello: reference \"world\" not found"),
},
}
packageObj := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "cue.oam.dev/v1alpha1",
"kind": "Package",
"metadata": map[string]interface{}{
"name": "test-package",
"namespace": "vela-system",
},
"spec": map[string]interface{}{
"path": "test/ext",
"templates": map[string]interface{}{
"test/ext": strings.TrimSpace(`
package ext
#Add: {
a: number
b: number
result: a + b
}
`),
},
},
},
}
dcl := dynamicfake.NewSimpleDynamicClient(runtime.NewScheme(), packageObj)
singleton.DynamicClient.Set(dcl)
cuex.DefaultCompiler.Reload()
defer singleton.ReloadClients()
defer cuex.DefaultCompiler.Reload()
for caseName, cs := range cases {
t.Run(caseName, func(t *testing.T) {
err := ValidateCuexTemplate(context.Background(), cs.cueTemplate)
if diff := cmp.Diff(cs.want, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nValidateCueTemplate: -want , +got \n%s\n", cs.want, diff)
}
})
}
}
func TestValidateSemanticVersion(t *testing.T) {
cases := map[string]struct {
version string