Fix(app): When only the policy is specified, the resources in the app need to be rendered and created (#2197)

* Fix(app): apply app when policy is specified

* Fix(policy): update env-binding policy

* Test(policy): add test
This commit is contained in:
yangsoon
2021-09-01 17:27:47 +08:00
committed by GitHub
parent f130d1d922
commit d4277f644e
6 changed files with 79 additions and 31 deletions
@@ -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
}
@@ -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
```
+1 -2
View File
@@ -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
@@ -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) {
@@ -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())
})
@@ -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
}
}