This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
/
clientV6.go
191 lines (167 loc) · 5.49 KB
/
clientV6.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package rpc
import (
"context"
"io"
"net/rpc"
"net/rpc/jsonrpc"
"github.com/weaveworks/flux/api/v10"
"github.com/weaveworks/flux/api/v11"
"github.com/weaveworks/flux/api/v6"
fluxerr "github.com/weaveworks/flux/errors"
"github.com/weaveworks/flux/job"
"github.com/weaveworks/flux/remote"
"github.com/weaveworks/flux/update"
)
// RPCClient is the rpc-backed implementation of a server, for
// talking to remote daemons.
type RPCClientV6 struct {
*baseClient
client *rpc.Client
}
type clientV6 interface {
v6.Server
v6.Upstream
}
// We don't get proper application error structs back from v6, but we
// do know that anything that's not considered a fatal error can be
// translated into an application error.
func remoteApplicationError(err error) error {
return &fluxerr.Error{
Type: fluxerr.User,
Err: err,
Help: `Error from daemon
The daemon (fluxd, running in your cluster) reported this error when
attempting to fulfil your request:
` + err.Error() + `
`,
}
}
var _ clientV6 = &RPCClientV6{}
var supportedKindsV6 = []string{"service"}
// NewClient creates a new rpc-backed implementation of the server.
func NewClientV6(conn io.ReadWriteCloser) *RPCClientV6 {
return &RPCClientV6{&baseClient{}, jsonrpc.NewClient(conn)}
}
// Ping is used to check if the remote server is available.
func (p *RPCClientV6) Ping(ctx context.Context) error {
err := p.client.Call("RPCServer.Ping", struct{}{}, nil)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return remote.FatalError{err}
}
return err
}
// Version is used to check the version of the remote server.
func (p *RPCClientV6) Version(ctx context.Context) (string, error) {
var version string
err := p.client.Call("RPCServer.Version", struct{}{}, &version)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return "", remote.FatalError{err}
} else if err != nil && err.Error() == "rpc: can't find method RPCServer.Version" {
// "Version" is not supported by this version of fluxd (it is old). Fail
// gracefully.
return "unknown", nil
}
return version, err
}
// Export is used to get service configuration in cluster-specific format
func (p *RPCClientV6) Export(ctx context.Context) ([]byte, error) {
var config []byte
err := p.client.Call("RPCServer.Export", struct{}{}, &config)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return nil, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return config, err
}
func (p *RPCClientV6) ListServices(ctx context.Context, namespace string) ([]v6.ControllerStatus, error) {
var services []v6.ControllerStatus
err := p.client.Call("RPCServer.ListServices", namespace, &services)
listServicesRolloutStatus(services)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return nil, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return services, err
}
func (p *RPCClientV6) ListServicesWithOptions(ctx context.Context, opts v11.ListServicesOptions) ([]v6.ControllerStatus, error) {
return listServicesWithOptions(ctx, p, opts, supportedKindsV6)
}
func (p *RPCClientV6) ListImages(ctx context.Context, spec update.ResourceSpec) ([]v6.ImageStatus, error) {
var images []v6.ImageStatus
if err := requireServiceSpecKinds(spec, supportedKindsV6); err != nil {
return images, remote.UpgradeNeededError(err)
}
err := p.client.Call("RPCServer.ListImages", spec, &images)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return nil, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return images, err
}
func (p *RPCClientV6) ListImagesWithOptions(ctx context.Context, opts v10.ListImagesOptions) ([]v6.ImageStatus, error) {
return listImagesWithOptions(ctx, p, opts)
}
func (p *RPCClientV6) UpdateManifests(ctx context.Context, u update.Spec) (job.ID, error) {
var result job.ID
if err := requireSpecKinds(u, supportedKindsV6); err != nil {
return result, remote.UpgradeNeededError(err)
}
err := p.client.Call("RPCServer.UpdateManifests", u, &result)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return result, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return result, err
}
func (p *RPCClientV6) SyncNotify(ctx context.Context) error {
var result struct{}
err := p.client.Call("RPCServer.SyncNotify", struct{}{}, &result)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return err
}
func (p *RPCClientV6) JobStatus(ctx context.Context, jobID job.ID) (job.Status, error) {
var result job.Status
err := p.client.Call("RPCServer.JobStatus", jobID, &result)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return job.Status{}, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return result, err
}
func (p *RPCClientV6) SyncStatus(ctx context.Context, ref string) ([]string, error) {
var result []string
err := p.client.Call("RPCServer.SyncStatus", ref, &result)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return nil, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return result, err
}
func (p *RPCClientV6) GitRepoConfig(ctx context.Context, regenerate bool) (v6.GitConfig, error) {
var result v6.GitConfig
err := p.client.Call("RPCServer.GitRepoConfig", regenerate, &result)
if _, ok := err.(rpc.ServerError); !ok && err != nil {
return v6.GitConfig{}, remote.FatalError{err}
}
if err != nil {
err = remoteApplicationError(err)
}
return result, err
}