github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/agent/resource.go (about) 1 package agent 2 3 import ( 4 "context" 5 6 "github.com/docker/swarmkit/api" 7 ) 8 9 type resourceAllocator struct { 10 agent *Agent 11 } 12 13 // ResourceAllocator is an interface to allocate resource such as 14 // network attachments from a worker node. 15 type ResourceAllocator interface { 16 // AttachNetwork creates a network attachment in the manager 17 // given a target network and a unique ID representing the 18 // connecting entity and optionally a list of ipv4/ipv6 19 // addresses to be assigned to the attachment. AttachNetwork 20 // returns a unique ID for the attachment if successful or an 21 // error in case of failure. 22 AttachNetwork(ctx context.Context, id, target string, addresses []string) (string, error) 23 24 // DetachNetworks deletes a network attachment for the passed 25 // attachment ID. The attachment ID is obtained from a 26 // previous AttachNetwork call. 27 DetachNetwork(ctx context.Context, aID string) error 28 } 29 30 // AttachNetwork creates a network attachment. 31 func (r *resourceAllocator) AttachNetwork(ctx context.Context, id, target string, addresses []string) (string, error) { 32 var taskID string 33 if err := r.agent.withSession(ctx, func(session *session) error { 34 client := api.NewResourceAllocatorClient(session.conn.ClientConn) 35 r, err := client.AttachNetwork(ctx, &api.AttachNetworkRequest{ 36 Config: &api.NetworkAttachmentConfig{ 37 Target: target, 38 Addresses: addresses, 39 }, 40 ContainerID: id, 41 }) 42 if err != nil { 43 return err 44 } 45 taskID = r.AttachmentID 46 return nil 47 }); err != nil { 48 return "", err 49 } 50 51 return taskID, nil 52 } 53 54 // DetachNetwork deletes a network attachment. 55 func (r *resourceAllocator) DetachNetwork(ctx context.Context, aID string) error { 56 return r.agent.withSession(ctx, func(session *session) error { 57 client := api.NewResourceAllocatorClient(session.conn.ClientConn) 58 _, err := client.DetachNetwork(ctx, &api.DetachNetworkRequest{ 59 AttachmentID: aID, 60 }) 61 62 return err 63 }) 64 } 65 66 // ResourceAllocator provides an interface to access resource 67 // allocation methods such as AttachNetwork and DetachNetwork. 68 func (a *Agent) ResourceAllocator() ResourceAllocator { 69 return &resourceAllocator{agent: a} 70 }