Skip to content

Commit d593e61

Browse files
authored
Merge pull request #6403 from thaJeztah/28.x_backport_deprecate_context_funcs
[28.x backport] cli/command/context: deprecate exported types and functions
2 parents aced30d + 925db59 commit d593e61

File tree

12 files changed

+238
-152
lines changed

12 files changed

+238
-152
lines changed

cli/command/context/create.go

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
)
2020

2121
// CreateOptions are the options used for creating a context
22+
//
23+
// Deprecated: this type was for internal use and will be removed in the next release.
2224
type CreateOptions struct {
2325
Name string
2426
Description string
@@ -30,6 +32,18 @@ type CreateOptions struct {
3032
metaData map[string]any
3133
}
3234

35+
// createOptions are the options used for creating a context
36+
type createOptions struct {
37+
name string
38+
description string
39+
endpoint map[string]string
40+
from string
41+
42+
// Additional Metadata to store in the context. This option is not
43+
// currently exposed to the user.
44+
metaData map[string]any
45+
}
46+
3347
func longCreateDescription() string {
3448
buf := bytes.NewBuffer(nil)
3549
buf.WriteString("Create a context\n\nDocker endpoint config:\n\n")
@@ -44,52 +58,68 @@ func longCreateDescription() string {
4458
}
4559

4660
func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
47-
opts := &CreateOptions{}
61+
opts := createOptions{}
4862
cmd := &cobra.Command{
4963
Use: "create [OPTIONS] CONTEXT",
5064
Short: "Create a context",
5165
Args: cli.ExactArgs(1),
5266
RunE: func(cmd *cobra.Command, args []string) error {
53-
opts.Name = args[0]
54-
return RunCreate(dockerCLI, opts)
67+
opts.name = args[0]
68+
return runCreate(dockerCLI, &opts)
5569
},
5670
Long: longCreateDescription(),
5771
ValidArgsFunction: completion.NoComplete,
5872
}
5973
flags := cmd.Flags()
60-
flags.StringVar(&opts.Description, "description", "", "Description of the context")
61-
flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint")
62-
flags.StringVar(&opts.From, "from", "", "create context from a named context")
74+
flags.StringVar(&opts.description, "description", "", "Description of the context")
75+
flags.StringToStringVar(&opts.endpoint, "docker", nil, "set the docker endpoint")
76+
flags.StringVar(&opts.from, "from", "", "create context from a named context")
6377
return cmd
6478
}
6579

6680
// RunCreate creates a Docker context
81+
82+
// Deprecated: this function was for internal use and will be removed in the next release.
6783
func RunCreate(dockerCLI command.Cli, o *CreateOptions) error {
84+
if o == nil {
85+
o = &CreateOptions{}
86+
}
87+
88+
return runCreate(dockerCLI, &createOptions{
89+
name: o.Name,
90+
description: o.Description,
91+
endpoint: o.Docker,
92+
metaData: o.metaData,
93+
})
94+
}
95+
96+
// runCreate creates a Docker context
97+
func runCreate(dockerCLI command.Cli, opts *createOptions) error {
6898
s := dockerCLI.ContextStore()
69-
err := checkContextNameForCreation(s, o.Name)
99+
err := checkContextNameForCreation(s, opts.name)
70100
if err != nil {
71101
return err
72102
}
73103
switch {
74-
case o.From == "" && o.Docker == nil:
75-
err = createFromExistingContext(s, dockerCLI.CurrentContext(), o)
76-
case o.From != "":
77-
err = createFromExistingContext(s, o.From, o)
104+
case opts.from == "" && opts.endpoint == nil:
105+
err = createFromExistingContext(s, dockerCLI.CurrentContext(), opts)
106+
case opts.from != "":
107+
err = createFromExistingContext(s, opts.from, opts)
78108
default:
79-
err = createNewContext(s, o)
109+
err = createNewContext(s, opts)
80110
}
81111
if err == nil {
82-
_, _ = fmt.Fprintln(dockerCLI.Out(), o.Name)
83-
_, _ = fmt.Fprintf(dockerCLI.Err(), "Successfully created context %q\n", o.Name)
112+
_, _ = fmt.Fprintln(dockerCLI.Out(), opts.name)
113+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Successfully created context %q\n", opts.name)
84114
}
85115
return err
86116
}
87117

88-
func createNewContext(contextStore store.ReaderWriter, o *CreateOptions) error {
89-
if o.Docker == nil {
118+
func createNewContext(contextStore store.ReaderWriter, opts *createOptions) error {
119+
if opts.endpoint == nil {
90120
return errors.New("docker endpoint configuration is required")
91121
}
92-
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(contextStore, o.Docker)
122+
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(contextStore, opts.endpoint)
93123
if err != nil {
94124
return fmt.Errorf("unable to create docker endpoint config: %w", err)
95125
}
@@ -98,10 +128,10 @@ func createNewContext(contextStore store.ReaderWriter, o *CreateOptions) error {
98128
docker.DockerEndpoint: dockerEP,
99129
},
100130
Metadata: command.DockerContext{
101-
Description: o.Description,
102-
AdditionalFields: o.metaData,
131+
Description: opts.description,
132+
AdditionalFields: opts.metaData,
103133
},
104-
Name: o.Name,
134+
Name: opts.name,
105135
}
106136
contextTLSData := store.ContextTLSData{}
107137
if dockerTLS != nil {
@@ -115,7 +145,7 @@ func createNewContext(contextStore store.ReaderWriter, o *CreateOptions) error {
115145
if err := contextStore.CreateOrUpdate(contextMetadata); err != nil {
116146
return err
117147
}
118-
return contextStore.ResetTLSMaterial(o.Name, &contextTLSData)
148+
return contextStore.ResetTLSMaterial(opts.name, &contextTLSData)
119149
}
120150

121151
func checkContextNameForCreation(s store.Reader, name string) error {
@@ -131,16 +161,16 @@ func checkContextNameForCreation(s store.Reader, name string) error {
131161
return nil
132162
}
133163

134-
func createFromExistingContext(s store.ReaderWriter, fromContextName string, o *CreateOptions) error {
135-
if len(o.Docker) != 0 {
164+
func createFromExistingContext(s store.ReaderWriter, fromContextName string, opts *createOptions) error {
165+
if len(opts.endpoint) != 0 {
136166
return errors.New("cannot use --docker flag when --from is set")
137167
}
138168
reader := store.Export(fromContextName, &descriptionDecorator{
139169
Reader: s,
140-
description: o.Description,
170+
description: opts.description,
141171
})
142172
defer reader.Close()
143-
return store.Import(o.Name, s, reader)
173+
return store.Import(opts.name, s, reader)
144174
}
145175

146176
type descriptionDecorator struct {

cli/command/context/create_test.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestCreate(t *testing.T) {
6060
assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
6161
tests := []struct {
6262
doc string
63-
options CreateOptions
63+
options createOptions
6464
expecterErr string
6565
}{
6666
{
@@ -69,58 +69,58 @@ func TestCreate(t *testing.T) {
6969
},
7070
{
7171
doc: "reserved name",
72-
options: CreateOptions{
73-
Name: "default",
72+
options: createOptions{
73+
name: "default",
7474
},
7575
expecterErr: `"default" is a reserved context name`,
7676
},
7777
{
7878
doc: "whitespace-only name",
79-
options: CreateOptions{
80-
Name: " ",
79+
options: createOptions{
80+
name: " ",
8181
},
8282
expecterErr: `context name " " is invalid`,
8383
},
8484
{
8585
doc: "existing context",
86-
options: CreateOptions{
87-
Name: "existing-context",
86+
options: createOptions{
87+
name: "existing-context",
8888
},
8989
expecterErr: `context "existing-context" already exists`,
9090
},
9191
{
9292
doc: "invalid docker host",
93-
options: CreateOptions{
94-
Name: "invalid-docker-host",
95-
Docker: map[string]string{
93+
options: createOptions{
94+
name: "invalid-docker-host",
95+
endpoint: map[string]string{
9696
"host": "some///invalid/host",
9797
},
9898
},
9999
expecterErr: `unable to parse docker host`,
100100
},
101101
{
102102
doc: "ssh host with skip-tls-verify=false",
103-
options: CreateOptions{
104-
Name: "skip-tls-verify-false",
105-
Docker: map[string]string{
103+
options: createOptions{
104+
name: "skip-tls-verify-false",
105+
endpoint: map[string]string{
106106
"host": "ssh://example.com,skip-tls-verify=false",
107107
},
108108
},
109109
},
110110
{
111111
doc: "ssh host with skip-tls-verify=true",
112-
options: CreateOptions{
113-
Name: "skip-tls-verify-true",
114-
Docker: map[string]string{
112+
options: createOptions{
113+
name: "skip-tls-verify-true",
114+
endpoint: map[string]string{
115115
"host": "ssh://example.com,skip-tls-verify=true",
116116
},
117117
},
118118
},
119119
{
120120
doc: "ssh host with skip-tls-verify=INVALID",
121-
options: CreateOptions{
122-
Name: "skip-tls-verify-invalid",
123-
Docker: map[string]string{
121+
options: createOptions{
122+
name: "skip-tls-verify-invalid",
123+
endpoint: map[string]string{
124124
"host": "ssh://example.com",
125125
"skip-tls-verify": "INVALID",
126126
},
@@ -129,9 +129,9 @@ func TestCreate(t *testing.T) {
129129
},
130130
{
131131
doc: "unknown option",
132-
options: CreateOptions{
133-
Name: "unknown-option",
134-
Docker: map[string]string{
132+
options: createOptions{
133+
name: "unknown-option",
134+
endpoint: map[string]string{
135135
"UNKNOWN": "value",
136136
},
137137
},
@@ -140,7 +140,7 @@ func TestCreate(t *testing.T) {
140140
}
141141
for _, tc := range tests {
142142
t.Run(tc.doc, func(t *testing.T) {
143-
err := RunCreate(cli, &tc.options)
143+
err := runCreate(cli, &tc.options)
144144
if tc.expecterErr == "" {
145145
assert.NilError(t, err)
146146
} else {
@@ -159,9 +159,9 @@ func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
159159
func TestCreateOrchestratorEmpty(t *testing.T) {
160160
cli := makeFakeCli(t)
161161

162-
err := RunCreate(cli, &CreateOptions{
163-
Name: "test",
164-
Docker: map[string]string{},
162+
err := runCreate(cli, &createOptions{
163+
name: "test",
164+
endpoint: map[string]string{},
165165
})
166166
assert.NilError(t, err)
167167
assertContextCreateLogging(t, cli, "test")
@@ -187,20 +187,20 @@ func TestCreateFromContext(t *testing.T) {
187187

188188
cli := makeFakeCli(t)
189189
cli.ResetOutputBuffers()
190-
assert.NilError(t, RunCreate(cli, &CreateOptions{
191-
Name: "original",
192-
Description: "original description",
193-
Docker: map[string]string{
190+
assert.NilError(t, runCreate(cli, &createOptions{
191+
name: "original",
192+
description: "original description",
193+
endpoint: map[string]string{
194194
keyHost: "tcp://42.42.42.42:2375",
195195
},
196196
}))
197197
assertContextCreateLogging(t, cli, "original")
198198

199199
cli.ResetOutputBuffers()
200-
assert.NilError(t, RunCreate(cli, &CreateOptions{
201-
Name: "dummy",
202-
Description: "dummy description",
203-
Docker: map[string]string{
200+
assert.NilError(t, runCreate(cli, &createOptions{
201+
name: "dummy",
202+
description: "dummy description",
203+
endpoint: map[string]string{
204204
keyHost: "tcp://24.24.24.24:2375",
205205
},
206206
}))
@@ -211,11 +211,11 @@ func TestCreateFromContext(t *testing.T) {
211211
for _, tc := range cases {
212212
t.Run(tc.name, func(t *testing.T) {
213213
cli.ResetOutputBuffers()
214-
err := RunCreate(cli, &CreateOptions{
215-
From: "original",
216-
Name: tc.name,
217-
Description: tc.description,
218-
Docker: tc.docker,
214+
err := runCreate(cli, &createOptions{
215+
from: "original",
216+
name: tc.name,
217+
description: tc.description,
218+
endpoint: tc.docker,
219219
})
220220
assert.NilError(t, err)
221221
assertContextCreateLogging(t, cli, tc.name)
@@ -251,10 +251,10 @@ func TestCreateFromCurrent(t *testing.T) {
251251

252252
cli := makeFakeCli(t)
253253
cli.ResetOutputBuffers()
254-
assert.NilError(t, RunCreate(cli, &CreateOptions{
255-
Name: "original",
256-
Description: "original description",
257-
Docker: map[string]string{
254+
assert.NilError(t, runCreate(cli, &createOptions{
255+
name: "original",
256+
description: "original description",
257+
endpoint: map[string]string{
258258
keyHost: "tcp://42.42.42.42:2375",
259259
},
260260
}))
@@ -265,9 +265,9 @@ func TestCreateFromCurrent(t *testing.T) {
265265
for _, tc := range cases {
266266
t.Run(tc.name, func(t *testing.T) {
267267
cli.ResetOutputBuffers()
268-
err := RunCreate(cli, &CreateOptions{
269-
Name: tc.name,
270-
Description: tc.description,
268+
err := runCreate(cli, &createOptions{
269+
name: tc.name,
270+
description: tc.description,
271271
})
272272
assert.NilError(t, err)
273273
assertContextCreateLogging(t, cli, tc.name)

cli/command/context/export-import_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ func TestExportImportWithFile(t *testing.T) {
2121
"MyCustomMetadata": t.Name(),
2222
})
2323
cli.ErrBuffer().Reset()
24-
assert.NilError(t, RunExport(cli, &ExportOptions{
25-
ContextName: "test",
26-
Dest: contextFile,
27-
}))
24+
assert.NilError(t, runExport(cli, "test", contextFile))
2825
assert.Equal(t, cli.ErrBuffer().String(), fmt.Sprintf("Written file %q\n", contextFile))
2926
cli.OutBuffer().Reset()
3027
cli.ErrBuffer().Reset()
31-
assert.NilError(t, RunImport(cli, "test2", contextFile))
28+
assert.NilError(t, runImport(cli, "test2", contextFile))
3229
context1, err := cli.ContextStore().GetMetadata("test")
3330
assert.NilError(t, err)
3431
context2, err := cli.ContextStore().GetMetadata("test2")
@@ -55,15 +52,12 @@ func TestExportImportPipe(t *testing.T) {
5552
})
5653
cli.ErrBuffer().Reset()
5754
cli.OutBuffer().Reset()
58-
assert.NilError(t, RunExport(cli, &ExportOptions{
59-
ContextName: "test",
60-
Dest: "-",
61-
}))
55+
assert.NilError(t, runExport(cli, "test", "-"))
6256
assert.Equal(t, cli.ErrBuffer().String(), "")
6357
cli.SetIn(streams.NewIn(io.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
6458
cli.OutBuffer().Reset()
6559
cli.ErrBuffer().Reset()
66-
assert.NilError(t, RunImport(cli, "test2", "-"))
60+
assert.NilError(t, runImport(cli, "test2", "-"))
6761
context1, err := cli.ContextStore().GetMetadata("test")
6862
assert.NilError(t, err)
6963
context2, err := cli.ContextStore().GetMetadata("test2")
@@ -88,6 +82,6 @@ func TestExportExistingFile(t *testing.T) {
8882
cli := makeFakeCli(t)
8983
cli.ErrBuffer().Reset()
9084
assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0o644))
91-
err := RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
85+
err := runExport(cli, "test", contextFile)
9286
assert.Assert(t, os.IsExist(err))
9387
}

0 commit comments

Comments
 (0)