Remove duplicated TOML duration parsers

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko 2024-02-01 11:48:33 -08:00
parent ac54047344
commit 9340be717f
No known key found for this signature in database
3 changed files with 11 additions and 39 deletions

View File

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

View File

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

View File

@ -25,6 +25,7 @@ import (
containerd "github.com/containerd/containerd/v2/client"
"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/plugins"
"github.com/containerd/log"
@ -32,24 +33,10 @@ import (
"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
type Config struct {
// Interval for how long to wait to check for state changes
Interval duration `toml:"interval"`
Interval tomlext.Duration `toml:"interval"`
}
func init() {
@ -61,9 +48,7 @@ func init() {
},
ID: "restart",
Config: &Config{
Interval: duration{
Duration: 10 * time.Second,
},
Interval: tomlext.FromStdTime(10 * time.Second),
},
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
ic.Meta.Capabilities = []string{"no", "always", "on-failure", "unless-stopped"}
@ -74,7 +59,7 @@ func init() {
m := &monitor{
client: client,
}
go m.run(ic.Config.(*Config).Interval.Duration)
go m.run(tomlext.ToStdTime(ic.Config.(*Config).Interval))
return m, nil
},
})