-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhandler.go
More file actions
82 lines (72 loc) · 2.98 KB
/
Copy pathhandler.go
File metadata and controls
82 lines (72 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright SAP SE
// SPDX-License-Identifier: Apache-2.0
package api
import (
"net/http"
"strings"
"sync"
"github.com/cobaltcore-dev/cortex/internal/scheduling/reservations"
commitments "github.com/cobaltcore-dev/cortex/internal/scheduling/reservations/commitments"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var apiLog = ctrl.Log.WithName("committed-resource-api").WithValues("module", "committed-resources")
// HTTPAPI implements Limes LIQUID commitment validation endpoints.
type HTTPAPI struct {
client client.Client
config commitments.APIConfig
usageDB reservations.VMSource
monitor ChangeCommitmentsAPIMonitor
usageMonitor ReportUsageAPIMonitor
capacityMonitor ReportCapacityAPIMonitor
infoMonitor InfoAPIMonitor
quotaMonitor QuotaAPIMonitor
// Mutex to serialize change-commitments requests
changeMutex sync.Mutex
}
func NewAPI(client client.Client) *HTTPAPI {
return NewAPIWithConfig(client, commitments.DefaultAPIConfig(), nil)
}
// NewAPIWithConfig creates an HTTPAPI with the given config and optional usageDB client.
func NewAPIWithConfig(k8sClient client.Client, config commitments.APIConfig, vmSource reservations.VMSource) *HTTPAPI {
return &HTTPAPI{
client: k8sClient,
config: config,
usageDB: vmSource,
monitor: NewChangeCommitmentsAPIMonitor(),
usageMonitor: NewReportUsageAPIMonitor(),
capacityMonitor: NewReportCapacityAPIMonitor(),
infoMonitor: NewInfoAPIMonitor(),
quotaMonitor: NewQuotaAPIMonitor(),
}
}
func (api *HTTPAPI) Init(mux *http.ServeMux, registry prometheus.Registerer, log logr.Logger) {
registry.MustRegister(&api.monitor)
registry.MustRegister(&api.usageMonitor)
registry.MustRegister(&api.capacityMonitor)
registry.MustRegister(&api.infoMonitor)
registry.MustRegister(&api.quotaMonitor)
mux.HandleFunc("/commitments/v1/change-commitments", api.HandleChangeCommitments)
mux.HandleFunc("/commitments/v1/report-capacity", api.HandleReportCapacity)
mux.HandleFunc("/commitments/v1/info", api.HandleInfo)
mux.HandleFunc("/commitments/v1/projects/", api.handleProjectEndpoint) // routes to report-usage or quota
log.Info("commitments API initialized",
"changeCommitmentsEnabled", api.config.EnableChangeCommitments,
"reportUsageEnabled", api.config.EnableReportUsage,
"reportCapacityEnabled", api.config.EnableReportCapacity)
commitments.LogFlavorGroupResourceConfig(log, api.config.FlavorGroupResourceConfig)
}
// handleProjectEndpoint routes /commitments/v1/projects/:project_id/... requests to the appropriate handler.
func (api *HTTPAPI) handleProjectEndpoint(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch {
case strings.HasSuffix(path, "/report-usage"):
api.HandleReportUsage(w, r)
case strings.HasSuffix(path, "/quota"):
api.HandleQuota(w, r)
default:
http.Error(w, "Not found", http.StatusNotFound)
}
}