Skip to content

Commit

Permalink
manifests: sched: support for LeaderElection params
Browse files Browse the repository at this point in the history
add support to get/set LeaderEletion parameters
programmatically.

Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Mar 20, 2024
1 parent 1f994c8 commit bdb83e7
Show file tree
Hide file tree
Showing 5 changed files with 504 additions and 16 deletions.
8 changes: 5 additions & 3 deletions pkg/manifests/sched/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ import (
)

const (
DefaultProfileName = "topology-aware-scheduler"
DefaultResyncPeriod = 5 * time.Second
DefaultVerbose = 4
DefaultProfileName = "topology-aware-scheduler"
DefaultResyncPeriod = 5 * time.Second
DefaultVerbose = 4
DefaultCtrlPlaneAffinity = true
DefaultLeaderElectResource = manifests.LeaderElectionDefaultNamespace + "/" + manifests.LeaderElectionDefaultName
)

const (
Expand Down
67 changes: 67 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 = "nrtmatch-scheduler"
LeaderElectionDefaultNamespace = "tas-scheduler"
)

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"`
ResourceNamespace string `json:"resourceNamespace,omitempty"`
ResourceName string `json:"resourceName,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 All @@ -151,6 +172,20 @@ func DecodeSchedulerProfilesFromData(data []byte) ([]ConfigParams, error) {
return params, nil
}

lead, ok, err := unstructured.NestedMap(r.Object, "leaderElection")
if err != nil {
klog.ErrorS(err, "failed to process unstructured data")
return params, err
}
var electParams *LeaderElectionParams
if ok {
electParams, err = extractLeaderElectionParams(lead)
if err != nil {
klog.ErrorS(err, "failed to extract leader election params")
return params, nil
}
}

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 @@ -200,6 +235,10 @@ func DecodeSchedulerProfilesFromData(data []byte) ([]ConfigParams, error) {
klog.ErrorS(err, "failed to extract params", "name", name, "profile", profileName)
continue
}
// since Leader Election Params is a global setting (independent from profiles),
// all profiles must share the same data. This is a modelization error which
// we should fix on later releases.
profileParams.LeaderElection = electParams

params = append(params, profileParams)
}
Expand Down Expand Up @@ -324,6 +363,34 @@ func extractParams(profileName string, args map[string]interface{}) (ConfigParam
return params, nil
}

func extractLeaderElectionParams(lead map[string]interface{}) (*LeaderElectionParams, error) {
var params LeaderElectionParams

enabled, ok, err := unstructured.NestedBool(lead, "leaderElect")
if !ok || err != nil {
return &params, fmt.Errorf("unexpected leaderElect data (err=%v)", err)
}
params.LeaderElect = enabled

resourceNamespace, ok, err := unstructured.NestedString(lead, "resourceNamespace")
if err != nil {
return &params, fmt.Errorf("unexpected resourceNamespace data: %w", err)
}
if ok {
params.ResourceNamespace = resourceNamespace
}

resourceName, ok, err := unstructured.NestedString(lead, "resourceName")
if err != nil {
return &params, fmt.Errorf("unexpected resourceName data: %w", err)
}
if ok {
params.ResourceName = resourceName
}

return &params, nil
}

func newInt64(v int64) *int64 {
return &v
}
Expand Down
Loading

0 comments on commit bdb83e7

Please sign in to comment.