github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/leases/proxy/manager.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 proxy
    18  
    19  import (
    20  	"context"
    21  
    22  	leasesapi "github.com/containerd/containerd/api/services/leases/v1"
    23  	"github.com/containerd/containerd/errdefs"
    24  	"github.com/containerd/containerd/leases"
    25  )
    26  
    27  type proxyManager struct {
    28  	client leasesapi.LeasesClient
    29  }
    30  
    31  // NewLeaseManager returns a lease manager which communicates
    32  // through a grpc lease service.
    33  func NewLeaseManager(client leasesapi.LeasesClient) leases.Manager {
    34  	return &proxyManager{
    35  		client: client,
    36  	}
    37  }
    38  
    39  func (pm *proxyManager) Create(ctx context.Context, opts ...leases.Opt) (leases.Lease, error) {
    40  	l := leases.Lease{}
    41  	for _, opt := range opts {
    42  		if err := opt(&l); err != nil {
    43  			return leases.Lease{}, err
    44  		}
    45  	}
    46  	resp, err := pm.client.Create(ctx, &leasesapi.CreateRequest{
    47  		ID:     l.ID,
    48  		Labels: l.Labels,
    49  	})
    50  	if err != nil {
    51  		return leases.Lease{}, errdefs.FromGRPC(err)
    52  	}
    53  
    54  	return leases.Lease{
    55  		ID:        resp.Lease.ID,
    56  		CreatedAt: resp.Lease.CreatedAt,
    57  		Labels:    resp.Lease.Labels,
    58  	}, nil
    59  }
    60  
    61  func (pm *proxyManager) Delete(ctx context.Context, l leases.Lease, opts ...leases.DeleteOpt) error {
    62  	var do leases.DeleteOptions
    63  	for _, opt := range opts {
    64  		if err := opt(ctx, &do); err != nil {
    65  			return err
    66  		}
    67  	}
    68  
    69  	_, err := pm.client.Delete(ctx, &leasesapi.DeleteRequest{
    70  		ID:   l.ID,
    71  		Sync: do.Synchronous,
    72  	})
    73  	return errdefs.FromGRPC(err)
    74  }
    75  
    76  func (pm *proxyManager) List(ctx context.Context, filters ...string) ([]leases.Lease, error) {
    77  	resp, err := pm.client.List(ctx, &leasesapi.ListRequest{
    78  		Filters: filters,
    79  	})
    80  	if err != nil {
    81  		return nil, errdefs.FromGRPC(err)
    82  	}
    83  	l := make([]leases.Lease, len(resp.Leases))
    84  	for i := range resp.Leases {
    85  		l[i] = leases.Lease{
    86  			ID:        resp.Leases[i].ID,
    87  			CreatedAt: resp.Leases[i].CreatedAt,
    88  			Labels:    resp.Leases[i].Labels,
    89  		}
    90  	}
    91  
    92  	return l, nil
    93  }
    94  
    95  func (pm *proxyManager) AddResource(ctx context.Context, lease leases.Lease, r leases.Resource) error {
    96  	_, err := pm.client.AddResource(ctx, &leasesapi.AddResourceRequest{
    97  		ID: lease.ID,
    98  		Resource: leasesapi.Resource{
    99  			ID:   r.ID,
   100  			Type: r.Type,
   101  		},
   102  	})
   103  	return errdefs.FromGRPC(err)
   104  }
   105  
   106  func (pm *proxyManager) DeleteResource(ctx context.Context, lease leases.Lease, r leases.Resource) error {
   107  	_, err := pm.client.DeleteResource(ctx, &leasesapi.DeleteResourceRequest{
   108  		ID: lease.ID,
   109  		Resource: leasesapi.Resource{
   110  			ID:   r.ID,
   111  			Type: r.Type,
   112  		},
   113  	})
   114  	return errdefs.FromGRPC(err)
   115  }
   116  
   117  func (pm *proxyManager) ListResources(ctx context.Context, lease leases.Lease) ([]leases.Resource, error) {
   118  	resp, err := pm.client.ListResources(ctx, &leasesapi.ListResourcesRequest{
   119  		ID: lease.ID,
   120  	})
   121  	if err != nil {
   122  		return nil, errdefs.FromGRPC(err)
   123  	}
   124  
   125  	rs := make([]leases.Resource, 0, len(resp.Resources))
   126  	for _, i := range resp.Resources {
   127  		rs = append(rs, leases.Resource{
   128  			ID:   i.ID,
   129  			Type: i.Type,
   130  		})
   131  	}
   132  	return rs, nil
   133  }