mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-23 22:46:53 +00:00
add context data in process context
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
This commit is contained in:
+42
-23
@@ -224,7 +224,8 @@ 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, af.AppAnnotations)
|
||||
ctxData := GenerateContextDataWithCtx(context.Background(), af, workload.Name)
|
||||
un, err := generateUnstructuredFromCUEModule(workload, af.Artifacts, ctxData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -235,13 +236,13 @@ 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, anno map[string]string) (*unstructured.Unstructured, error) {
|
||||
pCtx := process.NewPolicyContext(ns, wl.Name, appName, revision, components, anno)
|
||||
func generateUnstructuredFromCUEModule(wl *Workload, artifacts []*types.ComponentManifest, ctxData process.ContextData) (*unstructured.Unstructured, error) {
|
||||
pCtx := process.NewContext(ctxData)
|
||||
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)
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", ctxData.AppName, ctxData.Namespace)
|
||||
}
|
||||
return makeWorkloadWithContext(pCtx, wl, ns, appName)
|
||||
return makeWorkloadWithContext(pCtx, wl, ctxData.Namespace, ctxData.AppName)
|
||||
}
|
||||
|
||||
// artifacts contains resources in unstructured shape of all components
|
||||
@@ -292,17 +293,18 @@ func (af *Appfile) GenerateComponentManifest(wl *Workload) (*types.ComponentMani
|
||||
if af.Namespace == "" {
|
||||
af.Namespace = corev1.NamespaceDefault
|
||||
}
|
||||
ctxData := GenerateContextDataWithCtx(context.Background(), af, wl.Name)
|
||||
// generate context here to avoid nil pointer panic
|
||||
wl.Ctx = NewBasicContext(af.Name, wl.Name, af.AppRevisionName, af.Namespace, wl.Params, af.AppAnnotations)
|
||||
wl.Ctx = NewBasicContext(GenerateContextDataWithCtx(context.Background(), af, wl.Name), wl.Params)
|
||||
switch wl.CapabilityCategory {
|
||||
case types.HelmCategory:
|
||||
return generateComponentFromHelmModule(wl, af.Name, af.AppRevisionName, af.Namespace, af.AppAnnotations)
|
||||
return generateComponentFromHelmModule(wl, ctxData)
|
||||
case types.KubeCategory:
|
||||
return generateComponentFromKubeModule(wl, af.Name, af.AppRevisionName, af.Namespace, af.AppAnnotations)
|
||||
return generateComponentFromKubeModule(wl, ctxData)
|
||||
case types.TerraformCategory:
|
||||
return generateComponentFromTerraformModule(wl, af.Name, af.Namespace)
|
||||
default:
|
||||
return generateComponentFromCUEModule(wl, af.Name, af.AppRevisionName, af.Namespace, af.AppAnnotations)
|
||||
return generateComponentFromCUEModule(wl, ctxData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,31 +473,31 @@ func (af *Appfile) setWorkloadRefToTrait(wlRef corev1.ObjectReference, trait *un
|
||||
}
|
||||
|
||||
// PrepareProcessContext prepares a DSL process Context
|
||||
func PrepareProcessContext(wl *Workload, applicationName, revision, namespace string, anno map[string]string) (process.Context, error) {
|
||||
func PrepareProcessContext(wl *Workload, ctxData process.ContextData) (process.Context, error) {
|
||||
if wl.Ctx == nil {
|
||||
wl.Ctx = NewBasicContext(applicationName, wl.Name, revision, namespace, wl.Params, anno)
|
||||
wl.Ctx = NewBasicContext(ctxData, wl.Params)
|
||||
}
|
||||
if err := wl.EvalContext(wl.Ctx); err != nil {
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", applicationName, namespace)
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", ctxData.AppName, ctxData.Namespace)
|
||||
}
|
||||
return wl.Ctx, nil
|
||||
}
|
||||
|
||||
// NewBasicContext prepares a basic DSL process Context
|
||||
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)
|
||||
func NewBasicContext(contextData process.ContextData, params map[string]interface{}) process.Context {
|
||||
pCtx := process.NewContext(contextData)
|
||||
if params != nil {
|
||||
pCtx.SetParameters(params)
|
||||
}
|
||||
return pCtx
|
||||
}
|
||||
|
||||
func generateComponentFromCUEModule(wl *Workload, appName, revision, ns string, anno map[string]string) (*types.ComponentManifest, error) {
|
||||
pCtx, err := PrepareProcessContext(wl, appName, revision, ns, anno)
|
||||
func generateComponentFromCUEModule(wl *Workload, ctxData process.ContextData) (*types.ComponentManifest, error) {
|
||||
pCtx, err := PrepareProcessContext(wl, ctxData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return baseGenerateComponent(pCtx, wl, appName, ns)
|
||||
return baseGenerateComponent(pCtx, wl, ctxData.AppName, ctxData.Namespace)
|
||||
}
|
||||
|
||||
func generateComponentFromTerraformModule(wl *Workload, appName, ns string) (*types.ComponentManifest, error) {
|
||||
@@ -664,7 +666,7 @@ output: {
|
||||
return templateStr, nil
|
||||
}
|
||||
|
||||
func generateComponentFromKubeModule(wl *Workload, appName, revision, ns string, anno map[string]string) (*types.ComponentManifest, error) {
|
||||
func generateComponentFromKubeModule(wl *Workload, ctxData process.ContextData) (*types.ComponentManifest, error) {
|
||||
templateStr, err := GenerateCUETemplate(wl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -672,7 +674,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, anno)
|
||||
compManifest, err := generateComponentFromCUEModule(wl, ctxData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -839,7 +841,7 @@ func setParameterValuesToKubeObj(obj *unstructured.Unstructured, values paramVal
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateComponentFromHelmModule(wl *Workload, appName, revision, ns string, anno map[string]string) (*types.ComponentManifest, error) {
|
||||
func generateComponentFromHelmModule(wl *Workload, ctxData process.ContextData) (*types.ComponentManifest, error) {
|
||||
templateStr, err := GenerateCUETemplate(wl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -849,22 +851,39 @@ func generateComponentFromHelmModule(wl *Workload, appName, revision, ns string,
|
||||
// re-use the way CUE module generates comp & acComp
|
||||
compManifest := &types.ComponentManifest{
|
||||
Name: wl.Name,
|
||||
Namespace: ns,
|
||||
Namespace: ctxData.Namespace,
|
||||
ExternalRevision: wl.ExternalRevision,
|
||||
StandardWorkload: &unstructured.Unstructured{},
|
||||
}
|
||||
|
||||
if wl.FullTemplate.Reference.Type != types.AutoDetectWorkloadDefinition {
|
||||
compManifest, err = generateComponentFromCUEModule(wl, appName, revision, ns, anno)
|
||||
compManifest, err = generateComponentFromCUEModule(wl, ctxData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
rls, repo, err := helm.RenderHelmReleaseAndHelmRepo(wl.FullTemplate.Helm, wl.Name, appName, ns, wl.Params)
|
||||
rls, repo, err := helm.RenderHelmReleaseAndHelmRepo(wl.FullTemplate.Helm, wl.Name, ctxData.AppName, ctxData.Namespace, wl.Params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
compManifest.PackagedWorkloadResources = []*unstructured.Unstructured{rls, repo}
|
||||
return compManifest, nil
|
||||
}
|
||||
|
||||
// GenerateContextDataWithCtx generates process context data with context and appfile
|
||||
func GenerateContextDataWithCtx(ctx context.Context, appfile *Appfile, wlName string) process.ContextData {
|
||||
data := process.ContextData{
|
||||
Namespace: appfile.Namespace,
|
||||
AppName: appfile.Name,
|
||||
CompName: wlName,
|
||||
AppRevisionName: appfile.AppRevisionName,
|
||||
Ctx: ctx,
|
||||
Components: appfile.Components,
|
||||
}
|
||||
if appfile.AppAnnotations != nil {
|
||||
data.WorkflowName = appfile.AppAnnotations[oam.AnnotationWorkflowName]
|
||||
data.PublishVersion = appfile.AppAnnotations[oam.AnnotationPublishVersion]
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -874,7 +874,12 @@ variable "password" {
|
||||
revision: "v1",
|
||||
}
|
||||
|
||||
pCtx := NewBasicContext(args.appName, args.wl.Name, args.revision, ns, args.wl.Params, nil)
|
||||
ctxData := GenerateContextDataWithCtx(context.Background(), &Appfile{
|
||||
Name: args.appName,
|
||||
Namespace: ns,
|
||||
AppRevisionName: args.revision,
|
||||
}, args.wl.Name)
|
||||
pCtx := NewBasicContext(ctxData, args.wl.Params)
|
||||
comp, err := evalWorkloadWithContext(pCtx, args.wl, ns, args.appName, compName)
|
||||
Expect(comp.StandardWorkload).ShouldNot(BeNil())
|
||||
Expect(comp.Name).Should(Equal(""))
|
||||
@@ -1332,10 +1337,15 @@ func TestBaseGenerateComponent(t *testing.T) {
|
||||
var wlName = "my-wl-1"
|
||||
var workflowName = "my-wf"
|
||||
var publishVersion = "123"
|
||||
pContext := NewBasicContext(appName, wlName, "rev-1", ns, nil, map[string]string{
|
||||
oam.AnnotationWorkflowName: workflowName,
|
||||
oam.AnnotationPublishVersion: publishVersion,
|
||||
})
|
||||
ctxData := GenerateContextDataWithCtx(context.Background(), &Appfile{
|
||||
Name: appName,
|
||||
Namespace: ns,
|
||||
AppAnnotations: map[string]string{
|
||||
oam.AnnotationWorkflowName: workflowName,
|
||||
oam.AnnotationPublishVersion: publishVersion,
|
||||
},
|
||||
}, wlName)
|
||||
pContext := NewBasicContext(ctxData, nil)
|
||||
base := `
|
||||
apiVersion: "apps/v1"
|
||||
kind: "Deployment"
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package appfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -33,7 +34,8 @@ func (p *Parser) ValidateCUESchematicAppfile(a *Appfile) error {
|
||||
if wl.CapabilityCategory != types.CUECategory {
|
||||
continue
|
||||
}
|
||||
pCtx, err := newValidationProcessContext(wl, a.Name, a.AppRevisionName, a.Namespace, a.app.Annotations)
|
||||
ctxData := GenerateContextDataWithCtx(context.Background(), a, wl.Name)
|
||||
pCtx, err := newValidationProcessContext(wl, ctxData)
|
||||
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 +51,7 @@ func (p *Parser) ValidateCUESchematicAppfile(a *Appfile) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func newValidationProcessContext(wl *Workload, appName, revisionName, ns string, anno map[string]string) (process.Context, error) {
|
||||
func newValidationProcessContext(wl *Workload, ctxData process.ContextData) (process.Context, error) {
|
||||
baseHooks := []process.BaseHook{
|
||||
// add more hook funcs here to validate CUE base
|
||||
}
|
||||
@@ -58,9 +60,11 @@ func newValidationProcessContext(wl *Workload, appName, revisionName, ns string,
|
||||
validateAuxiliaryNameUnique(),
|
||||
}
|
||||
|
||||
pCtx := process.NewContextWithHooks(ns, wl.Name, appName, revisionName, baseHooks, auxiliaryHooks, anno)
|
||||
ctxData.BaseHooks = baseHooks
|
||||
ctxData.AuxiliaryHooks = auxiliaryHooks
|
||||
pCtx := process.NewContext(ctxData)
|
||||
if err := wl.EvalContext(pCtx); err != nil {
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", appName, ns)
|
||||
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", ctxData.AppName, ctxData.Namespace)
|
||||
}
|
||||
return pCtx, nil
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ limitations under the License.
|
||||
package appfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/ginkgo/extensions/table"
|
||||
. "github.com/onsi/gomega"
|
||||
@@ -58,7 +60,13 @@ var _ = Describe("Test validate CUE schematic Appfile", func() {
|
||||
},
|
||||
engine: definition.NewWorkloadAbstractEngine("myweb", pd),
|
||||
}
|
||||
pCtx, err := newValidationProcessContext(wl, "myapp", "myapp-v1", "test-ns", nil)
|
||||
|
||||
ctxData := GenerateContextDataWithCtx(context.Background(), &Appfile{
|
||||
Name: "myapp",
|
||||
Namespace: "test-ns",
|
||||
AppRevisionName: "myapp-v1",
|
||||
}, wl.Name)
|
||||
pCtx, err := newValidationProcessContext(wl, ctxData)
|
||||
Expect(err).Should(BeNil())
|
||||
Eventually(func() string {
|
||||
for _, tr := range wl.Traits {
|
||||
|
||||
@@ -67,7 +67,7 @@ func (h *AppHandler) GenerateApplicationSteps(ctx context.Context,
|
||||
oamProvider.Install(handlerProviders, app, h.applyComponentFunc(
|
||||
appParser, appRev, af), h.renderComponentFunc(appParser, appRev, af))
|
||||
http.Install(handlerProviders, h.r.Client, app.Namespace)
|
||||
pCtx := process.NewContext(app.Namespace, app.Name, app.Name, appRev.Name, app.Annotations)
|
||||
pCtx := process.NewContext(generateContextDataFromApp(app, appRev.Name))
|
||||
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) {
|
||||
@@ -294,3 +294,17 @@ func generateStepID(stepName string, wfStatus *common.WorkflowStatus) string {
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func generateContextDataFromApp(app *v1beta1.Application, appRev string) process.ContextData {
|
||||
data := process.ContextData{
|
||||
Namespace: app.Namespace,
|
||||
AppName: app.Name,
|
||||
CompName: app.Name,
|
||||
AppRevisionName: appRev,
|
||||
}
|
||||
if app.Annotations != nil {
|
||||
data.WorkflowName = app.Annotations[oam.AnnotationWorkflowName]
|
||||
data.PublishVersion = app.Annotations[oam.AnnotationPublishVersion]
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -439,8 +439,8 @@ 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, appfile.AppAnnotations)
|
||||
ctx := context.Background()
|
||||
pCtx = af.NewBasicContext(af.GenerateContextDataWithCtx(ctx, appfile, wl.Name), wl.Params)
|
||||
var configuration terraformapi.Configuration
|
||||
if err := c.Get(ctx, client.ObjectKey{Name: wl.Name, Namespace: ns}, &configuration); err != nil {
|
||||
wlHealth.HealthStatus = StatusUnhealthy
|
||||
@@ -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, appfile.AppAnnotations)
|
||||
pCtx = process.NewContext(af.GenerateContextDataWithCtx(ctx, appfile, wl.Name))
|
||||
if wl.CapabilityCategory != oamtypes.CUECategory {
|
||||
templateStr, err := af.GenerateCUETemplate(wl)
|
||||
if err != nil {
|
||||
|
||||
@@ -217,7 +217,12 @@ parameter: {
|
||||
}
|
||||
|
||||
for _, v := range testCases {
|
||||
ctx := process.NewContext("default", "test", "myapp", "myapp-v1", nil)
|
||||
ctx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "test",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
wt := NewWorkloadAbstractEngine("testWorkload", &packages.PackageDiscover{})
|
||||
err := wt.Complete(ctx, v.workloadTemplate, v.params)
|
||||
hasError := err != nil
|
||||
@@ -918,7 +923,12 @@ parameter: [string]: string`,
|
||||
}
|
||||
|
||||
`
|
||||
ctx := process.NewContext("default", "test", "myapp", "myapp-v1", nil)
|
||||
ctx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "test",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
wt := NewWorkloadAbstractEngine("-", &packages.PackageDiscover{})
|
||||
if err := wt.Complete(ctx, baseTemplate, map[string]interface{}{
|
||||
"replicas": 2,
|
||||
@@ -1017,7 +1027,12 @@ outputs: service :{
|
||||
}
|
||||
for k, v := range testcases {
|
||||
wd := NewWorkloadAbstractEngine(k, &packages.PackageDiscover{})
|
||||
ctx := process.NewContext("default", k, "myapp", "myapp-v1", nil)
|
||||
ctx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: k,
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
err := wd.Complete(ctx, v.template, map[string]interface{}{})
|
||||
assert.NoError(t, err)
|
||||
_, assists := ctx.Output()
|
||||
@@ -1095,7 +1110,12 @@ outputs: abc :{
|
||||
}
|
||||
for k, v := range testcases {
|
||||
td := NewTraitAbstractEngine(k, &packages.PackageDiscover{})
|
||||
ctx := process.NewContext("default", k, "myapp", "myapp-v1", nil)
|
||||
ctx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: k,
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
err := td.Complete(ctx, v.template, map[string]interface{}{})
|
||||
assert.NoError(t, err)
|
||||
_, assists := ctx.Output()
|
||||
|
||||
+28
-32
@@ -27,7 +27,6 @@ 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"
|
||||
)
|
||||
|
||||
@@ -44,7 +43,6 @@ type Context interface {
|
||||
GetCtx() context.Context
|
||||
SetCtx(context.Context)
|
||||
SetHooks(baseHooks []BaseHook, auxHooks []AuxiliaryHook)
|
||||
SetComponents(components []common.ApplicationComponent)
|
||||
}
|
||||
|
||||
// Auxiliary are objects rendered by definition template.
|
||||
@@ -99,45 +97,43 @@ type RequiredSecrets struct {
|
||||
Data map[string]interface{}
|
||||
}
|
||||
|
||||
// ContextData is the core data of process context
|
||||
type ContextData struct {
|
||||
Namespace string
|
||||
AppName string
|
||||
CompName string
|
||||
AppRevisionName string
|
||||
WorkflowName string
|
||||
PublishVersion string
|
||||
|
||||
Ctx context.Context
|
||||
BaseHooks []BaseHook
|
||||
AuxiliaryHooks []AuxiliaryHook
|
||||
Components []common.ApplicationComponent
|
||||
}
|
||||
|
||||
// NewContext create render templateContext
|
||||
func NewContext(namespace, name, appName, appRevision string, anno map[string]string) Context {
|
||||
func NewContext(data ContextData) Context {
|
||||
ctx := &templateContext{
|
||||
name: name,
|
||||
appName: appName,
|
||||
appRevision: appRevision,
|
||||
namespace: data.Namespace,
|
||||
name: data.CompName,
|
||||
appName: data.AppName,
|
||||
appRevision: data.AppRevisionName,
|
||||
workflowName: data.WorkflowName,
|
||||
publishVersion: data.PublishVersion,
|
||||
|
||||
configs: []map[string]string{},
|
||||
auxiliaries: []Auxiliary{},
|
||||
namespace: namespace,
|
||||
parameters: map[string]interface{}{},
|
||||
}
|
||||
if anno != nil {
|
||||
ctx.workflowName = anno[oam.AnnotationWorkflowName]
|
||||
ctx.publishVersion = anno[oam.AnnotationPublishVersion]
|
||||
|
||||
ctx: data.Ctx,
|
||||
baseHooks: data.BaseHooks,
|
||||
auxiliaryHooks: data.AuxiliaryHooks,
|
||||
components: data.Components,
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// NewProcessContextWithCtx create render templateContext with 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, 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, anno map[string]string) Context {
|
||||
pCtx := NewContext(namespace, name, appName, appRevision, anno)
|
||||
pCtx.SetComponents(components)
|
||||
return pCtx
|
||||
}
|
||||
|
||||
// SetParameters sets templateContext parameters
|
||||
func (ctx *templateContext) SetParameters(params map[string]interface{}) {
|
||||
ctx.parameters = params
|
||||
|
||||
@@ -23,7 +23,6 @@ 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) {
|
||||
@@ -101,9 +100,13 @@ image: "myserver"
|
||||
},
|
||||
}
|
||||
|
||||
ctx := NewContext("myns", "mycomp", "myapp", "myapp-v1", map[string]string{
|
||||
oam.AnnotationWorkflowName: "myworkflow",
|
||||
oam.AnnotationPublishVersion: "mypublishversion",
|
||||
ctx := NewContext(ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "mycomp",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
WorkflowName: "myworkflow",
|
||||
PublishVersion: "mypublishversion",
|
||||
})
|
||||
ctx.SetBase(base)
|
||||
ctx.AppendAuxiliaries(svcAux)
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@ func (handler *ViewHandler) QueryView(ctx context.Context, qv QueryView) (*value
|
||||
Outputs: queryKey.Outputs,
|
||||
}
|
||||
|
||||
pCtx := process.NewContext("", "", "", "", nil)
|
||||
pCtx := process.NewContext(process.ContextData{})
|
||||
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 {
|
||||
|
||||
@@ -76,7 +76,12 @@ myIP: value: "1.1.1.1"
|
||||
},
|
||||
})
|
||||
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
pCtx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "mycomp",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
|
||||
steps := []v1beta1.WorkflowStep{
|
||||
@@ -180,7 +185,12 @@ close({
|
||||
return errors.New("mock error")
|
||||
},
|
||||
})
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
pCtx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "mycomp",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
|
||||
steps := []v1beta1.WorkflowStep{
|
||||
@@ -417,7 +427,12 @@ func TestPendingInputCheck(t *testing.T) {
|
||||
ParameterKey: "score",
|
||||
}},
|
||||
}
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
pCtx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "mycomp",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
gen, err := tasksLoader.GetTaskGenerator(context.Background(), step.Type)
|
||||
r.NoError(err)
|
||||
@@ -447,7 +462,12 @@ func TestPendingDependsOnCheck(t *testing.T) {
|
||||
Type: "ok",
|
||||
DependsOn: []string{"depend"},
|
||||
}
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
pCtx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "mycomp",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
tasksLoader := NewTaskLoader(mockLoadTemplate, nil, discover, 0, pCtx)
|
||||
gen, err := tasksLoader.GetTaskGenerator(context.Background(), step.Type)
|
||||
r.NoError(err)
|
||||
|
||||
@@ -47,7 +47,12 @@ func TestDiscover(t *testing.T) {
|
||||
return "", makeErr(name)
|
||||
}
|
||||
}
|
||||
pCtx := process.NewContext("default", "test", "test", "test-v1", nil)
|
||||
pCtx := process.NewContext(process.ContextData{
|
||||
AppName: "myapp",
|
||||
CompName: "mycomp",
|
||||
Namespace: "default",
|
||||
AppRevisionName: "myapp-v1",
|
||||
})
|
||||
discover := &taskDiscover{
|
||||
builtins: map[string]types.TaskGenerator{
|
||||
"suspend": suspend,
|
||||
|
||||
@@ -33,7 +33,7 @@ import (
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/appfile"
|
||||
"github.com/oam-dev/kubevela/pkg/controller/utils"
|
||||
"github.com/oam-dev/kubevela/pkg/cue/process"
|
||||
util2 "github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/common"
|
||||
"github.com/oam-dev/kubevela/pkg/utils/util"
|
||||
@@ -75,15 +75,13 @@ func ApplyTerraform(app *v1beta1.Application, k8sClient client.Client, ioStream
|
||||
return nil, err
|
||||
}
|
||||
|
||||
revisionName, _ := utils.GetAppNextRevision(app)
|
||||
|
||||
for i, wl := range appFile.Workloads {
|
||||
switch wl.CapabilityCategory {
|
||||
case types.TerraformCategory:
|
||||
name := wl.Name
|
||||
ioStream.Infof("\nApplying cloud resources %s\n", name)
|
||||
|
||||
tf, err := getTerraformJSONFiles(wl, appFile.Name, revisionName, namespace)
|
||||
tf, err := getTerraformJSONFiles(wl, appfile.GenerateContextDataWithCtx(ctx, appFile, wl.Name))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get Terraform JSON files from workload %s: %w", name, err)
|
||||
}
|
||||
@@ -197,8 +195,8 @@ 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, nil)
|
||||
func getTerraformJSONFiles(wl *appfile.Workload, ctxData process.ContextData) ([]byte, error) {
|
||||
pCtx, err := appfile.PrepareProcessContext(wl, ctxData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user