Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Mar 5, 2024
1 parent 1f994c8 commit 723eaee
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func NewDeploySchedulerPluginCommand(env *deployer.Environment, commonOpts *opti
Verbose: commonOpts.SchedVerbose,
ScoringStratConfigData: commonOpts.SchedScoringStratConfigData,
CacheParamsConfigData: commonOpts.SchedCacheParamsConfigData,
LeaderElection: commonOpts.Replicas > 1,
})
},
Args: cobra.NoArgs,
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func NewRemoveCommand(env *deployer.Environment, commonOpts *options.Options) *c
Verbose: commonOpts.SchedVerbose,
ScoringStratConfigData: commonOpts.SchedScoringStratConfigData,
CacheParamsConfigData: commonOpts.SchedCacheParamsConfigData,
LeaderElection: commonOpts.Replicas > 1,
})
if err != nil {
// intentionally keep going to remove as much as possible
Expand Down Expand Up @@ -166,6 +167,7 @@ func NewRemoveSchedulerPluginCommand(env *deployer.Environment, commonOpts *opti
Verbose: commonOpts.SchedVerbose,
ScoringStratConfigData: commonOpts.SchedScoringStratConfigData,
CacheParamsConfigData: commonOpts.SchedCacheParamsConfigData,
LeaderElection: commonOpts.Replicas > 1,
})
},
Args: cobra.NoArgs,
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func NewRenderSchedulerPluginCommand(env *deployer.Environment, commonOpts *opti
CacheResyncPeriod: commonOpts.SchedResyncPeriod,
ScoringStratConfigData: commonOpts.SchedScoringStratConfigData,
CacheParamsConfigData: commonOpts.SchedCacheParamsConfigData,
LeaderElection: commonOpts.Replicas > 1,
}
schedObjs, err := schedManifests.Render(env.Log, renderOpts)
if err != nil {
Expand Down Expand Up @@ -186,6 +187,7 @@ func RenderManifests(env *deployer.Environment, commonOpts *options.Options) err
Verbose: commonOpts.SchedVerbose,
ScoringStratConfigData: commonOpts.SchedScoringStratConfigData,
CacheParamsConfigData: commonOpts.SchedCacheParamsConfigData,
LeaderElection: commonOpts.Replicas > 1,
}

schedObjs, err := schedManifests.Render(env.Log, schedRenderOpts)
Expand Down
13 changes: 10 additions & 3 deletions pkg/manifests/sched/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,20 @@ func (mf Manifests) Render(logger logr.Logger, opts options.Scheduler) (Manifest
Cache: manifests.NewConfigCacheParams(),
}

if opts.LeaderElection {
leap := manifests.LeaderElectionParams{}
manifests.SetDefaultsLeaderElection(&leap)
leap.LeaderElect = true
params.LeaderElection = &leap
}

if len(opts.CacheParamsConfigData) > 0 {
err = yaml.Unmarshal([]byte(opts.CacheParamsConfigData), params.Cache)
if err != nil {
return ret, err
}
}

// always override
params.Cache.ResyncPeriodSeconds = newInt64(int64(opts.CacheResyncPeriod.Seconds()))

if len(opts.ScoringStratConfigData) > 0 {
params.ScoringStrategy = &manifests.ScoringStrategyParams{}
err = yaml.Unmarshal([]byte(opts.ScoringStratConfigData), params.ScoringStrategy)
Expand All @@ -122,6 +126,9 @@ func (mf Manifests) Render(logger logr.Logger, opts options.Scheduler) (Manifest
}
}

// always override
params.Cache.ResyncPeriodSeconds = newInt64(int64(opts.CacheResyncPeriod.Seconds()))

err = schedupdate.SchedulerConfig(ret.ConfigMap, DefaultProfileName, &params)
if err != nil {
return ret, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/manifests/schedparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const (
ScoringStrategyLeastAllocated = "LeastAllocated"
)

const (
LeaderElectionDefaultName = "k8stawwg-nrtmatch-scheduler"
LeaderElectionDefaultNamespace = "k8stawwg"
)

func ValidateForeignPodsDetectMode(value string) error {
switch value {
case ForeignPodsDetectNone:
Expand Down Expand Up @@ -136,10 +141,26 @@ func ValidateScoringStrategyType(value string) error {
}
}

type LeaderElectionParams struct {
LeaderElect bool `json:"leaderElect,omitempty"`
ResourceName string `json:"resourceName,omitempty"`
ResourceNamespace string `json:"resourceNamespace,omitempty"`
}

func SetDefaultsLeaderElection(lep *LeaderElectionParams) {
if lep.ResourceName == "" {
lep.ResourceName = LeaderElectionDefaultName
}
if lep.ResourceNamespace == "" {
lep.ResourceNamespace = LeaderElectionDefaultNamespace
}
}

type ConfigParams struct {
ProfileName string // can't be empty, so no need for pointer
Cache *ConfigCacheParams
ScoringStrategy *ScoringStrategyParams
LeaderElection *LeaderElectionParams
}

func DecodeSchedulerProfilesFromData(data []byte) ([]ConfigParams, error) {
Expand Down
49 changes: 49 additions & 0 deletions pkg/objectupdate/sched/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ func RenderConfig(data []byte, schedulerName string, params *manifests.ConfigPar

updated := false

if params.LeaderElection != nil {
lead, ok, err := unstructured.NestedMap(r.Object, "leaderElection")
if !ok || err != nil {
klog.ErrorS(err, "failed to process unstructured data", "leaderElection", ok)
return data, false, err
}

leadUpdated, err := updateLeaderElection(lead, params)
if err != nil {
klog.ErrorS(err, "failed to update unstructured data", "leaderElection", lead, "params", params)
return data, false, err
}
if leadUpdated {
updated = true
}

if err := unstructured.SetNestedMap(r.Object, lead, "leaderElection"); err != nil {
klog.ErrorS(err, "failed to override unstructured data", "data", "leaderElection")
return data, false, err
}

}

profiles, ok, err := unstructured.NestedSlice(r.Object, "profiles")
if !ok || err != nil {
klog.ErrorS(err, "failed to process unstructured data", "profiles", ok)
Expand Down Expand Up @@ -153,6 +176,32 @@ func RenderConfig(data []byte, schedulerName string, params *manifests.ConfigPar
return newData, updated, nil
}

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

err = unstructured.SetNestedField(lead, params.LeaderElection.LeaderElect, "leaderElect")
if err != nil {
return updated > 0, err
}
updated++

err = unstructured.SetNestedField(lead, params.LeaderElection.ResourceName, "resourceName")
if err != nil {
return updated > 0, err
}
updated++

err = unstructured.SetNestedField(lead, params.LeaderElection.ResourceNamespace, "resourceNamespace")
if err != nil {
return updated > 0, err
}
updated++

return updated > 0, nil

}

func updateArgs(args map[string]interface{}, params *manifests.ConfigParams) (bool, error) {
var updated int
var err error
Expand Down
2 changes: 2 additions & 0 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Options struct {
SchedResyncPeriod time.Duration
SchedVerbose int
SchedCtrlPlaneAffinity bool
SchedLeaderElection bool
WaitInterval time.Duration
WaitTimeout time.Duration
ClusterPlatform platform.Platform
Expand All @@ -61,6 +62,7 @@ type Scheduler struct {
PullIfNotPresent bool
CacheResyncPeriod time.Duration
CtrlPlaneAffinity bool
LeaderElection bool
Verbose int
ScoringStratConfigData string
CacheParamsConfigData string
Expand Down

0 comments on commit 723eaee

Please sign in to comment.