-
Notifications
You must be signed in to change notification settings - Fork 6
Implement in-flight reservations controller #957
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
Open
PhilippMatthes
wants to merge
9
commits into
main
Choose a base branch
from
in-flight-reservations-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
34aa2f4
Implement in-flight reservations controller
PhilippMatthes 189de61
Tests, polishing, and add to main func
PhilippMatthes 29afa24
Add kpi & alert tracking stalled in-flight reservations
PhilippMatthes d869070
Address CodeRabbit review comments
PhilippMatthes c43cfd8
Fix whitespace lint issues in inflight controller
cortex-ai-agents[bot] efbda50
Ensure reservations are only pruned if lifecycle op is complete
PhilippMatthes 12bdbaf
Polishing & fixing
PhilippMatthes 71be3ac
Address CodeRabbit review: semantic Quantity compare & host-guard
PhilippMatthes 27718bb
Small code comment refinement
PhilippMatthes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
internal/knowledge/kpis/plugins/deployment/reservation_state.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright SAP SE | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package deployment | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/cobaltcore-dev/cortex/api/v1alpha1" | ||
| "github.com/cobaltcore-dev/cortex/internal/knowledge/db" | ||
| "github.com/cobaltcore-dev/cortex/internal/knowledge/kpis/plugins" | ||
| "github.com/cobaltcore-dev/cortex/pkg/conf" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| var reservationStateKPILogger = ctrl.Log.WithName("reservation-state-kpi") | ||
|
|
||
| type ReservationStateKPIOpts struct { | ||
| // The scheduling domain to filter reservations by. | ||
| ReservationSchedulingDomain v1alpha1.SchedulingDomain `json:"reservationSchedulingDomain"` | ||
| } | ||
|
|
||
| // KPI observing the state of reservation resources managed by cortex. | ||
| // Metrics are labeled by the reservation type (e.g. InFlightReservation, | ||
| // CommittedResourceReservation, FailoverReservation) so that operators can | ||
| // alert on specific reservation kinds. The state label mirrors the | ||
| // v1alpha1.ReservationConditionReady condition status and the reason label | ||
| // carries the condition's Reason so that alerts can target specific failure | ||
| // modes (e.g. reason="InstanceNotFound" for in-flight reservations whose VM | ||
| // has not spawned on any hypervisor yet). | ||
| type ReservationStateKPI struct { | ||
| // Common base for all KPIs that provides standard functionality. | ||
| plugins.BaseKPI[ReservationStateKPIOpts] | ||
|
|
||
| // Prometheus descriptor for the reservation state metric. | ||
| counter *prometheus.Desc | ||
| } | ||
|
|
||
| func (ReservationStateKPI) GetName() string { return "reservation_state_kpi" } | ||
|
|
||
| // Initialize the KPI. | ||
| func (k *ReservationStateKPI) Init(db *db.DB, client client.Client, opts conf.RawOpts) error { | ||
| if err := k.BaseKPI.Init(db, client, opts); err != nil { | ||
| return err | ||
| } | ||
| k.counter = prometheus.NewDesc( | ||
| "cortex_reservation_state", | ||
| "State of cortex managed reservations", | ||
| []string{"domain", "type", "state", "reason"}, | ||
| nil, | ||
| ) | ||
| return nil | ||
| } | ||
|
|
||
| // Conform to the prometheus collector interface by providing the descriptor. | ||
| func (k *ReservationStateKPI) Describe(ch chan<- *prometheus.Desc) { ch <- k.counter } | ||
|
|
||
| // Collect the reservation state metrics. | ||
| func (k *ReservationStateKPI) Collect(ch chan<- prometheus.Metric) { | ||
| // Bound the list call so a slow API server can't hang the Prometheus | ||
| // scrape indefinitely; if it fails we log so the disappearance of the | ||
| // reservation metric is not silent. | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| // Get all reservations. The scheduling domain filter is applied per item | ||
| // since a Reservation is cluster-scoped and may cover multiple domains. | ||
| reservationList := &v1alpha1.ReservationList{} | ||
| if err := k.Client.List(ctx, reservationList); err != nil { | ||
| reservationStateKPILogger.Error(err, "Failed to list reservations") | ||
| return | ||
| } | ||
| // Aggregate counts by (type, state, reason) so that we emit one time | ||
| // series per bucket rather than one per reservation. This keeps metric | ||
| // cardinality bounded regardless of how many reservations exist. | ||
| type bucket struct { | ||
| reservationType string | ||
| state string | ||
| reason string | ||
| } | ||
| counts := map[bucket]float64{} | ||
| for _, r := range reservationList.Items { | ||
| if r.Spec.SchedulingDomain != k.Options.ReservationSchedulingDomain { | ||
| continue | ||
| } | ||
| state := "unknown" | ||
| reason := "" | ||
| cond := meta.FindStatusCondition(r.Status.Conditions, v1alpha1.ReservationConditionReady) | ||
| if cond != nil { | ||
| reason = cond.Reason | ||
| switch cond.Status { | ||
| case metav1.ConditionTrue: | ||
| state = "ready" | ||
| case metav1.ConditionFalse: | ||
| state = "error" | ||
| default: | ||
| state = "unknown" | ||
| } | ||
| } | ||
| counts[bucket{ | ||
| reservationType: string(r.Spec.Type), | ||
| state: state, | ||
| reason: reason, | ||
| }]++ | ||
| } | ||
| for b, v := range counts { | ||
| ch <- prometheus.MustNewConstMetric( | ||
| k.counter, prometheus.GaugeValue, v, | ||
| string(k.Options.ReservationSchedulingDomain), | ||
| b.reservationType, b.state, b.reason, | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.