mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-26 09:01:17 +02:00
chore: refactor container image import code to avoid panics (#518)
Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
This commit is contained in:
parent
7cb8485788
commit
c24f1531cb
1
go.mod
1
go.mod
@ -27,6 +27,7 @@ require (
|
|||||||
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
|
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
|
||||||
github.com/google/uuid v1.0.0
|
github.com/google/uuid v1.0.0
|
||||||
github.com/googleapis/gnostic v0.2.0 // indirect
|
github.com/googleapis/gnostic v0.2.0 // indirect
|
||||||
|
github.com/hashicorp/go-multierror v1.0.0
|
||||||
github.com/hashicorp/golang-lru v0.5.0 // indirect
|
github.com/hashicorp/golang-lru v0.5.0 // indirect
|
||||||
github.com/hpcloud/tail v1.0.0 // indirect
|
github.com/hpcloud/tail v1.0.0 // indirect
|
||||||
github.com/imdario/mergo v0.3.6 // indirect
|
github.com/imdario/mergo v0.3.6 // indirect
|
||||||
|
4
go.sum
4
go.sum
@ -58,6 +58,10 @@ github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
|
|||||||
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/googleapis/gnostic v0.2.0 h1:l6N3VoaVzTncYYW+9yOz2LJJammFZGBO13sqgEhpy9g=
|
github.com/googleapis/gnostic v0.2.0 h1:l6N3VoaVzTncYYW+9yOz2LJJammFZGBO13sqgEhpy9g=
|
||||||
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
|
||||||
|
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||||
|
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
|
||||||
|
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||||
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
|
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
|
||||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||||
|
@ -8,11 +8,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
"github.com/containerd/containerd/defaults"
|
"github.com/containerd/containerd/defaults"
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
|
multierror "github.com/hashicorp/go-multierror"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/talos-systems/talos/internal/app/init/pkg/system/conditions"
|
"github.com/talos-systems/talos/internal/app/init/pkg/system/conditions"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,25 +38,24 @@ func Import(namespace string, reqs ...*ImportRequest) (err error) {
|
|||||||
// nolint: errcheck
|
// nolint: errcheck
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
errCh := make(chan error)
|
||||||
|
var result *multierror.Error
|
||||||
wg.Add(len(reqs))
|
|
||||||
|
|
||||||
for _, req := range reqs {
|
for _, req := range reqs {
|
||||||
go func(wg *sync.WaitGroup, r *ImportRequest) {
|
go func(errCh chan<- error, r *ImportRequest) {
|
||||||
defer wg.Done()
|
errCh <- func() error {
|
||||||
|
|
||||||
tarball, err := os.Open(r.Path)
|
tarball, err := os.Open(r.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return errors.Wrapf(err, "error opening %v", r.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
imgs, err := client.Import(ctx, tarball, r.Options...)
|
imgs, err := client.Import(ctx, tarball, r.Options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return errors.Wrapf(err, "error importing %v", r.Path)
|
||||||
}
|
}
|
||||||
if err = tarball.Close(); err != nil {
|
if err = tarball.Close(); err != nil {
|
||||||
panic(err)
|
return errors.Wrapf(err, "error closing %v", r.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, img := range imgs {
|
for _, img := range imgs {
|
||||||
@ -63,13 +63,18 @@ func Import(namespace string, reqs ...*ImportRequest) (err error) {
|
|||||||
log.Printf("unpacking %s (%s)\n", img.Name, img.Target.Digest)
|
log.Printf("unpacking %s (%s)\n", img.Name, img.Target.Digest)
|
||||||
err = image.Unpack(ctx, containerd.DefaultSnapshotter)
|
err = image.Unpack(ctx, containerd.DefaultSnapshotter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return errors.Wrapf(err, "error unpacking %v", img.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(&wg, req)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
}()
|
||||||
|
}(errCh, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
for range reqs {
|
||||||
|
result = multierror.Append(result, <-errCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.ErrorOrNil()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user