Skip to content

Commit

Permalink
Merge pull request #252 from ffromani/sched-cache-fine-tuning-0.18
Browse files Browse the repository at this point in the history
[release-0.18][manual] manifests: objectupdate: cache fine-tuning support
  • Loading branch information
ffromani authored Nov 28, 2023
2 parents 66cbe68 + 4ce19bb commit e80847b
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 17 deletions.
79 changes: 72 additions & 7 deletions pkg/manifests/schedparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,48 @@ const (
SchedulerPluginName = "NodeResourceTopologyMatch"
)

const (
ForeignPodsDetectNone = "None"
ForeignPodsDetectAll = "All"
ForeignPodsDetectOnlyExclusiveResources = "OnlyExclusiveResources"
)

const (
CacheResyncAutodetect = "Autodetect"
CacheResyncAll = "All"
CacheResyncOnlyExclusiveResources = "OnlyExclusiveResources"
)

func ValidateForeignPodsDetectMode(value string) error {
switch value {
case ForeignPodsDetectNone:
return nil
case ForeignPodsDetectAll:
return nil
case ForeignPodsDetectOnlyExclusiveResources:
return nil
default:
return fmt.Errorf("unsupported foreignPodsDetectMode: %v", value)
}
}

func ValidateCacheResyncMethod(value string) error {
switch value {
case CacheResyncAutodetect:
return nil
case CacheResyncAll:
return nil
case CacheResyncOnlyExclusiveResources:
return nil
default:
return fmt.Errorf("unsupported cacheResyncMethod: %v", value)
}
}

type ConfigCacheParams struct {
ResyncPeriodSeconds *int64
ResyncPeriodSeconds *int64
ResyncMethod *string
ForeignPodsDetectMode *string
}

type ConfigParams struct {
Expand Down Expand Up @@ -122,15 +162,40 @@ func extractParams(profileName string, args map[string]interface{}) (ConfigParam
}
// json quirk: we know it's int64, yet it's detected as float64
resyncPeriod, ok, err := unstructured.NestedFloat64(args, "cacheResyncPeriodSeconds")
if !ok {
// nothing to do
return params, nil
}
if err != nil {
return params, fmt.Errorf("cannot process field cacheResyncPeriodSeconds: %w", err)
}
if ok {
val := int64(resyncPeriod)
params.Cache.ResyncPeriodSeconds = &val
}

cacheArgs, ok, err := unstructured.NestedMap(args, "cache")
if err != nil {
return params, fmt.Errorf("cannot process field cache: %w", err)
}
if ok {
resyncMethod, cacheOk, err := unstructured.NestedString(cacheArgs, "resyncMethod")
if err != nil {
return params, fmt.Errorf("cannot process field cache.resyncMethod: %w", err)
}
if cacheOk {
if err := ValidateCacheResyncMethod(resyncMethod); err != nil {
return params, err
}
params.Cache.ResyncMethod = &resyncMethod
}

val := int64(resyncPeriod)
params.Cache.ResyncPeriodSeconds = &val
foreignPodsDetect, cacheOk, err := unstructured.NestedString(cacheArgs, "foreignPodsDetect")
if err != nil {
return params, fmt.Errorf("cannot process field cache.foreignPodsDetect: %w", err)
}
if cacheOk {
if err := ValidateForeignPodsDetectMode(foreignPodsDetect); err != nil {
return params, err
}
params.Cache.ForeignPodsDetectMode = &foreignPodsDetect
}
}
return params, nil
}
109 changes: 109 additions & 0 deletions pkg/manifests/schedparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,111 @@ profiles:
},
expectedFound: true,
},
{
name: "nonzero resync period and all cache params",
data: []byte(`apiVersion: kubescheduler.config.k8s.io/v1beta3
kind: KubeSchedulerConfiguration
leaderElection:
leaderElect: false
profiles:
- pluginConfig:
- args:
cache:
foreignPodsDetect: None
resyncMethod: Autodetect
cacheResyncPeriodSeconds: 5
name: NodeResourceTopologyMatch
plugins:
filter:
enabled:
- name: NodeResourceTopologyMatch
reserve:
enabled:
- name: NodeResourceTopologyMatch
score:
enabled:
- name: NodeResourceTopologyMatch
schedulerName: topology-aware-scheduler
`),
schedulerName: "topology-aware-scheduler",
expectedParams: ConfigParams{
ProfileName: "topology-aware-scheduler",
Cache: &ConfigCacheParams{
ResyncPeriodSeconds: newInt64(5),
ResyncMethod: newString("Autodetect"),
ForeignPodsDetectMode: newString("None"),
},
},
expectedFound: true,
},
{
name: "nonzero resync period and some cache params",
data: []byte(`apiVersion: kubescheduler.config.k8s.io/v1beta3
kind: KubeSchedulerConfiguration
leaderElection:
leaderElect: false
profiles:
- pluginConfig:
- args:
cache:
resyncMethod: OnlyExclusiveResources
cacheResyncPeriodSeconds: 5
name: NodeResourceTopologyMatch
plugins:
filter:
enabled:
- name: NodeResourceTopologyMatch
reserve:
enabled:
- name: NodeResourceTopologyMatch
score:
enabled:
- name: NodeResourceTopologyMatch
schedulerName: topology-aware-scheduler
`),
schedulerName: "topology-aware-scheduler",
expectedParams: ConfigParams{
ProfileName: "topology-aware-scheduler",
Cache: &ConfigCacheParams{
ResyncPeriodSeconds: newInt64(5),
ResyncMethod: newString("OnlyExclusiveResources"),
},
},
expectedFound: true,
},
{
name: "zero resync period and some cache params",
data: []byte(`apiVersion: kubescheduler.config.k8s.io/v1beta3
kind: KubeSchedulerConfiguration
leaderElection:
leaderElect: false
profiles:
- pluginConfig:
- args:
cache:
foreignPodsDetect: OnlyExclusiveResources
name: NodeResourceTopologyMatch
plugins:
filter:
enabled:
- name: NodeResourceTopologyMatch
reserve:
enabled:
- name: NodeResourceTopologyMatch
score:
enabled:
- name: NodeResourceTopologyMatch
schedulerName: topology-aware-scheduler
`),
schedulerName: "topology-aware-scheduler",
expectedParams: ConfigParams{
ProfileName: "topology-aware-scheduler",
Cache: &ConfigCacheParams{
ForeignPodsDetectMode: newString("OnlyExclusiveResources"),
},
},
expectedFound: true,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -187,3 +292,7 @@ func toJSON(v any) string {
func newInt64(value int64) *int64 {
return &value
}

func newString(value string) *string {
return &value
}
54 changes: 54 additions & 0 deletions pkg/objectupdate/sched/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,63 @@ func updateArgs(args map[string]interface{}, params *manifests.ConfigParams) (bo
updated++
}
}

cacheArgs, ok, err := unstructured.NestedMap(args, "cache")
if !ok {
cacheArgs = make(map[string]interface{})
}
if err != nil {
return updated > 0, err
}

cacheArgsUpdated, err := updateCacheArgs(cacheArgs, params)
if err != nil {
return updated > 0, err
}
updated += cacheArgsUpdated

if cacheArgsUpdated > 0 {
if err := unstructured.SetNestedMap(args, cacheArgs, "cache"); err != nil {
return updated > 0, err
}
}
return updated > 0, ensureBackwardCompatibility(args)
}

func updateCacheArgs(args map[string]interface{}, params *manifests.ConfigParams) (int, error) {
var updated int
var err error

if params.Cache != nil {
if params.Cache.ResyncMethod != nil {
resyncMethod := *params.Cache.ResyncMethod // shortcut
err = manifests.ValidateCacheResyncMethod(resyncMethod)
if err != nil {
return updated, err
}
err = unstructured.SetNestedField(args, resyncMethod, "resyncMethod")
if err != nil {
return updated, err
}
updated++
}
if params.Cache.ForeignPodsDetectMode != nil {
foreignPodsMode := *params.Cache.ForeignPodsDetectMode // shortcut
err = manifests.ValidateForeignPodsDetectMode(foreignPodsMode)
if err != nil {
return updated, err
}
err = unstructured.SetNestedField(args, foreignPodsMode, "foreignPodsDetect")
if err != nil {
return updated, err
}
updated++
}
}

return updated, nil
}

func ensureBackwardCompatibility(args map[string]interface{}) error {
resyncPeriod, ok, err := unstructured.NestedInt64(args, "cacheResyncPeriodSeconds")
if !ok {
Expand Down
Loading

0 comments on commit e80847b

Please sign in to comment.