Merge pull request #9729 from mxpv/duration

Remove duplicated TOML duration parsers
This commit is contained in:
Samuel Karp 2024-02-05 07:43:51 +00:00 committed by GitHub
commit 0125a42fb5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 39 deletions

View File

@ -23,6 +23,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/containerd/containerd/v2/internal/tomlext"
"github.com/containerd/containerd/v2/pkg/gc" "github.com/containerd/containerd/v2/pkg/gc"
"github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins"
"github.com/containerd/log" "github.com/containerd/log"
@ -70,7 +71,7 @@ type config struct {
// schedule. Use suffix "ms" for millisecond and "s" for second. // schedule. Use suffix "ms" for millisecond and "s" for second.
// //
// Default is "0ms" // Default is "0ms"
ScheduleDelay duration `toml:"schedule_delay"` ScheduleDelay tomlext.Duration `toml:"schedule_delay"`
// StartupDelay is the delay duration to do an initial garbage // StartupDelay is the delay duration to do an initial garbage
// collection after startup. The initial garbage collection is used to // collection after startup. The initial garbage collection is used to
@ -79,22 +80,7 @@ type config struct {
// "ms" for millisecond and "s" for second. // "ms" for millisecond and "s" for second.
// //
// Default is "100ms" // Default is "100ms"
StartupDelay duration `toml:"startup_delay"` StartupDelay tomlext.Duration `toml:"startup_delay"`
}
type duration time.Duration
func (d *duration) UnmarshalText(text []byte) error {
ed, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*d = duration(ed)
return nil
}
func (d duration) MarshalText() (text []byte, err error) {
return []byte(time.Duration(d).String()), nil
} }
func init() { func init() {
@ -108,8 +94,8 @@ func init() {
PauseThreshold: 0.02, PauseThreshold: 0.02,
DeletionThreshold: 0, DeletionThreshold: 0,
MutationThreshold: 100, MutationThreshold: 100,
ScheduleDelay: duration(0), ScheduleDelay: tomlext.FromStdTime(0),
StartupDelay: duration(100 * time.Millisecond), StartupDelay: tomlext.FromStdTime(100 * time.Millisecond),
}, },
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
md, err := ic.GetSingle(plugins.MetadataPlugin) md, err := ic.GetSingle(plugins.MetadataPlugin)

View File

@ -22,6 +22,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/containerd/containerd/v2/internal/tomlext"
"github.com/containerd/containerd/v2/pkg/gc" "github.com/containerd/containerd/v2/pkg/gc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -152,7 +153,7 @@ func TestStartupDelay(t *testing.T) {
cfg = &config{ cfg = &config{
// Prevent GC from scheduling again before check // Prevent GC from scheduling again before check
PauseThreshold: 0.001, PauseThreshold: 0.001,
StartupDelay: duration(startupDelay), StartupDelay: tomlext.Duration(startupDelay),
} }
tc = &testCollector{ tc = &testCollector{
d: time.Second, d: time.Second,

View File

@ -25,6 +25,7 @@ import (
containerd "github.com/containerd/containerd/v2/client" containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/runtime/restart" "github.com/containerd/containerd/v2/core/runtime/restart"
"github.com/containerd/containerd/v2/internal/tomlext"
"github.com/containerd/containerd/v2/pkg/namespaces" "github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins"
"github.com/containerd/log" "github.com/containerd/log"
@ -32,24 +33,10 @@ import (
"github.com/containerd/plugin/registry" "github.com/containerd/plugin/registry"
) )
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (d duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}
// Config for the restart monitor // Config for the restart monitor
type Config struct { type Config struct {
// Interval for how long to wait to check for state changes // Interval for how long to wait to check for state changes
Interval duration `toml:"interval"` Interval tomlext.Duration `toml:"interval"`
} }
func init() { func init() {
@ -61,9 +48,7 @@ func init() {
}, },
ID: "restart", ID: "restart",
Config: &Config{ Config: &Config{
Interval: duration{ Interval: tomlext.FromStdTime(10 * time.Second),
Duration: 10 * time.Second,
},
}, },
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
ic.Meta.Capabilities = []string{"no", "always", "on-failure", "unless-stopped"} ic.Meta.Capabilities = []string{"no", "always", "on-failure", "unless-stopped"}
@ -74,7 +59,7 @@ func init() {
m := &monitor{ m := &monitor{
client: client, client: client,
} }
go m.run(ic.Config.(*Config).Interval.Duration) go m.run(tomlext.ToStdTime(ic.Config.(*Config).Interval))
return m, nil return m, nil
}, },
}) })