
Now that we have most of the services required for use with containerd, it was found that common patterns were used throughout services. By defining a central `errdefs` package, we ensure that services will map errors to and from grpc consistently and cleanly. One can decorate an error with as much context as necessary, using `pkg/errors` and still have the error mapped correctly via grpc. We make a few sacrifices. At this point, the common errors we use across the repository all map directly to grpc error codes. While this seems positively crazy, it actually works out quite well. The error conditions that were specific weren't super necessary and the ones that were necessary now simply have better context information. We lose the ability to add new codes, but this constraint may not be a bad thing. Effectively, as long as one uses the errors defined in `errdefs`, the error class will be mapped correctly across the grpc boundary and everything will be good. If you don't use those definitions, the error maps to "unknown" and the error message is preserved. Signed-off-by: Stephen J Day <stephen.day@docker.com>
97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package namespaces
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
api "github.com/containerd/containerd/api/services/namespaces/v1"
|
|
"github.com/containerd/containerd/errdefs"
|
|
"github.com/containerd/containerd/namespaces"
|
|
"github.com/gogo/protobuf/types"
|
|
)
|
|
|
|
func NewStoreFromClient(client api.NamespacesClient) namespaces.Store {
|
|
return &remote{client: client}
|
|
}
|
|
|
|
type remote struct {
|
|
client api.NamespacesClient
|
|
}
|
|
|
|
func (r *remote) Create(ctx context.Context, namespace string, labels map[string]string) error {
|
|
var req api.CreateNamespaceRequest
|
|
|
|
req.Namespace = api.Namespace{
|
|
Name: namespace,
|
|
Labels: labels,
|
|
}
|
|
|
|
_, err := r.client.Create(ctx, &req)
|
|
if err != nil {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *remote) Labels(ctx context.Context, namespace string) (map[string]string, error) {
|
|
var req api.GetNamespaceRequest
|
|
req.Name = namespace
|
|
|
|
resp, err := r.client.Get(ctx, &req)
|
|
if err != nil {
|
|
return nil, errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return resp.Namespace.Labels, nil
|
|
}
|
|
|
|
func (r *remote) SetLabel(ctx context.Context, namespace, key, value string) error {
|
|
var req api.UpdateNamespaceRequest
|
|
|
|
req.Namespace = api.Namespace{
|
|
Name: namespace,
|
|
Labels: map[string]string{key: value},
|
|
}
|
|
|
|
req.UpdateMask = &types.FieldMask{
|
|
Paths: []string{strings.Join([]string{"labels", key}, ".")},
|
|
}
|
|
|
|
_, err := r.client.Update(ctx, &req)
|
|
if err != nil {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *remote) List(ctx context.Context) ([]string, error) {
|
|
var req api.ListNamespacesRequest
|
|
|
|
resp, err := r.client.List(ctx, &req)
|
|
if err != nil {
|
|
return nil, errdefs.FromGRPC(err)
|
|
}
|
|
|
|
var namespaces []string
|
|
|
|
for _, ns := range resp.Namespaces {
|
|
namespaces = append(namespaces, ns.Name)
|
|
}
|
|
|
|
return namespaces, nil
|
|
}
|
|
|
|
func (r *remote) Delete(ctx context.Context, namespace string) error {
|
|
var req api.DeleteNamespaceRequest
|
|
|
|
req.Name = namespace
|
|
_, err := r.client.Delete(ctx, &req)
|
|
if err != nil {
|
|
return errdefs.FromGRPC(err)
|
|
}
|
|
|
|
return nil
|
|
}
|