Feat: support outputs objects for policy (#5183)

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>

Signed-off-by: Jianbo Sun <jianbo.sjb@alibaba-inc.com>
This commit is contained in:
Jianbo Sun
2022-12-13 14:52:16 +08:00
committed by GitHub
parent e63aa444e5
commit b6f4328167
2 changed files with 64 additions and 14 deletions
+31 -10
View File
@@ -197,35 +197,56 @@ type Appfile struct {
func (af *Appfile) GeneratePolicyManifests(ctx context.Context) ([]*unstructured.Unstructured, error) {
var manifests []*unstructured.Unstructured
for _, policy := range af.PolicyWorkloads {
un, err := af.generateUnstructured(policy)
un, err := af.generatePolicyUnstructured(policy)
if err != nil {
return nil, err
}
manifests = append(manifests, un)
manifests = append(manifests, un...)
}
return manifests, nil
}
func (af *Appfile) generateUnstructured(workload *Workload) (*unstructured.Unstructured, error) {
func (af *Appfile) generatePolicyUnstructured(workload *Workload) ([]*unstructured.Unstructured, error) {
ctxData := GenerateContextDataFromAppFile(af, workload.Name)
un, err := generateUnstructuredFromCUEModule(workload, af.Artifacts, ctxData)
uns, err := generatePolicyUnstructuredFromCUEModule(workload, af.Artifacts, ctxData)
if err != nil {
return nil, err
}
un.SetName(workload.Name)
if len(un.GetNamespace()) == 0 {
un.SetNamespace(af.Namespace)
for _, un := range uns {
if len(un.GetName()) == 0 {
un.SetName(workload.Name)
}
if len(un.GetNamespace()) == 0 {
un.SetNamespace(af.Namespace)
}
}
return un, nil
return uns, nil
}
func generateUnstructuredFromCUEModule(wl *Workload, artifacts []*types.ComponentManifest, ctxData velaprocess.ContextData) (*unstructured.Unstructured, error) {
func generatePolicyUnstructuredFromCUEModule(wl *Workload, artifacts []*types.ComponentManifest, ctxData velaprocess.ContextData) ([]*unstructured.Unstructured, error) {
pCtx := velaprocess.NewContext(ctxData)
pCtx.PushData(velaprocess.ContextDataArtifacts, prepareArtifactsData(artifacts))
if err := wl.EvalContext(pCtx); err != nil {
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", ctxData.AppName, ctxData.Namespace)
}
return makeWorkloadWithContext(pCtx, wl, ctxData.Namespace, ctxData.AppName)
base, auxs := pCtx.Output()
workload, err := base.Unstructured()
if err != nil {
return nil, errors.Wrapf(err, "evaluate base template policy=%s app=%s", wl.Name, ctxData.AppName)
}
commonLabels := definition.GetCommonLabels(definition.GetBaseContextLabels(pCtx))
util.AddLabels(workload, commonLabels)
var res = []*unstructured.Unstructured{workload}
for _, assist := range auxs {
tr, err := assist.Ins.Unstructured()
if err != nil {
return nil, errors.Wrapf(err, "evaluate auxiliary=%s template for policy=%s app=%s", assist.Name, wl.Name, ctxData.AppName)
}
util.AddLabels(tr, commonLabels)
res = append(res, tr)
}
return res, nil
}
// artifacts contains resources in unstructured shape of all components
+33 -4
View File
@@ -392,6 +392,14 @@ spec:
},
}
}
outputs: virtualservice: {
apiVersion: "networking.istio.io/v1alpha3"
kind: "VirtualService"
spec: {
hosts: "abc"
http: ["abc"]
}
}
parameter: {
boundComponents: [...string]
}`},
@@ -407,7 +415,7 @@ spec:
Expect(err).Should(BeNil())
Expect(len(gotPolicies)).ShouldNot(Equal(0))
expectPolicy := unstructured.Unstructured{
expectPolicy0 := unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"compName": "test-comp",
@@ -424,17 +432,38 @@ spec:
"app.oam.dev/name": "test-app",
"app.oam.dev/component": "test-policy",
"app.oam.dev/appRevision": "",
"workload.oam.dev/type": "test-policy",
},
},
"apiVersion": "core.oam.dev/v1alpha2",
"kind": "HealthScope",
},
}
Expect(len(gotPolicies)).ShouldNot(Equal(0))
Expect(len(gotPolicies)).Should(Equal(2))
gotPolicy := gotPolicies[0]
Expect(cmp.Diff(gotPolicy.Object, expectPolicy.Object)).Should(BeEmpty())
Expect(cmp.Diff(gotPolicy.Object, expectPolicy0.Object)).Should(BeEmpty())
expectPolicy1 := unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
"hosts": "abc",
"http": []interface{}{"abc"},
},
"metadata": map[string]interface{}{
"name": "test-policy",
"namespace": "default",
"labels": map[string]interface{}{
"app.oam.dev/name": "test-app",
"app.oam.dev/component": "test-policy",
"app.oam.dev/appRevision": "",
},
},
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "VirtualService",
},
}
gotPolicy = gotPolicies[1]
Expect(cmp.Diff(gotPolicy.Object, expectPolicy1.Object)).Should(BeEmpty())
})
})
var _ = Describe("Test Terraform schematic appfile", func() {