Skip to content

feat: preemptive scheduling #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions api/clusters/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"encoding/json"
"strings"

"github.com/openmcp-project/controller-utils/pkg/collections"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
)

// ClusterSpec defines the desired state of Cluster
Expand Down Expand Up @@ -122,18 +122,40 @@ func (cs *ClusterStatus) SetProviderStatus(from any) error {

// GetTenancyCount returns the number of ClusterRequests currently pointing to this cluster.
// This is determined by counting the finalizers that have the corresponding prefix.
// Note that only unique finalizers are counted, so if there are multiple identical request finalizers
// (which should not happen), this method's return value might not match the actual number of finalizers with the prefix.
func (c *Cluster) GetTenancyCount() int {
return c.GetRequestUIDs().Len()
return collections.AggregateMap(c.GetRequestUIDs(), func(_ string, count int, current int) int {
return count + current
}, 0)
}

// GetRequestUIDs returns the UIDs of all ClusterRequests that have marked this cluster with a corresponding finalizer.
func (c *Cluster) GetRequestUIDs() sets.Set[string] {
res := sets.New[string]()
// GetRequestUIDs returns the UIDs of all ClusterRequests that have marked this cluster with a corresponding finalizer,
// mapped to their respective counts.
// Note that a regular request is currently expected to have exactly one finalizer, so the counts for each UID should be 1.
func (c *Cluster) GetRequestUIDs() map[string]int {
res := map[string]int{}
for _, fin := range c.Finalizers {
if strings.HasPrefix(fin, RequestFinalizerOnClusterPrefix) {
res.Insert(strings.TrimPrefix(fin, RequestFinalizerOnClusterPrefix))
res[strings.TrimPrefix(fin, RequestFinalizerOnClusterPrefix)]++
}
}
return res
}

// GetPreemptiveTenancyCount returns the number of PreemptiveClusterRequests currently pointing to this cluster.
// This is determined by counting the finalizers that have the corresponding prefix.
func (c *Cluster) GetPreemptiveTenancyCount() int {
return collections.AggregateMap(c.GetPreemptiveRequestUIDs(), func(_ string, count int, current int) int {
return count + current
}, 0)
}

// GetPreemptiveRequestUIDs returns the UIDs of all PreemptiveClusterRequests that have marked this cluster with a corresponding finalizer,
// mapped to their respective counts.
func (c *Cluster) GetPreemptiveRequestUIDs() map[string]int {
res := map[string]int{}
for _, fin := range c.Finalizers {
if strings.HasPrefix(fin, PreemptiveRequestFinalizerOnClusterPrefix) {
res[strings.TrimPrefix(fin, PreemptiveRequestFinalizerOnClusterPrefix)]++
}
}
return res
Expand Down
2 changes: 1 addition & 1 deletion api/clusters/v1alpha1/clusterrequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type ClusterRequest struct {
type ClusterRequestList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Cluster `json:"items"`
Items []ClusterRequest `json:"items"`
}

func init() {
Expand Down
10 changes: 7 additions & 3 deletions api/clusters/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,20 @@ const (
// For AccessRequests, the generic controller that is part of the openMCP Operator sets it.
ProviderLabel = GroupName + "/provider"

// DeleteWithoutRequestsLabel marks that the corresponding cluster can be deleted if the scheduler removes the last request pointing to it.
// Its value must be "true" for the label to take effect.
DeleteWithoutRequestsLabel = GroupName + "/delete-without-requests"
// ProfileLabel is used to make the profile information easily accessible on AccessRequests.
ProfileLabel = GroupName + "/profile"
)

const (
// DeleteWithoutRequestsLabel marks that the corresponding cluster can be deleted if the scheduler removes the last request pointing to it.
// Its value must be "true" for the label to take effect.
DeleteWithoutRequestsLabel = GroupName + "/delete-without-requests"
// ClusterRequestFinalizer is the finalizer used on ClusterRequest resources
ClusterRequestFinalizer = GroupName + "/request"
// RequestFinalizerOnClusterPrefix is the prefix for the finalizers that mark a Cluster as being referenced by a ClusterRequest.
RequestFinalizerOnClusterPrefix = "request." + GroupName + "/"
// PreemptiveRequestFinalizerOnClusterPrefix is the prefix for the finalizers that mark a Cluster as being referenced by a preemptive ClusterRequest.
PreemptiveRequestFinalizerOnClusterPrefix = "preemptive." + GroupName + "/"
// SchedulerCreationIDLabel can be used to track which scheduling operation created a specific Cluster. Mainly for internal purposes.
SchedulerCreationIDLabel = GroupName + "/creation-id"
)
2 changes: 2 additions & 0 deletions api/clusters/v1alpha1/constants/reasons.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ const (
ReasonConfigurationProblem = "ConfigurationProblem"
// ReasonInternalError indicates that something went wrong internally.
ReasonInternalError = "InternalError"
// ReasonSchedulingFailed indicates that there was a problem with scheduling a request.
ReasonSchedulingFailed = "SchedulingFailed"
)
63 changes: 63 additions & 0 deletions api/clusters/v1alpha1/preemptiveclusterrequest_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type PreemptiveClusterRequestSpec struct {
// Purpose is the purpose of the requested cluster.
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="purpose is immutable"
// +kubebuilder:validation:MinLength=1
Purpose string `json:"purpose"`

// Workload specifies for how many workloads this preemptive cluster request should account.
// Must be greater than 0.
// +kubebuilder:validation:Minimum=1
Workload int `json:"workload"`
}

type PreemptiveClusterRequestStatus struct {
CommonStatus `json:",inline"`

// Phase is the current phase of the request.
// +kubebuilder:default=Pending
// +kubebuilder:validation:Enum=Pending;Granted;Denied
Phase RequestPhase `json:"phase"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=pcr;pcreq
// +kubebuilder:metadata:labels="openmcp.cloud/cluster=platform"
// +kubebuilder:selectablefield:JSONPath=".spec.purpose"
// +kubebuilder:selectablefield:JSONPath=".status.phase"
// +kubebuilder:printcolumn:JSONPath=".spec.purpose",name="Purpose",type=string
// +kubebuilder:printcolumn:JSONPath=".spec.workload",name="Workload",type=string
// +kubebuilder:printcolumn:JSONPath=".status.phase",name="Phase",type=string

type PreemptiveClusterRequest struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PreemptiveClusterRequestSpec `json:"spec,omitempty"`
Status PreemptiveClusterRequestStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// PreemptiveClusterRequestList contains a list of Cluster
type PreemptiveClusterRequestList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []PreemptiveClusterRequest `json:"items"`
}

func init() {
SchemeBuilder.Register(&PreemptiveClusterRequest{}, &PreemptiveClusterRequestList{})
}

// FinalizerForCluster returns the finalizer that is used to mark that a specific request has pointed to a specific cluster.
// Apart from preventing the Cluster's deletion, this information is used to recover the Cluster if the status of the PreemptiveClusterRequest ever gets lost.
func (cr *PreemptiveClusterRequest) FinalizerForCluster() string {
return PreemptiveRequestFinalizerOnClusterPrefix + string(cr.UID)
}
92 changes: 91 additions & 1 deletion api/clusters/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading