Skip to content

Commit 7da4d42

Browse files
committed
Fix serialization of conf object
1 parent 9cade65 commit 7da4d42

File tree

12 files changed

+157
-116
lines changed

12 files changed

+157
-116
lines changed

pkg/env-tests/timeaware/timeaware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ func setupControllers(backgroundCtx context.Context, cfg *rest.Config,
120120
ClientType: "fake-with-history",
121121
ConnectionString: "fake-connection",
122122
UsageParams: &api.UsageParams{
123-
WindowSize: &[]time.Duration{time.Second * time.Duration(*windowSize)}[0],
124-
FetchInterval: &[]time.Duration{time.Millisecond}[0],
125-
HalfLifePeriod: &[]time.Duration{time.Second * time.Duration(*halfLifePeriod)}[0],
123+
WindowSize: &metav1.Duration{Duration: time.Second * time.Duration(*windowSize)},
124+
FetchInterval: &metav1.Duration{Duration: time.Millisecond},
125+
HalfLifePeriod: &metav1.Duration{Duration: time.Second * time.Duration(*halfLifePeriod)},
126126
},
127127
}
128128
schedulerConf.UsageDBConfig.UsageParams.SetDefaults()

pkg/operator/operands/scheduler/resources_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func TestValidateJobDepthMap(t *testing.T) {
187187

188188
for _, tt := range tests {
189189
t.Run(tt.name, func(t *testing.T) {
190-
innerConfig := config{
190+
innerConfig := conf.SchedulerConfiguration{
191191
Actions: strings.Join(tt.actions, ", "),
192192
}
193193

@@ -454,15 +454,15 @@ tiers:
454454
require.True(t, found, "ConfigMap missing config.yaml")
455455

456456
// Unmarshal expected YAML from test case
457-
var expectedConfig config
457+
var expectedConfig conf.SchedulerConfiguration
458458
if _, ok := tt.expected["config.yaml"]; !ok {
459459
t.Fatal("Test case must provide expected YAML for config.yaml")
460460
}
461461
err = yaml.Unmarshal([]byte(tt.expected["config.yaml"]), &expectedConfig)
462462
require.NoError(t, err, "Failed to unmarshal expected config")
463463

464464
// Unmarshal actual YAML from ConfigMap
465-
var actualConfig config
465+
var actualConfig conf.SchedulerConfiguration
466466
err = yaml.Unmarshal([]byte(actualYAML), &actualConfig)
467467
require.NoError(t, err, "Failed to unmarshal actual config")
468468

pkg/operator/operands/scheduler/scheduler.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ const (
2222
defaultResourceName = "scheduler"
2323
)
2424

25-
type config struct {
26-
Actions string `yaml:"actions"`
27-
Tiers []tier `yaml:"tiers,omitempty"`
28-
QueueDepthPerAction map[string]int `yaml:"queueDepthPerAction,omitempty"`
29-
}
30-
31-
type tier struct {
32-
Plugins []plugin `yaml:"plugins"`
33-
}
34-
35-
type plugin struct {
36-
Name string `yaml:"name"`
37-
Arguments map[string]string `yaml:"arguments,omitempty"`
38-
}
39-
4025
type SchedulerForShard struct {
4126
schedulingShard *kaiv1.SchedulingShard
4227

pkg/operator/operands/scheduler/scheduler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ var _ = Describe("Scheduler", func() {
137137
cm := cmObj.(*v1.ConfigMap)
138138

139139
Expect(err).To(BeNil())
140-
Expect(cm.Data["config.yaml"]).To(Equal(`actions: allocate, consolidation, reclaim, preempt, stalegangeviction
140+
Expect(cm.Data["config.yaml"]).To(MatchYAML(`actions: allocate, consolidation, reclaim, preempt, stalegangeviction
141141
tiers:
142142
- plugins:
143143
- name: predicates
@@ -176,7 +176,7 @@ tiers:
176176
cm := cmObj.(*v1.ConfigMap)
177177

178178
Expect(err).To(BeNil())
179-
Expect(cm.Data["config.yaml"]).To(Equal(`actions: allocate, reclaim, preempt, stalegangeviction
179+
Expect(cm.Data["config.yaml"]).To(MatchYAML(`actions: allocate, reclaim, preempt, stalegangeviction
180180
tiers:
181181
- plugins:
182182
- name: predicates

pkg/scheduler/cache/cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ func newSchedulerCache(schedulerCacheParams *SchedulerCacheParams) *SchedulerCac
169169

170170
if schedulerCacheParams.UsageDBClient != nil {
171171
sc.usageLister = usagedb.NewUsageLister(schedulerCacheParams.UsageDBClient,
172-
schedulerCacheParams.UsageDBParams.FetchInterval,
173-
schedulerCacheParams.UsageDBParams.StalenessPeriod,
174-
schedulerCacheParams.UsageDBParams.WaitTimeout)
172+
&schedulerCacheParams.UsageDBParams.FetchInterval.Duration,
173+
&schedulerCacheParams.UsageDBParams.StalenessPeriod.Duration,
174+
&schedulerCacheParams.UsageDBParams.WaitTimeout.Duration)
175175
}
176176

177177
clusterInfo, err := cluster_info.New(sc.informerFactory, sc.kubeAiSchedulerInformerFactory, sc.kueueInformerFactory, sc.usageLister, sc.schedulingNodePoolParams,

pkg/scheduler/cache/usagedb/api/defaults.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33

44
package api
55

6-
import "time"
6+
import (
7+
"time"
78

8-
func (up *UsageParams) SetDefaults() {
9-
if up.HalfLifePeriod == nil {
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
)
11+
12+
func (p *UsageParams) SetDefaults() {
13+
if p.HalfLifePeriod == nil {
1014
// noop: disabled by default
1115
}
12-
if up.WindowSize == nil {
13-
windowSize := time.Hour * 24 * 7
14-
up.WindowSize = &windowSize
16+
if p.WindowSize == nil {
17+
p.WindowSize = &metav1.Duration{Duration: time.Hour * 24 * 7}
1518
}
16-
if up.WindowType == nil {
19+
if p.WindowType == nil {
1720
windowType := SlidingWindow
18-
up.WindowType = &windowType
21+
p.WindowType = &windowType
1922
}
20-
if up.FetchInterval == nil {
21-
fetchInterval := 1 * time.Minute
22-
up.FetchInterval = &fetchInterval
23+
if p.FetchInterval == nil {
24+
p.FetchInterval = &metav1.Duration{Duration: 1 * time.Minute}
2325
}
24-
if up.StalenessPeriod == nil {
25-
stalenessPeriod := 5 * time.Minute
26-
up.StalenessPeriod = &stalenessPeriod
26+
if p.StalenessPeriod == nil {
27+
p.StalenessPeriod = &metav1.Duration{Duration: 5 * time.Minute}
2728
}
28-
if up.WaitTimeout == nil {
29-
waitTimeout := 1 * time.Minute
30-
up.WaitTimeout = &waitTimeout
29+
if p.WaitTimeout == nil {
30+
p.WaitTimeout = &metav1.Duration{Duration: 1 * time.Minute}
3131
}
3232
}
3333

@@ -54,12 +54,12 @@ func (wt WindowType) IsValid() bool {
5454
}
5555
}
5656

57-
func (up *UsageParams) GetExtraDurationParamOrDefault(key string, defaultValue time.Duration) time.Duration {
58-
if up.ExtraParams == nil {
57+
func (p *UsageParams) GetExtraDurationParamOrDefault(key string, defaultValue time.Duration) time.Duration {
58+
if p.ExtraParams == nil {
5959
return defaultValue
6060
}
6161

62-
value, exists := up.ExtraParams[key]
62+
value, exists := p.ExtraParams[key]
6363
if !exists {
6464
return defaultValue
6565
}
@@ -72,12 +72,12 @@ func (up *UsageParams) GetExtraDurationParamOrDefault(key string, defaultValue t
7272
return duration
7373
}
7474

75-
func (up *UsageParams) GetExtraStringParamOrDefault(key string, defaultValue string) string {
76-
if up.ExtraParams == nil {
75+
func (p *UsageParams) GetExtraStringParamOrDefault(key string, defaultValue string) string {
76+
if p.ExtraParams == nil {
7777
return defaultValue
7878
}
7979

80-
value, exists := up.ExtraParams[key]
80+
value, exists := p.ExtraParams[key]
8181
if !exists {
8282
return defaultValue
8383
}

pkg/scheduler/cache/usagedb/api/interface.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
package api
55

66
import (
7-
"time"
8-
97
"github.com/NVIDIA/KAI-scheduler/pkg/scheduler/api/queue_info"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
)
1110

1211
type Interface interface {
@@ -15,8 +14,8 @@ type Interface interface {
1514
type UsageDBConfig struct {
1615
ClientType string `yaml:"clientType" json:"clientType"`
1716
ConnectionString string `yaml:"connectionString" json:"connectionString"`
18-
ConnectionStringEnvVar string `yaml:"connectionStringEnvVar" json:"connectionStringEnvVar"`
19-
UsageParams *UsageParams `yaml:"usageParams" json:"usageParams"`
17+
ConnectionStringEnvVar string `yaml:"connectionStringEnvVar,omitempty" json:"connectionStringEnvVar,omitempty"`
18+
UsageParams *UsageParams `yaml:"usageParams,omitempty" json:"usageParams,omitempty"`
2019
}
2120

2221
// GetUsageParams returns the usage params if set, and default params if not set.
@@ -29,23 +28,70 @@ func (c *UsageDBConfig) GetUsageParams() *UsageParams {
2928
return &up
3029
}
3130

31+
func (c *UsageDBConfig) DeepCopy() *UsageDBConfig {
32+
out := new(UsageDBConfig)
33+
out.ClientType = c.ClientType
34+
out.ConnectionString = c.ConnectionString
35+
out.ConnectionStringEnvVar = c.ConnectionStringEnvVar
36+
if c.UsageParams != nil {
37+
out.UsageParams = c.UsageParams.DeepCopy()
38+
}
39+
return out
40+
}
41+
3242
// UsageParams defines common params for all usage db clients. Some clients may not support all the params.
3343
type UsageParams struct {
3444
// Half life period of the usage. If not set, or set to 0, the usage will not be decayed.
35-
HalfLifePeriod *time.Duration `yaml:"halfLifePeriod" json:"halfLifePeriod"`
45+
HalfLifePeriod *metav1.Duration `yaml:"halfLifePeriod,omitempty" json:"halfLifePeriod,omitempty"`
3646
// Window size of the usage. Default is 1 week.
37-
WindowSize *time.Duration `yaml:"windowSize" json:"windowSize"`
47+
WindowSize *metav1.Duration `yaml:"windowSize,omitempty" json:"windowSize,omitempty"`
3848
// Window type for time-series aggregation. If not set, defaults to sliding.
39-
WindowType *WindowType `yaml:"windowType" json:"windowType"`
49+
WindowType *WindowType `yaml:"windowType,omitempty" json:"windowType,omitempty"`
4050
// A cron string used to determine when to reset resource usage for all queues.
41-
TumblingWindowCronString string `yaml:"tumblingWindowCronString" json:"tumblingWindowCronString"`
51+
TumblingWindowCronString string `yaml:"tumblingWindowCronString,omitempty" json:"tumblingWindowCronString,omitempty"`
4252
// Fetch interval of the usage. Default is 1 minute.
43-
FetchInterval *time.Duration `yaml:"fetchInterval" json:"fetchInterval"`
53+
FetchInterval *metav1.Duration `yaml:"fetchInterval,omitempty" json:"fetchInterval,omitempty"`
4454
// Staleness period of the usage. Default is 5 minutes.
45-
StalenessPeriod *time.Duration `yaml:"stalenessPeriod" json:"stalenessPeriod"`
55+
StalenessPeriod *metav1.Duration `yaml:"stalenessPeriod,omitempty" json:"stalenessPeriod,omitempty"`
4656
// Wait timeout of the usage. Default is 1 minute.
47-
WaitTimeout *time.Duration `yaml:"waitTimeout" json:"waitTimeout"`
57+
WaitTimeout *metav1.Duration `yaml:"waitTimeout,omitempty" json:"waitTimeout,omitempty"`
4858

4959
// ExtraParams are extra parameters for the usage db client, which are client specific.
50-
ExtraParams map[string]string `yaml:"extraParams" json:"extraParams"`
60+
ExtraParams map[string]string `yaml:"extraParams,omitempty" json:"extraParams,omitempty"`
61+
}
62+
63+
func (p *UsageParams) DeepCopy() *UsageParams {
64+
out := new(UsageParams)
65+
if p.HalfLifePeriod != nil {
66+
duration := *p.HalfLifePeriod
67+
out.HalfLifePeriod = &duration
68+
}
69+
if p.WindowSize != nil {
70+
duration := *p.WindowSize
71+
out.WindowSize = &duration
72+
}
73+
if p.WindowType != nil {
74+
windowType := *p.WindowType
75+
out.WindowType = &windowType
76+
}
77+
out.TumblingWindowCronString = p.TumblingWindowCronString
78+
if p.FetchInterval != nil {
79+
duration := *p.FetchInterval
80+
out.FetchInterval = &duration
81+
}
82+
if p.StalenessPeriod != nil {
83+
duration := *p.StalenessPeriod
84+
out.StalenessPeriod = &duration
85+
}
86+
if p.WaitTimeout != nil {
87+
duration := *p.WaitTimeout
88+
out.WaitTimeout = &duration
89+
}
90+
if p.ExtraParams != nil {
91+
out.ExtraParams = make(map[string]string, len(p.ExtraParams))
92+
for k, v := range p.ExtraParams {
93+
out.ExtraParams[k] = v
94+
}
95+
}
96+
return out
5197
}

0 commit comments

Comments
 (0)