From 855cbfe3ec26a28367e41ac89eadc48ed60ccc67 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 3 Nov 2021 19:10:18 +0800 Subject: [PATCH] [Backport release-1.1] Feat: store workflow step def properties in cm (#2614) * Fix: fix notification def (cherry picked from commit f35a21304889d51e0668786b2f22baf95613ab1b) * Feat: store workflow step def properties in cm (cherry picked from commit 7f3902536c7fdb0466cff4431bcf56d85d68f4e1) * fix ci (cherry picked from commit a252749f773b1bcb52fb4bf2adb8e7eb2742ed04) * fix data race (cherry picked from commit 0b55ce8386eaa3164681020f3feceeac0339bb86) Co-authored-by: FogDong --- .../v1beta1/workflow_step_definition.go | 3 +- .../core.oam.dev_applicationrevisions.yaml | 4 ++ .../core.oam.dev_definitionrevisions.yaml | 4 ++ .../core.oam.dev_workflowstepdefinitions.yaml | 4 ++ .../defwithtemplate/webhook-notification.yaml | 18 ++--- .../core.oam.dev_applicationrevisions.yaml | 4 ++ .../core.oam.dev_definitionrevisions.yaml | 4 ++ .../core.oam.dev_workflowstepdefinitions.yaml | 4 ++ .../defwithtemplate/webhook-notification.yaml | 18 ++--- .../core.oam.dev_applicationrevisions.yaml | 4 ++ .../core.oam.dev_definitionrevisions.yaml | 4 ++ .../core.oam.dev_workflowstepdefinitions.yaml | 4 ++ .../workflowstepdefinition_controller.go | 22 ++++++ pkg/controller/utils/capability.go | 71 ++++++++++++++++++- pkg/cue/packages/package.go | 5 ++ pkg/oam/util/helper.go | 2 + pkg/stdlib/packages.go | 5 +- pkg/stdlib/pkgs/slack.cue | 18 ++--- .../internal/webhook-notification.cue | 18 ++--- 19 files changed, 175 insertions(+), 41 deletions(-) diff --git a/apis/core.oam.dev/v1beta1/workflow_step_definition.go b/apis/core.oam.dev/v1beta1/workflow_step_definition.go index a2518fd72..10aac8254 100644 --- a/apis/core.oam.dev/v1beta1/workflow_step_definition.go +++ b/apis/core.oam.dev/v1beta1/workflow_step_definition.go @@ -38,7 +38,8 @@ type WorkflowStepDefinitionSpec struct { type WorkflowStepDefinitionStatus struct { // ConditionedStatus reflects the observed status of a resource condition.ConditionedStatus `json:",inline"` - + // ConfigMapRef refer to a ConfigMap which contains OpenAPI V3 JSON schema of Component parameters. + ConfigMapRef string `json:"configMapRef,omitempty"` // LatestRevision of the component definition // +optional LatestRevision *common.Revision `json:"latestRevision,omitempty"` diff --git a/charts/vela-core/crds/core.oam.dev_applicationrevisions.yaml b/charts/vela-core/crds/core.oam.dev_applicationrevisions.yaml index 028b2b53e..dbc74dd46 100644 --- a/charts/vela-core/crds/core.oam.dev_applicationrevisions.yaml +++ b/charts/vela-core/crds/core.oam.dev_applicationrevisions.yaml @@ -4399,6 +4399,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains + OpenAPI V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/charts/vela-core/crds/core.oam.dev_definitionrevisions.yaml b/charts/vela-core/crds/core.oam.dev_definitionrevisions.yaml index d09f351d3..ce9d540dd 100644 --- a/charts/vela-core/crds/core.oam.dev_definitionrevisions.yaml +++ b/charts/vela-core/crds/core.oam.dev_definitionrevisions.yaml @@ -1123,6 +1123,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains + OpenAPI V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/charts/vela-core/crds/core.oam.dev_workflowstepdefinitions.yaml b/charts/vela-core/crds/core.oam.dev_workflowstepdefinitions.yaml index 880959871..c6a0d4353 100644 --- a/charts/vela-core/crds/core.oam.dev_workflowstepdefinitions.yaml +++ b/charts/vela-core/crds/core.oam.dev_workflowstepdefinitions.yaml @@ -216,6 +216,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains OpenAPI + V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/charts/vela-core/templates/defwithtemplate/webhook-notification.yaml b/charts/vela-core/templates/defwithtemplate/webhook-notification.yaml index bf6496d74..9929238a6 100644 --- a/charts/vela-core/templates/defwithtemplate/webhook-notification.yaml +++ b/charts/vela-core/templates/defwithtemplate/webhook-notification.yaml @@ -97,17 +97,17 @@ spec: url?: string value?: string style?: string - text?: text + text?: textType confirm?: { - title: text - text: text - confirm: text - deny: text + title: textType + text: textType + confirm: textType + deny: textType style?: string } options?: [...option] initial_options?: [...option] - placeholder?: text + placeholder?: textType initial_date?: string image_url?: string alt_text?: string @@ -121,16 +121,16 @@ spec: initial_time?: string }] } - text: { + textType: { type: string text: string emoji?: bool verbatim?: bool } option: { - text: text + text: textType value: string - description?: text + description?: textType url?: string } // send webhook notification diff --git a/charts/vela-minimal/crds/core.oam.dev_applicationrevisions.yaml b/charts/vela-minimal/crds/core.oam.dev_applicationrevisions.yaml index 028b2b53e..dbc74dd46 100644 --- a/charts/vela-minimal/crds/core.oam.dev_applicationrevisions.yaml +++ b/charts/vela-minimal/crds/core.oam.dev_applicationrevisions.yaml @@ -4399,6 +4399,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains + OpenAPI V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/charts/vela-minimal/crds/core.oam.dev_definitionrevisions.yaml b/charts/vela-minimal/crds/core.oam.dev_definitionrevisions.yaml index d09f351d3..ce9d540dd 100644 --- a/charts/vela-minimal/crds/core.oam.dev_definitionrevisions.yaml +++ b/charts/vela-minimal/crds/core.oam.dev_definitionrevisions.yaml @@ -1123,6 +1123,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains + OpenAPI V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/charts/vela-minimal/crds/core.oam.dev_workflowstepdefinitions.yaml b/charts/vela-minimal/crds/core.oam.dev_workflowstepdefinitions.yaml index 880959871..c6a0d4353 100644 --- a/charts/vela-minimal/crds/core.oam.dev_workflowstepdefinitions.yaml +++ b/charts/vela-minimal/crds/core.oam.dev_workflowstepdefinitions.yaml @@ -216,6 +216,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains OpenAPI + V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/charts/vela-minimal/templates/defwithtemplate/webhook-notification.yaml b/charts/vela-minimal/templates/defwithtemplate/webhook-notification.yaml index bf6496d74..9929238a6 100644 --- a/charts/vela-minimal/templates/defwithtemplate/webhook-notification.yaml +++ b/charts/vela-minimal/templates/defwithtemplate/webhook-notification.yaml @@ -97,17 +97,17 @@ spec: url?: string value?: string style?: string - text?: text + text?: textType confirm?: { - title: text - text: text - confirm: text - deny: text + title: textType + text: textType + confirm: textType + deny: textType style?: string } options?: [...option] initial_options?: [...option] - placeholder?: text + placeholder?: textType initial_date?: string image_url?: string alt_text?: string @@ -121,16 +121,16 @@ spec: initial_time?: string }] } - text: { + textType: { type: string text: string emoji?: bool verbatim?: bool } option: { - text: text + text: textType value: string - description?: text + description?: textType url?: string } // send webhook notification diff --git a/legacy/charts/vela-core-legacy/crds/core.oam.dev_applicationrevisions.yaml b/legacy/charts/vela-core-legacy/crds/core.oam.dev_applicationrevisions.yaml index 79b8b8b03..359245ee1 100644 --- a/legacy/charts/vela-core-legacy/crds/core.oam.dev_applicationrevisions.yaml +++ b/legacy/charts/vela-core-legacy/crds/core.oam.dev_applicationrevisions.yaml @@ -4399,6 +4399,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains + OpenAPI V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/legacy/charts/vela-core-legacy/crds/core.oam.dev_definitionrevisions.yaml b/legacy/charts/vela-core-legacy/crds/core.oam.dev_definitionrevisions.yaml index 8ffc3eaac..f723729fd 100644 --- a/legacy/charts/vela-core-legacy/crds/core.oam.dev_definitionrevisions.yaml +++ b/legacy/charts/vela-core-legacy/crds/core.oam.dev_definitionrevisions.yaml @@ -1123,6 +1123,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains + OpenAPI V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/legacy/charts/vela-core-legacy/crds/core.oam.dev_workflowstepdefinitions.yaml b/legacy/charts/vela-core-legacy/crds/core.oam.dev_workflowstepdefinitions.yaml index 3d2de420f..9303ba2d9 100644 --- a/legacy/charts/vela-core-legacy/crds/core.oam.dev_workflowstepdefinitions.yaml +++ b/legacy/charts/vela-core-legacy/crds/core.oam.dev_workflowstepdefinitions.yaml @@ -216,6 +216,10 @@ spec: - type type: object type: array + configMapRef: + description: ConfigMapRef refer to a ConfigMap which contains OpenAPI + V3 JSON schema of Component parameters. + type: string latestRevision: description: LatestRevision of the component definition properties: diff --git a/pkg/controller/core.oam.dev/v1alpha2/core/workflow/workflowstepdefinition/workflowstepdefinition_controller.go b/pkg/controller/core.oam.dev/v1alpha2/core/workflow/workflowstepdefinition/workflowstepdefinition_controller.go index 66068e266..165725d42 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/core/workflow/workflowstepdefinition/workflowstepdefinition_controller.go +++ b/pkg/controller/core.oam.dev/v1alpha2/core/workflow/workflowstepdefinition/workflowstepdefinition_controller.go @@ -122,6 +122,28 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu r.record.Event(&wfstepdefinition, event.Warning("failed to garbage collect DefinitionRevision of type WorkflowStepDefinition", err)) } + def := utils.NewCapabilityStepDef(&wfstepdefinition) + def.Name = req.NamespacedName.Name + // Store the parameter of stepDefinition to configMap + cmName, err := def.StoreOpenAPISchema(ctx, r.Client, r.pd, req.Namespace, req.Name, defRev.Name) + if err != nil { + klog.InfoS("Could not store capability in ConfigMap", "err", err) + r.record.Event(&(wfstepdefinition), event.Warning("Could not store capability in ConfigMap", err)) + return ctrl.Result{}, util.PatchCondition(ctx, r, &wfstepdefinition, + condition.ReconcileError(fmt.Errorf(util.ErrStoreCapabilityInConfigMap, wfstepdefinition.Name, err))) + } + + if wfstepdefinition.Status.ConfigMapRef != cmName { + wfstepdefinition.Status.ConfigMapRef = cmName + if err := r.UpdateStatus(ctx, &wfstepdefinition); err != nil { + klog.ErrorS(err, "Could not update WorkflowStepDefinition Status", "workflowStepDefinition", klog.KRef(req.Namespace, req.Name)) + r.record.Event(&wfstepdefinition, event.Warning("Could not update WorkflowStepDefinition Status", err)) + return ctrl.Result{}, util.PatchCondition(ctx, r, &wfstepdefinition, + condition.ReconcileError(fmt.Errorf(util.ErrUpdateWorkflowStepDefinition, wfstepdefinition.Name, err))) + } + klog.InfoS("Successfully updated the status.configMapRef of the WorkflowStepDefinition", "workflowStepDefinition", + klog.KRef(req.Namespace, req.Name), "status.configMapRef", cmName) + } return ctrl.Result{}, nil } diff --git a/pkg/controller/utils/capability.go b/pkg/controller/utils/capability.go index 7b31e2992..81d56cd29 100644 --- a/pkg/controller/utils/capability.go +++ b/pkg/controller/utils/capability.go @@ -351,6 +351,75 @@ func (def *CapabilityTraitDefinition) StoreOpenAPISchema(ctx context.Context, k8 return cmName, nil } +// CapabilityStepDefinition is the Capability struct for WorkflowStepDefinition +type CapabilityStepDefinition struct { + Name string `json:"name"` + StepDefinition v1beta1.WorkflowStepDefinition `json:"stepDefinition"` + + CapabilityBaseDefinition +} + +// NewCapabilityStepDef will create a CapabilityStepDefinition +func NewCapabilityStepDef(stepdefinition *v1beta1.WorkflowStepDefinition) CapabilityStepDefinition { + var def CapabilityStepDefinition + def.Name = stepdefinition.Name + def.StepDefinition = *stepdefinition.DeepCopy() + return def +} + +// GetOpenAPISchema gets OpenAPI v3 schema by StepDefinition name +func (def *CapabilityStepDefinition) GetOpenAPISchema(pd *packages.PackageDiscover, name string) ([]byte, error) { + capability, err := appfile.ConvertTemplateJSON2Object(name, nil, def.StepDefinition.Spec.Schematic) + if err != nil { + return nil, fmt.Errorf("failed to convert WorkflowStepDefinition to Capability Object") + } + return getOpenAPISchema(capability, pd) +} + +// StoreOpenAPISchema stores OpenAPI v3 schema from StepDefinition in ConfigMap +func (def *CapabilityStepDefinition) StoreOpenAPISchema(ctx context.Context, k8sClient client.Client, pd *packages.PackageDiscover, namespace, name string, revName string) (string, error) { + var jsonSchema []byte + var err error + + jsonSchema, err = def.GetOpenAPISchema(pd, name) + if err != nil { + return "", fmt.Errorf("failed to generate OpenAPI v3 JSON schema for capability %s: %w", def.Name, err) + } + + stepDefinition := def.StepDefinition + ownerReference := []metav1.OwnerReference{{ + APIVersion: stepDefinition.APIVersion, + Kind: stepDefinition.Kind, + Name: stepDefinition.Name, + UID: stepDefinition.GetUID(), + Controller: pointer.BoolPtr(true), + BlockOwnerDeletion: pointer.BoolPtr(true), + }} + cmName, err := def.CreateOrUpdateConfigMap(ctx, k8sClient, namespace, stepDefinition.Name, jsonSchema, ownerReference) + if err != nil { + return cmName, err + } + + // Create a configmap to store parameter for each definitionRevision + defRev := new(v1beta1.DefinitionRevision) + if err = k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: revName}, defRev); err != nil { + return "", err + } + ownerReference = []metav1.OwnerReference{{ + APIVersion: defRev.APIVersion, + Kind: defRev.Kind, + Name: defRev.Name, + UID: defRev.GetUID(), + Controller: pointer.BoolPtr(true), + BlockOwnerDeletion: pointer.BoolPtr(true), + }} + _, err = def.CreateOrUpdateConfigMap(ctx, k8sClient, namespace, revName, jsonSchema, ownerReference) + if err != nil { + return cmName, err + } + return cmName, nil +} + // CapabilityBaseDefinition is the base struct for CapabilityWorkloadDefinition and CapabilityTraitDefinition type CapabilityBaseDefinition struct { } @@ -399,7 +468,7 @@ func (def *CapabilityBaseDefinition) CreateOrUpdateConfigMap(ctx context.Context return cmName, nil } -// getDefinition is the main function for GetDefinition API +// getOpenAPISchema is the main function for GetDefinition API func getOpenAPISchema(capability types.Capability, pd *packages.PackageDiscover) ([]byte, error) { openAPISchema, err := generateOpenAPISchemaFromCapabilityParameter(capability, pd) if err != nil { diff --git a/pkg/cue/packages/package.go b/pkg/cue/packages/package.go index 93b281617..bddc1dd04 100644 --- a/pkg/cue/packages/package.go +++ b/pkg/cue/packages/package.go @@ -35,6 +35,8 @@ import ( "k8s.io/apimachinery/pkg/runtime/serializer" clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" + + "github.com/oam-dev/kubevela/pkg/stdlib" ) const ( @@ -107,6 +109,9 @@ func (pd *PackageDiscover) ImportBuiltinPackagesFor(bi *build.Instance) { // ImportPackagesAndBuildInstance Combine import built-in packages and build cue template together to avoid data race func (pd *PackageDiscover) ImportPackagesAndBuildInstance(bi *build.Instance) (inst *cue.Instance, err error) { pd.ImportBuiltinPackagesFor(bi) + if err := stdlib.AddImportsFor(bi, ""); err != nil { + return nil, err + } var r cue.Runtime pd.mutex.Lock() defer pd.mutex.Unlock() diff --git a/pkg/oam/util/helper.go b/pkg/oam/util/helper.go index 226578ec0..8dea9e04d 100644 --- a/pkg/oam/util/helper.go +++ b/pkg/oam/util/helper.go @@ -100,6 +100,8 @@ const ( ErrUpdateComponentDefinition = "cannot update ComponentDefinition %s: %v" // ErrUpdateTraitDefinition is the error while update TraitDefinition ErrUpdateTraitDefinition = "cannot update TraitDefinition %s: %v" + // ErrUpdateStepDefinition is the error while update WorkflowStepDefinition + ErrUpdateStepDefinition = "cannot update WorkflowStepDefinition %s: %v" // ErrUpdatePolicyDefinition is the error while update PolicyDefinition ErrUpdatePolicyDefinition = "cannot update PolicyDefinition %s: %v" // ErrUpdateWorkflowStepDefinition is the error while update WorkflowStepDefinition diff --git a/pkg/stdlib/packages.go b/pkg/stdlib/packages.go index 1a8e5ddd8..9a2ed616f 100644 --- a/pkg/stdlib/packages.go +++ b/pkg/stdlib/packages.go @@ -27,8 +27,7 @@ import ( var ( //go:embed pkgs op.cue - fs embed.FS - pkgContent string + fs embed.FS ) // GetPackages Get Stdlib packages @@ -44,7 +43,7 @@ func GetPackages(tagTempl string) (map[string]string, error) { return nil, err } - pkgContent = string(opBytes) + "\n" + pkgContent := string(opBytes) + "\n" for _, file := range files { body, err := fs.ReadFile("pkgs/" + file.Name()) if err != nil { diff --git a/pkg/stdlib/pkgs/slack.cue b/pkg/stdlib/pkgs/slack.cue index a903ed612..ee255772b 100644 --- a/pkg/stdlib/pkgs/slack.cue +++ b/pkg/stdlib/pkgs/slack.cue @@ -18,17 +18,17 @@ url?: string value?: string style?: string - text?: #text + text?: #textType confirm?: { - title: #text - text: #text - confirm: #text - deny: #text + title: #textType + text: #textType + confirm: #textType + deny: #textType style?: string } options?: [...#option] initial_options?: [...#option] - placeholder?: #text + placeholder?: #textType initial_date?: string image_url?: string alt_text?: string @@ -45,7 +45,7 @@ }] } -#text: { +#textType: { type: string text: string emoji?: bool @@ -53,8 +53,8 @@ } #option: { - text: text + text: #textType value: string - description?: text + description?: #textType url?: string } diff --git a/vela-templates/definitions/internal/webhook-notification.cue b/vela-templates/definitions/internal/webhook-notification.cue index da90790e5..ecc154441 100644 --- a/vela-templates/definitions/internal/webhook-notification.cue +++ b/vela-templates/definitions/internal/webhook-notification.cue @@ -93,17 +93,17 @@ template: { url?: string value?: string style?: string - text?: text + text?: textType confirm?: { - title: text - text: text - confirm: text - deny: text + title: textType + text: textType + confirm: textType + deny: textType style?: string } options?: [...option] initial_options?: [...option] - placeholder?: text + placeholder?: textType initial_date?: string image_url?: string alt_text?: string @@ -120,7 +120,7 @@ template: { }] } - text: { + textType: { type: string text: string emoji?: bool @@ -128,9 +128,9 @@ template: { } option: { - text: text + text: textType value: string - description?: text + description?: textType url?: string }