Skip to content

Commit

Permalink
allow to configure size and disk type of containers (#795)
Browse files Browse the repository at this point in the history
* allow to configure size and disk type of containers

fixes #793

* contd: wait for shim-log binary to be present on the system before
starting

* go mod tidy

* use the configured disk type for flist backend filesystem
  • Loading branch information
zaibon authored May 26, 2020
1 parent 655c74a commit cadfab1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 14 deletions.
15 changes: 15 additions & 0 deletions cmds/contd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"
"flag"
"os"
"os/exec"
"time"

"github.com/cenkalti/backoff/v3"
"github.com/rs/zerolog/log"

"github.com/threefoldtech/zbus"
Expand Down Expand Up @@ -38,6 +41,18 @@ func main() {
version.ShowAndExit(false)
}

// wait for shim-logs to be available before starting
log.Info().Msg("wait for shim-logs binary to be available")
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 0 //forever
_ = backoff.RetryNotify(func() error {
_, err := exec.LookPath("shim-logs")
return err
// return fmt.Errorf("wait forever")
}, bo, func(err error, d time.Duration) {
log.Warn().Err(err).Msgf("shim-logs binary not found, retying in %s", d.String())
})

if err := os.MkdirAll(moduleRoot, 0750); err != nil {
log.Fatal().Msgf("fail to create module root: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/shirou/gopsutil v2.19.11+incompatible
github.com/stretchr/testify v1.5.1
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae
github.com/threefoldtech/tfexplorer v0.2.7-0.20200506105748-dd92c058d776
github.com/threefoldtech/tfexplorer v0.3.1-0.20200520102753-5af0aaa729ab
github.com/threefoldtech/zbus v0.1.3
github.com/urfave/cli v1.22.3
github.com/vishvananda/netlink v1.0.0
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,8 @@ github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae h1:vgGSvdW5Lqg+I1aZOlG32uyE6xHpLdKhZzcTEktz5wM=
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae/go.mod h1:quDq6Se6jlGwiIKia/itDZxqC5rj6/8OdFyMMAwTxCs=
github.com/threefoldtech/tfexplorer v0.2.5/go.mod h1:TugylEDgMNKk4ZIzee9PpVmIcLNLaRLdiXuhHTt/AR0=
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505125555-7d20b5212c0e h1:HqNiMBz0RpYg8RBDQl67soy2WPjAWHCBPzb7jl1Dtxc=
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505125555-7d20b5212c0e/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505141628-4d7a0c749784 h1:CCyZTGGV7BVVIGDduxLtebFsVHxJt6i2Z7rq6ostfmU=
github.com/threefoldtech/tfexplorer v0.2.7-0.20200505141628-4d7a0c749784/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
github.com/threefoldtech/tfexplorer v0.2.7-0.20200506105748-dd92c058d776 h1:wRAZ10WYNNADcuEfQ1YlL+UepxkPIyUhtSjFrj8Vugs=
github.com/threefoldtech/tfexplorer v0.2.7-0.20200506105748-dd92c058d776/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
github.com/threefoldtech/tfexplorer v0.3.1-0.20200520102753-5af0aaa729ab h1:ScS48/VSgl3Eu25XGtex4z7ylGxc1KhHVyIRNc1LbmQ=
github.com/threefoldtech/tfexplorer v0.3.1-0.20200520102753-5af0aaa729ab/go.mod h1:ozAe2OYR4ALp2PtyKT1A6UiTI+0ZcFU2UfUaRd1P5eE=
github.com/threefoldtech/zbus v0.1.3 h1:18DnIzximRbATle5ZdZz0i84n/bCYB8k/gkhr2dXayc=
github.com/threefoldtech/zbus v0.1.3/go.mod h1:ZtiRpcqzEBJetVQDsEbw0p48h/AF3O1kf0tvd30I0BU=
github.com/threefoldtech/zos v0.2.4-rc2/go.mod h1:7A2oflcmSVsHFC4slOcydWgJyFBMFMH9wsaTRv+CnTA=
Expand Down
3 changes: 3 additions & 0 deletions pkg/flist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var (
DefaultMountOptions = MountOptions{
ReadOnly: false,
Limit: 256, //Mib
Type: SSDDevice,
}

//ReadOnlyMountOptions shortcut for readonly mount options
Expand All @@ -23,6 +24,8 @@ type MountOptions struct {
ReadOnly bool
// Limit size of read-write layer in Mib
Limit uint64
// Type of disk to use
Type DeviceType
}

//Flister is the interface for the flist module
Expand Down
12 changes: 10 additions & 2 deletions pkg/flist/flist.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,18 @@ func (f *flistModule) mount(name, url, storage string, opts pkg.MountOptions) (s

var args []string
if !opts.ReadOnly {
path, err := f.storage.CreateFilesystem(name, opts.Limit*mib, pkg.SSDDevice)
sublog.Info().Msgf("check if subvolume %s already exists", name)
// check if the filesystem doesn't already exists
path, err := f.storage.Path(name)
if err != nil {
return "", errors.Wrap(err, "failed to create read-write subvolume for 0-fs")
sublog.Info().Msgf("create new subvolume %s", name)
// and only create a new one if it doesn't exist
path, err = f.storage.CreateFilesystem(name, opts.Limit*mib, opts.Type)
if err != nil {
return "", errors.Wrap(err, "failed to create read-write subvolume for 0-fs")
}
}

args = append(args, "-backend", path)
} else {
args = append(args, "-ro")
Expand Down
18 changes: 16 additions & 2 deletions pkg/provision/primitives/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type ContainerCapacity struct {
CPU uint `json:"cpu"`
// Memory in MiB
Memory uint64 `json:"memory"`
//DiskType is the type of disk to use for root fs
DiskType pkg.DeviceType `json:"disk_type"`
// DiskSize of the root fs in MiB
DiskSize uint64 `json:"disk_size"`
}

func (p *Provisioner) containerProvision(ctx context.Context, reservation *provision.Reservation) (interface{}, error) {
Expand Down Expand Up @@ -109,8 +113,18 @@ func (p *Provisioner) containerProvisionImpl(ctx context.Context, reservation *p
}

log.Debug().Str("flist", config.FList).Msg("mounting flist")

rootfsMntOpt := pkg.MountOptions{
Limit: config.Capacity.DiskSize,
ReadOnly: false,
Type: config.Capacity.DiskType,
}
if rootfsMntOpt.Limit == 0 || rootfsMntOpt.Type == "" {
rootfsMntOpt = pkg.DefaultMountOptions
}

var mnt string
mnt, err = flistClient.Mount(config.FList, config.FlistStorage, pkg.DefaultMountOptions)
mnt, err = flistClient.NamedMount(reservation.ID, config.FList, config.FlistStorage, rootfsMntOpt)
if err != nil {
return ContainerResult{}, err
}
Expand Down Expand Up @@ -218,7 +232,7 @@ func (p *Provisioner) containerProvisionImpl(ctx context.Context, reservation *p
Entrypoint: config.Entrypoint,
Interactive: config.Interactive,
CPU: config.Capacity.CPU,
Memory: config.Capacity.Memory * 1024 * 1024,
Memory: config.Capacity.Memory * mib,
Logs: config.Logs,
StatsAggregator: config.StatsAggregator,
},
Expand Down
6 changes: 4 additions & 2 deletions pkg/provision/primitives/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ func ContainerToProvisionType(c workloads.Container, reservationID string) (Cont
Logs: make([]logger.Logs, len(c.Logs)),
StatsAggregator: make([]stats.Aggregator, len(c.StatsAggregator)),
Capacity: ContainerCapacity{
CPU: uint(c.Capacity.Cpu),
Memory: uint64(c.Capacity.Memory),
CPU: uint(c.Capacity.Cpu),
Memory: uint64(c.Capacity.Memory),
DiskType: pkg.DeviceType(c.Capacity.DiskType),
DiskSize: uint64(c.Capacity.DiskSize),
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provision/primitives/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (p *Provisioner) kubernetesProvisionImpl(ctx context.Context, reservation *
if err != nil {
return result, errors.Wrap(err, "could not mount k3os flist")
}
// In case of future errrors in the provisioning make sure we clean up
// In case of future errors in the provisioning make sure we clean up
defer func() {
if err != nil {
_ = flist.Umount(imagePath)
Expand Down

0 comments on commit cadfab1

Please sign in to comment.