From c6fee6d01a427bdab9781d352e53250ee63ced69 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 5 Jul 2022 13:20:10 +0300 Subject: [PATCH] [#240] pool: Add `SyncContainerWithNetwork` It is a helper function that uses a `Pool` to fetch network information and apply it to the container (change the container if it does not fit the network). Currently, only applies the homomorphic hashing parameter. Signed-off-by: Pavel Karpy --- pool/pool.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pool/pool.go b/pool/pool.go index b51d21a..42caaf5 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -1811,3 +1811,26 @@ func (p *Pool) Close() { p.cancel() <-p.closedCh } + +// SyncContainerWithNetwork applies network configuration received via +// the Pool to the container. Changes the container if it does not satisfy +// network configuration. +// +// Pool and container MUST not be nil. +// +// Returns any error that does not allow reading configuration +// from the network. +func SyncContainerWithNetwork(ctx context.Context, cnr *container.Container, p *Pool) error { + ni, err := p.NetworkInfo(ctx) + if err != nil { + return fmt.Errorf("network info: %w", err) + } + + if ni == nil { + return errors.New("empty network info") + } + + container.ApplyNetworkConfig(cnr, *ni) + + return nil +}