mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-06 01:17:09 +00:00
add context.appRevisionNum (#1466)
This commit is contained in:
@@ -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 |
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,29 @@ 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: "v1-v10",
|
||||
wantRevisionNum: "10",
|
||||
}, {
|
||||
appRevision: "v10-v1-v1",
|
||||
wantRevisionNum: "1",
|
||||
}, {
|
||||
appRevision: "myapp-v1-v2",
|
||||
wantRevisionNum: "2",
|
||||
}}
|
||||
|
||||
for _, tt := range testcases {
|
||||
assert.Equal(t, tt.wantRevisionNum, extractRevisionNum(tt.appRevision))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user