Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 7265009

Browse files
Add a docker app version to the bundle custom section
Signed-off-by: Silvin Lubecki <[email protected]>
1 parent e18898a commit 7265009

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

internal/packager/cnab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func ToCNAB(app *types.App, invocationImageName string) (*bundle.Bundle, error)
172172
SchemaVersion: CNABVersion1_0_0,
173173
Custom: map[string]interface{}{
174174
internal.CustomDockerAppName: DockerAppCustom{
175-
Version: DockerAppCustomVersionCurrent,
175+
Version: DockerAppPayloadVersionCurrent,
176176
Payload: payload,
177177
},
178178
},

internal/packager/cnab_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"regexp"
77
"testing"
88

9+
"github.com/docker/app/internal"
10+
911
"github.com/docker/app/types"
1012
"gotest.tools/assert"
1113
"gotest.tools/golden"
@@ -20,7 +22,7 @@ func TestToCNAB(t *testing.T) {
2022
assert.NilError(t, err)
2123
s := golden.Get(t, "bundle-json.golden")
2224
expectedLiteral := regexp.QuoteMeta(string(s))
23-
expected := fmt.Sprintf(expectedLiteral, DockerAppCustomVersionCurrent, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z`)
25+
expected := fmt.Sprintf(expectedLiteral, DockerAppPayloadVersionCurrent, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z`, internal.Version)
2426
matches, err := regexp.Match(expected, actualJSON)
2527
assert.NilError(t, err)
2628
assert.Assert(t, matches)

internal/packager/custom.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import (
1010

1111
const (
1212
// DockerAppCustomVersion1_0_0 is the custom payload version 1.0.0
13-
DockerAppCustomVersion1_0_0 = "1.0.0"
13+
DockerAppPayloadVersion1_0_0 = "1.0.0"
1414

1515
// DockerAppCustomVersionCurrent the current payload version
16-
DockerAppCustomVersionCurrent = DockerAppCustomVersion1_0_0
16+
// The version must be bumped each time the Payload format change.
17+
DockerAppPayloadVersionCurrent = DockerAppPayloadVersion1_0_0
1718
)
1819

1920
// DockerAppCustom contains extension custom data that docker app injects
2021
// in the bundle.
2122
type DockerAppCustom struct {
22-
Version string `json:"version,omitempty"`
23+
// Payload format version
24+
Version string `json:"version,omitempty"`
25+
// Custom payload format depends on version
2326
Payload json.RawMessage `json:"payload,omitempty"`
2427
}
2528

@@ -28,16 +31,26 @@ type CustomPayloadCreated interface {
2831
CreatedTime() time.Time
2932
}
3033

34+
// CustomPayloadAppVersion is a custom payload with a docker app version
35+
type CustomPayloadAppVersion interface {
36+
AppVersion() string
37+
}
38+
3139
type payloadV1_0 struct {
3240
Created time.Time `json:"created"`
41+
Version string `json:"app-version"`
3342
}
3443

3544
func (p payloadV1_0) CreatedTime() time.Time {
3645
return p.Created
3746
}
3847

48+
func (p payloadV1_0) AppVersion() string {
49+
return p.Version
50+
}
51+
3952
func newCustomPayload() (json.RawMessage, error) {
40-
p := payloadV1_0{Created: time.Now().UTC()}
53+
p := payloadV1_0{Created: time.Now().UTC(), Version: internal.Version}
4154
j, err := json.Marshal(&p)
4255
if err != nil {
4356
return nil, err
@@ -53,7 +66,7 @@ func CustomPayload(b *bundle.Bundle) (interface{}, error) {
5366
}
5467

5568
switch version := custom.Version; version {
56-
case DockerAppCustomVersion1_0_0:
69+
case DockerAppPayloadVersion1_0_0:
5770
var payload payloadV1_0
5871
if err := json.Unmarshal(custom.Payload, &payload); err != nil {
5972
return nil, err

internal/packager/custom_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestCustomPayloadNil(t *testing.T) {
4242
},
4343
{
4444
testName: "NoPayload",
45-
version: DockerAppCustomVersionCurrent,
45+
version: DockerAppPayloadVersionCurrent,
4646
payload: nil,
4747
},
4848
}
@@ -58,12 +58,13 @@ func TestCustomPayloadNil(t *testing.T) {
5858

5959
func TestCustomPayloadV1_0_0(t *testing.T) {
6060
now := time.Now().UTC()
61-
b := createBundle(t, DockerAppCustomVersion1_0_0, payloadV1_0{now})
61+
b := createBundle(t, DockerAppPayloadVersion1_0_0, payloadV1_0{now, "version"})
6262
payload, err := CustomPayload(&b)
6363
assert.NilError(t, err)
6464
v1, ok := payload.(payloadV1_0)
6565
assert.Assert(t, ok)
6666
assert.Assert(t, now.Equal(v1.CreatedTime()))
67+
assert.Equal(t, "version", v1.AppVersion())
6768
}
6869

6970
func createBundle(t *testing.T, version string, payload interface{}) bundle.Bundle {

internal/packager/testdata/bundle-json.golden

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@
184184
"com.docker.app": {
185185
"version": "%s",
186186
"payload": {
187-
"created": "%s"
187+
"created": "%s",
188+
"app-version": "%s"
188189
}
189190
}
190191
}

0 commit comments

Comments
 (0)