diff --git a/charts/vela-core/templates/defwithtemplate/env-binding.yaml b/charts/vela-core/templates/defwithtemplate/env-binding.yaml index b9106bd93..23dd5cdc1 100644 --- a/charts/vela-core/templates/defwithtemplate/env-binding.yaml +++ b/charts/vela-core/templates/defwithtemplate/env-binding.yaml @@ -15,7 +15,7 @@ spec: apiVersion: "core.oam.dev/v1alpha1" kind: "EnvBinding" spec: { - engine: parameter.engine + engine: parameter.clusterManagementEngine appTemplate: { apiVersion: "core.oam.dev/v1beta1" kind: "Application" @@ -26,11 +26,9 @@ spec: spec: components: context.components } envs: parameter.envs - if !parameter.created { - outputResourcesTo: { - name: context.name - namespace: context.namespace - } + outputResourcesTo: { + name: context.name + namespace: context.namespace } } } @@ -57,8 +55,7 @@ spec: } } parameter: { - engine: *"ocm" | string + clusterManagementEngine: *"ocm" | string envs: [...#Env] - created: *true | bool } diff --git a/docs/examples/workflow-with-ocm/README.md b/docs/examples/workflow-with-ocm/README.md index d4b6617df..7a2199b16 100644 --- a/docs/examples/workflow-with-ocm/README.md +++ b/docs/examples/workflow-with-ocm/README.md @@ -90,6 +90,12 @@ poc-01 true {{ APIServer address }} True True 30s ## Deploy the resource to ack cluster +install trait `expose` from default capability center. + +```shell +vela cap install default-cap-center/expose +``` + ```shell kubectl apply -f app.yaml ``` diff --git a/docs/examples/workflow-with-ocm/app.yaml b/docs/examples/workflow-with-ocm/app.yaml index 109d4249a..5a82f8490 100644 --- a/docs/examples/workflow-with-ocm/app.yaml +++ b/docs/examples/workflow-with-ocm/app.yaml @@ -20,7 +20,6 @@ spec: type: env-binding properties: engine: ocm - created: false envs: - name: prod patch: @@ -43,7 +42,7 @@ spec: workflow: steps: - name: deploy-server - type: deploy2cluster + type: mutil-env properties: env: prod policy: prod-env \ No newline at end of file diff --git a/pkg/controller/core.oam.dev/v1alpha2/application/apply.go b/pkg/controller/core.oam.dev/v1alpha2/application/apply.go index 74ff95e3b..8ec6ab582 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/application/apply.go +++ b/pkg/controller/core.oam.dev/v1alpha2/application/apply.go @@ -95,11 +95,15 @@ func (h *AppHandler) initDispatcher() { // ApplyAppManifests will dispatch Application manifests func (h *AppHandler) ApplyAppManifests(ctx context.Context, comps []*types.ComponentManifest, policies []*unstructured.Unstructured) error { - appRev := h.currentAppRev - if (h.app.Spec.Workflow != nil && len(h.app.Spec.Workflow.Steps) > 0) || h.app.Annotations[oam.AnnotationAppRevisionOnly] == "true" || len(h.app.Spec.Policies) != 0 { + // dispatch workload in policy before workflow start + if len(policies) != 0 { if err := h.Dispatch(ctx, policies...); err != nil { - return errors.WithMessage(err, "cannot dispatch policies before workflow") + return errors.WithMessage(err, "cannot dispatch policies") } + } + + appRev := h.currentAppRev + if (h.app.Spec.Workflow != nil && len(h.app.Spec.Workflow.Steps) > 0) || h.app.Annotations[oam.AnnotationAppRevisionOnly] == "true" { return h.createResourcesConfigMap(ctx, appRev, comps, policies) } if appWillRollout(h.app) { diff --git a/pkg/controller/core.oam.dev/v1alpha2/application/workflow_test.go b/pkg/controller/core.oam.dev/v1alpha2/application/workflow_test.go index 4c316cc05..c525d08f3 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/application/workflow_test.go +++ b/pkg/controller/core.oam.dev/v1alpha2/application/workflow_test.go @@ -72,6 +72,25 @@ var _ = Describe("Test Workflow", func() { Properties: runtime.RawExtension{Raw: []byte(`{"key":"test"}`)}, }} + appWithPolicy := &oamcore.Application{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-app-only-with-policy", + Namespace: namespace, + }, + Spec: oamcore.ApplicationSpec{ + Components: []common.ApplicationComponent{{ + Name: "test-component", + Type: "worker", + Properties: runtime.RawExtension{Raw: []byte(`{"cmd":["sleep","1000"],"image":"busybox"}`)}, + }}, + Policies: []oamcore.AppPolicy{{ + Name: "test-policy", + Type: "foopolicy", + Properties: runtime.RawExtension{Raw: []byte(`{"key":"test"}`)}, + }}, + }, + } + testDefinitions := []string{componentDefYaml, policyDefYaml, wfStepDefYaml} BeforeEach(func() { @@ -109,21 +128,47 @@ var _ = Describe("Test Workflow", func() { Expect(cm.Data[ConfigMapKeyComponents]).Should(Equal(testConfigMapComponentValue)) }) - It("should create workload in policy before workflow start", func() { - appWithPolicy := appWithWorkflow.DeepCopy() - appWithPolicy.SetName("test-app-with-policy") - appWithPolicy.Spec.Policies = []oamcore.AppPolicy{{ - Name: "test-foo-policy", - Type: "foopolicy", - Properties: runtime.RawExtension{Raw: []byte(`{"key":"test"}`)}, - }} - + It("should create workload in application when policy is specified", func() { Expect(k8sClient.Create(ctx, appWithPolicy)).Should(BeNil()) // first try to add finalizer tryReconcile(reconciler, appWithPolicy.Name, appWithPolicy.Namespace) tryReconcile(reconciler, appWithPolicy.Name, appWithPolicy.Namespace) + deploy := &appsv1.Deployment{} + Expect(k8sClient.Get(ctx, client.ObjectKey{ + Name: appWithPolicy.Spec.Components[0].Name, + Namespace: appWithPolicy.Namespace, + }, deploy)).Should(BeNil()) + + policyObj := &unstructured.Unstructured{} + policyObj.SetGroupVersionKind(schema.GroupVersionKind{ + Group: "example.com", + Kind: "Foo", + Version: "v1", + }) + + Expect(k8sClient.Get(ctx, client.ObjectKey{ + Name: "test-policy", + Namespace: appWithPolicy.Namespace, + }, policyObj)).Should(BeNil()) + }) + + It("should create workload in policy before workflow start", func() { + appWithPolicyAndWorkflow := appWithWorkflow.DeepCopy() + appWithPolicyAndWorkflow.SetName("test-app-with-policy") + appWithPolicyAndWorkflow.Spec.Policies = []oamcore.AppPolicy{{ + Name: "test-foo-policy", + Type: "foopolicy", + Properties: runtime.RawExtension{Raw: []byte(`{"key":"test"}`)}, + }} + + Expect(k8sClient.Create(ctx, appWithPolicyAndWorkflow)).Should(BeNil()) + + // first try to add finalizer + tryReconcile(reconciler, appWithPolicyAndWorkflow.Name, appWithPolicyAndWorkflow.Namespace) + tryReconcile(reconciler, appWithPolicyAndWorkflow.Name, appWithPolicyAndWorkflow.Namespace) + policyObj := &unstructured.Unstructured{} policyObj.SetGroupVersionKind(schema.GroupVersionKind{ Group: "example.com", @@ -133,7 +178,7 @@ var _ = Describe("Test Workflow", func() { Expect(k8sClient.Get(ctx, client.ObjectKey{ Name: "test-foo-policy", - Namespace: appWithPolicy.Namespace, + Namespace: appWithPolicyAndWorkflow.Namespace, }, policyObj)).Should(BeNil()) }) diff --git a/vela-templates/definitions/internal/env-binding.cue b/vela-templates/definitions/internal/env-binding.cue index c8f782f03..69a647bff 100644 --- a/vela-templates/definitions/internal/env-binding.cue +++ b/vela-templates/definitions/internal/env-binding.cue @@ -9,7 +9,7 @@ template: { apiVersion: "core.oam.dev/v1alpha1" kind: "EnvBinding" spec: { - engine: parameter.engine + engine: parameter.clusterManagementEngine appTemplate: { apiVersion: "core.oam.dev/v1beta1" kind: "Application" @@ -22,11 +22,9 @@ template: { } } envs: parameter.envs - if !parameter.created { - outputResourcesTo: { - name: context.name - namespace: context.namespace - } + outputResourcesTo: { + name: context.name + namespace: context.namespace } } } @@ -53,8 +51,7 @@ template: { } } parameter: { - engine: *"ocm" | string + clusterManagementEngine: *"ocm" | string envs: [...#Env] - created: *true | bool } }