-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi.go
More file actions
66 lines (57 loc) · 2.31 KB
/
api.go
File metadata and controls
66 lines (57 loc) · 2.31 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
// Copyright SAP SE
// SPDX-License-Identifier: Apache-2.0
package commitments
import (
"context"
"net/http"
"sync"
"github.com/cobaltcore-dev/cortex/internal/scheduling/nova"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// UsageNovaClient is a minimal interface for the Nova client needed by the usage API.
// This allows for easy mocking in tests without implementing the full NovaClient interface.
type UsageNovaClient interface {
ListProjectServers(ctx context.Context, projectID string) ([]nova.ServerDetail, error)
}
// HTTPAPI implements Limes LIQUID commitment validation endpoints.
type HTTPAPI struct {
client client.Client
config Config
novaClient UsageNovaClient
monitor ChangeCommitmentsAPIMonitor
usageMonitor ReportUsageAPIMonitor
capacityMonitor ReportCapacityAPIMonitor
infoMonitor InfoAPIMonitor
// Mutex to serialize change-commitments requests
changeMutex sync.Mutex
}
func NewAPI(client client.Client) *HTTPAPI {
return NewAPIWithConfig(client, DefaultConfig(), nil)
}
func NewAPIWithConfig(client client.Client, config Config, novaClient UsageNovaClient) *HTTPAPI {
return &HTTPAPI{
client: client,
config: config,
novaClient: novaClient,
monitor: NewChangeCommitmentsAPIMonitor(),
usageMonitor: NewReportUsageAPIMonitor(),
capacityMonitor: NewReportCapacityAPIMonitor(),
infoMonitor: NewInfoAPIMonitor(),
}
}
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)
mux.HandleFunc("/v1/commitments/change-commitments", api.HandleChangeCommitments)
mux.HandleFunc("/v1/commitments/report-capacity", api.HandleReportCapacity)
mux.HandleFunc("/v1/commitments/info", api.HandleInfo)
mux.HandleFunc("/v1/commitments/projects/", api.HandleReportUsage) // matches /v1/commitments/projects/:project_id/report-usage
log.Info("commitments API initialized",
"changeCommitmentsEnabled", api.config.EnableChangeCommitmentsAPI,
"reportUsageEnabled", api.config.EnableReportUsageAPI,
"reportCapacityEnabled", api.config.EnableReportCapacityAPI)
}