Skip to content

Commit 7041c02

Browse files
authored
Merge pull request moby#51320 from thaJeztah/refactor_client_system_step1
client: refactor Events, Info, RegistryLogin
2 parents b5971b6 + e46058c commit 7041c02

32 files changed

+181
-77
lines changed

client/client_interfaces.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77

88
"github.com/moby/moby/api/types"
99
"github.com/moby/moby/api/types/container"
10-
"github.com/moby/moby/api/types/events"
1110
"github.com/moby/moby/api/types/network"
12-
"github.com/moby/moby/api/types/registry"
1311
"github.com/moby/moby/api/types/system"
1412
)
1513

@@ -176,9 +174,9 @@ type SwarmAPIClient interface {
176174

177175
// SystemAPIClient defines API client methods for the system
178176
type SystemAPIClient interface {
179-
Events(ctx context.Context, options EventsListOptions) (<-chan events.Message, <-chan error)
180-
Info(ctx context.Context) (system.Info, error)
181-
RegistryLogin(ctx context.Context, auth registry.AuthConfig) (registry.AuthenticateOKBody, error)
177+
Events(ctx context.Context, options EventsListOptions) EventsResult
178+
Info(ctx context.Context, options InfoOptions) (SystemInfoResult, error)
179+
RegistryLogin(ctx context.Context, auth RegistryLoginOptions) (RegistryLoginResult, error)
182180
DiskUsage(ctx context.Context, options DiskUsageOptions) (system.DiskUsage, error)
183181
Ping(ctx context.Context, options PingOptions) (PingResult, error)
184182
}

client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ func TestNegotiateAPIVersionAutomatic(t *testing.T) {
408408
// First request should trigger negotiation
409409
pingVersion = "1.50"
410410
expected = "1.50"
411-
_, _ = client.Info(ctx)
411+
_, _ = client.Info(ctx, InfoOptions{})
412412
assert.Check(t, is.Equal(client.ClientVersion(), expected))
413413

414414
// Once successfully negotiated, subsequent requests should not re-negotiate
415415
pingVersion = "1.49"
416416
expected = "1.50"
417-
_, _ = client.Info(ctx)
417+
_, _ = client.Info(ctx, InfoOptions{})
418418
assert.Check(t, is.Equal(client.ClientVersion(), expected))
419419
}
420420

client/login.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,38 @@ import (
88
"github.com/moby/moby/api/types/registry"
99
)
1010

11+
type RegistryLoginOptions struct {
12+
Username string
13+
Password string
14+
ServerAddress string
15+
IdentityToken string
16+
RegistryToken string
17+
}
18+
19+
// RegistryLoginResult holds the result of a RegistryLogin query.
20+
type RegistryLoginResult struct {
21+
Auth registry.AuthenticateOKBody
22+
}
23+
1124
// RegistryLogin authenticates the docker server with a given docker registry.
1225
// It returns unauthorizedError when the authentication fails.
13-
func (cli *Client) RegistryLogin(ctx context.Context, auth registry.AuthConfig) (registry.AuthenticateOKBody, error) {
26+
func (cli *Client) RegistryLogin(ctx context.Context, options RegistryLoginOptions) (RegistryLoginResult, error) {
27+
auth := registry.AuthConfig{
28+
Username: options.Username,
29+
Password: options.Password,
30+
ServerAddress: options.ServerAddress,
31+
IdentityToken: options.IdentityToken,
32+
RegistryToken: options.RegistryToken,
33+
}
34+
1435
resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
1536
defer ensureReaderClosed(resp)
1637

1738
if err != nil {
18-
return registry.AuthenticateOKBody{}, err
39+
return RegistryLoginResult{}, err
1940
}
2041

2142
var response registry.AuthenticateOKBody
2243
err = json.NewDecoder(resp.Body).Decode(&response)
23-
return response, err
44+
return RegistryLoginResult{Auth: response}, err
2445
}

client/system_events.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ type EventsListOptions struct {
1919
Filters Filters
2020
}
2121

22+
// EventsResult holds the result of an Events query.
23+
type EventsResult struct {
24+
Messages <-chan events.Message
25+
Err <-chan error
26+
}
27+
2228
// Events returns a stream of events in the daemon. It's up to the caller to close the stream
2329
// by cancelling the context. Once the stream has been completely read an [io.EOF] error is
2430
// sent over the error channel. If an error is sent, all processing is stopped. It's up
2531
// to the caller to reopen the stream in the event of an error by reinvoking this method.
26-
func (cli *Client) Events(ctx context.Context, options EventsListOptions) (<-chan events.Message, <-chan error) {
32+
func (cli *Client) Events(ctx context.Context, options EventsListOptions) EventsResult {
2733
messages := make(chan events.Message)
2834
errs := make(chan error, 1)
2935

@@ -76,7 +82,10 @@ func (cli *Client) Events(ctx context.Context, options EventsListOptions) (<-cha
7682
}()
7783
<-started
7884

79-
return messages, errs
85+
return EventsResult{
86+
Messages: messages,
87+
Err: errs,
88+
}
8089
}
8190

8291
func buildEventsQueryParams(options EventsListOptions) (url.Values, error) {

client/system_events_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ func TestEventsErrorInOptions(t *testing.T) {
3737
for _, tc := range errorCases {
3838
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
3939
assert.NilError(t, err)
40-
_, errs := client.Events(context.Background(), tc.options)
41-
err = <-errs
40+
events := client.Events(context.Background(), tc.options)
41+
err = <-events.Err
4242
assert.Check(t, is.ErrorContains(err, tc.expectedError))
4343
}
4444
}
4545

4646
func TestEventsErrorFromServer(t *testing.T) {
4747
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
4848
assert.NilError(t, err)
49-
_, errs := client.Events(context.Background(), EventsListOptions{})
50-
err = <-errs
49+
events := client.Events(context.Background(), EventsListOptions{})
50+
err = <-events.Err
5151
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
5252
}
5353

@@ -133,18 +133,18 @@ func TestEvents(t *testing.T) {
133133
}))
134134
assert.NilError(t, err)
135135

136-
messages, errs := client.Events(context.Background(), eventsCase.options)
136+
events := client.Events(context.Background(), eventsCase.options)
137137

138138
loop:
139139
for {
140140
select {
141-
case err := <-errs:
141+
case err := <-events.Err:
142142
if err != nil && !errors.Is(err, io.EOF) {
143143
t.Fatal(err)
144144
}
145145

146146
break loop
147-
case e := <-messages:
147+
case e := <-events.Messages:
148148
_, ok := eventsCase.expectedEvents[e.Actor.ID]
149149
assert.Check(t, ok, "event received not expected with action %s & id %s", e.Action, e.Actor.ID)
150150
}

client/system_info.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,26 @@ import (
99
"github.com/moby/moby/api/types/system"
1010
)
1111

12+
type InfoOptions struct {
13+
// No options currently; placeholder for future use
14+
}
15+
16+
type SystemInfoResult struct {
17+
Info system.Info
18+
}
19+
1220
// Info returns information about the docker server.
13-
func (cli *Client) Info(ctx context.Context) (system.Info, error) {
14-
var info system.Info
21+
func (cli *Client) Info(ctx context.Context, options InfoOptions) (SystemInfoResult, error) {
1522
resp, err := cli.get(ctx, "/info", url.Values{}, nil)
1623
defer ensureReaderClosed(resp)
1724
if err != nil {
18-
return info, err
25+
return SystemInfoResult{}, err
1926
}
2027

28+
var info system.Info
2129
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
22-
return info, fmt.Errorf("Error reading remote info: %v", err)
30+
return SystemInfoResult{}, fmt.Errorf("Error reading remote info: %v", err)
2331
}
2432

25-
return info, nil
33+
return SystemInfoResult{Info: info}, nil
2634
}

client/system_info_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
func TestInfoServerError(t *testing.T) {
1515
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
1616
assert.NilError(t, err)
17-
_, err = client.Info(context.Background())
17+
_, err = client.Info(context.Background(), InfoOptions{})
1818
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
1919
}
2020

2121
func TestInfoInvalidResponseJSONError(t *testing.T) {
2222
client, err := NewClientWithOpts(WithMockClient(mockResponse(http.StatusOK, nil, "invalid json")))
2323
assert.NilError(t, err)
24-
_, err = client.Info(context.Background())
24+
_, err = client.Info(context.Background(), InfoOptions{})
2525
assert.Check(t, is.ErrorContains(err, "invalid character"))
2626
}
2727

@@ -38,8 +38,9 @@ func TestInfo(t *testing.T) {
3838
}))
3939
assert.NilError(t, err)
4040

41-
info, err := client.Info(context.Background())
41+
result, err := client.Info(context.Background(), InfoOptions{})
4242
assert.NilError(t, err)
43+
info := result.Info
4344

4445
assert.Check(t, is.Equal(info.ID, "daemonID"))
4546
assert.Check(t, is.Equal(info.Containers, 3))
@@ -68,8 +69,9 @@ func TestInfoWithDiscoveredDevices(t *testing.T) {
6869
}))
6970
assert.NilError(t, err)
7071

71-
info, err := client.Info(context.Background())
72+
result, err := client.Info(context.Background(), InfoOptions{})
7273
assert.NilError(t, err)
74+
info := result.Info
7375

7476
assert.Check(t, is.Equal(info.ID, "daemonID"))
7577
assert.Check(t, is.Equal(info.Containers, 3))

integration-cli/docker_api_swarm_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,9 @@ func checkClusterHealth(t *testing.T, cl []*daemon.Daemon, managerCount, workerC
756756

757757
// check info in a poll.WaitOn(), because if the cluster doesn't have a leader, `info` will return an error
758758
checkInfo := func(t *testing.T) (any, string) {
759-
client := d.NewClientT(t)
760-
daemonInfo, err := client.Info(ctx)
761-
info = daemonInfo.Swarm
759+
apiClient := d.NewClientT(t)
760+
result, err := apiClient.Info(ctx, client.InfoOptions{})
761+
info = result.Info.Swarm
762762
return err, "cluster not ready in time"
763763
}
764764
poll.WaitOn(t, pollCheck(t, checkInfo, checker.IsNil()), poll.WithTimeout(defaultReconciliationTimeout))

integration-cli/docker_cli_info_unix_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ func (s *DockerCLIInfoSuite) TestInfoSecurityOptions(c *testing.T) {
2121
apiClient, err := client.NewClientWithOpts(client.FromEnv)
2222
assert.NilError(c, err)
2323
defer apiClient.Close()
24-
info, err := apiClient.Info(testutil.GetContext(c))
24+
result, err := apiClient.Info(testutil.GetContext(c), client.InfoOptions{})
2525
assert.NilError(c, err)
26+
info := result.Info
2627

2728
if Apparmor() {
2829
assert.Check(c, is.Contains(info.SecurityOptions, "name=apparmor"))

integration-cli/docker_cli_plugins_logdriver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ func (s *DockerCLIPluginLogDriverSuite) TestPluginLogDriverInfoList(c *testing.T
5454
assert.NilError(c, err)
5555
defer apiClient.Close()
5656

57-
info, err := apiClient.Info(testutil.GetContext(c))
57+
result, err := apiClient.Info(testutil.GetContext(c), client.InfoOptions{})
5858
assert.NilError(c, err)
59+
info := result.Info
5960

6061
drivers := strings.Join(info.Plugins.Log, " ")
6162
assert.Assert(c, is.Contains(drivers, "json-file"))

0 commit comments

Comments
 (0)