Create config struct to take user input

Signed-off-by: Tony Fang <nhfang@amazon.com>
This commit is contained in:
Tony Fang 2023-03-02 02:08:08 +00:00
parent f681d409ed
commit 2e96ba95e0
2 changed files with 62 additions and 23 deletions

View File

@ -22,7 +22,6 @@ import (
"io" "io"
"time" "time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
@ -30,7 +29,6 @@ import (
"github.com/containerd/containerd/pkg/kmutex" "github.com/containerd/containerd/pkg/kmutex"
"github.com/containerd/containerd/pkg/transfer" "github.com/containerd/containerd/pkg/transfer"
"github.com/containerd/containerd/pkg/unpack" "github.com/containerd/containerd/pkg/unpack"
"github.com/containerd/containerd/platforms"
"github.com/containerd/typeurl/v2" "github.com/containerd/typeurl/v2"
"golang.org/x/sync/semaphore" "golang.org/x/sync/semaphore"
) )
@ -153,10 +151,9 @@ func (ts *localTransferService) withLease(ctx context.Context, opts ...leases.Op
type TransferConfig struct { type TransferConfig struct {
// MaxConcurrentDownloads is the max concurrent content downloads for pull. // MaxConcurrentDownloads is the max concurrent content downloads for pull.
MaxConcurrentDownloads int `toml:"max_concurrent_downloads"` MaxConcurrentDownloads int
// MaxConcurrentUploadedLayers is the max concurrent uploads for push // MaxConcurrentUploadedLayers is the max concurrent uploads for push
MaxConcurrentUploadedLayers int `toml:"max_concurrent_uploaded_layers"` MaxConcurrentUploadedLayers int
// DuplicationSuppressor is used to make sure that there is only one // DuplicationSuppressor is used to make sure that there is only one
// in-flight fetch request or unpack handler for a given descriptor's // in-flight fetch request or unpack handler for a given descriptor's
@ -169,21 +166,8 @@ type TransferConfig struct {
BaseHandlers []images.Handler BaseHandlers []images.Handler
// UnpackPlatforms are used to specify supported combination of platforms and snapshotters // UnpackPlatforms are used to specify supported combination of platforms and snapshotters
UnpackPlatforms []unpack.Platform `toml:"unpack_platforms"` UnpackPlatforms []unpack.Platform
// RegistryConfigPath is a path to the root directory containing registry-specific configurations // RegistryConfigPath is a path to the root directory containing registry-specific configurations
RegistryConfigPath string `toml:"config_path"` RegistryConfigPath string
}
func DefaultConfig() *TransferConfig {
return &TransferConfig{
MaxConcurrentDownloads: 3,
MaxConcurrentUploadedLayers: 3,
UnpackPlatforms: []unpack.Platform{
{
Platform: platforms.Only(platforms.DefaultSpec()),
SnapshotterKey: containerd.DefaultSnapshotter,
},
},
}
} }

View File

@ -17,9 +17,14 @@
package transfer package transfer
import ( import (
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/leases" "github.com/containerd/containerd/leases"
"github.com/containerd/containerd/metadata" "github.com/containerd/containerd/metadata"
"github.com/containerd/containerd/pkg/transfer/local" "github.com/containerd/containerd/pkg/transfer/local"
"github.com/containerd/containerd/pkg/unpack"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
// Load packages with type registrations // Load packages with type registrations
@ -36,9 +41,9 @@ func init() {
plugin.LeasePlugin, plugin.LeasePlugin,
plugin.MetadataPlugin, plugin.MetadataPlugin,
}, },
Config: local.DefaultConfig(), Config: defaultConfig(),
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
config := ic.Config.(*local.TransferConfig) config := ic.Config.(*transferConfig)
m, err := ic.Get(plugin.MetadataPlugin) m, err := ic.Get(plugin.MetadataPlugin)
if err != nil { if err != nil {
return nil, err return nil, err
@ -49,7 +54,57 @@ func init() {
return nil, err return nil, err
} }
return local.NewTransferService(l.(leases.Manager), ms.ContentStore(), metadata.NewImageStore(ms), config), nil // Set configuration based on default or user input
var lc local.TransferConfig
lc.MaxConcurrentDownloads = config.maxConcurrentDownloads
lc.MaxConcurrentUploadedLayers = config.maxConcurrentUploadedLayers
for _, uc := range config.unpackConfiguration {
p, err := platforms.Parse(uc.platform)
if err != nil {
return nil, fmt.Errorf("%s: platform configuration %v invalid", plugin.TransferPlugin, uc.platform)
}
up := unpack.Platform{
Platform: platforms.OnlyStrict(p),
SnapshotterKey: uc.snapshotter,
}
lc.UnpackPlatforms = append(lc.UnpackPlatforms, up)
}
lc.RegistryConfigPath = config.registryConfigPath
return local.NewTransferService(l.(leases.Manager), ms.ContentStore(), metadata.NewImageStore(ms), &lc), nil
}, },
}) })
} }
type transferConfig struct {
// maxConcurrentDownloads is the max concurrent content downloads for pull.
maxConcurrentDownloads int `toml:"max_concurrent_downloads"`
// maxConcurrentUploadedLayers is the max concurrent uploads for push
maxConcurrentUploadedLayers int `toml:"max_concurrent_uploaded_layers"`
// unpackConfiguration is used to read config from toml
unpackConfiguration []unpackConfiguration `toml:"unpack_config"`
// registryConfigPath is a path to the root directory containing registry-specific configurations
registryConfigPath string `toml:"config_path"`
}
type unpackConfiguration struct {
platform string
snapshotter string
}
func defaultConfig() *transferConfig {
return &transferConfig{
maxConcurrentDownloads: 3,
maxConcurrentUploadedLayers: 3,
unpackConfiguration: []unpackConfiguration{
{
platform: platforms.Format(platforms.DefaultSpec()),
snapshotter: containerd.DefaultSnapshotter,
},
},
}
}