mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-19 04:26:39 +00:00
refine error message: trait definition not found
This commit is contained in:
@@ -74,7 +74,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (result ctrl.Result, gerr error
|
||||
|
||||
applog.Info("parse template")
|
||||
// parse template
|
||||
appParser := parser.NewParser(template.GetHanler(fclient.NewDefinitionClient(r.Client)))
|
||||
appParser := parser.NewParser(template.GetHandler(fclient.NewDefinitionClient(r.Client)))
|
||||
|
||||
appfile, err := appParser.Parse(app.Name, app)
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/application/template"
|
||||
"github.com/oam-dev/kubevela/pkg/dsl/definition"
|
||||
"github.com/oam-dev/kubevela/pkg/dsl/process"
|
||||
@@ -183,13 +184,10 @@ func (pser *Parser) parseWorkload(comp v1alpha2.ApplicationComponent) (*Workload
|
||||
workload.traits = []*Trait{}
|
||||
workload.name = comp.Name
|
||||
workload.typ = comp.WorkloadType
|
||||
templ, kind, err := pser.templ(workload.typ)
|
||||
templ, err := pser.templ(workload.typ, types.TypeWorkload)
|
||||
if err != nil && !kerrors.IsNotFound(err) {
|
||||
return nil, errors.WithMessagef(err, "fetch type of %s", comp.Name)
|
||||
}
|
||||
if kind != template.WorkloadKind {
|
||||
return nil, errors.Errorf("%s type (%s) invalid", comp.Name, workload.typ)
|
||||
}
|
||||
workload.template = templ
|
||||
settings, err := DecodeJSONMarshaler(comp.Settings)
|
||||
if err != nil {
|
||||
@@ -214,14 +212,14 @@ func (pser *Parser) parseWorkload(comp v1alpha2.ApplicationComponent) (*Workload
|
||||
}
|
||||
|
||||
func (pser *Parser) parseTrait(name string, properties map[string]interface{}) (*Trait, error) {
|
||||
|
||||
templ, kind, err := pser.templ(name)
|
||||
if err != nil && !kerrors.IsNotFound(err) {
|
||||
templ, err := pser.templ(name, types.TypeTrait)
|
||||
if kerrors.IsNotFound(err) {
|
||||
return nil, errors.Errorf("trait definition of %s not found", name)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if kind != template.TraitKind {
|
||||
return nil, errors.Errorf("kind of %s is not trait", name)
|
||||
}
|
||||
|
||||
trait := new(Trait)
|
||||
trait.template = templ
|
||||
trait.name = name
|
||||
|
||||
@@ -116,7 +116,7 @@ spec:
|
||||
o := v1alpha2.Application{}
|
||||
yaml.Unmarshal([]byte(appfileYaml), &o)
|
||||
|
||||
appfile, err := NewParser(template.GetHanler(mock)).Parse("test", &o)
|
||||
appfile, err := NewParser(template.GetHandler(mock)).Parse("test", &o)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
|
||||
@@ -14,8 +16,8 @@ type manager struct {
|
||||
defclient.DefinitionClient
|
||||
}
|
||||
|
||||
// GetHanler get template handler
|
||||
func GetHanler(cli defclient.DefinitionClient) Handler {
|
||||
// GetHandler get template handler
|
||||
func GetHandler(cli defclient.DefinitionClient) Handler {
|
||||
m := &manager{
|
||||
DefinitionClient: cli,
|
||||
}
|
||||
@@ -23,51 +25,46 @@ func GetHanler(cli defclient.DefinitionClient) Handler {
|
||||
}
|
||||
|
||||
// Handler is template handler type
|
||||
type Handler func(key string) (string, Kind, error)
|
||||
type Handler func(key string, kind types.CapType) (string, error)
|
||||
|
||||
// Kind is template kind
|
||||
type Kind uint16
|
||||
|
||||
const (
|
||||
// WorkloadKind ...
|
||||
WorkloadKind Kind = (1 << iota)
|
||||
// TraitKind ...
|
||||
TraitKind
|
||||
// Unkownkind ...
|
||||
Unkownkind
|
||||
)
|
||||
type Kind = types.CapType
|
||||
|
||||
// LoadTemplate Get template according to key
|
||||
func (m *manager) LoadTemplate(key string) (string, Kind, error) {
|
||||
wd, err := m.GetWorkloadDefinition(key)
|
||||
if err != nil && !kerrors.IsNotFound(err) {
|
||||
return "", Unkownkind, errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
}
|
||||
if wd != nil {
|
||||
func (m *manager) LoadTemplate(key string, kd types.CapType) (string, error) {
|
||||
switch kd {
|
||||
case types.TypeWorkload:
|
||||
wd, err := m.GetWorkloadDefinition(key)
|
||||
if err != nil {
|
||||
return "", errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
}
|
||||
jsonRaw, err := getTemplate(wd.Spec.Extension.Raw)
|
||||
if err != nil {
|
||||
return "", Unkownkind, errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
return "", errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
}
|
||||
if jsonRaw != "" {
|
||||
return jsonRaw, WorkloadKind, nil
|
||||
if jsonRaw == "" {
|
||||
return "", errors.New("no template found in definition")
|
||||
}
|
||||
return jsonRaw, nil
|
||||
|
||||
case types.TypeTrait:
|
||||
td, err := m.GetTraitDefition(key)
|
||||
if err != nil && !kerrors.IsNotFound(err) {
|
||||
return "", errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
}
|
||||
}
|
||||
td, err := m.GetTraitDefition(key)
|
||||
if err != nil && !kerrors.IsNotFound(err) {
|
||||
return "", Unkownkind, errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
}
|
||||
if td != nil {
|
||||
jsonRaw, err := getTemplate(td.Spec.Extension.Raw)
|
||||
if err != nil {
|
||||
return "", Unkownkind, errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
return "", errors.WithMessagef(err, "LoadTemplate [%s] ", key)
|
||||
}
|
||||
|
||||
if jsonRaw != "" {
|
||||
return jsonRaw, TraitKind, nil
|
||||
if jsonRaw == "" {
|
||||
return "", errors.New("no template found in definition")
|
||||
}
|
||||
|
||||
return jsonRaw, nil
|
||||
case types.TypeScope:
|
||||
// TODO: add scope template support
|
||||
}
|
||||
return "", Unkownkind, nil
|
||||
|
||||
return "", fmt.Errorf("kind(%s) of %s not supported", kd, key)
|
||||
}
|
||||
|
||||
func getTemplate(raw []byte) (string, error) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/application/defclient"
|
||||
)
|
||||
|
||||
@@ -78,16 +79,11 @@ spec:
|
||||
m := manager{
|
||||
mock,
|
||||
}
|
||||
temp, kind, err := m.LoadTemplate("worker")
|
||||
temp, err := m.LoadTemplate("worker", types.TypeWorkload)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if kind != WorkloadKind {
|
||||
t.Errorf("template.LoadTemplate kind invalid")
|
||||
return
|
||||
}
|
||||
|
||||
var r cue.Runtime
|
||||
inst, err := r.Compile("-", temp)
|
||||
if err != nil {
|
||||
|
||||
@@ -59,7 +59,7 @@ func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) a
|
||||
}
|
||||
|
||||
// try render to validate
|
||||
appParser := parser.NewParser(template.GetHanler(fclient.NewDefinitionClient(h.Client)))
|
||||
appParser := parser.NewParser(template.GetHandler(fclient.NewDefinitionClient(h.Client)))
|
||||
if _, err := appParser.Parse(app.Name, app); err != nil {
|
||||
return admission.Denied(err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user