Skip to content

Commit

Permalink
move template out from extension
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderflow committed Feb 5, 2021
1 parent b6f10bb commit 96a589b
Show file tree
Hide file tree
Showing 25 changed files with 685 additions and 478 deletions.
20 changes: 20 additions & 0 deletions apis/core.oam.dev/v1alpha2/core_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ type WorkloadDefinitionSpec struct {
// +optional
Status *Status `json:"status,omitempty"`

// Template defines the abstraction template data of the workload, it will replace the old template in extension field.
// the data format depends on templateType, by default it's CUE
// +optional
Template string `json:"template,omitempty"`

// TemplateType defines the data format of the template, by default it's CUE format
// Terraform HCL, Helm Chart will also be candidates in the near future.
// +optional
TemplateType string `json:"templateType,omitempty"`

// Extension is used for extension needs by OAM platform builders
// +optional
// +kubebuilder:pruning:PreserveUnknownFields
Expand Down Expand Up @@ -140,6 +150,16 @@ type TraitDefinitionSpec struct {
// +optional
ConflictsWith []string `json:"conflictsWith,omitempty"`

// Template defines the abstraction template data of the workload, it will replace the old template in extension field.
// the data format depends on templateType, by default it's CUE
// +optional
Template string `json:"template,omitempty"`

// TemplateType defines the data format of the template, by default it's CUE format
// Terraform HCL, Helm Chart will also be candidates in the near future.
// +optional
TemplateType string `json:"templateType,omitempty"`

// Status defines the custom health policy and status message for trait
// +optional
Status *Status `json:"status,omitempty"`
Expand Down
16 changes: 0 additions & 16 deletions apis/types/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ package types

import (
"encoding/json"
"fmt"

"cuelang.org/go/cue"
"github.com/google/go-cmp/cmp"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime"
)

// Source record the source of Capability
Expand Down Expand Up @@ -107,20 +105,6 @@ type Parameter struct {
Alias string `json:"alias,omitempty"`
}

// ConvertTemplateJSON2Object convert spec.extension to object
func ConvertTemplateJSON2Object(in *runtime.RawExtension) (Capability, error) {
var t Capability
var extension Capability
if in == nil || in.Raw == nil {
return t, fmt.Errorf("no template found")
}
err := json.Unmarshal(in.Raw, &extension)
if err == nil {
t = extension
}
return t, err
}

// SetFlagBy set cli flag from Parameter
func SetFlagBy(flags *pflag.FlagSet, v Parameter) {
name := v.Name
Expand Down
6 changes: 6 additions & 0 deletions charts/vela-core/crds/core.oam.dev_traitdefinitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ spec:
description: HealthPolicy defines the health check policy for the abstraction
type: string
type: object
template:
description: Template defines the abstraction template data of the workload, it will replace the old template in extension field. the data format depends on templateType, by default it's CUE
type: string
templateType:
description: TemplateType defines the data format of the template, by default it's CUE format Terraform HCL, Helm Chart will also be candidates in the near future.
type: string
workloadRefPath:
description: WorkloadRefPath indicates where/if a trait accepts a workloadRef object
type: string
Expand Down
6 changes: 6 additions & 0 deletions charts/vela-core/crds/core.oam.dev_workloaddefinitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ spec:
description: HealthPolicy defines the health check policy for the abstraction
type: string
type: object
template:
description: Template defines the abstraction template data of the workload, it will replace the old template in extension field. the data format depends on templateType, by default it's CUE
type: string
templateType:
description: TemplateType defines the data format of the template, by default it's CUE format Terraform HCL, Helm Chart will also be candidates in the near future.
type: string
required:
- definitionRef
type: object
Expand Down
95 changes: 47 additions & 48 deletions charts/vela-core/templates/defwithtemplate/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,50 @@ spec:
appliesToWorkloads:
- webservice
- worker
extension:
template: |
parameter: {
domain: string
http: [string]: int
}
// trait template can have multiple outputs in one trait
outputs: service: {
apiVersion: "v1"
kind: "Service"
metadata:
name: context.name
spec: {
selector:
"app.oam.dev/component": context.name
ports: [
for k, v in parameter.http {
port: v
targetPort: v
},
]
}
}
outputs: ingress: {
apiVersion: "networking.k8s.io/v1beta1"
kind: "Ingress"
metadata:
name: context.name
spec: {
rules: [{
host: parameter.domain
http: {
paths: [
for k, v in parameter.http {
path: k
backend: {
serviceName: context.name
servicePort: v
}
},
]
}
}]
}
}
template: |
parameter: {
domain: string
http: [string]: int
}
// trait template can have multiple outputs in one trait
outputs: service: {
apiVersion: "v1"
kind: "Service"
metadata:
name: context.name
spec: {
selector:
"app.oam.dev/component": context.name
ports: [
for k, v in parameter.http {
port: v
targetPort: v
},
]
}
}
outputs: ingress: {
apiVersion: "networking.k8s.io/v1beta1"
kind: "Ingress"
metadata:
name: context.name
spec: {
rules: [{
host: parameter.domain
http: {
paths: [
for k, v in parameter.http {
path: k
backend: {
serviceName: context.name
servicePort: v
}
},
]
}
}]
}
}
29 changes: 14 additions & 15 deletions charts/vela-core/templates/defwithtemplate/manualscale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ spec:
definitionRef:
name: manualscalertraits.core.oam.dev
workloadRefPath: spec.workloadRef
extension:
template: |-
output: {
apiVersion: "core.oam.dev/v1alpha2"
kind: "ManualScalerTrait"
spec: {
replicaCount: parameter.replicas
}
}
parameter: {
//+short=r
//+usage=Replicas of the workload
replicas: *1 | int
}
template: |
output: {
apiVersion: "core.oam.dev/v1alpha2"
kind: "ManualScalerTrait"
spec: {
replicaCount: parameter.replicas
}
}
parameter: {
//+short=r
//+usage=Replicas of the workload
replicas: *1 | int
}
73 changes: 36 additions & 37 deletions charts/vela-core/templates/defwithtemplate/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,39 @@ metadata:
spec:
definitionRef:
name: jobs.batch
extension:
template: |
output: {
apiVersion: "batch/v1"
kind: "Job"
spec: {
parallelism: parameter.count
completions: parameter.count
template: spec: {
restartPolicy: parameter.restart
containers: [{
name: context.name
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
}]
}
}
}
parameter: {
// +usage=specify number of tasks to run in parallel
// +short=c
count: *1 | int
// +usage=Which image would you like to use for your service
// +short=i
image: string
// +usage=Define the job restart policy, the value can only be Never or OnFailure. By default, it's Never.
restart: *"Never" | string
// +usage=Commands to run in the container
cmd?: [...string]
}
template: |
output: {
apiVersion: "batch/v1"
kind: "Job"
spec: {
parallelism: parameter.count
completions: parameter.count
template: spec: {
restartPolicy: parameter.restart
containers: [{
name: context.name
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
}]
}
}
}
parameter: {
// +usage=specify number of tasks to run in parallel
// +short=c
count: *1 | int
// +usage=Which image would you like to use for your service
// +short=i
image: string
// +usage=Define the job restart policy, the value can only be Never or OnFailure. By default, it's Never.
restart: *"Never" | string
// +usage=Commands to run in the container
cmd?: [...string]
}

0 comments on commit 96a589b

Please sign in to comment.