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

Commit 9873c34

Browse files
Introduce a new Installation object, on top of Claim, tracking the reference where the installed application comes from.
Signed-off-by: Silvin Lubecki <[email protected]>
1 parent 3508b0e commit 9873c34

File tree

12 files changed

+131
-92
lines changed

12 files changed

+131
-92
lines changed

internal/commands/cnab.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,17 @@ func getAppNameKind(name string) (string, nameKind) {
201201
return name, nameKindFile
202202
}
203203

204-
func extractAndLoadAppBasedBundle(dockerCli command.Cli, name string) (*bundle.Bundle, error) {
204+
func extractAndLoadAppBasedBundle(dockerCli command.Cli, name string) (*bundle.Bundle, string, error) {
205205
app, err := packager.Extract(name)
206206
if err != nil {
207-
return nil, err
207+
return nil, "", err
208208
}
209209
defer app.Cleanup()
210-
return makeBundleFromApp(dockerCli, app)
210+
bndl, err := makeBundleFromApp(dockerCli, app)
211+
return bndl, "", err
211212
}
212213

213-
func resolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string, pullRef bool, insecureRegistries []string) (*bundle.Bundle, error) {
214+
func resolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string, pullRef bool, insecureRegistries []string) (*bundle.Bundle, string, error) {
214215
// resolution logic:
215216
// - if there is a docker-app package in working directory, or an http:// / https:// prefix, use packager.Extract result
216217
// - the name has a .json or .cnab extension and refers to an existing file or web resource: load the bundle
@@ -220,28 +221,31 @@ func resolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name
220221
switch kind {
221222
case nameKindFile:
222223
if pullRef {
223-
return nil, errors.Errorf("%s: cannot pull when referencing a file based app", name)
224+
return nil, "", errors.Errorf("%s: cannot pull when referencing a file based app", name)
224225
}
225226
if strings.HasSuffix(name, internal.AppExtension) {
226227
return extractAndLoadAppBasedBundle(dockerCli, name)
227228
}
228-
return loader.NewLoader().Load(name)
229+
bndl, err := loader.NewLoader().Load(name)
230+
return bndl, "", err
229231
case nameKindDir, nameKindEmpty:
230232
if pullRef {
231233
if kind == nameKindDir {
232-
return nil, errors.Errorf("%s: cannot pull when referencing a directory based app", name)
234+
return nil, "", errors.Errorf("%s: cannot pull when referencing a directory based app", name)
233235
}
234-
return nil, errors.Errorf("cannot pull when referencing a directory based app")
236+
return nil, "", errors.Errorf("cannot pull when referencing a directory based app")
235237
}
236238
return extractAndLoadAppBasedBundle(dockerCli, name)
237239
case nameKindReference:
238240
ref, err := reference.ParseNormalizedNamed(name)
239241
if err != nil {
240-
return nil, errors.Wrap(err, name)
242+
return nil, "", errors.Wrap(err, name)
241243
}
242-
return bundleStore.LookupOrPullBundle(reference.TagNameOnly(ref), pullRef, dockerCli.ConfigFile(), insecureRegistries)
244+
tagRef := reference.TagNameOnly(ref)
245+
bndl, err := bundleStore.LookupOrPullBundle(tagRef, pullRef, dockerCli.ConfigFile(), insecureRegistries)
246+
return bndl, tagRef.String(), err
243247
}
244-
return nil, fmt.Errorf("could not resolve bundle %q", name)
248+
return nil, "", fmt.Errorf("could not resolve bundle %q", name)
245249
}
246250

247251
func requiredClaimBindMount(c claim.Claim, targetContextName string, dockerCli command.Cli) (bindMount, error) {
@@ -298,7 +302,7 @@ func isDockerHostLocal(host string) bool {
298302
}
299303

300304
func prepareCustomAction(actionName string, dockerCli command.Cli, appname string, stdout io.Writer,
301-
registryOpts registryOptions, pullOpts pullOptions, paramsOpts parametersOptions) (*action.RunCustom, *claim.Claim, *bytes.Buffer, error) {
305+
registryOpts registryOptions, pullOpts pullOptions, paramsOpts parametersOptions) (*action.RunCustom, *appstore.Installation, *bytes.Buffer, error) {
302306
s, err := appstore.NewApplicationStore(config.Dir())
303307
if err != nil {
304308
return nil, nil, nil, err
@@ -307,22 +311,21 @@ func prepareCustomAction(actionName string, dockerCli command.Cli, appname strin
307311
if err != nil {
308312
return nil, nil, nil, err
309313
}
310-
311-
c, err := claim.New("custom-action")
314+
driverImpl, errBuf, err := prepareDriver(dockerCli, bindMount{}, stdout)
312315
if err != nil {
313316
return nil, nil, nil, err
314317
}
315-
driverImpl, errBuf, err := prepareDriver(dockerCli, bindMount{}, stdout)
318+
bundle, ref, err := resolveBundle(dockerCli, bundleStore, appname, pullOpts.pull, registryOpts.insecureRegistries)
316319
if err != nil {
317320
return nil, nil, nil, err
318321
}
319-
bundle, err := resolveBundle(dockerCli, bundleStore, appname, pullOpts.pull, registryOpts.insecureRegistries)
322+
installation, err := appstore.NewInstallation("custom-action", ref)
320323
if err != nil {
321324
return nil, nil, nil, err
322325
}
323-
c.Bundle = bundle
326+
installation.Bundle = bundle
324327

325-
if err := mergeBundleParameters(c,
328+
if err := mergeBundleParameters(installation,
326329
withFileParameters(paramsOpts.parametersFiles),
327330
withCommandLineParameters(paramsOpts.overrides),
328331
); err != nil {
@@ -333,10 +336,10 @@ func prepareCustomAction(actionName string, dockerCli command.Cli, appname strin
333336
Action: actionName,
334337
Driver: driverImpl,
335338
}
336-
return a, c, errBuf, nil
339+
return a, installation, errBuf, nil
337340
}
338341

339-
func isInstallationFailed(installation *claim.Claim) bool {
342+
func isInstallationFailed(installation *appstore.Installation) bool {
340343
return installation.Result.Action == claim.ActionInstall &&
341344
installation.Result.Status == claim.StatusFailure
342345
}

internal/commands/inspect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
3434

3535
func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) error {
3636
defer muteDockerCli(dockerCli)()
37-
a, c, errBuf, err := prepareCustomAction(internal.ActionInspectName, dockerCli, appname, nil, opts.registryOptions, opts.pullOptions, opts.parametersOptions)
37+
action, installation, errBuf, err := prepareCustomAction(internal.ActionInspectName, dockerCli, appname, nil, opts.registryOptions, opts.pullOptions, opts.parametersOptions)
3838
if err != nil {
3939
return err
4040
}
41-
if err := a.Run(c, nil, nil); err != nil {
41+
if err := action.Run(&installation.Claim, nil, nil); err != nil {
4242
return fmt.Errorf("inspect failed: %s", errBuf)
4343
}
4444
return nil

internal/commands/install.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"os"
66

77
"github.com/deislabs/duffle/pkg/action"
8-
"github.com/deislabs/duffle/pkg/claim"
98
"github.com/deislabs/duffle/pkg/credentials"
9+
"github.com/docker/app/internal/store"
1010
"github.com/docker/cli/cli"
1111
"github.com/docker/cli/cli/command"
1212
"github.com/spf13/cobra"
@@ -80,7 +80,7 @@ func runInstall(dockerCli command.Cli, appname string, opts installOptions) erro
8080
return err
8181
}
8282

83-
bndl, err := resolveBundle(dockerCli, bundleStore, appname, opts.pull, opts.insecureRegistries)
83+
bndl, ref, err := resolveBundle(dockerCli, bundleStore, appname, opts.pull, opts.insecureRegistries)
8484
if err != nil {
8585
return err
8686
}
@@ -93,15 +93,15 @@ func runInstall(dockerCli command.Cli, appname string, opts installOptions) erro
9393
}
9494
if installation, err := installationStore.Read(installationName); err == nil {
9595
// A failed installation can be overridden, but with a warning
96-
if isInstallationFailed(&installation) {
96+
if isInstallationFailed(installation) {
9797
fmt.Fprintf(os.Stderr, "WARNING: installing over previously failed installation %q\n", installationName)
9898
} else {
9999
// Return an error in case of successful installation, or even failed upgrade, which means
100100
// their was already a successful installation.
101101
return fmt.Errorf("Installation %q already exists, use 'docker app upgrade' instead", installationName)
102102
}
103103
}
104-
c, err := claim.New(installationName)
104+
installation, err := store.NewInstallation(installationName, ref)
105105
if err != nil {
106106
return err
107107
}
@@ -110,9 +110,9 @@ func runInstall(dockerCli command.Cli, appname string, opts installOptions) erro
110110
if err != nil {
111111
return err
112112
}
113-
c.Bundle = bndl
113+
installation.Bundle = bndl
114114

115-
if err := mergeBundleParameters(c,
115+
if err := mergeBundleParameters(installation,
116116
withFileParameters(opts.parametersFiles),
117117
withCommandLineParameters(opts.overrides),
118118
withOrchestratorParameters(opts.orchestrator, opts.kubeNamespace),
@@ -131,10 +131,10 @@ func runInstall(dockerCli command.Cli, appname string, opts installOptions) erro
131131
inst := &action.Install{
132132
Driver: driverImpl,
133133
}
134-
err = inst.Run(c, creds, os.Stdout)
134+
err = inst.Run(&installation.Claim, creds, os.Stdout)
135135
// Even if the installation failed, the installation is persisted with its failure status,
136136
// so any installation needs a clean uninstallation.
137-
err2 := installationStore.Store(*c)
137+
err2 := installationStore.Store(installation)
138138
if err != nil {
139139
return fmt.Errorf("Installation failed: %s", errBuf)
140140
}

internal/commands/parameters.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55

66
"github.com/deislabs/cnab-go/bundle"
7-
"github.com/deislabs/duffle/pkg/claim"
87
"github.com/docker/app/internal"
8+
"github.com/docker/app/internal/store"
99
"github.com/docker/app/types/parameters"
1010
cliopts "github.com/docker/cli/opts"
1111
"github.com/pkg/errors"
@@ -61,22 +61,22 @@ func withOrchestratorParameters(orchestrator string, kubeNamespace string) merge
6161
}
6262
}
6363

64-
func mergeBundleParameters(c *claim.Claim, ops ...mergeBundleOpt) error {
65-
bndl := c.Bundle
66-
if c.Parameters == nil {
67-
c.Parameters = make(map[string]interface{})
64+
func mergeBundleParameters(installation *store.Installation, ops ...mergeBundleOpt) error {
65+
bndl := installation.Bundle
66+
if installation.Parameters == nil {
67+
installation.Parameters = make(map[string]interface{})
6868
}
6969
userParams := map[string]string{}
7070
for _, op := range ops {
7171
if err := op(bndl, userParams); err != nil {
7272
return err
7373
}
7474
}
75-
if err := matchAndMergeParametersDefinition(c.Parameters, userParams, bndl.Parameters); err != nil {
75+
if err := matchAndMergeParametersDefinition(installation.Parameters, userParams, bndl.Parameters); err != nil {
7676
return err
7777
}
7878
var err error
79-
c.Parameters, err = bundle.ValuesOrDefaults(c.Parameters, bndl)
79+
installation.Parameters, err = bundle.ValuesOrDefaults(installation.Parameters, bndl)
8080
return err
8181
}
8282

internal/commands/parameters_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/deislabs/cnab-go/bundle"
77
"github.com/deislabs/duffle/pkg/claim"
88
"github.com/docker/app/internal"
9+
"github.com/docker/app/internal/store"
910
"gotest.tools/assert"
1011
"gotest.tools/assert/cmp"
1112
"gotest.tools/fs"
@@ -106,16 +107,16 @@ func TestMergeBundleParameters(t *testing.T) {
106107
},
107108
},
108109
}
109-
c := &claim.Claim{Bundle: bundle}
110-
err := mergeBundleParameters(c,
110+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
111+
err := mergeBundleParameters(i,
111112
first,
112113
second,
113114
)
114115
assert.NilError(t, err)
115116
expected := map[string]interface{}{
116117
"param": "second",
117118
}
118-
assert.Assert(t, cmp.DeepEqual(c.Parameters, expected))
119+
assert.Assert(t, cmp.DeepEqual(i.Parameters, expected))
119120
})
120121

121122
t.Run("Default values", func(t *testing.T) {
@@ -127,13 +128,13 @@ func TestMergeBundleParameters(t *testing.T) {
127128
},
128129
},
129130
}
130-
c := &claim.Claim{Bundle: bundle}
131-
err := mergeBundleParameters(c)
131+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
132+
err := mergeBundleParameters(i)
132133
assert.NilError(t, err)
133134
expected := map[string]interface{}{
134135
"param": "default",
135136
}
136-
assert.Assert(t, cmp.DeepEqual(c.Parameters, expected))
137+
assert.Assert(t, cmp.DeepEqual(i.Parameters, expected))
137138
})
138139

139140
t.Run("Converting values", func(t *testing.T) {
@@ -149,13 +150,13 @@ func TestMergeBundleParameters(t *testing.T) {
149150
},
150151
},
151152
}
152-
c := &claim.Claim{Bundle: bundle}
153-
err := mergeBundleParameters(c, withIntValue)
153+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
154+
err := mergeBundleParameters(i, withIntValue)
154155
assert.NilError(t, err)
155156
expected := map[string]interface{}{
156157
"param": 1,
157158
}
158-
assert.Assert(t, cmp.DeepEqual(c.Parameters, expected))
159+
assert.Assert(t, cmp.DeepEqual(i.Parameters, expected))
159160
})
160161

161162
t.Run("Default values", func(t *testing.T) {
@@ -167,13 +168,13 @@ func TestMergeBundleParameters(t *testing.T) {
167168
},
168169
},
169170
}
170-
c := &claim.Claim{Bundle: bundle}
171-
err := mergeBundleParameters(c)
171+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
172+
err := mergeBundleParameters(i)
172173
assert.NilError(t, err)
173174
expected := map[string]interface{}{
174175
"param": "default",
175176
}
176-
assert.Assert(t, cmp.DeepEqual(c.Parameters, expected))
177+
assert.Assert(t, cmp.DeepEqual(i.Parameters, expected))
177178
})
178179

179180
t.Run("Undefined parameter is rejected", func(t *testing.T) {
@@ -184,8 +185,8 @@ func TestMergeBundleParameters(t *testing.T) {
184185
bundle := &bundle.Bundle{
185186
Parameters: map[string]bundle.ParameterDefinition{},
186187
}
187-
c := &claim.Claim{Bundle: bundle}
188-
err := mergeBundleParameters(c, withUndefined)
188+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
189+
err := mergeBundleParameters(i, withUndefined)
189190
assert.ErrorContains(t, err, "is not defined in the bundle")
190191
})
191192

@@ -201,8 +202,8 @@ func TestMergeBundleParameters(t *testing.T) {
201202
},
202203
},
203204
}
204-
c := &claim.Claim{Bundle: bundle}
205-
err := mergeBundleParameters(c, withIntValue)
205+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
206+
err := mergeBundleParameters(i, withIntValue)
206207
assert.ErrorContains(t, err, "invalid value for parameter")
207208
})
208209

@@ -219,8 +220,8 @@ func TestMergeBundleParameters(t *testing.T) {
219220
},
220221
},
221222
}
222-
c := &claim.Claim{Bundle: bundle}
223-
err := mergeBundleParameters(c, withIntValue)
223+
i := &store.Installation{Claim: claim.Claim{Bundle: bundle}}
224+
err := mergeBundleParameters(i, withIntValue)
224225
assert.ErrorContains(t, err, "invalid value for parameter")
225226
})
226227
}

internal/commands/render.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func runRender(dockerCli command.Cli, appname string, opts renderOptions) error
5353
w = f
5454
}
5555

56-
a, c, errBuf, err := prepareCustomAction(internal.ActionRenderName, dockerCli, appname, w, opts.registryOptions, opts.pullOptions, opts.parametersOptions)
56+
action, installation, errBuf, err := prepareCustomAction(internal.ActionRenderName, dockerCli, appname, w, opts.registryOptions, opts.pullOptions, opts.parametersOptions)
5757
if err != nil {
5858
return err
5959
}
60-
c.Parameters[internal.ParameterRenderFormatName] = opts.formatDriver
60+
installation.Parameters[internal.ParameterRenderFormatName] = opts.formatDriver
6161

62-
if err := a.Run(c, nil, nil); err != nil {
62+
if err := action.Run(&installation.Claim, nil, nil); err != nil {
6363
return fmt.Errorf("render failed: %s", errBuf)
6464
}
6565
return nil

internal/commands/status.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,35 @@ func runStatus(dockerCli command.Cli, installationName string, opts credentialOp
3838
return err
3939
}
4040

41-
c, err := installationStore.Read(installationName)
41+
installation, err := installationStore.Read(installationName)
4242
if err != nil {
4343
return err
4444
}
45-
bind, err := requiredClaimBindMount(c, opts.targetContext, dockerCli)
45+
bind, err := requiredClaimBindMount(installation.Claim, opts.targetContext, dockerCli)
4646
if err != nil {
4747
return err
4848
}
4949
driverImpl, errBuf, err := prepareDriver(dockerCli, bind, nil)
5050
if err != nil {
5151
return err
5252
}
53-
if err := mergeBundleParameters(&c,
53+
if err := mergeBundleParameters(installation,
5454
withSendRegistryAuth(opts.sendRegistryAuth),
5555
); err != nil {
5656
return err
5757
}
58-
creds, err := prepareCredentialSet(c.Bundle, opts.CredentialSetOpts(dockerCli, credentialStore)...)
58+
creds, err := prepareCredentialSet(installation.Bundle, opts.CredentialSetOpts(dockerCli, credentialStore)...)
5959
if err != nil {
6060
return err
6161
}
62-
if err := credentials.Validate(creds, c.Bundle.Credentials); err != nil {
62+
if err := credentials.Validate(creds, installation.Bundle.Credentials); err != nil {
6363
return err
6464
}
6565
status := &action.RunCustom{
6666
Action: internal.ActionStatusName,
6767
Driver: driverImpl,
6868
}
69-
if err := status.Run(&c, creds, dockerCli.Out()); err != nil {
69+
if err := status.Run(&installation.Claim, creds, dockerCli.Out()); err != nil {
7070
return fmt.Errorf("status failed: %s\n%s", err, errBuf)
7171
}
7272
return nil

0 commit comments

Comments
 (0)