Skip to content

Commit

Permalink
Merge pull request k0sproject#801 from soider/airgap-reduce-delay-bet…
Browse files Browse the repository at this point in the history
…ween-attempts

Reduce amounts of attempts in OCI bundle import
  • Loading branch information
jnummelin authored Mar 25, 2021
2 parents afcc624 + 244c212 commit 4224326
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
3 changes: 3 additions & 0 deletions docs/airgap-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ You also need to have containerd CLI management tool `ctr` installed on the work
## Steps

#### 1. Create OCI bundle

k0s supports only uncompressed image bundles.

##### 1.1 Using Docker
Use following commands to build OCI bundle by utilizing your docker environment.
```
Expand Down
64 changes: 37 additions & 27 deletions pkg/component/worker/ocibundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@ func (a *OCIBundleReconciler) Init() error {
return util.InitDirectory(a.k0sVars.OCIBundleDir, constant.ManifestsDirMode)
}

func (a *OCIBundleReconciler) Run() error {
files, err := ioutil.ReadDir(a.k0sVars.OCIBundleDir)
if err != nil {
return fmt.Errorf("can't read bundles directory")
}
if len(files) == 0 {
return nil
}
var client *containerd.Client
sock := filepath.Join(a.k0sVars.RunDir, "containerd.sock")
err = retry.Do(func() error {
client, err = containerd.New(sock, containerd.WithDefaultNamespace("k8s.io"))
if err != nil {
logrus.WithError(err).Errorf("can't connect to containerd socket %s", sock)
return err
}
_, err := client.ListImages(context.Background())
if err != nil {
logrus.WithError(err).Errorf("can't use containerd client")
return err
}
return nil
}, retry.Delay(time.Second*5))
if err != nil {
return fmt.Errorf("can't connect to containerd socket %s: %v", sock, err)
}
defer client.Close()

for _, file := range files {
if err := a.unpackBundle(client, a.k0sVars.OCIBundleDir+"/"+file.Name()); err != nil {
logrus.WithError(err).Errorf("can't unpack bundle %s", file.Name())
return fmt.Errorf("can't unpack bundle %s: %w", file.Name(), err)
}
}
return nil
}

func (a OCIBundleReconciler) unpackBundle(client *containerd.Client, bundlePath string) error {
r, err := os.Open(bundlePath)
if err != nil {
Expand All @@ -48,33 +85,6 @@ func (a OCIBundleReconciler) unpackBundle(client *containerd.Client, bundlePath
return nil
}

func (a *OCIBundleReconciler) Run() error {
files, err := ioutil.ReadDir(a.k0sVars.OCIBundleDir)
if err != nil {
return fmt.Errorf("can't read bundles directory")
}
if len(files) == 0 {
return nil
}
return retry.Do(func() error {
sock := filepath.Join(a.k0sVars.RunDir, "containerd.sock")
client, err := containerd.New(sock, containerd.WithDefaultNamespace("k8s.io"))

if err != nil {
return fmt.Errorf("can't connect to containerd socket %s: %v", sock, err)
}
defer client.Close()
for _, file := range files {
if err := a.unpackBundle(client, a.k0sVars.OCIBundleDir+"/"+file.Name()); err != nil {
return fmt.Errorf("can't unpack bundle %s: %w", file.Name(), err)
}
}
return nil
}, retry.Delay(time.Second*5),
retry.Attempts(50))

}

func (a *OCIBundleReconciler) Stop() error {
return nil
}
Expand Down

0 comments on commit 4224326

Please sign in to comment.