mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-27 16:17:34 +00:00
Fix: add process context in workflow
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
This commit is contained in:
+17
-17
@@ -224,7 +224,7 @@ func (af *Appfile) PrepareWorkflowAndPolicy(ctx context.Context) ([]*unstructure
|
||||
}
|
||||
|
||||
func (af *Appfile) generateUnstructured(workload *Workload) (*unstructured.Unstructured, error) {
|
||||
un, err := generateUnstructuredFromCUEModule(workload, af.Name, af.AppRevisionName, af.Namespace, af.Components, af.Artifacts)
|
||||
un, err := generateUnstructuredFromCUEModule(workload, af.Name, af.AppRevisionName, af.Namespace, af.Components, af.Artifacts, af.AppAnnotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -235,8 +235,8 @@ func (af *Appfile) generateUnstructured(workload *Workload) (*unstructured.Unstr
|
||||
return un, nil
|
||||
}
|
||||
|
||||
func generateUnstructuredFromCUEModule(wl *Workload, appName, revision, ns string, components []common.ApplicationComponent, artifacts []*types.ComponentManifest) (*unstructured.Unstructured, error) {
|
||||
pCtx := process.NewPolicyContext(ns, wl.Name, appName, revision, components)
|
||||
func generateUnstructuredFromCUEModule(wl *Workload, appName, revision, ns string, components []common.ApplicationComponent, artifacts []*types.ComponentManifest, anno map[string]string) (*unstructured.Unstructured, error) {
|
||||
pCtx := process.NewPolicyContext(ns, wl.Name, appName, revision, components, anno)
|
||||
pCtx.PushData(model.ContextDataArtifacts, prepareArtifactsData(artifacts))
|
||||
if err := wl.EvalContext(pCtx); err != nil {
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", appName, ns)
|
||||
@@ -293,16 +293,16 @@ func (af *Appfile) GenerateComponentManifest(wl *Workload) (*types.ComponentMani
|
||||
af.Namespace = corev1.NamespaceDefault
|
||||
}
|
||||
// generate context here to avoid nil pointer panic
|
||||
wl.Ctx = NewBasicContext(af.Name, wl.Name, af.AppRevisionName, af.Namespace, wl.Params)
|
||||
wl.Ctx = NewBasicContext(af.Name, wl.Name, af.AppRevisionName, af.Namespace, wl.Params, af.AppAnnotations)
|
||||
switch wl.CapabilityCategory {
|
||||
case types.HelmCategory:
|
||||
return generateComponentFromHelmModule(wl, af.Name, af.AppRevisionName, af.Namespace)
|
||||
return generateComponentFromHelmModule(wl, af.Name, af.AppRevisionName, af.Namespace, af.AppAnnotations)
|
||||
case types.KubeCategory:
|
||||
return generateComponentFromKubeModule(wl, af.Name, af.AppRevisionName, af.Namespace)
|
||||
return generateComponentFromKubeModule(wl, af.Name, af.AppRevisionName, af.Namespace, af.AppAnnotations)
|
||||
case types.TerraformCategory:
|
||||
return generateComponentFromTerraformModule(wl, af.Name, af.Namespace)
|
||||
default:
|
||||
return generateComponentFromCUEModule(wl, af.Name, af.AppRevisionName, af.Namespace)
|
||||
return generateComponentFromCUEModule(wl, af.Name, af.AppRevisionName, af.Namespace, af.AppAnnotations)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,9 +471,9 @@ func (af *Appfile) setWorkloadRefToTrait(wlRef corev1.ObjectReference, trait *un
|
||||
}
|
||||
|
||||
// PrepareProcessContext prepares a DSL process Context
|
||||
func PrepareProcessContext(wl *Workload, applicationName, revision, namespace string) (process.Context, error) {
|
||||
func PrepareProcessContext(wl *Workload, applicationName, revision, namespace string, anno map[string]string) (process.Context, error) {
|
||||
if wl.Ctx == nil {
|
||||
wl.Ctx = NewBasicContext(applicationName, wl.Name, revision, namespace, wl.Params)
|
||||
wl.Ctx = NewBasicContext(applicationName, wl.Name, revision, namespace, wl.Params, anno)
|
||||
}
|
||||
if err := wl.EvalContext(wl.Ctx); err != nil {
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", applicationName, namespace)
|
||||
@@ -482,16 +482,16 @@ func PrepareProcessContext(wl *Workload, applicationName, revision, namespace st
|
||||
}
|
||||
|
||||
// NewBasicContext prepares a basic DSL process Context
|
||||
func NewBasicContext(applicationName, workloadName, revision, namespace string, params map[string]interface{}) process.Context {
|
||||
pCtx := process.NewContext(namespace, workloadName, applicationName, revision)
|
||||
func NewBasicContext(applicationName, workloadName, revision, namespace string, params map[string]interface{}, anno map[string]string) process.Context {
|
||||
pCtx := process.NewContext(namespace, workloadName, applicationName, revision, anno)
|
||||
if params != nil {
|
||||
pCtx.SetParameters(params)
|
||||
}
|
||||
return pCtx
|
||||
}
|
||||
|
||||
func generateComponentFromCUEModule(wl *Workload, appName, revision, ns string) (*types.ComponentManifest, error) {
|
||||
pCtx, err := PrepareProcessContext(wl, appName, revision, ns)
|
||||
func generateComponentFromCUEModule(wl *Workload, appName, revision, ns string, anno map[string]string) (*types.ComponentManifest, error) {
|
||||
pCtx, err := PrepareProcessContext(wl, appName, revision, ns, anno)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -664,7 +664,7 @@ output: {
|
||||
return templateStr, nil
|
||||
}
|
||||
|
||||
func generateComponentFromKubeModule(wl *Workload, appName, revision, ns string) (*types.ComponentManifest, error) {
|
||||
func generateComponentFromKubeModule(wl *Workload, appName, revision, ns string, anno map[string]string) (*types.ComponentManifest, error) {
|
||||
templateStr, err := GenerateCUETemplate(wl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -672,7 +672,7 @@ func generateComponentFromKubeModule(wl *Workload, appName, revision, ns string)
|
||||
wl.FullTemplate.TemplateStr = templateStr
|
||||
|
||||
// re-use the way CUE module generates comp & acComp
|
||||
compManifest, err := generateComponentFromCUEModule(wl, appName, revision, ns)
|
||||
compManifest, err := generateComponentFromCUEModule(wl, appName, revision, ns, anno)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -839,7 +839,7 @@ func setParameterValuesToKubeObj(obj *unstructured.Unstructured, values paramVal
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateComponentFromHelmModule(wl *Workload, appName, revision, ns string) (*types.ComponentManifest, error) {
|
||||
func generateComponentFromHelmModule(wl *Workload, appName, revision, ns string, anno map[string]string) (*types.ComponentManifest, error) {
|
||||
templateStr, err := GenerateCUETemplate(wl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -855,7 +855,7 @@ func generateComponentFromHelmModule(wl *Workload, appName, revision, ns string)
|
||||
}
|
||||
|
||||
if wl.FullTemplate.Reference.Type != types.AutoDetectWorkloadDefinition {
|
||||
compManifest, err = generateComponentFromCUEModule(wl, appName, revision, ns)
|
||||
compManifest, err = generateComponentFromCUEModule(wl, appName, revision, ns, anno)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ import (
|
||||
oamtypes "github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/definition"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
)
|
||||
|
||||
@@ -873,7 +874,7 @@ variable "password" {
|
||||
revision: "v1",
|
||||
}
|
||||
|
||||
pCtx := NewBasicContext(args.appName, args.wl.Name, args.revision, ns, args.wl.Params)
|
||||
pCtx := NewBasicContext(args.appName, args.wl.Name, args.revision, ns, args.wl.Params, nil)
|
||||
comp, err := evalWorkloadWithContext(pCtx, args.wl, ns, args.appName, compName)
|
||||
Expect(comp.StandardWorkload).ShouldNot(BeNil())
|
||||
Expect(comp.Name).Should(Equal(""))
|
||||
@@ -1329,7 +1330,12 @@ func TestBaseGenerateComponent(t *testing.T) {
|
||||
var ns = "test-ns"
|
||||
var traitName = "mytrait"
|
||||
var wlName = "my-wl-1"
|
||||
pContext := NewBasicContext(appName, wlName, "rev-1", ns, nil)
|
||||
var workflowName = "my-wf"
|
||||
var publishVersion = "123"
|
||||
pContext := NewBasicContext(appName, wlName, "rev-1", ns, nil, map[string]string{
|
||||
oam.AnnotationWorkflowName: workflowName,
|
||||
oam.AnnotationPublishVersion: publishVersion,
|
||||
})
|
||||
base := `
|
||||
apiVersion: "apps/v1"
|
||||
kind: "Deployment"
|
||||
@@ -1359,11 +1365,14 @@ if context.componentType == "stateless" {
|
||||
}
|
||||
name: context.name
|
||||
envSourceContainerName: context.name
|
||||
workflowName: context.workflowName
|
||||
publishVersion: context.publishVersion
|
||||
}`,
|
||||
}
|
||||
wl := &Workload{Type: "stateful", Traits: []*Trait{tr}}
|
||||
cm, err := baseGenerateComponent(pContext, wl, appName, ns)
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, cm.Traits[0].Object["kind"], "StatefulSet")
|
||||
assert.Equal(t, cm.Traits[0].Object["name"], wlName)
|
||||
assert.Equal(t, cm.Traits[0].Object["workflowName"], workflowName)
|
||||
assert.Equal(t, cm.Traits[0].Object["publishVersion"], publishVersion)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (p *Parser) ValidateCUESchematicAppfile(a *Appfile) error {
|
||||
if wl.CapabilityCategory != types.CUECategory {
|
||||
continue
|
||||
}
|
||||
pCtx, err := newValidationProcessContext(wl, a.Name, a.AppRevisionName, a.Namespace)
|
||||
pCtx, err := newValidationProcessContext(wl, a.Name, a.AppRevisionName, a.Namespace, a.app.Annotations)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "cannot create the validation process context of app=%s in namespace=%s", a.Name, a.Namespace)
|
||||
}
|
||||
@@ -49,7 +49,7 @@ func (p *Parser) ValidateCUESchematicAppfile(a *Appfile) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newValidationProcessContext(wl *Workload, appName, revisionName, ns string) (process.Context, error) {
|
||||
func newValidationProcessContext(wl *Workload, appName, revisionName, ns string, anno map[string]string) (process.Context, error) {
|
||||
baseHooks := []process.BaseHook{
|
||||
// add more hook funcs here to validate CUE base
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func newValidationProcessContext(wl *Workload, appName, revisionName, ns string)
|
||||
validateAuxiliaryNameUnique(),
|
||||
}
|
||||
|
||||
pCtx := process.NewContextWithHooks(ns, wl.Name, appName, revisionName, baseHooks, auxiliaryHooks)
|
||||
pCtx := process.NewContextWithHooks(ns, wl.Name, appName, revisionName, baseHooks, auxiliaryHooks, anno)
|
||||
if err := wl.EvalContext(pCtx); err != nil {
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", appName, ns)
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ var _ = Describe("Test validate CUE schematic Appfile", func() {
|
||||
},
|
||||
engine: definition.NewWorkloadAbstractEngine("myweb", pd),
|
||||
}
|
||||
pCtx, err := newValidationProcessContext(wl, "myapp", "myapp-v1", "test-ns")
|
||||
pCtx, err := newValidationProcessContext(wl, "myapp", "myapp-v1", "test-ns", nil)
|
||||
Expect(err).Should(BeNil())
|
||||
Eventually(func() string {
|
||||
for _, tr := range wl.Traits {
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/pkg/appfile"
|
||||
"github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/application/assemble"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model/value"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
"github.com/oam-dev/kubevela/pkg/monitor/metrics"
|
||||
"github.com/oam-dev/kubevela/pkg/multicluster"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
@@ -60,16 +61,19 @@ func (h *AppHandler) GenerateApplicationSteps(ctx context.Context,
|
||||
appParser *appfile.Parser,
|
||||
af *appfile.Appfile,
|
||||
appRev *v1beta1.ApplicationRevision) ([]wfTypes.TaskRunner, error) {
|
||||
|
||||
handlerProviders := providers.NewProviders()
|
||||
kube.Install(handlerProviders, h.r.Client, h.Dispatch, h.Delete)
|
||||
oamProvider.Install(handlerProviders, app, h.applyComponentFunc(
|
||||
appParser, appRev, af), h.renderComponentFunc(appParser, appRev, af))
|
||||
http.Install(handlerProviders, h.r.Client, app.Namespace)
|
||||
taskDiscover := tasks.NewTaskDiscover(handlerProviders, h.r.pd, h.r.Client, h.r.dm)
|
||||
pCtx := process.NewContext(app.Namespace, app.Name, app.Name, appRev.Name, app.Annotations)
|
||||
taskDiscover := tasks.NewTaskDiscover(handlerProviders, h.r.pd, h.r.Client, h.r.dm, pCtx)
|
||||
multiclusterProvider.Install(handlerProviders, h.r.Client, app)
|
||||
terraformProvider.Install(handlerProviders, app, func(comp common.ApplicationComponent) (*appfile.Workload, error) {
|
||||
return appParser.ParseWorkloadFromRevision(comp, appRev)
|
||||
})
|
||||
|
||||
var tasks []wfTypes.TaskRunner
|
||||
for _, step := range af.WorkflowSteps {
|
||||
options := &wfTypes.GeneratorOptions{
|
||||
|
||||
@@ -439,7 +439,7 @@ func CUEBasedHealthCheck(ctx context.Context, c client.Client, wlRef WorkloadRef
|
||||
|
||||
switch wl.CapabilityCategory {
|
||||
case oamtypes.TerraformCategory:
|
||||
pCtx = af.NewBasicContext(appfile.Name, wl.Name, appfile.AppRevisionName, appfile.Namespace, wl.Params)
|
||||
pCtx = af.NewBasicContext(appfile.Name, wl.Name, appfile.AppRevisionName, appfile.Namespace, wl.Params, appfile.AppAnnotations)
|
||||
ctx := context.Background()
|
||||
var configuration terraformapi.Configuration
|
||||
if err := c.Get(ctx, client.ObjectKey{Name: wl.Name, Namespace: ns}, &configuration); err != nil {
|
||||
@@ -454,7 +454,7 @@ func CUEBasedHealthCheck(ctx context.Context, c client.Client, wlRef WorkloadRef
|
||||
wlHealth.Diagnosis = configuration.Status.Apply.Message
|
||||
okToCheckTrait = true
|
||||
default:
|
||||
pCtx = process.NewProcessContextWithCtx(ctx, ns, wl.Name, appfile.Name, appfile.AppRevisionName)
|
||||
pCtx = process.NewProcessContextWithCtx(ctx, ns, wl.Name, appfile.Name, appfile.AppRevisionName, appfile.AppAnnotations)
|
||||
if wl.CapabilityCategory != oamtypes.CUECategory {
|
||||
templateStr, err := af.GenerateCUETemplate(wl)
|
||||
if err != nil {
|
||||
|
||||
@@ -217,7 +217,7 @@ parameter: {
|
||||
}
|
||||
|
||||
for _, v := range testCases {
|
||||
ctx := process.NewContext("default", "test", "myapp", "myapp-v1")
|
||||
ctx := process.NewContext("default", "test", "myapp", "myapp-v1", nil)
|
||||
wt := NewWorkloadAbstractEngine("testWorkload", &packages.PackageDiscover{})
|
||||
err := wt.Complete(ctx, v.workloadTemplate, v.params)
|
||||
hasError := err != nil
|
||||
@@ -918,7 +918,7 @@ parameter: [string]: string`,
|
||||
}
|
||||
|
||||
`
|
||||
ctx := process.NewContext("default", "test", "myapp", "myapp-v1")
|
||||
ctx := process.NewContext("default", "test", "myapp", "myapp-v1", nil)
|
||||
wt := NewWorkloadAbstractEngine("-", &packages.PackageDiscover{})
|
||||
if err := wt.Complete(ctx, baseTemplate, map[string]interface{}{
|
||||
"replicas": 2,
|
||||
@@ -1017,7 +1017,7 @@ outputs: service :{
|
||||
}
|
||||
for k, v := range testcases {
|
||||
wd := NewWorkloadAbstractEngine(k, &packages.PackageDiscover{})
|
||||
ctx := process.NewContext("default", k, "myapp", "myapp-v1")
|
||||
ctx := process.NewContext("default", k, "myapp", "myapp-v1", nil)
|
||||
err := wd.Complete(ctx, v.template, map[string]interface{}{})
|
||||
assert.NoError(t, err)
|
||||
_, assists := ctx.Output()
|
||||
@@ -1095,7 +1095,7 @@ outputs: abc :{
|
||||
}
|
||||
for k, v := range testcases {
|
||||
td := NewTraitAbstractEngine(k, &packages.PackageDiscover{})
|
||||
ctx := process.NewContext("default", k, "myapp", "myapp-v1")
|
||||
ctx := process.NewContext("default", k, "myapp", "myapp-v1", nil)
|
||||
err := td.Complete(ctx, v.template, map[string]interface{}{})
|
||||
assert.NoError(t, err)
|
||||
_, assists := ctx.Output()
|
||||
|
||||
@@ -35,6 +35,10 @@ const (
|
||||
ContextAppRevisionNum = "appRevisionNum"
|
||||
// ContextNamespace is the namespace of the app
|
||||
ContextNamespace = "namespace"
|
||||
// ContextPublishVersion is the publish version of the app
|
||||
ContextPublishVersion = "publishVersion"
|
||||
// ContextWorkflowName is the name of the workflow
|
||||
ContextWorkflowName = "workflowName"
|
||||
// OutputSecretName is used to store all secret names which are generated by cloud resource components
|
||||
OutputSecretName = "outputSecretName"
|
||||
// ContextCompRevisionName is the component revision name of context
|
||||
|
||||
+39
-37
@@ -27,6 +27,7 @@ import (
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
)
|
||||
|
||||
@@ -42,6 +43,8 @@ type Context interface {
|
||||
PushData(key string, data interface{})
|
||||
GetCtx() context.Context
|
||||
SetCtx(context.Context)
|
||||
SetHooks(baseHooks []BaseHook, auxHooks []AuxiliaryHook)
|
||||
SetComponents(components []common.ApplicationComponent)
|
||||
}
|
||||
|
||||
// Auxiliary are objects rendered by definition template.
|
||||
@@ -62,10 +65,12 @@ type templateContext struct {
|
||||
// appName is the name of Application
|
||||
appName string
|
||||
// appRevision is the revision name of Application
|
||||
appRevision string
|
||||
configs []map[string]string
|
||||
base model.Instance
|
||||
auxiliaries []Auxiliary
|
||||
appRevision string
|
||||
workflowName string
|
||||
publishVersion string
|
||||
configs []map[string]string
|
||||
base model.Instance
|
||||
auxiliaries []Auxiliary
|
||||
// namespace is the namespace of Application which is used to set the namespace for Crossplane connection secret,
|
||||
// ComponentDefinition/TratiDefinition OpenAPI v3 schema
|
||||
namespace string
|
||||
@@ -95,8 +100,8 @@ type RequiredSecrets struct {
|
||||
}
|
||||
|
||||
// NewContext create render templateContext
|
||||
func NewContext(namespace, name, appName, appRevision string) Context {
|
||||
return &templateContext{
|
||||
func NewContext(namespace, name, appName, appRevision string, anno map[string]string) Context {
|
||||
ctx := &templateContext{
|
||||
name: name,
|
||||
appName: appName,
|
||||
appRevision: appRevision,
|
||||
@@ -105,46 +110,32 @@ func NewContext(namespace, name, appName, appRevision string) Context {
|
||||
namespace: namespace,
|
||||
parameters: map[string]interface{}{},
|
||||
}
|
||||
if anno != nil {
|
||||
ctx.workflowName = anno[oam.AnnotationWorkflowName]
|
||||
ctx.publishVersion = anno[oam.AnnotationPublishVersion]
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// NewProcessContextWithCtx create render templateContext with ctx
|
||||
func NewProcessContextWithCtx(ctx context.Context, namespace, name, appName, appRevision string) Context {
|
||||
return &templateContext{
|
||||
name: name,
|
||||
appName: appName,
|
||||
appRevision: appRevision,
|
||||
configs: []map[string]string{},
|
||||
auxiliaries: []Auxiliary{},
|
||||
namespace: namespace,
|
||||
parameters: map[string]interface{}{},
|
||||
ctx: ctx,
|
||||
}
|
||||
func NewProcessContextWithCtx(ctx context.Context, namespace, name, appName, appRevision string, anno map[string]string) Context {
|
||||
pCtx := NewContext(namespace, name, appName, appRevision, anno)
|
||||
pCtx.SetCtx(ctx)
|
||||
return pCtx
|
||||
}
|
||||
|
||||
// NewContextWithHooks create render templateContext with hooks for validation
|
||||
func NewContextWithHooks(namespace, name, appName, appRevision string, baseHooks []BaseHook, auxHooks []AuxiliaryHook) Context {
|
||||
return &templateContext{
|
||||
name: name,
|
||||
appName: appName,
|
||||
appRevision: appRevision,
|
||||
configs: []map[string]string{},
|
||||
auxiliaries: []Auxiliary{},
|
||||
namespace: namespace,
|
||||
parameters: map[string]interface{}{},
|
||||
baseHooks: baseHooks,
|
||||
auxiliaryHooks: auxHooks,
|
||||
}
|
||||
func NewContextWithHooks(namespace, name, appName, appRevision string, baseHooks []BaseHook, auxHooks []AuxiliaryHook, anno map[string]string) Context {
|
||||
pCtx := NewContext(namespace, name, appName, appRevision, anno)
|
||||
pCtx.SetHooks(baseHooks, auxHooks)
|
||||
return pCtx
|
||||
}
|
||||
|
||||
// NewPolicyContext create Application Scope templateContext for Policy
|
||||
func NewPolicyContext(namespace, name, appName, appRevision string, components []common.ApplicationComponent) Context {
|
||||
return &templateContext{
|
||||
name: name,
|
||||
appName: appName,
|
||||
appRevision: appRevision,
|
||||
namespace: namespace,
|
||||
components: components,
|
||||
}
|
||||
func NewPolicyContext(namespace, name, appName, appRevision string, components []common.ApplicationComponent, anno map[string]string) Context {
|
||||
pCtx := NewContext(namespace, name, appName, appRevision, anno)
|
||||
pCtx.SetComponents(components)
|
||||
return pCtx
|
||||
}
|
||||
|
||||
// SetParameters sets templateContext parameters
|
||||
@@ -185,6 +176,8 @@ func (ctx *templateContext) BaseContextFile() string {
|
||||
buff += fmt.Sprintf(model.ContextAppRevisionNum+": %d\n", revNum)
|
||||
buff += fmt.Sprintf(model.ContextNamespace+": \"%s\"\n", ctx.namespace)
|
||||
buff += fmt.Sprintf(model.ContextCompRevisionName+": \"%s\"\n", model.ComponentRevisionPlaceHolder)
|
||||
buff += fmt.Sprintf(model.ContextWorkflowName+": \"%s\"\n", ctx.workflowName)
|
||||
buff += fmt.Sprintf(model.ContextPublishVersion+": \"%s\"\n", ctx.publishVersion)
|
||||
|
||||
if ctx.base != nil {
|
||||
buff += fmt.Sprintf(model.OutputFieldName+": %s\n", structMarshal(ctx.base.String()))
|
||||
@@ -300,6 +293,15 @@ func (ctx *templateContext) SetCtx(newContext context.Context) {
|
||||
ctx.ctx = newContext
|
||||
}
|
||||
|
||||
func (ctx *templateContext) SetHooks(baseHooks []BaseHook, auxHooks []AuxiliaryHook) {
|
||||
ctx.baseHooks = baseHooks
|
||||
ctx.auxiliaryHooks = auxHooks
|
||||
}
|
||||
|
||||
func (ctx *templateContext) SetComponents(components []common.ApplicationComponent) {
|
||||
ctx.components = components
|
||||
}
|
||||
|
||||
func structMarshal(v string) string {
|
||||
skip := false
|
||||
v = strings.TrimFunc(v, func(r rune) bool {
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/bmizerany/assert"
|
||||
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
)
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
@@ -100,7 +101,10 @@ image: "myserver"
|
||||
},
|
||||
}
|
||||
|
||||
ctx := NewContext("myns", "mycomp", "myapp", "myapp-v1")
|
||||
ctx := NewContext("myns", "mycomp", "myapp", "myapp-v1", map[string]string{
|
||||
oam.AnnotationWorkflowName: "myworkflow",
|
||||
oam.AnnotationPublishVersion: "mypublishversion",
|
||||
})
|
||||
ctx.SetBase(base)
|
||||
ctx.AppendAuxiliaries(svcAux)
|
||||
ctx.AppendAuxiliaries(svcAuxWithAbnormalName)
|
||||
@@ -130,6 +134,14 @@ image: "myserver"
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, int64(1), myAppRevisionNum)
|
||||
|
||||
myWorkflowName, err := ctxInst.Lookup("context", model.ContextWorkflowName).String()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "myworkflow", myWorkflowName)
|
||||
|
||||
myPublishVersion, err := ctxInst.Lookup("context", model.ContextPublishVersion).String()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "mypublishversion", myPublishVersion)
|
||||
|
||||
inputJs, err := ctxInst.Lookup("context", model.OutputFieldName).MarshalJSON()
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, `{"image":"myserver"}`, string(inputJs))
|
||||
|
||||
+3
-1
@@ -30,6 +30,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model/value"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
"github.com/oam-dev/kubevela/pkg/multicluster"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
|
||||
oamutil "github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
@@ -82,7 +83,8 @@ func (handler *ViewHandler) QueryView(ctx context.Context, qv QueryView) (*value
|
||||
Outputs: queryKey.Outputs,
|
||||
}
|
||||
|
||||
taskDiscover := tasks.NewViewTaskDiscover(handler.pd, handler.cli, handler.cfg, handler.dispatch, handler.delete, handler.namespace, 3)
|
||||
pCtx := process.NewContext("", "", "", "", nil)
|
||||
taskDiscover := tasks.NewViewTaskDiscover(handler.pd, handler.cli, handler.cfg, handler.dispatch, handler.delete, handler.namespace, 3, pCtx)
|
||||
genTask, err := taskDiscover.GetTaskGenerator(ctx, handler.viewTask.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -31,6 +31,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model/sets"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model/value"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
monitorContext "github.com/oam-dev/kubevela/pkg/monitor/context"
|
||||
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
|
||||
"github.com/oam-dev/kubevela/pkg/workflow/hooks"
|
||||
@@ -197,7 +198,7 @@ func (t *TaskLoader) makeTaskGenerator(templ string) (wfTypes.TaskGenerator, err
|
||||
paramFile = fmt.Sprintf(model.ParameterFieldName+": {%s}\n", ps)
|
||||
}
|
||||
|
||||
taskv, err := t.makeValue(ctx, strings.Join([]string{templ, paramFile}, "\n"), exec.wfStatus.ID)
|
||||
taskv, err := t.makeValue(ctx, strings.Join([]string{templ, paramFile}, "\n"), exec.wfStatus.ID, options.PCtx)
|
||||
if err != nil {
|
||||
exec.err(ctx, err, StatusReasonRendering)
|
||||
return exec.status(), exec.operation(), nil
|
||||
@@ -227,7 +228,7 @@ func (t *TaskLoader) makeTaskGenerator(templ string) (wfTypes.TaskGenerator, err
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *TaskLoader) makeValue(ctx wfContext.Context, templ string, id string) (*value.Value, error) {
|
||||
func (t *TaskLoader) makeValue(ctx wfContext.Context, templ string, id string, pCtx process.Context) (*value.Value, error) {
|
||||
var contextTempl string
|
||||
meta, _ := ctx.GetVar(wfTypes.ContextKeyMetadata)
|
||||
if meta != nil {
|
||||
@@ -237,6 +238,7 @@ func (t *TaskLoader) makeValue(ctx wfContext.Context, templ string, id string) (
|
||||
}
|
||||
contextTempl = fmt.Sprintf("\ncontext: {%s}\ncontext: stepSessionID: \"%s\"", ms, id)
|
||||
}
|
||||
contextTempl += "\n" + pCtx.ExtendedContextFile()
|
||||
|
||||
return value.NewValue(templ+contextTempl, t.pd, contextTempl, value.ProcessScript, value.TagFieldOrder)
|
||||
}
|
||||
@@ -415,7 +417,7 @@ func getLabel(v *value.Value, label string) string {
|
||||
}
|
||||
|
||||
// NewTaskLoader create a tasks loader.
|
||||
func NewTaskLoader(lt LoadTaskTemplate, pkgDiscover *packages.PackageDiscover, handlers providers.Providers, logLevel int) *TaskLoader {
|
||||
func NewTaskLoader(lt LoadTaskTemplate, pkgDiscover *packages.PackageDiscover, handlers providers.Providers, logLevel int, pCtx process.Context) *TaskLoader {
|
||||
return &TaskLoader{
|
||||
loadTemplate: lt,
|
||||
pd: pkgDiscover,
|
||||
@@ -423,6 +425,7 @@ func NewTaskLoader(lt LoadTaskTemplate, pkgDiscover *packages.PackageDiscover, h
|
||||
runOptionsProcess: func(options *wfTypes.TaskRunOptions) {
|
||||
options.PreStartHooks = append(options.PreStartHooks, hooks.Input)
|
||||
options.PostStopHooks = append(options.PostStopHooks, hooks.Output)
|
||||
options.PCtx = pCtx
|
||||
},
|
||||
logLevel: logLevel,
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model/value"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
|
||||
"github.com/oam-dev/kubevela/pkg/workflow/hooks"
|
||||
"github.com/oam-dev/kubevela/pkg/workflow/providers"
|
||||
@@ -75,7 +76,8 @@ myIP: value: "1.1.1.1"
|
||||
},
|
||||
})
|
||||
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0)
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
|
||||
steps := []v1beta1.WorkflowStep{
|
||||
{
|
||||
@@ -178,7 +180,8 @@ close({
|
||||
return errors.New("mock error")
|
||||
},
|
||||
})
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0)
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
|
||||
steps := []v1beta1.WorkflowStep{
|
||||
{
|
||||
@@ -414,7 +417,8 @@ func TestPendingInputCheck(t *testing.T) {
|
||||
ParameterKey: "score",
|
||||
}},
|
||||
}
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0)
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
gen, err := tasksLoader.GetTaskGenerator(context.Background(), step.Type)
|
||||
r.NoError(err)
|
||||
run, err := gen(step, &types.GeneratorOptions{})
|
||||
@@ -443,7 +447,8 @@ func TestPendingDependsOnCheck(t *testing.T) {
|
||||
Type: "ok",
|
||||
DependsOn: []string{"depend"},
|
||||
}
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0)
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
gen, err := tasksLoader.GetTaskGenerator(context.Background(), step.Type)
|
||||
r.NoError(err)
|
||||
run, err := gen(step, &types.GeneratorOptions{})
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/packages"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
|
||||
"github.com/oam-dev/kubevela/pkg/velaql/providers/query"
|
||||
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
|
||||
@@ -74,7 +75,7 @@ func suspend(step v1beta1.WorkflowStep, opt *types.GeneratorOptions) (types.Task
|
||||
}
|
||||
|
||||
// NewTaskDiscover will create a client for load task generator.
|
||||
func NewTaskDiscover(providerHandlers providers.Providers, pd *packages.PackageDiscover, cli client.Client, dm discoverymapper.DiscoveryMapper) types.TaskDiscover {
|
||||
func NewTaskDiscover(providerHandlers providers.Providers, pd *packages.PackageDiscover, cli client.Client, dm discoverymapper.DiscoveryMapper, pCtx process.Context) types.TaskDiscover {
|
||||
// install builtin provider
|
||||
workspace.Install(providerHandlers)
|
||||
email.Install(providerHandlers)
|
||||
@@ -85,7 +86,7 @@ func NewTaskDiscover(providerHandlers providers.Providers, pd *packages.PackageD
|
||||
builtins: map[string]types.TaskGenerator{
|
||||
"suspend": suspend,
|
||||
},
|
||||
remoteTaskDiscover: custom.NewTaskLoader(templateLoader.LoadTaskTemplate, pd, providerHandlers, 0),
|
||||
remoteTaskDiscover: custom.NewTaskLoader(templateLoader.LoadTaskTemplate, pd, providerHandlers, 0, pCtx),
|
||||
templateLoader: templateLoader,
|
||||
}
|
||||
}
|
||||
@@ -116,7 +117,7 @@ func (tr *suspendTaskRunner) Pending(ctx wfContext.Context) bool {
|
||||
}
|
||||
|
||||
// NewViewTaskDiscover will create a client for load task generator.
|
||||
func NewViewTaskDiscover(pd *packages.PackageDiscover, cli client.Client, cfg *rest.Config, apply kube.Dispatcher, delete kube.Deleter, viewNs string, logLevel int) types.TaskDiscover {
|
||||
func NewViewTaskDiscover(pd *packages.PackageDiscover, cli client.Client, cfg *rest.Config, apply kube.Dispatcher, delete kube.Deleter, viewNs string, logLevel int, pCtx process.Context) types.TaskDiscover {
|
||||
handlerProviders := providers.NewProviders()
|
||||
|
||||
// install builtin provider
|
||||
@@ -128,7 +129,7 @@ func NewViewTaskDiscover(pd *packages.PackageDiscover, cli client.Client, cfg *r
|
||||
|
||||
templateLoader := template.NewViewTemplateLoader(cli, viewNs)
|
||||
return &taskDiscover{
|
||||
remoteTaskDiscover: custom.NewTaskLoader(templateLoader.LoadTaskTemplate, pd, handlerProviders, logLevel),
|
||||
remoteTaskDiscover: custom.NewTaskLoader(templateLoader.LoadTaskTemplate, pd, handlerProviders, logLevel, pCtx),
|
||||
templateLoader: templateLoader,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"gotest.tools/assert"
|
||||
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
"github.com/oam-dev/kubevela/pkg/workflow/tasks/custom"
|
||||
"github.com/oam-dev/kubevela/pkg/workflow/types"
|
||||
)
|
||||
@@ -46,11 +47,12 @@ func TestDiscover(t *testing.T) {
|
||||
return "", makeErr(name)
|
||||
}
|
||||
}
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
discover := &taskDiscover{
|
||||
builtins: map[string]types.TaskGenerator{
|
||||
"suspend": suspend,
|
||||
},
|
||||
remoteTaskDiscover: custom.NewTaskLoader(loadTemplate, nil, nil, 0),
|
||||
remoteTaskDiscover: custom.NewTaskLoader(loadTemplate, nil, nil, 0, pCtx),
|
||||
}
|
||||
|
||||
_, err := discover.GetTaskGenerator(context.Background(), "suspend")
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/model/value"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
monitorCtx "github.com/oam-dev/kubevela/pkg/monitor/context"
|
||||
wfContext "github.com/oam-dev/kubevela/pkg/workflow/context"
|
||||
)
|
||||
@@ -41,6 +42,7 @@ type TaskDiscover interface {
|
||||
// TaskRunOptions is the options for task run.
|
||||
type TaskRunOptions struct {
|
||||
Data *value.Value
|
||||
PCtx process.Context
|
||||
PreStartHooks []TaskPreStartHook
|
||||
PostStopHooks []TaskPostStopHook
|
||||
GetTracer func(id string, step v1beta1.WorkflowStep) monitorCtx.Context
|
||||
|
||||
@@ -198,7 +198,7 @@ func generateSecretFromTerraformOutput(k8sClient client.Client, outputList []str
|
||||
|
||||
// getTerraformJSONFiles gets Terraform JSON files or modules from workload
|
||||
func getTerraformJSONFiles(wl *appfile.Workload, applicationName, revisionName string, namespace string) ([]byte, error) {
|
||||
pCtx, err := appfile.PrepareProcessContext(wl, applicationName, revisionName, namespace)
|
||||
pCtx, err := appfile.PrepareProcessContext(wl, applicationName, revisionName, namespace, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user