Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: refactor the cue engine with cuex #162

Merged
merged 17 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import (
"github.com/kubevela/workflow/controllers"
"github.com/kubevela/workflow/pkg/backup"
"github.com/kubevela/workflow/pkg/common"
"github.com/kubevela/workflow/pkg/cue/packages"
"github.com/kubevela/workflow/pkg/features"
"github.com/kubevela/workflow/pkg/monitor/watcher"
"github.com/kubevela/workflow/pkg/types"
Expand Down Expand Up @@ -222,15 +221,6 @@ func main() {
}
}

pd, err := packages.NewPackageDiscover(mgr.GetConfig())
if err != nil {
klog.Error(err, "Failed to create CRD discovery for CUE package client")
if !packages.IsCUEParseErr(err) {
os.Exit(1)
}
}
controllerArgs.PackageDiscover = pd

if useWebhook {
klog.InfoS("Enable webhook", "server port", strconv.Itoa(webhookPort))
webhook.Register(mgr, controllerArgs)
Expand Down
9 changes: 2 additions & 7 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/kubevela/pkg/util/singleton"
"github.com/kubevela/workflow/api/v1alpha1"
"github.com/kubevela/workflow/pkg/cue/packages"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -69,9 +69,6 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

pd, err := packages.NewPackageDiscover(cfg)
Expect(err).To(BeNil())

testScheme = scheme.Scheme
err = v1alpha1.AddToScheme(testScheme)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -81,14 +78,12 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: testScheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
singleton.KubeClient.Set(k8sClient)

reconciler = &WorkflowRunReconciler{
Client: k8sClient,
Scheme: testScheme,
Recorder: event.NewAPIRecorder(recorder),
Args: Args{
PackageDiscover: pd,
},
}

}, 60)
Expand Down
18 changes: 15 additions & 3 deletions controllers/testdata/apply-object.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ metadata:
spec:
schematic:
cue:
template: "import (\n\t\"vela/op\"\n)\n\napply: op.#Apply & {\n\tvalue: parameter.value\n\tcluster:
parameter.cluster\n}\nparameter: {\n\t// +usage=Specify the value of the object\n\tvalue:
{...}\n\t// +usage=Specify the cluster of the object\n\tcluster: *\"\" | string\n}\n"
template: |
import (
"vela/op"
)

apply: op.#Apply & {
value: parameter.value
cluster: parameter.cluster
}
parameter: {
// +usage=Specify the value of the object
value: {...}
// +usage=Specify the cluster of the object
cluster: *"" | string
}
54 changes: 43 additions & 11 deletions controllers/testdata/test-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,46 @@ metadata:
spec:
schematic:
cue:
template: "import (\t\"vela/op\"\n)\n\noutput: op.#Apply & {\n\tvalue: {\n\t\tapiVersion:
\"apps/v1\"\n\t\tkind: \"Deployment\"\n\t\tmetadata: {\n\t\t\tname:
\ context.stepName\n\t\t\tnamespace: context.namespace\n\t\t}\n\t\tspec:
{\n\t\t\tselector: matchLabels: wr: context.stepName\n\t\t\ttemplate: {\n\t\t\t\tmetadata:
labels: wr: context.stepName\n\t\t\t\tspec: containers: [{\n\t\t\t\t\tname:
\ context.stepName\n\t\t\t\t\timage: parameter.image\n\t\t\t\t\tif parameter[\"cmd\"]
!= _|_ {\n\t\t\t\t\t\tcommand: parameter.cmd\n\t\t\t\t\t}\n\t\t\t\t\tif parameter[\"message\"]
!= _|_ {\n\t\t\t\t\t\tenv: [{\n\t\t\t\t\t\t\tname: \"MESSAGE\"\n\t\t\t\t\t\t\tvalue:
parameter.message\n\t\t\t\t\t\t}]\n\t\t\t\t\t}\n\t\t\t\t}]\n\t\t\t}\n\t\t}\n\t}\n}\nwait:
op.#ConditionalWait & {\n\tcontinue: output.value.status.readyReplicas ==
1\n}\nparameter: {\n\timage: string\n\tcmd?: [...string]\n\tmessage?: string\n}\n"
template: |
FogDong marked this conversation as resolved.
Show resolved Hide resolved
import ( "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
}]
}
}]
}
}
}
}
wait: op.#ConditionalWait & {
if len(output.value.status) > 0 if output.value.status.readyReplicas == 1 {
continue: true
}
}
parameter: {
image: string
cmd?: [...string]
message?: string
}
10 changes: 9 additions & 1 deletion controllers/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,19 @@ var _ = Describe("Test Workflow", func() {
checkRun := &v1alpha1.WorkflowRun{}
Expect(k8sClient.Get(ctx, wrKey, checkRun)).Should(BeNil())

for i := 0; i < wfTypes.MaxWorkflowStepErrorRetryTimes; i++ {
tryReconcile(reconciler, wr.Name, wr.Namespace)
Expect(k8sClient.Get(ctx, wrKey, checkRun)).Should(BeNil())
Expect(checkRun.Status.Message).Should(BeEquivalentTo(""))
Expect(checkRun.Status.Phase).Should(BeEquivalentTo(v1alpha1.WorkflowStateExecuting))
Expect(checkRun.Status.Steps[0].Phase).Should(BeEquivalentTo(v1alpha1.WorkflowStepPhaseFailed))
}

tryReconcile(reconciler, wr.Name, wr.Namespace)
Expect(k8sClient.Get(ctx, wrKey, checkRun)).Should(BeNil())
Expect(checkRun.Status.Phase).Should(BeEquivalentTo(v1alpha1.WorkflowStateFailed))
Expect(checkRun.Status.Steps[0].Phase).Should(BeEquivalentTo(v1alpha1.WorkflowStepPhaseFailed))
Expect(checkRun.Status.Steps[0].Reason).Should(BeEquivalentTo(wfTypes.StatusReasonRendering))
Expect(checkRun.Status.Steps[0].Reason).Should(BeEquivalentTo(wfTypes.StatusReasonFailedAfterRetries))
Expect(checkRun.Status.Steps[1].Phase).Should(BeEquivalentTo(v1alpha1.WorkflowStepPhaseSkipped))
})

Expand Down
15 changes: 7 additions & 8 deletions controllers/workflowrun_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ import (
"github.com/kubevela/workflow/api/condition"
"github.com/kubevela/workflow/api/v1alpha1"
wfContext "github.com/kubevela/workflow/pkg/context"
"github.com/kubevela/workflow/pkg/cue/packages"
"github.com/kubevela/workflow/pkg/executor"
"github.com/kubevela/workflow/pkg/features"
"github.com/kubevela/workflow/pkg/generator"
"github.com/kubevela/workflow/pkg/monitor/metrics"
providertypes "github.com/kubevela/workflow/pkg/providers/types"
"github.com/kubevela/workflow/pkg/types"
)

Expand All @@ -58,8 +58,6 @@ type Args struct {
ConcurrentReconciles int
// IgnoreWorkflowWithoutControllerRequirement indicates that workflow controller will not process the workflowrun without 'workflowrun.oam.dev/controller-version-require' annotation.
IgnoreWorkflowWithoutControllerRequirement bool
// PackageDiscover discover the packages
PackageDiscover *packages.PackageDiscover
}

// WorkflowRunReconciler reconciles a WorkflowRun object
Expand Down Expand Up @@ -90,6 +88,10 @@ func (r *WorkflowRunReconciler) Reconcile(ctx context.Context, req ctrl.Request)
defer cancel()

ctx = types.SetNamespaceInCtx(ctx, req.Namespace)
ctx = providertypes.WithLabelParams(ctx, map[string]string{
types.LabelWorkflowRunName: req.Name,
types.LabelWorkflowRunNamespace: req.Namespace,
})

logCtx := monitorContext.NewTraceContext(ctx, "").AddTag("workflowrun", req.String())
logCtx.Info("Start reconcile workflowrun")
Expand Down Expand Up @@ -128,10 +130,7 @@ func (r *WorkflowRunReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
isUpdate := instance.Status.Message != ""

runners, err := generator.GenerateRunners(logCtx, instance, types.StepGeneratorOptions{
PackageDiscover: r.PackageDiscover,
Client: r.Client,
})
runners, err := generator.GenerateRunners(logCtx, instance, types.StepGeneratorOptions{})
if err != nil {
logCtx.Error(err, "[generate runners]")
r.Recorder.Event(run, event.Warning(v1alpha1.ReasonGenerate, errors.WithMessage(err, v1alpha1.MessageFailedGenerate)))
Expand All @@ -143,7 +142,7 @@ func (r *WorkflowRunReconciler) Reconcile(ctx context.Context, req ctrl.Request)
Client: r.Client,
run: run,
}
executor := executor.New(instance, r.Client, patcher.patchStatus)
executor := executor.New(instance, executor.WithStatusPatcher(patcher.patchStatus))
state, err := executor.ExecuteRunners(logCtx, runners)
if err != nil {
logCtx.Error(err, "[execute runners]")
Expand Down
3 changes: 3 additions & 0 deletions e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/kubevela/pkg/util/singleton"
"github.com/kubevela/pkg/util/test/definition"

"github.com/kubevela/workflow/api/v1alpha1"
Expand Down Expand Up @@ -58,9 +59,11 @@ var k8sClient client.Client
var _ = BeforeSuite(func() {
conf, err := config.GetConfig()
Expect(err).Should(BeNil())
singleton.KubeConfig.Set(conf)

k8sClient, err = client.New(conf, client.Options{Scheme: scheme})
Expect(err).Should(BeNil())
singleton.KubeClient.Set(k8sClient)

prepareWorkflowDefinitions()
})
Expand Down
Loading
Loading