Skip to content

Commit 00fe7a4

Browse files
authored
Merge pull request containerd#9827 from dmcgowan/move-config-version
Move config version to version package
2 parents f1a3c37 + a086125 commit 00fe7a4

File tree

10 files changed

+40
-31
lines changed

10 files changed

+40
-31
lines changed

cmd/containerd/command/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/containerd/containerd/v2/core/images"
2727
"github.com/containerd/containerd/v2/defaults"
2828
"github.com/containerd/containerd/v2/pkg/timeout"
29+
"github.com/containerd/containerd/v2/version"
2930
"github.com/containerd/plugin/registry"
3031
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3132
"github.com/pelletier/go-toml/v2"
@@ -69,7 +70,7 @@ func outputConfig(ctx gocontext.Context, config *srvconfig.Config) error {
6970
// when a config without a version is loaded from disk and has no version
7071
// set, we assume it's a v1 config. But when generating new configs via
7172
// this command, generate the max configuration version
72-
config.Version = srvconfig.CurrentConfigVersion
73+
config.Version = version.ConfigVersion
7374

7475
return toml.NewEncoder(os.Stdout).SetIndentTables(true).Encode(config)
7576
}
@@ -112,7 +113,7 @@ var configCommand = cli.Command{
112113
return err
113114
}
114115

115-
if config.Version < srvconfig.CurrentConfigVersion {
116+
if config.Version < version.ConfigVersion {
116117
plugins := registry.Graph(srvconfig.V2DisabledFilter(config.DisabledPlugins))
117118
for _, p := range plugins {
118119
if p.ConfigMigration != nil {
@@ -145,7 +146,7 @@ var configCommand = cli.Command{
145146
}
146147
}
147148

148-
config.Version = srvconfig.CurrentConfigVersion
149+
config.Version = version.ConfigVersion
149150

150151
return toml.NewEncoder(os.Stdout).SetIndentTables(true).Encode(config)
151152
},
@@ -155,7 +156,7 @@ var configCommand = cli.Command{
155156

156157
func platformAgnosticDefaultConfig() *srvconfig.Config {
157158
return &srvconfig.Config{
158-
Version: srvconfig.CurrentConfigVersion,
159+
Version: version.ConfigVersion,
159160
Root: defaults.DefaultRootDir,
160161
State: defaults.DefaultStateDir,
161162
GRPC: srvconfig.GRPCConfig{

cmd/containerd/server/config/config.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ import (
3636
"dario.cat/mergo"
3737
"github.com/pelletier/go-toml/v2"
3838

39+
"github.com/containerd/containerd/v2/version"
3940
"github.com/containerd/errdefs"
4041
"github.com/containerd/log"
4142
"github.com/containerd/plugin"
4243
)
4344

44-
// CurrentConfigVersion is the max config version which is supported
45-
const CurrentConfigVersion = 3
46-
4745
// migrations hold the migration functions for every prior containerd config version
4846
var migrations = []func(context.Context, *Config) error{
4947
nil, // Version 0 is not defined, treated at version 1
@@ -115,8 +113,8 @@ type StreamProcessor struct {
115113

116114
// ValidateVersion validates the config for a v2 file
117115
func (c *Config) ValidateVersion() error {
118-
if c.Version > CurrentConfigVersion {
119-
return fmt.Errorf("expected containerd config version equal to or less than `%d`, got `%d`", CurrentConfigVersion, c.Version)
116+
if c.Version > version.ConfigVersion {
117+
return fmt.Errorf("expected containerd config version equal to or less than `%d`, got `%d`", version.ConfigVersion, c.Version)
120118
}
121119

122120
for _, p := range c.DisabledPlugins {
@@ -135,7 +133,7 @@ func (c *Config) ValidateVersion() error {
135133

136134
// MigrateConfig will convert the config to the latest version before using
137135
func (c *Config) MigrateConfig(ctx context.Context) error {
138-
for c.Version < CurrentConfigVersion {
136+
for c.Version < version.ConfigVersion {
139137
if m := migrations[c.Version]; m != nil {
140138
if err := m(ctx, c); err != nil {
141139
return err

cmd/containerd/server/config/config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525

2626
"github.com/stretchr/testify/assert"
2727

28+
"github.com/containerd/containerd/v2/version"
2829
"github.com/containerd/log/logtest"
2930
)
3031

3132
func TestMigrations(t *testing.T) {
32-
if len(migrations) != CurrentConfigVersion {
33-
t.Fatalf("Migration missing, expected %d migrations, only %d defined", CurrentConfigVersion, len(migrations))
33+
if len(migrations) != version.ConfigVersion {
34+
t.Fatalf("Migration missing, expected %d migrations, only %d defined", version.ConfigVersion, len(migrations))
3435
}
3536
}
3637

cmd/containerd/server/server.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import (
6464
"github.com/containerd/containerd/v2/plugins"
6565
"github.com/containerd/containerd/v2/plugins/content/local"
6666
"github.com/containerd/containerd/v2/plugins/services/warning"
67+
"github.com/containerd/containerd/v2/version"
6768
"github.com/containerd/platforms"
6869
"github.com/containerd/plugin"
6970
"github.com/containerd/plugin/dynamic"
@@ -112,10 +113,10 @@ func CreateTopLevelDirectories(config *srvconfig.Config) error {
112113
// New creates and initializes a new containerd server
113114
func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
114115
var (
115-
version = config.Version
116-
migrationT time.Duration
116+
currentVersion = config.Version
117+
migrationT time.Duration
117118
)
118-
if version < srvconfig.CurrentConfigVersion {
119+
if currentVersion < version.ConfigVersion {
119120
// Migrate config to latest version
120121
t1 := time.Now()
121122
err := config.MigrateConfig(ctx)
@@ -224,12 +225,12 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
224225
required[r] = struct{}{}
225226
}
226227

227-
if version < srvconfig.CurrentConfigVersion {
228+
if currentVersion < version.ConfigVersion {
228229
t1 := time.Now()
229230
// Run migration for each configuration version
230231
// Run each plugin migration for each version to ensure that migration logic is simple and
231232
// focused on upgrading from one version at a time.
232-
for v := version; v < srvconfig.CurrentConfigVersion; v++ {
233+
for v := currentVersion; v < version.ConfigVersion; v++ {
233234
for _, p := range loaded {
234235
if p.ConfigMigration != nil {
235236
if err := p.ConfigMigration(ctx, v, config.Plugins); err != nil {
@@ -241,7 +242,7 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) {
241242
migrationT = migrationT + time.Since(t1)
242243
}
243244
if migrationT > 0 {
244-
log.G(ctx).WithField("t", migrationT).Warnf("Configuration migrated from version %d, use `containerd config migrate` to avoid migration", version)
245+
log.G(ctx).WithField("t", migrationT).Warnf("Configuration migrated from version %d, use `containerd config migrate` to avoid migration", currentVersion)
245246
}
246247

247248
for _, p := range loaded {

cmd/containerd/server/server_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
24+
"github.com/containerd/containerd/v2/version"
2425
"github.com/containerd/plugin"
2526
"github.com/containerd/plugin/registry"
2627
"github.com/stretchr/testify/assert"
@@ -61,7 +62,7 @@ func TestMigration(t *testing.T) {
6162
registry.Reset()
6263
defer registry.Reset()
6364

64-
version := srvconfig.CurrentConfigVersion - 1
65+
configVersion := version.ConfigVersion - 1
6566

6667
type testConfig struct {
6768
Migrated string `toml:"migrated"`
@@ -109,7 +110,7 @@ func TestMigration(t *testing.T) {
109110
return nil, nil
110111
},
111112
ConfigMigration: func(ctx context.Context, v int, plugins map[string]interface{}) error {
112-
if v != version {
113+
if v != configVersion {
113114
t.Errorf("unxpected version: %d", v)
114115
}
115116
t1, ok := plugins["io.containerd.test.t1"]
@@ -133,7 +134,7 @@ func TestMigration(t *testing.T) {
133134
})
134135

135136
config := &srvconfig.Config{}
136-
config.Version = version
137+
config.Version = configVersion
137138
config.Plugins = map[string]interface{}{
138139
"io.containerd.test.t1": map[string]interface{}{
139140
"migrated": "migrate me",

contrib/fuzz/daemon.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/containerd/containerd/v2/cmd/containerd/server/config"
2828
"github.com/containerd/containerd/v2/defaults"
2929
"github.com/containerd/containerd/v2/pkg/sys"
30+
"github.com/containerd/containerd/v2/version"
3031
"github.com/containerd/log"
3132
)
3233

@@ -51,7 +52,7 @@ func startDaemon() {
5152
defer close(errC)
5253

5354
srvconfig := &config.Config{
54-
Version: config.CurrentConfigVersion,
55+
Version: version.ConfigVersion,
5556
Root: defaultRoot,
5657
State: defaultState,
5758
Debug: config.Debug{

plugins/cri/cri.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/containerd/plugin/registry"
2727

2828
containerd "github.com/containerd/containerd/v2/client"
29-
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
3029
"github.com/containerd/containerd/v2/core/sandbox"
3130
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
3231
"github.com/containerd/containerd/v2/internal/cri/constants"
@@ -36,6 +35,7 @@ import (
3635
nriservice "github.com/containerd/containerd/v2/pkg/nri"
3736
"github.com/containerd/containerd/v2/plugins"
3837
"github.com/containerd/containerd/v2/plugins/services/warning"
38+
"github.com/containerd/containerd/v2/version"
3939
"github.com/containerd/platforms"
4040

4141
"google.golang.org/grpc"
@@ -61,8 +61,8 @@ func init() {
6161
plugins.WarningPlugin,
6262
},
6363
Config: &defaultConfig,
64-
ConfigMigration: func(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
65-
if version >= srvconfig.CurrentConfigVersion {
64+
ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
65+
if configVersion >= version.ConfigVersion {
6666
return nil
6767
}
6868
const pluginName = string(plugins.GRPCPlugin) + ".cri"

plugins/cri/images/plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222
"path/filepath"
2323

2424
containerd "github.com/containerd/containerd/v2/client"
25-
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
2625
"github.com/containerd/containerd/v2/core/metadata"
2726
"github.com/containerd/containerd/v2/core/snapshots"
2827
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
2928
"github.com/containerd/containerd/v2/internal/cri/constants"
3029
"github.com/containerd/containerd/v2/internal/cri/server/images"
3130
"github.com/containerd/containerd/v2/plugins"
3231
"github.com/containerd/containerd/v2/plugins/services/warning"
32+
"github.com/containerd/containerd/v2/version"
3333
"github.com/containerd/log"
3434
"github.com/containerd/platforms"
3535
"github.com/containerd/plugin"
@@ -154,8 +154,8 @@ func init() {
154154
})
155155
}
156156

157-
func configMigration(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
158-
if version >= srvconfig.CurrentConfigVersion {
157+
func configMigration(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
158+
if configVersion >= version.ConfigVersion {
159159
return nil
160160
}
161161
original, ok := pluginConfigs[string(plugins.GRPCPlugin)+".cri"]

plugins/cri/runtime/plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import (
3030
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
3131
"k8s.io/klog/v2"
3232

33-
srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
3433
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
3534
"github.com/containerd/containerd/v2/internal/cri/constants"
3635
"github.com/containerd/containerd/v2/pkg/oci"
3736
"github.com/containerd/containerd/v2/plugins"
3837
"github.com/containerd/containerd/v2/plugins/services/warning"
38+
"github.com/containerd/containerd/v2/version"
3939
"github.com/containerd/errdefs"
4040
"github.com/containerd/platforms"
4141
)
@@ -51,8 +51,8 @@ func init() {
5151
Requires: []plugin.Type{
5252
plugins.WarningPlugin,
5353
},
54-
ConfigMigration: func(ctx context.Context, version int, pluginConfigs map[string]interface{}) error {
55-
if version >= srvconfig.CurrentConfigVersion {
54+
ConfigMigration: func(ctx context.Context, configVersion int, pluginConfigs map[string]interface{}) error {
55+
if configVersion >= version.ConfigVersion {
5656
return nil
5757
}
5858
c, ok := pluginConfigs[string(plugins.GRPCPlugin)+".cri"]

version/version.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ var (
3232
// GoVersion is Go tree's version.
3333
GoVersion = runtime.Version()
3434
)
35+
36+
// ConfigVersion is the current highest supported configuration version.
37+
// This version is used by the main configuration as well as all plugins.
38+
// Any configuration less than this version which has structural changes
39+
// should migrate the configuration structures used by this version.
40+
const ConfigVersion = 3

0 commit comments

Comments
 (0)