Repository navigation
Expand file tree
/
Copy pathscale.go
More file actions
57 lines (50 loc) · 2.39 KB
/
Copy pathscale.go
File metadata and controls
57 lines (50 loc) · 2.39 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
// SPDX-License-Identifier: Apache-2.0
package typeset
import "k8s.io/apimachinery/pkg/runtime/schema"
// ScaleSourceBuiltinRegistry labels a ScaleBinding resolved from the built-in
// registry below, so it reads identically to a CRD-sourced binding for the writer.
const ScaleSourceBuiltinRegistry = "builtin-registry"
// builtinSpecReplicasPath is the parent replica path every currently-served
// built-in scalable resource maps scale.spec.replicas onto. The version is
// intentionally ignored: this path is stable for these resources across their
// served versions.
const builtinSpecReplicasPath = ".spec.replicas"
// builtinScaleResponseGVK is the standardized object Kubernetes returns for a
// built-in /scale call.
func builtinScaleResponseGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{Group: "autoscaling", Version: "v1", Kind: "Scale"}
}
// builtinScalable is the closed set of currently-served built-in scalable
// resources, keyed by API group and plural resource. Each maps scale.spec.replicas
// to the parent's .spec.replicas. CRDs draw the path from the CRD definition and
// aggregated APIs have no generic discovery field, so neither is listed here; both
// must resolve their own binding rather than default to .spec.replicas.
func builtinScalable(group, resource string) bool {
switch {
case group == "apps" && resource == "deployments",
group == "apps" && resource == "statefulsets",
group == "apps" && resource == "replicasets",
group == "" && resource == "replicationcontrollers":
return true
default:
return false
}
}
// BuiltinScale returns the /scale binding for a currently-served built-in scalable
// resource identified by API group and plural resource. ok is false for any other
// resource — a CRD, an aggregated API, or a non-scalable built-in — in which case
// the scale event must be resolved elsewhere or dropped, never defaulted to
// .spec.replicas. It is the single source of built-in scale facts, shared by the
// cluster registry (origin/scale enrichment) and the audit consumer (scale write).
func BuiltinScale(group, resource string) (ScaleBinding, bool) {
if !builtinScalable(group, resource) {
return ScaleBinding{}, false
}
return ScaleBinding{
Enabled: true,
Source: ScaleSourceBuiltinRegistry,
ResponseGVK: builtinScaleResponseGVK(),
SpecReplicasPath: builtinSpecReplicasPath,
Usable: true,
}, true
}