Skip to content

Commit 7707f36

Browse files
authored
feat(agentctl): Add config get/update commands (#1709)
1 parent e90a0ef commit 7707f36

32 files changed

+783
-327
lines changed

client/client_api.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ type ModelInfo = generic.ModelDetail
2626

2727
type StateItem = generic.StateItem
2828

29-
// ConfigClient defines the client-side interface for config.
30-
type ConfigClient interface {
29+
// ConfigClient ...
30+
// Deprecated: use GenericClient instead
31+
type ConfigClient = GenericClient
32+
33+
// GenericClient is the client-side interface for generic handler.
34+
type GenericClient interface {
3135
// KnownModels retrieves list of known modules.
3236
KnownModels(class string) ([]*ModelInfo, error)
3337

cmd/agentctl/api/types/client.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,9 @@
1414

1515
package types
1616

17-
type Model struct {
18-
Name string
19-
Class string
20-
Module string
21-
Type string
22-
Version string
23-
KeyPrefix string
24-
NameTemplate string `json:",omitempty"`
25-
ProtoName string
26-
ProtoFile string `json:",omitempty"`
27-
GoType string `json:",omitempty"`
28-
PkgPath string `json:",omitempty"`
29-
}
30-
3117
type ModelListOptions struct {
32-
Class string
18+
Class string
19+
Module string
3320
}
3421

3522
type SchedulerDumpOptions struct {
@@ -45,3 +32,7 @@ type SchedulerResyncOptions struct {
4532
Retry bool
4633
Verbose bool
4734
}
35+
36+
type SchedulerHistoryOptions struct {
37+
Count int
38+
}

cmd/agentctl/api/types/types.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,35 @@ type Version struct {
2626
Version string
2727
GitCommit string
2828
GitBranch string
29+
2930
BuildUser string
3031
BuildHost string
3132
BuildTime int64
33+
3234
GoVersion string
3335
OS string
3436
Arch string
35-
}
3637

37-
// Ping contains response of Engine API:
38-
// GET "/_ping"
39-
type Ping struct {
4038
APIVersion string
4139
OSType string
4240
}
4341

4442
type Logger struct {
45-
Logger string `json:"logger,omitempty"`
43+
Logger string
4644
Level string `json:"level,omitempty"`
4745
}
46+
47+
// Model provides info about registered model.
48+
type Model struct {
49+
Name string
50+
Class string
51+
Module string
52+
Type string
53+
Version string
54+
KeyPrefix string
55+
NameTemplate string `json:",omitempty"`
56+
ProtoName string
57+
ProtoFile string `json:",omitempty"`
58+
GoType string `json:",omitempty"`
59+
PkgPath string `json:",omitempty"`
60+
}

cmd/agentctl/cli/cli.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,19 @@ func (cli *AgentCli) Initialize(opts *ClientOptions, ops ...InitializeOpt) error
147147
return err
148148
}
149149
}
150-
151150
if opts.Debug {
152151
debug.Enable()
153152
SetLogLevel("debug")
154153
} else {
155154
SetLogLevel(opts.LogLevel)
156155
}
157-
158156
cfg, err := MakeConfig()
159157
if err != nil {
160158
return err
161159
}
162160
if opts.Debug {
163161
logging.Debug(cfg.DebugOutput())
164162
}
165-
166163
if cli.client == nil {
167164
clientOptions := buildClientOptions(cfg)
168165
cli.client, err = client.NewClientWithOpts(clientOptions...)
@@ -189,7 +186,6 @@ func buildClientOptions(cfg *Config) []client.Opt {
189186
client.WithEtcdEndpoints(cfg.EtcdEndpoints),
190187
client.WithEtcdDialTimeout(cfg.EtcdDialTimeout),
191188
}
192-
193189
if cfg.ShouldUseSecureGRPC() {
194190
clientOpts = append(clientOpts, client.WithGrpcTLS(
195191
cfg.GRPCSecure.CertFile,
@@ -214,26 +210,24 @@ func buildClientOptions(cfg *Config) []client.Opt {
214210
cfg.KVDBSecure.SkipVerify,
215211
))
216212
}
217-
218213
return clientOpts
219214
}
220215

221216
func (cli *AgentCli) initializeFromClient() {
222217
logging.Debugf("initializeFromClient (DefaultVersion: %v)", cli.DefaultVersion())
223218

224-
ping, err := cli.client.Ping(context.Background())
219+
version, err := cli.client.AgentVersion(context.Background())
225220
if err != nil {
226221
// Default to true if we fail to connect to daemon
227222
cli.serverInfo = ServerInfo{}
228223

229-
if ping.APIVersion != "" {
230-
cli.client.NegotiateAPIVersionPing(ping)
224+
if version != nil && version.APIVersion != "" {
225+
cli.client.NegotiateAPIVersionPing(version)
231226
}
232227
return
233228
}
234-
235229
cli.serverInfo = ServerInfo{
236-
OSType: ping.OSType,
230+
OSType: version.OSType,
237231
}
238-
cli.client.NegotiateAPIVersionPing(ping)
232+
cli.client.NegotiateAPIVersionPing(version)
239233
}

cmd/agentctl/cli/flags.go

-2
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ func SetLogLevel(logLevel string) {
8484
logging.DefaultLogger.SetLevel(logging.WarnLevel)
8585
return
8686
}
87-
8887
lvl, err := logrus.ParseLevel(logLevel)
8988
if err != nil {
9089
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
9190
os.Exit(1)
9291
}
93-
9492
logrus.SetLevel(lvl)
9593
logging.DefaultLogger.SetLevel(logging.ParseLogLevel(logLevel))
9694
}

cmd/agentctl/client/api.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.ligato.io/vpp-agent/v3/client"
1313
"go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types"
1414
"go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
15+
"go.ligato.io/vpp-agent/v3/proto/ligato/configurator"
1516
"go.ligato.io/vpp-agent/v3/proto/ligato/kvscheduler"
1617
)
1718

@@ -23,7 +24,8 @@ type APIClient interface {
2324
VppAPIClient
2425
MetricsAPIClient
2526

26-
ConfigClient() (client.ConfigClient, error)
27+
GenericClient() (client.GenericClient, error)
28+
ConfiguratorClient() (configurator.ConfiguratorServiceClient, error)
2729

2830
AgentHost() string
2931
Version() string
@@ -32,14 +34,13 @@ type APIClient interface {
3234
HTTPClient() *http.Client
3335
AgentVersion(ctx context.Context) (*types.Version, error)
3436
NegotiateAPIVersion(ctx context.Context)
35-
NegotiateAPIVersionPing(types.Ping)
37+
NegotiateAPIVersionPing(version *types.Version)
3638
Close() error
3739
}
3840

3941
// InfraAPIClient defines API client methods for the system
4042
type InfraAPIClient interface {
4143
Status(ctx context.Context) (*probe.ExposedStatus, error)
42-
Ping(ctx context.Context) (types.Ping, error)
4344
LoggerList(ctx context.Context) ([]types.Logger, error)
4445
LoggerSet(ctx context.Context, logger, level string) error
4546
}
@@ -54,11 +55,13 @@ type SchedulerAPIClient interface {
5455
SchedulerDump(ctx context.Context, opts types.SchedulerDumpOptions) ([]api.KVWithMetadata, error)
5556
SchedulerValues(ctx context.Context, opts types.SchedulerValuesOptions) ([]*kvscheduler.BaseValueStatus, error)
5657
SchedulerResync(ctx context.Context, opts types.SchedulerResyncOptions) (*api.RecordedTxn, error)
58+
SchedulerHistory(ctx context.Context, opts types.SchedulerHistoryOptions) (api.RecordedTxns, error)
5759
}
5860

5961
// VppAPIClient defines API client methods for the VPP
6062
type VppAPIClient interface {
6163
VppRunCli(ctx context.Context, cmd string) (reply string, err error)
64+
VppGetStats(ctx context.Context, typ string) error
6265
}
6366

6467
type MetricsAPIClient interface {

cmd/agentctl/client/client.go

+17-20
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ import (
2828

2929
"github.com/coreos/etcd/clientv3"
3030
"github.com/docker/docker/api/types/versions"
31-
"google.golang.org/grpc"
32-
"google.golang.org/grpc/credentials"
33-
3431
"go.ligato.io/cn-infra/v2/db/keyval"
3532
"go.ligato.io/cn-infra/v2/db/keyval/etcd"
3633
"go.ligato.io/cn-infra/v2/logging"
3734
"go.ligato.io/cn-infra/v2/logging/logrus"
35+
"google.golang.org/grpc"
36+
"google.golang.org/grpc/credentials"
3837

3938
"go.ligato.io/vpp-agent/v3/client"
4039
"go.ligato.io/vpp-agent/v3/client/remoteclient"
4140
"go.ligato.io/vpp-agent/v3/cmd/agentctl/api"
4241
"go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types"
4342
"go.ligato.io/vpp-agent/v3/pkg/debug"
43+
"go.ligato.io/vpp-agent/v3/proto/ligato/configurator"
4444
)
4545

4646
const (
@@ -107,16 +107,13 @@ func NewClientWithOpts(ops ...Opt) (*Client, error) {
107107
grpcPort: DefaultPortGRPC,
108108
httpPort: DefaultPortHTTP,
109109
}
110-
111110
for _, op := range ops {
112111
if err := op(c); err != nil {
113112
return nil, err
114113
}
115114
}
116-
117115
c.grpcAddr = net.JoinHostPort(c.host, strconv.Itoa(c.grpcPort))
118116
c.httpAddr = net.JoinHostPort(c.host, strconv.Itoa(c.httpPort))
119-
120117
return c, nil
121118
}
122119

@@ -155,15 +152,23 @@ func (c *Client) GRPCConn() (*grpc.ClientConn, error) {
155152
return c.grpcClient, nil
156153
}
157154

158-
// ConfigClient returns "remoteclient" with gRPC connection.
159-
func (c *Client) ConfigClient() (client.ConfigClient, error) {
155+
func (c *Client) GenericClient() (client.GenericClient, error) {
160156
conn, err := c.GRPCConn()
161157
if err != nil {
162158
return nil, err
163159
}
164160
return remoteclient.NewClientGRPC(conn), nil
165161
}
166162

163+
// ConfiguratorClient returns "confi" with gRPC connection.
164+
func (c *Client) ConfiguratorClient() (configurator.ConfiguratorServiceClient, error) {
165+
conn, err := c.GRPCConn()
166+
if err != nil {
167+
return nil, err
168+
}
169+
return configurator.NewConfiguratorServiceClient(conn), nil
170+
}
171+
167172
// HTTPClient returns configured HTTP client.
168173
func (c *Client) HTTPClient() *http.Client {
169174
if c.httpClient == nil {
@@ -231,35 +236,32 @@ func (c *Client) getAPIPath(ctx context.Context, p string, query url.Values) str
231236

232237
func (c *Client) NegotiateAPIVersion(ctx context.Context) {
233238
if !c.manualOverride {
234-
ping, _ := c.Ping(ctx)
235-
c.negotiateAPIVersionPing(ping)
239+
version, _ := c.AgentVersion(ctx)
240+
c.negotiateAPIVersionPing(version)
236241
}
237242
}
238243

239-
func (c *Client) NegotiateAPIVersionPing(p types.Ping) {
244+
func (c *Client) NegotiateAPIVersionPing(p *types.Version) {
240245
if !c.manualOverride {
241246
c.negotiateAPIVersionPing(p)
242247
}
243248
}
244249

245250
// negotiateAPIVersionPing queries the API and updates the version to match the
246251
// API version. Any errors are silently ignored.
247-
func (c *Client) negotiateAPIVersionPing(p types.Ping) {
252+
func (c *Client) negotiateAPIVersionPing(p *types.Version) {
248253
// try the latest version before versioning headers existed
249254
if p.APIVersion == "" {
250255
p.APIVersion = "0.1"
251256
}
252-
253257
// if the client is not initialized with a version, start with the latest supported version
254258
if c.version == "" {
255259
c.version = api.DefaultVersion
256260
}
257-
258261
// if server version is lower than the client version, downgrade
259262
if versions.LessThan(p.APIVersion, c.version) {
260263
c.version = p.APIVersion
261264
}
262-
263265
// Store the results, so that automatic API version negotiation (if enabled)
264266
// won't be performed on the next request.
265267
if c.negotiateVersion {
@@ -272,9 +274,7 @@ func connectGrpc(addr string, tc *tls.Config) (*grpc.ClientConn, error) {
272274
if tc != nil {
273275
dialOpt = grpc.WithTransportCredentials(credentials.NewTLS(tc))
274276
}
275-
276277
logging.Debugf("dialing grpc address: %v", addr)
277-
278278
return grpc.Dial(addr, dialOpt)
279279
}
280280

@@ -285,12 +285,10 @@ func connectEtcd(endpoints []string, dialTimeout time.Duration, tc *tls.Config)
285285
} else {
286286
log.SetLevel(logging.WarnLevel)
287287
}
288-
289288
dt := defaultEtcdDialTimeout
290289
if dialTimeout != 0 {
291290
dt = dialTimeout
292291
}
293-
294292
cfg := etcd.ClientConfig{
295293
Config: &clientv3.Config{
296294
Endpoints: endpoints,
@@ -299,7 +297,6 @@ func connectEtcd(endpoints []string, dialTimeout time.Duration, tc *tls.Config)
299297
},
300298
OpTimeout: defaultEtcdOpTimeout,
301299
}
302-
303300
kvdb, err := etcd.NewEtcdConnectionWithBytes(cfg, log)
304301
if err != nil {
305302
return nil, err

0 commit comments

Comments
 (0)