Merge pull request #9827 from dmcgowan/move-config-version

Move config version to version package
This commit is contained in:
Phil Estes
2024-02-15 14:58:04 +00:00
committed by GitHub
10 changed files with 40 additions and 31 deletions

View File

@@ -26,6 +26,7 @@ import (
"github.com/containerd/containerd/v2/core/images"
"github.com/containerd/containerd/v2/defaults"
"github.com/containerd/containerd/v2/pkg/timeout"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/plugin/registry"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pelletier/go-toml/v2"
@@ -69,7 +70,7 @@ func outputConfig(ctx gocontext.Context, config *srvconfig.Config) error {
// when a config without a version is loaded from disk and has no version
// set, we assume it's a v1 config. But when generating new configs via
// this command, generate the max configuration version
config.Version = srvconfig.CurrentConfigVersion
config.Version = version.ConfigVersion
return toml.NewEncoder(os.Stdout).SetIndentTables(true).Encode(config)
}
@@ -112,7 +113,7 @@ var configCommand = cli.Command{
return err
}
if config.Version < srvconfig.CurrentConfigVersion {
if config.Version < version.ConfigVersion {
plugins := registry.Graph(srvconfig.V2DisabledFilter(config.DisabledPlugins))
for _, p := range plugins {
if p.ConfigMigration != nil {
@@ -145,7 +146,7 @@ var configCommand = cli.Command{
}
}
config.Version = srvconfig.CurrentConfigVersion
config.Version = version.ConfigVersion
return toml.NewEncoder(os.Stdout).SetIndentTables(true).Encode(config)
},
@@ -155,7 +156,7 @@ var configCommand = cli.Command{
func platformAgnosticDefaultConfig() *srvconfig.Config {
return &srvconfig.Config{
Version: srvconfig.CurrentConfigVersion,
Version: version.ConfigVersion,
Root: defaults.DefaultRootDir,
State: defaults.DefaultStateDir,
GRPC: srvconfig.GRPCConfig{

View File

@@ -36,14 +36,12 @@ import (
"dario.cat/mergo"
"github.com/pelletier/go-toml/v2"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/errdefs"
"github.com/containerd/log"
"github.com/containerd/plugin"
)
// CurrentConfigVersion is the max config version which is supported
const CurrentConfigVersion = 3
// migrations hold the migration functions for every prior containerd config version
var migrations = []func(context.Context, *Config) error{
nil, // Version 0 is not defined, treated at version 1
@@ -115,8 +113,8 @@ type StreamProcessor struct {
// ValidateVersion validates the config for a v2 file
func (c *Config) ValidateVersion() error {
if c.Version > CurrentConfigVersion {
return fmt.Errorf("expected containerd config version equal to or less than `%d`, got `%d`", CurrentConfigVersion, c.Version)
if c.Version > version.ConfigVersion {
return fmt.Errorf("expected containerd config version equal to or less than `%d`, got `%d`", version.ConfigVersion, c.Version)
}
for _, p := range c.DisabledPlugins {
@@ -135,7 +133,7 @@ func (c *Config) ValidateVersion() error {
// MigrateConfig will convert the config to the latest version before using
func (c *Config) MigrateConfig(ctx context.Context) error {
for c.Version < CurrentConfigVersion {
for c.Version < version.ConfigVersion {
if m := migrations[c.Version]; m != nil {
if err := m(ctx, c); err != nil {
return err

View File

@@ -25,12 +25,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/log/logtest"
)
func TestMigrations(t *testing.T) {
if len(migrations) != CurrentConfigVersion {
t.Fatalf("Migration missing, expected %d migrations, only %d defined", CurrentConfigVersion, len(migrations))
if len(migrations) != version.ConfigVersion {
t.Fatalf("Migration missing, expected %d migrations, only %d defined", version.ConfigVersion, len(migrations))
}
}

View File

@@ -64,6 +64,7 @@ import (
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/containerd/v2/plugins/content/local"
"github.com/containerd/containerd/v2/plugins/services/warning"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/platforms"
"github.com/containerd/plugin"
"github.com/containerd/plugin/dynamic"
@@ -112,10 +113,10 @@ func CreateTopLevelDirectories(config *srvconfig.Config) error {
// New creates and initializes a new containerd server
func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
var (
version = config.Version
migrationT time.Duration
currentVersion = config.Version
migrationT time.Duration
)
if version < srvconfig.CurrentConfigVersion {
if currentVersion < version.ConfigVersion {
// Migrate config to latest version
t1 := time.Now()
err := config.MigrateConfig(ctx)
@@ -224,12 +225,12 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
required[r] = struct{}{}
}
if version < srvconfig.CurrentConfigVersion {
if currentVersion < version.ConfigVersion {
t1 := time.Now()
// Run migration for each configuration version
// Run each plugin migration for each version to ensure that migration logic is simple and
// focused on upgrading from one version at a time.
for v := version; v < srvconfig.CurrentConfigVersion; v++ {
for v := currentVersion; v < version.ConfigVersion; v++ {
for _, p := range loaded {
if p.ConfigMigration != nil {
if err := p.ConfigMigration(ctx, v, config.Plugins); err != nil {
@@ -241,7 +242,7 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
migrationT = migrationT + time.Since(t1)
}
if migrationT > 0 {
log.G(ctx).WithField("t", migrationT).Warnf("Configuration migrated from version %d, use `containerd config migrate` to avoid migration", version)
log.G(ctx).WithField("t", migrationT).Warnf("Configuration migrated from version %d, use `containerd config migrate` to avoid migration", currentVersion)
}
for _, p := range loaded {

View File

@@ -21,6 +21,7 @@ import (
"testing"
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/plugin"
"github.com/containerd/plugin/registry"
"github.com/stretchr/testify/assert"
@@ -61,7 +62,7 @@ func TestMigration(t *testing.T) {
registry.Reset()
defer registry.Reset()
version := srvconfig.CurrentConfigVersion - 1
configVersion := version.ConfigVersion - 1
type testConfig struct {
Migrated string `toml:"migrated"`
@@ -109,7 +110,7 @@ func TestMigration(t *testing.T) {
return nil, nil
},
ConfigMigration: func(ctx context.Context, v int, plugins map[string]interface{}) error {
if v != version {
if v != configVersion {
t.Errorf("unxpected version: %d", v)
}
t1, ok := plugins["io.containerd.test.t1"]
@@ -133,7 +134,7 @@ func TestMigration(t *testing.T) {
})
config := &srvconfig.Config{}
config.Version = version
config.Version = configVersion
config.Plugins = map[string]interface{}{
"io.containerd.test.t1": map[string]interface{}{
"migrated": "migrate me",