-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi_info.go
More file actions
148 lines (128 loc) · 5.23 KB
/
api_info.go
File metadata and controls
148 lines (128 loc) · 5.23 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright SAP SE
// SPDX-License-Identifier: Apache-2.0
package commitments
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/cobaltcore-dev/cortex/internal/scheduling/reservations"
"github.com/go-logr/logr"
"github.com/google/uuid"
liquid "github.com/sapcc/go-api-declarations/liquid"
)
// handles GET /v1/info requests from Limes:
// See: https://github.com/sapcc/go-api-declarations/blob/main/liquid/commitment.go
// See: https://pkg.go.dev/github.com/sapcc/go-api-declarations/liquid
func (api *HTTPAPI) HandleInfo(w http.ResponseWriter, r *http.Request) {
// Extract or generate request ID for tracing
requestID := r.Header.Get("X-Request-ID")
if requestID == "" {
requestID = uuid.New().String()
}
// Set request ID in response header for client correlation
w.Header().Set("X-Request-ID", requestID)
ctx := reservations.WithGlobalRequestID(r.Context(), "committed-resource-"+requestID)
logger := LoggerFromContext(ctx).WithValues("component", "api", "endpoint", "/v1/info")
// Only accept GET method
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
logger.V(1).Info("processing info request")
// Build info response
info, err := api.buildServiceInfo(ctx, logger)
if err != nil {
// Use Info level for expected conditions like knowledge not being ready yet
logger.Info("service info not available yet", "error", err.Error())
http.Error(w, "Service temporarily unavailable: "+err.Error(),
http.StatusServiceUnavailable)
return
}
// Return response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(info); err != nil {
logger.Error(err, "failed to encode service info")
return
}
}
// resourceAttributes holds the custom attributes for a resource in the info API response.
type resourceAttributes struct {
RamCoreRatio *uint64 `json:"ramCoreRatio,omitempty"`
RamCoreRatioMin *uint64 `json:"ramCoreRatioMin,omitempty"`
RamCoreRatioMax *uint64 `json:"ramCoreRatioMax,omitempty"`
}
// buildServiceInfo constructs the ServiceInfo response with metadata for all flavor groups.
func (api *HTTPAPI) buildServiceInfo(ctx context.Context, logger logr.Logger) (liquid.ServiceInfo, error) {
// Get all flavor groups from Knowledge CRDs
knowledge := &reservations.FlavorGroupKnowledgeClient{Client: api.client}
flavorGroups, err := knowledge.GetAllFlavorGroups(ctx, nil)
if err != nil {
// Return -1 as version when knowledge is not ready
return liquid.ServiceInfo{
Version: -1,
Resources: make(map[liquid.ResourceName]liquid.ResourceInfo),
}, err
}
// Build resources map
resources := make(map[liquid.ResourceName]liquid.ResourceInfo)
for groupName, groupData := range flavorGroups {
resourceName := liquid.ResourceName(commitmentResourceNamePrefix + groupName)
flavorNames := make([]string, 0, len(groupData.Flavors))
for _, flavor := range groupData.Flavors {
flavorNames = append(flavorNames, flavor.Name)
}
displayName := fmt.Sprintf(
"multiples of %d MiB (usable by: %s)",
groupData.SmallestFlavor.MemoryMB,
strings.Join(flavorNames, ", "),
)
// Only handle commitments for groups with a fixed RAM/core ratio
handlesCommitments := FlavorGroupAcceptsCommitments(&groupData)
// Build attributes JSON with ratio info
attrs := resourceAttributes{
RamCoreRatio: groupData.RamCoreRatio,
RamCoreRatioMin: groupData.RamCoreRatioMin,
RamCoreRatioMax: groupData.RamCoreRatioMax,
}
attrsJSON, err := json.Marshal(attrs)
if err != nil {
logger.Error(err, "failed to marshal resource attributes", "resourceName", resourceName)
attrsJSON = nil
}
resources[resourceName] = liquid.ResourceInfo{
DisplayName: displayName,
Unit: liquid.UnitNone, // Countable: multiples of smallest flavor instances
Topology: liquid.AZAwareTopology, // Commitments are per-AZ
NeedsResourceDemand: false, // Capacity planning out of scope for now
HasCapacity: handlesCommitments, // We report capacity via /v1/report-capacity only for groups that accept commitments
HasQuota: false, // No quota enforcement as of now
HandlesCommitments: handlesCommitments, // Only for groups with fixed RAM/core ratio
Attributes: attrsJSON,
}
logger.V(1).Info("registered flavor group resource",
"resourceName", resourceName,
"flavorGroup", groupName,
"displayName", displayName,
"smallestFlavor", groupData.SmallestFlavor.Name,
"smallestRamMB", groupData.SmallestFlavor.MemoryMB,
"handlesCommitments", handlesCommitments,
"ramCoreRatio", groupData.RamCoreRatio,
"ramCoreRatioMin", groupData.RamCoreRatioMin,
"ramCoreRatioMax", groupData.RamCoreRatioMax)
}
// Get last content changed from flavor group knowledge and treat it as version
var version int64 = -1
if knowledgeCRD, err := knowledge.Get(ctx); err == nil && knowledgeCRD != nil && !knowledgeCRD.Status.LastContentChange.IsZero() {
version = knowledgeCRD.Status.LastContentChange.Unix()
}
logger.Info("built service info",
"resourceCount", len(resources),
"version", version)
return liquid.ServiceInfo{
Version: version,
Resources: resources,
}, nil
}