namespaces.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package containerd
  14. import (
  15. "context"
  16. "strings"
  17. api "github.com/containerd/containerd/api/services/namespaces/v1"
  18. "github.com/containerd/containerd/errdefs"
  19. "github.com/containerd/containerd/namespaces"
  20. "github.com/containerd/containerd/protobuf/types"
  21. )
  22. // NewNamespaceStoreFromClient returns a new namespace store
  23. func NewNamespaceStoreFromClient(client api.NamespacesClient) namespaces.Store {
  24. return &remoteNamespaces{client: client}
  25. }
  26. type remoteNamespaces struct {
  27. client api.NamespacesClient
  28. }
  29. func (r *remoteNamespaces) Create(ctx context.Context, namespace string, labels map[string]string) error {
  30. var req api.CreateNamespaceRequest
  31. req.Namespace = &api.Namespace{
  32. Name: namespace,
  33. Labels: labels,
  34. }
  35. _, err := r.client.Create(ctx, &req)
  36. if err != nil {
  37. return errdefs.FromGRPC(err)
  38. }
  39. return nil
  40. }
  41. func (r *remoteNamespaces) Labels(ctx context.Context, namespace string) (map[string]string, error) {
  42. var req api.GetNamespaceRequest
  43. req.Name = namespace
  44. resp, err := r.client.Get(ctx, &req)
  45. if err != nil {
  46. return nil, errdefs.FromGRPC(err)
  47. }
  48. return resp.Namespace.Labels, nil
  49. }
  50. func (r *remoteNamespaces) SetLabel(ctx context.Context, namespace, key, value string) error {
  51. var req api.UpdateNamespaceRequest
  52. req.Namespace = &api.Namespace{
  53. Name: namespace,
  54. Labels: map[string]string{key: value},
  55. }
  56. req.UpdateMask = &types.FieldMask{
  57. Paths: []string{strings.Join([]string{"labels", key}, ".")},
  58. }
  59. _, err := r.client.Update(ctx, &req)
  60. if err != nil {
  61. return errdefs.FromGRPC(err)
  62. }
  63. return nil
  64. }
  65. func (r *remoteNamespaces) List(ctx context.Context) ([]string, error) {
  66. var req api.ListNamespacesRequest
  67. resp, err := r.client.List(ctx, &req)
  68. if err != nil {
  69. return nil, errdefs.FromGRPC(err)
  70. }
  71. var namespaces []string
  72. for _, ns := range resp.Namespaces {
  73. namespaces = append(namespaces, ns.Name)
  74. }
  75. return namespaces, nil
  76. }
  77. func (r *remoteNamespaces) Delete(ctx context.Context, namespace string, opts ...namespaces.DeleteOpts) error {
  78. i := namespaces.DeleteInfo{
  79. Name: namespace,
  80. }
  81. for _, o := range opts {
  82. if err := o(ctx, &i); err != nil {
  83. return err
  84. }
  85. }
  86. req := api.DeleteNamespaceRequest{
  87. Name: namespace,
  88. }
  89. _, err := r.client.Delete(ctx, &req)
  90. if err != nil {
  91. return errdefs.FromGRPC(err)
  92. }
  93. return nil
  94. }