-
Notifications
You must be signed in to change notification settings - Fork 55
/
sizing.go
81 lines (68 loc) · 2.74 KB
/
sizing.go
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
package sizing
// Note on package:
// I didn't really want to pull this out into its own package
// I wanted this in database or providers but I ran into dependency cycle problems
// no matter what I did. So easiest and cleanest solution was just to pull it out
// that said maybe in the future we can extend sizing out to other stuff in which case
// a sizing package will be helpful
// Note on naming:
// Naming is hard. In the context of this API "sizes" are
// the t shirt sizes (small, medium, etc) whereas "capacities"
// are the values k8s uses like Gi, M, m, etc. This distinction is
// important because most of what we're doing here is
// converting sizes to capacities, so I enforce the distinction
// strictly. The method names are long, but more importantly, accurate
import (
conf "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/sizing/sizingconfig"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// Public methods
// Get default resource requirement requests and limits
func GetDefaultResourceRequirements() core.ResourceRequirements {
return GetResourceRequirementsForSize(GetDefaultSizeCPURAM())
}
// Gets the default size for CPU and RAM
func GetDefaultSizeCPURAM() string {
return conf.DefaultSizeCPURAM
}
// Gets the default vol size
func GetDefaultSizeVol() string {
return conf.DefaultSizeVol
}
// Get the default volume capacity
func GetDefaultVolCapacity() string {
return conf.VolSizeToCapacity[GetDefaultSizeVol()]
}
// Get resource requirements - request and limits - for a given size
func GetResourceRequirementsForSize(tShirtSize string) core.ResourceRequirements {
requestSize := useDefaultIfEmptySize(tShirtSize, GetDefaultSizeCPURAM())
limitSize := conf.LimitSizeToRequestSize[requestSize]
return core.ResourceRequirements{
Limits: core.ResourceList{
"memory": resource.MustParse(conf.RAMSizeToCapacity[limitSize]),
"cpu": resource.MustParse(conf.CPUSizeToCapacity[limitSize]),
},
Requests: core.ResourceList{
"memory": resource.MustParse(conf.RAMSizeToCapacity[requestSize]),
"cpu": resource.MustParse(conf.CPUSizeToCapacity[requestSize]),
},
}
}
// For a givin vol size get the capacity. Providing "" gets the default.
func GetVolCapacityForSize(size string) string {
requestSize := useDefaultIfEmptySize(size, GetDefaultSizeVol())
return conf.VolSizeToCapacity[requestSize]
}
// Accepts 2 sizes. Returns true if first size is larger than second
func IsSizeLarger(capacityA string, capacityB string) bool {
return conf.SizeIndex[capacityA] > conf.SizeIndex[capacityB]
}
// Private methods
// Often we have to sanitize a size such that "" == whatever the default is
func useDefaultIfEmptySize(size string, def string) string {
if size == "" {
return def
}
return size
}