output not allowed in traitdefinition, should use outputs:<resource>:<full object>

This commit is contained in:
天元
2021-02-23 16:19:31 +08:00
parent 9bb996e9d5
commit 98b059c0ae
5 changed files with 13 additions and 31 deletions

View File

@@ -103,7 +103,7 @@ spec:
extension:
template: |-
patch: {spec: template: metadata: labels: app: context.name}
output: {
outputs: service: {
apiVersion: "v1"
kind: "Service"
metadata: name: context.name

View File

@@ -4,9 +4,10 @@ In this section we will introduce how to define a Trait with CUE template.
## Composition
Defining a *Trait* with CUE template is a bit different from *Workload Type*: a trait MUST use `outputs` keyword instead of `output` to define template.
Defining a *Trait* with CUE template is a bit different from *Workload Type*: a trait MUST use `outputs` keyword instead of `output` in template.
With the help of CUE template, it is very nature to compose multiple Kubernetes resources in one trait. Similarly, the format MUST be `outputs:<unique-name>:<full template>`.
With the help of CUE template, it is very nature to compose multiple Kubernetes resources in one trait.
Similarly, the format MUST be `outputs:<unique-name>:<full template>`.
Below is an example for `ingress` trait.
@@ -147,7 +148,7 @@ spec:
## Patch Trait
You could also use keyword `patch` to patch data to the component instance (before the resource is applied) and claim this behavior as a trait.
You could also use keyword `patch` to patch data to the component instance (before the resource applied) and claim this behavior as a trait.
Below is an example for `node-affinity` trait:
@@ -356,6 +357,7 @@ please make sure the trait resource name is unique, or the former data will be c
Below is an example
1. the main workload object(Deployment) in this example will render into the context.output before rendering traits.
2. the context.outputs.<xx> will keep all these rendered trait data and can be used in the traits after them.
```yaml
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition

View File

@@ -119,7 +119,7 @@ func (wd *workloadDef) Complete(ctx process.Context, abstractTemplate string) er
if err != nil {
return errors.WithMessagef(err, "invalid outputs(%s) of workload %s", fieldInfo.Name, wd.name)
}
ctx.AppendAuxiliaries(process.Auxiliary{Ins: other, Type: AuxiliaryWorkload, Name: fieldInfo.Name, IsOutputs: true})
ctx.AppendAuxiliaries(process.Auxiliary{Ins: other, Type: AuxiliaryWorkload, Name: fieldInfo.Name})
}
}
return nil
@@ -291,14 +291,6 @@ func (td *traitDef) Complete(ctx process.Context, abstractTemplate string) error
}
}
output := inst.Lookup(OutputFieldName)
if output.Exists() {
other, err := model.NewOther(output)
if err != nil {
return errors.WithMessagef(err, "invalid output of trait %s", td.name)
}
ctx.AppendAuxiliaries(process.Auxiliary{Ins: other, Type: td.name, IsOutputs: false})
}
outputs := inst.Lookup(OutputsFieldName)
if outputs.Exists() {
st, err := outputs.Struct()
@@ -314,7 +306,7 @@ func (td *traitDef) Complete(ctx process.Context, abstractTemplate string) error
if err != nil {
return errors.WithMessagef(err, "invalid outputs(resource=%s) of trait %s", fieldInfo.Name, td.name)
}
ctx.AppendAuxiliaries(process.Auxiliary{Ins: other, Type: td.name, Name: fieldInfo.Name, IsOutputs: true})
ctx.AppendAuxiliaries(process.Auxiliary{Ins: other, Type: td.name, Name: fieldInfo.Name})
}
}
@@ -361,11 +353,7 @@ func (td *traitDef) getTemplateContext(ctx process.Context, cli client.Reader, n
if err != nil {
return nil, err
}
if assist.IsOutputs {
outputs[assist.Name] = object
} else {
root[OutputFieldName] = object
}
outputs[assist.Name] = object
}
if len(outputs) > 0 {
root[OutputsFieldName] = outputs

View File

@@ -33,6 +33,7 @@ type Context interface {
}
// Auxiliary are objects rendered by definition template.
// the format for auxiliary resource is always: `outputs.<resourceName>`, it can be auxiliary workload or trait
type Auxiliary struct {
Ins model.Instance
// Type will be used to mark definition label for OAM runtime to get the CRD
@@ -41,12 +42,6 @@ type Auxiliary struct {
// Workload or trait with multiple `outputs` will have a name, if name is empty, than it's the main of this type.
Name string
// IsOutputs will record the output path format of the Auxiliary
// it can be one of these two cases:
// false: the format is `output`, this means it's the main resource of the trait
// true: the format is `outputs.<resourceName>`, this means it can be auxiliary workload or trait
IsOutputs bool
}
type templateContext struct {
@@ -99,9 +94,7 @@ func (ctx *templateContext) BaseContextFile() string {
if len(ctx.auxiliaries) > 0 {
var auxLines []string
for _, auxiliary := range ctx.auxiliaries {
if auxiliary.IsOutputs {
auxLines = append(auxLines, fmt.Sprintf("%s: %s", auxiliary.Name, structMarshal(auxiliary.Ins.String())))
}
auxLines = append(auxLines, fmt.Sprintf("%s: %s", auxiliary.Name, structMarshal(auxiliary.Ins.String())))
}
if len(auxLines) > 0 {
buff += fmt.Sprintf(OutputsFieldName+": {%s}\n", strings.Join(auxLines, "\n"))

View File

@@ -44,9 +44,8 @@ image: "myserver"
}
svcAux := Auxiliary{
Ins: svcIns,
Name: "service",
IsOutputs: true,
Ins: svcIns,
Name: "service",
}
ctx := NewContext("mycomp", "myapp")