Skip to content

Commit

Permalink
feat: add new providers
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <[email protected]>
  • Loading branch information
FogDong committed Aug 13, 2024
1 parent 7d94489 commit c96e6a4
Show file tree
Hide file tree
Showing 33 changed files with 3,473 additions and 85 deletions.
81 changes: 81 additions & 0 deletions charts/vela-workflow/crds/cue.oam.dev_packages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.3
creationTimestamp: null
name: packages.cue.oam.dev
spec:
group: cue.oam.dev
names:
kind: Package
listKind: PackageList
plural: packages
shortNames:
- pkg
- cpkg
- cuepkg
- cuepackage
singular: package
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.path
name: PATH
type: string
- jsonPath: .spec.provider.protocol
name: PROTO
type: string
- jsonPath: .spec.provider.endpoint
name: ENDPOINT
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: Package is an extension for cuex engine
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: PackageSpec the spec for Package
properties:
path:
type: string
provider:
description: Provider the external Provider in Package for cuex to
run functions
properties:
endpoint:
type: string
protocol:
description: ProviderProtocol the protocol type for external Provider
type: string
required:
- endpoint
- protocol
type: object
templates:
additionalProperties:
type: string
type: object
required:
- path
- templates
type: object
required:
- spec
type: object
served: true
storage: true
subresources: {}
20 changes: 11 additions & 9 deletions controllers/testdata/apply-object.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ spec:
cue:
template: |
import (
"vela/op"
"vela/kube"
)
apply: op.#Apply & {
value: parameter.value
cluster: parameter.cluster
apply: kube.#Apply & {
$params: {
value: parameter.value
cluster: parameter.cluster
}
}
parameter: {
// +usage=Specify the value of the object
value: {...}
// +usage=Specify the cluster of the object
cluster: *"" | string
}
// +usage=Specify Kubernetes native resource object to be applied
value: {...}
// +usage=The cluster you want to apply the resource to, default is the current control plane cluster
cluster: *"" | string
}
51 changes: 26 additions & 25 deletions controllers/testdata/suspend-and-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,44 @@ spec:
cue:
template: |
import (
"strconv"
"strings"
"vela/kube"
"vela/op"
)
suspend: op.#Suspend & {duration: "1s"}
output: op.#Apply & {
cluster: parameter.cluster
value: {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: {
name: context.stepName
namespace: context.namespace
}
spec: {
selector: matchLabels: "workflow.oam.dev/step-name": "\(context.name)-\(context.stepName)"
replicas: parameter.replicas
template: {
metadata: labels: "workflow.oam.dev/step-name": "\(context.name)-\(context.stepName)"
spec: containers: [{
name: context.stepName
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
}]
output: kube.#Apply & {
$params: {
cluster: parameter.cluster
value: {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: {
name: context.stepName
namespace: context.namespace
}
spec: {
selector: matchLabels: "workflow.oam.dev/step-name": "\(context.name)-\(context.stepName)"
replicas: parameter.replicas
template: {
metadata: labels: "workflow.oam.dev/step-name": "\(context.name)-\(context.stepName)"
spec: containers: [{
name: context.stepName
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
}]
}
}
}
}
}
wait: op.#ConditionalWait & {
continue: output.value.status.readyReplicas == parameter.replicas
continue: output.$returns.value.status.readyReplicas == parameter.replicas
}
parameter: {
image: string
replicas: *1 | int
cluster: *"" | string
cmd?: [...string]
}
75 changes: 39 additions & 36 deletions controllers/testdata/test-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,48 @@ spec:
schematic:
cue:
template: |
import ( "vela/op"
import (
"vela/kube"
"vela/op"
)
output: op.#Apply & {
value: {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: {
name: context.stepName
namespace: context.namespace
}
spec: {
selector: matchLabels: wr: context.stepName
template: {
metadata: labels: wr: context.stepName
spec: containers: [{
name: context.stepName
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
if parameter["message"] != _|_ {
env: [{
name: "MESSAGE"
value: parameter.message
}]
}
}]
}
}
}
output: kube.#Apply & {
$params: value: {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: {
name: context.stepName
namespace: context.namespace
}
spec: {
selector: matchLabels: wr: context.stepName
template: {
metadata: labels: wr: context.stepName
spec: containers: [{
name: context.stepName
image: parameter.image
if parameter["cmd"] != _|_ {
command: parameter.cmd
}
if parameter["message"] != _|_ {
env: [{
name: "MESSAGE"
value: parameter.message
}]
}
}]
}
}
}
}
wait: op.#ConditionalWait & {
if len(output.value.status) > 0 if output.value.status.readyReplicas == 1 {
continue: true
}
if len(output.$returns.value.status) > 0 if output.$returns.value.status.readyReplicas == 1 {
continue: true
}
}
parameter: {
image: string
cmd?: [...string]
message?: string
}
image: string
cmd?: [...string]
message?: string
}
4 changes: 2 additions & 2 deletions controllers/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ var _ = Describe("Test Workflow", func() {
Outputs: v1alpha1.StepOutputs{
{
Name: "message",
ValueFrom: `"message: " +output.value.status.conditions[0].message`,
ValueFrom: `"message: " +output.$returns.value.status.conditions[0].message`,
},
},
},
Expand Down Expand Up @@ -416,7 +416,7 @@ var _ = Describe("Test Workflow", func() {
Outputs: v1alpha1.StepOutputs{
{
Name: "message",
ValueFrom: `"message: " +output.value.status.conditions[0].message`,
ValueFrom: `"message: " +output.$returns.value.status.conditions[0].message`,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ func (tr *testTaskRunner) Run(ctx wfContext.Context, options *types.TaskRunOptio
resetter := tr.fillContext(logCtx, options.PCtx)
defer resetter(options.PCtx)

basicVal, err := custom.MakeBasicValue(logCtx, providers.Compiler.Get(), nil, options.PCtx)
basicVal, err := custom.MakeBasicValue(logCtx, providers.DefaultCompiler.Get(), nil, options.PCtx)
if err != nil {
return v1alpha1.StepStatus{}, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func initStepGeneratorOptions(_ monitorContext.Context, instance *types.Workflow
options.TemplateLoader = template.NewWorkflowStepTemplateLoader()
}
if options.Compiler == nil {
options.Compiler = providers.Compiler.Get()
options.Compiler = providers.DefaultCompiler.Get()
}
return options
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/hooks/data_passing.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ func Input(ctx wfContext.Context, paramValue cue.Value, step v1alpha1.WorkflowSt
}
}
if input.ParameterKey != "" {
fmt.Println("===filledVal", filledVal)
fmt.Println("===inputValue", inputValue)
fmt.Println("====path", strings.Join([]string{"parameter", input.ParameterKey}, "."))
filledVal, err = value.SetValueByScript(filledVal, inputValue, strings.Join([]string{"parameter", input.ParameterKey}, "."))
if err != nil || filledVal.Err() != nil {
if err != nil {
Expand Down
42 changes: 40 additions & 2 deletions pkg/providers/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,61 @@ limitations under the License.
package providers

import (
"context"

"github.com/kubevela/pkg/cue/cuex"
cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime"
"github.com/kubevela/pkg/util/runtime"
"github.com/kubevela/pkg/util/singleton"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"

"github.com/kubevela/workflow/pkg/providers/email"
"github.com/kubevela/workflow/pkg/providers/http"
"github.com/kubevela/workflow/pkg/providers/kube"
"github.com/kubevela/workflow/pkg/providers/legacy"
"github.com/kubevela/workflow/pkg/providers/metrics"
"github.com/kubevela/workflow/pkg/providers/time"
"github.com/kubevela/workflow/pkg/providers/util"
)

const (
// LegacyProviderName is the name of legacy provider
LegacyProviderName = "op"
)

// Compiler is the workflow default compiler
var Compiler = singleton.NewSingletonE[*cuex.Compiler](func() (*cuex.Compiler, error) {
var (
// EnableExternalPackageForDefaultCompiler .
EnableExternalPackageForDefaultCompiler = true
// EnableExternalPackageWatchForDefaultCompiler .
EnableExternalPackageWatchForDefaultCompiler = false
)

var compiler = singleton.NewSingletonE[*cuex.Compiler](func() (*cuex.Compiler, error) {
return cuex.NewCompilerWithInternalPackages(
// legacy packages
runtime.Must(cuexruntime.NewInternalPackage(LegacyProviderName, legacy.GetLegacyTemplate(), legacy.GetLegacyProviders())),

// internal packages
runtime.Must(cuexruntime.NewInternalPackage("email", email.GetTemplate(), email.GetProviders())),
runtime.Must(cuexruntime.NewInternalPackage("http", http.GetTemplate(), http.GetProviders())),
runtime.Must(cuexruntime.NewInternalPackage("kube", kube.GetTemplate(), kube.GetProviders())),
runtime.Must(cuexruntime.NewInternalPackage("metrics", metrics.GetTemplate(), metrics.GetProviders())),
runtime.Must(cuexruntime.NewInternalPackage("time", time.GetTemplate(), time.GetProviders())),
runtime.Must(cuexruntime.NewInternalPackage("util", util.GetTemplate(), util.GetProviders())),
), nil
})

// DefaultCompiler compiler for cuex to compile
var DefaultCompiler = singleton.NewSingleton[*cuex.Compiler](func() *cuex.Compiler {
c := compiler.Get()
if EnableExternalPackageForDefaultCompiler {
if err := c.LoadExternalPackages(context.Background()); err != nil && !kerrors.IsNotFound(err) {
klog.Errorf("failed to load external packages for cuex default compiler: %s", err.Error())
}
}
if EnableExternalPackageWatchForDefaultCompiler {
go c.ListenExternalPackages(nil)
}
return c
})
Loading

0 comments on commit c96e6a4

Please sign in to comment.