From 34030c91de938a6c6f28559a9154ea2b553e8e35 Mon Sep 17 00:00:00 2001 From: kubevela-bot Date: Mon, 12 Apr 2021 13:24:15 +0800 Subject: [PATCH] add context.appRevisionNum --- docs/en/cue/component.md | 1 + pkg/dsl/process/handle.go | 9 +++++++++ pkg/dsl/process/handle_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/docs/en/cue/component.md b/docs/en/cue/component.md index 81e89c830e3f..2e8f090688b3 100644 --- a/docs/en/cue/component.md +++ b/docs/en/cue/component.md @@ -209,6 +209,7 @@ output: { | Context Variable | Description | | :--: | :---------: | | `context.appRevision` | The revision of the application | +| `context.appRevisionNum` | The revision number(`int` type) of the application, e.g., `context.appRevisionNum` will be `1` if `context.appRevision` is `app-v1`| | `context.appName` | The name of the application | | `context.name` | The name of the component of the application | | `context.namespace` | The namespace of the application | diff --git a/pkg/dsl/process/handle.go b/pkg/dsl/process/handle.go index b4aaf80a671d..529832b45487 100644 --- a/pkg/dsl/process/handle.go +++ b/pkg/dsl/process/handle.go @@ -38,6 +38,8 @@ const ( ContextAppName = "appName" // ContextAppRevision is the revision name of app of context ContextAppRevision = "appRevision" + // ContextAppRevisionNum is the revision num of app of context + ContextAppRevisionNum = "appRevisionNum" // ContextNamespace is the namespace of the app ContextNamespace = "namespace" // OutputSecretName is used to store all secret names which are generated by cloud resource components @@ -128,6 +130,7 @@ func (ctx *templateContext) BaseContextFile() string { buff += fmt.Sprintf(ContextName+": \"%s\"\n", ctx.name) buff += fmt.Sprintf(ContextAppName+": \"%s\"\n", ctx.appName) buff += fmt.Sprintf(ContextAppRevision+": \"%s\"\n", ctx.appRevision) + buff += fmt.Sprintf(ContextAppRevisionNum+": %s\n", extractRevisionNum(ctx.appRevision)) buff += fmt.Sprintf(ContextNamespace+": \"%s\"\n", ctx.namespace) if ctx.base != nil { @@ -223,3 +226,9 @@ func structMarshal(v string) string { } return fmt.Sprintf("{%s}", v) } + +func extractRevisionNum(appRevision string) string { + app := strings.Split(appRevision, "-") + vision := app[len(app)-1] + return strings.Replace(vision, "v", "", 1) +} diff --git a/pkg/dsl/process/handle_test.go b/pkg/dsl/process/handle_test.go index dc2acf83a602..eb6e8ff98402 100644 --- a/pkg/dsl/process/handle_test.go +++ b/pkg/dsl/process/handle_test.go @@ -91,6 +91,10 @@ image: "myserver" assert.Equal(t, nil, err) assert.Equal(t, "myapp-v1", myAppRevision) + myAppRevisionNum, err := ctxInst.Lookup("context", ContextAppRevisionNum).Int64() + assert.Equal(t, nil, err) + assert.Equal(t, int64(1), myAppRevisionNum) + inputJs, err := ctxInst.Lookup("context", OutputFieldName).MarshalJSON() assert.Equal(t, nil, err) assert.Equal(t, `{"image":"myserver"}`, string(inputJs)) @@ -107,3 +111,23 @@ image: "myserver" assert.Equal(t, nil, err) assert.Equal(t, "{\"password\":\"123\"}", string(requiredSecrets)) } + +func TestExtractRevisionNum(t *testing.T) { + testcases := []struct { + appRevision string + wantRevisionNum string + }{{ + appRevision: "myapp-v1", + wantRevisionNum: "1", + }, { + appRevision: "new-app-v2", + wantRevisionNum: "2", + }, { + appRevision: "new-app-v10", + wantRevisionNum: "10", + }} + + for _, tt := range testcases { + assert.Equal(t, tt.wantRevisionNum, extractRevisionNum(tt.appRevision)) + } +}