Skip to content

Commit

Permalink
Merge pull request containerd#9827 from dmcgowan/move-config-version
Browse files Browse the repository at this point in the history
Move config version to version package
  • Loading branch information
estesp authored Feb 15, 2024
2 parents f1a3c37 + a086125 commit 00fe7a4
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 31 deletions.
9 changes: 5 additions & 4 deletions cmd/containerd/command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
},
Expand All @@ -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{
Expand Down
10 changes: 4 additions & 6 deletions cmd/containerd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions cmd/containerd/server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/containerd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions cmd/containerd/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"]
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion contrib/fuzz/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/containerd/containerd/v2/cmd/containerd/server/config"
"github.com/containerd/containerd/v2/defaults"
"github.com/containerd/containerd/v2/pkg/sys"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/log"
)

Expand All @@ -51,7 +52,7 @@ func startDaemon() {
defer close(errC)

srvconfig := &config.Config{
Version: config.CurrentConfigVersion,
Version: version.ConfigVersion,
Root: defaultRoot,
State: defaultState,
Debug: config.Debug{
Expand Down
6 changes: 3 additions & 3 deletions plugins/cri/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/containerd/plugin/registry"

containerd "github.com/containerd/containerd/v2/client"
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
"github.com/containerd/containerd/v2/core/sandbox"
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
"github.com/containerd/containerd/v2/internal/cri/constants"
Expand All @@ -36,6 +35,7 @@ import (
nriservice "github.com/containerd/containerd/v2/pkg/nri"
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/containerd/v2/plugins/services/warning"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/platforms"

"google.golang.org/grpc"
Expand All @@ -61,8 +61,8 @@ func init() {
plugins.WarningPlugin,
},
Config: &defaultConfig,
ConfigMigration: func(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
if version >= srvconfig.CurrentConfigVersion {
ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
if configVersion >= version.ConfigVersion {
return nil
}
const pluginName = string(plugins.GRPCPlugin) + ".cri"
Expand Down
6 changes: 3 additions & 3 deletions plugins/cri/images/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"path/filepath"

containerd "github.com/containerd/containerd/v2/client"
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
"github.com/containerd/containerd/v2/core/metadata"
"github.com/containerd/containerd/v2/core/snapshots"
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
"github.com/containerd/containerd/v2/internal/cri/constants"
"github.com/containerd/containerd/v2/internal/cri/server/images"
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/containerd/v2/plugins/services/warning"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/log"
"github.com/containerd/platforms"
"github.com/containerd/plugin"
Expand Down Expand Up @@ -154,8 +154,8 @@ func init() {
})
}

func configMigration(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
if version >= srvconfig.CurrentConfigVersion {
func configMigration(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
if configVersion >= version.ConfigVersion {
return nil
}
original, ok := pluginConfigs[string(plugins.GRPCPlugin)+".cri"]
Expand Down
6 changes: 3 additions & 3 deletions plugins/cri/runtime/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
"k8s.io/klog/v2"

srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
"github.com/containerd/containerd/v2/internal/cri/constants"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/containerd/v2/plugins/services/warning"
"github.com/containerd/containerd/v2/version"
"github.com/containerd/errdefs"
"github.com/containerd/platforms"
)
Expand All @@ -51,8 +51,8 @@ func init() {
Requires: []plugin.Type{
plugins.WarningPlugin,
},
ConfigMigration: func(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
if version >= srvconfig.CurrentConfigVersion {
ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
if configVersion >= version.ConfigVersion {
return nil
}
c, ok := pluginConfigs[string(plugins.GRPCPlugin)+".cri"]
Expand Down
6 changes: 6 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ var (
// GoVersion is Go tree's version.
GoVersion = runtime.Version()
)

// ConfigVersion is the current highest supported configuration version.
// This version is used by the main configuration as well as all plugins.
// Any configuration less than this version which has structural changes
// should migrate the configuration structures used by this version.
const ConfigVersion = 3

0 comments on commit 00fe7a4

Please sign in to comment.