Add leases manager interface
Add leases manager to the leases package and use the interface on the client and service. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
@@ -29,8 +29,8 @@ func WithLease(ctx context.Context, lid string) context.Context {
|
||||
return withGRPCLeaseHeader(ctx, lid)
|
||||
}
|
||||
|
||||
// Lease returns the lease from the context.
|
||||
func Lease(ctx context.Context) (string, bool) {
|
||||
// FromContext returns the lease from the context.
|
||||
func FromContext(ctx context.Context) (string, bool) {
|
||||
lid, ok := ctx.Value(leaseKey{}).(string)
|
||||
if !ok {
|
||||
return fromGRPCHeader(ctx)
|
||||
|
||||
43
leases/id.go
Normal file
43
leases/id.go
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright The containerd Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package leases
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WithRandomID sets the lease ID to a random unique value
|
||||
func WithRandomID() Opt {
|
||||
return func(l *Lease) error {
|
||||
t := time.Now()
|
||||
var b [3]byte
|
||||
rand.Read(b[:])
|
||||
l.ID = fmt.Sprintf("%d-%s", t.Nanosecond(), base64.URLEncoding.EncodeToString(b[:]))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithID sets the ID for the lease
|
||||
func WithID(id string) Opt {
|
||||
return func(l *Lease) error {
|
||||
l.ID = id
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,60 @@
|
||||
/*
|
||||
Copyright The containerd Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package leases
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LeaseOpt func(*Lease)
|
||||
// Opt is used to set options on a lease
|
||||
type Opt func(*Lease) error
|
||||
|
||||
type LeaseManager interface {
|
||||
Create(...LeaseOpt) (Lease, error)
|
||||
Delete(Lease) error
|
||||
List(...string) ([]Lease, error)
|
||||
// Manager is used to create, list, and remove leases
|
||||
type Manager interface {
|
||||
Create(context.Context, ...Opt) (Lease, error)
|
||||
Delete(context.Context, Lease) error
|
||||
List(context.Context, ...string) ([]Lease, error)
|
||||
}
|
||||
|
||||
// Lease retains resources to prevent cleanup before
|
||||
// the resources can be fully referenced.
|
||||
type Lease struct {
|
||||
ID string
|
||||
CreatedAt time.Time
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
// WithLabels sets labels on a lease
|
||||
func WithLabels(labels map[string]string) Opt {
|
||||
return func(l *Lease) error {
|
||||
l.Labels = labels
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithExpiration sets an expiration on the lease
|
||||
func WithExpiration(d time.Duration) Opt {
|
||||
return func(l *Lease) error {
|
||||
if l.Labels == nil {
|
||||
l.Labels = map[string]string{}
|
||||
}
|
||||
l.Labels["containerd.io/gc.expire"] = time.Now().Add(d).Format(time.RFC3339)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
84
leases/proxy/manager.go
Normal file
84
leases/proxy/manager.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright The containerd Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
leasesapi "github.com/containerd/containerd/api/services/leases/v1"
|
||||
"github.com/containerd/containerd/leases"
|
||||
)
|
||||
|
||||
type proxyManager struct {
|
||||
client leasesapi.LeasesClient
|
||||
}
|
||||
|
||||
// NewLeaseManager returns a lease manager which communicates
|
||||
// through a grpc lease service.
|
||||
func NewLeaseManager(client leasesapi.LeasesClient) leases.Manager {
|
||||
return &proxyManager{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (pm *proxyManager) Create(ctx context.Context, opts ...leases.Opt) (leases.Lease, error) {
|
||||
l := leases.Lease{}
|
||||
for _, opt := range opts {
|
||||
if err := opt(&l); err != nil {
|
||||
return leases.Lease{}, err
|
||||
}
|
||||
}
|
||||
resp, err := pm.client.Create(ctx, &leasesapi.CreateRequest{
|
||||
ID: l.ID,
|
||||
Labels: l.Labels,
|
||||
})
|
||||
if err != nil {
|
||||
return leases.Lease{}, err
|
||||
}
|
||||
|
||||
return leases.Lease{
|
||||
ID: resp.Lease.ID,
|
||||
CreatedAt: resp.Lease.CreatedAt,
|
||||
Labels: resp.Lease.Labels,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (pm *proxyManager) Delete(ctx context.Context, l leases.Lease) error {
|
||||
_, err := pm.client.Delete(ctx, &leasesapi.DeleteRequest{
|
||||
ID: l.ID,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (pm *proxyManager) List(ctx context.Context, filters ...string) ([]leases.Lease, error) {
|
||||
resp, err := pm.client.List(ctx, &leasesapi.ListRequest{
|
||||
Filters: filters,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l := make([]leases.Lease, len(resp.Leases))
|
||||
for i := range resp.Leases {
|
||||
l[i] = leases.Lease{
|
||||
ID: resp.Leases[i].ID,
|
||||
CreatedAt: resp.Leases[i].CreatedAt,
|
||||
Labels: resp.Leases[i].Labels,
|
||||
}
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
Reference in New Issue
Block a user