github.com/demonoid81/containerd@v1.3.4/namespaces.go (about)

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