2020-04-29 22:12:58 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-05-02 18:54:03 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-04-29 22:12:58 +02:00
|
|
|
cliv1 "github.com/docker/api/cli/v1"
|
|
|
|
containersv1 "github.com/docker/api/containers/v1"
|
|
|
|
"github.com/docker/api/context/store"
|
|
|
|
"github.com/docker/api/server"
|
2020-04-29 23:44:01 +02:00
|
|
|
"github.com/docker/api/server/proxy"
|
2020-04-29 22:12:58 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serveOpts struct {
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
// ServeCommand returns the command to serve the API
|
2020-04-29 22:12:58 +02:00
|
|
|
func ServeCommand() *cobra.Command {
|
2020-05-20 15:56:07 +02:00
|
|
|
// FIXME(chris-crone): Should warn that specified context is ignored
|
2020-04-29 22:12:58 +02:00
|
|
|
var opts serveOpts
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Start an api server",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runServe(cmd.Context(), opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().StringVar(&opts.address, "address", "", "The address to listen to")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runServe(ctx context.Context, opts serveOpts) error {
|
2020-05-20 15:56:07 +02:00
|
|
|
s := server.New(ctx)
|
2020-04-29 22:12:58 +02:00
|
|
|
|
2020-05-16 16:39:08 +02:00
|
|
|
listener, err := server.CreateListener(opts.address)
|
2020-04-29 22:12:58 +02:00
|
|
|
if err != nil {
|
2020-05-16 16:39:08 +02:00
|
|
|
return errors.Wrap(err, "listen address "+opts.address)
|
2020-04-29 22:12:58 +02:00
|
|
|
}
|
2020-05-04 23:50:00 +02:00
|
|
|
// nolint errcheck
|
2020-04-29 23:44:01 +02:00
|
|
|
defer listener.Close()
|
2020-04-29 22:12:58 +02:00
|
|
|
|
2020-05-04 23:00:21 +02:00
|
|
|
p := proxy.NewContainerAPI()
|
2020-04-29 22:12:58 +02:00
|
|
|
|
|
|
|
containersv1.RegisterContainersServer(s, p)
|
2020-05-21 14:17:23 +02:00
|
|
|
cliv1.RegisterCliServer(s, &cliServer{})
|
2020-04-29 22:12:58 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
logrus.Info("stopping server")
|
|
|
|
s.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
logrus.WithField("address", opts.address).Info("serving daemon API")
|
|
|
|
|
|
|
|
// start the GRPC server to serve on the listener
|
2020-04-29 23:44:01 +02:00
|
|
|
return s.Serve(listener)
|
2020-04-29 22:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type cliServer struct {
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:42:11 +02:00
|
|
|
func (cs *cliServer) Contexts(ctx context.Context, request *cliv1.ContextsRequest) (*cliv1.ContextsResponse, error) {
|
2020-05-20 15:56:07 +02:00
|
|
|
s := store.ContextStore(ctx)
|
2020-04-29 22:12:58 +02:00
|
|
|
contexts, err := s.List()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return &cliv1.ContextsResponse{}, err
|
|
|
|
}
|
|
|
|
result := &cliv1.ContextsResponse{}
|
|
|
|
for _, c := range contexts {
|
|
|
|
result.Contexts = append(result.Contexts, &cliv1.Context{
|
|
|
|
Name: c.Name,
|
Change the way a context is stored
Initially we stored the context data in the `Metadata` of the context
but in hindsight this data would be better of in the `Endpoints` because
that's what it is used for.
Before:
```json
{
"Name": "aci",
"Metadata": {
"Type": "aci",
"Data": {
"key": "value"
}
},
"Endpoints": {
"docker": {}
}
}
```
After:
```json
{
"Name": "aci",
"Type": "aci",
"Metadata": {},
"Endpoints": {
"aci": {
"key": "value"
},
"docker": {}
}
}
```
With this change the contexts that we create are more in line with the contexts the docker cli creates.
It also makes the code less complicated since we don't need to marsal twice any more. The API is nicer too:
```go
// Get a context:
c, err := store.Get(contextName)
// Get the stored endpoint:
var aciContext store.AciContext
if err := contextStore.GetEndpoint(currentContext, &aciContext); err != nil {
return nil, err
}
```
2020-05-22 11:16:01 +02:00
|
|
|
ContextType: c.Type,
|
2020-04-29 22:12:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|