/
type_distributions.go
197 lines (149 loc) · 5.5 KB
/
type_distributions.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2022-2023 ICS-FORTH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type DistributionName string
const (
// DistributionConstant is a fixed distributed with all elements having a probability of 1.
DistributionConstant DistributionName = "constant"
// DistributionUniform draws samples from a continuous uniform distribution
DistributionUniform DistributionName = "uniform"
// DistributionNormal draws samples from a normal (Gaussian) distribution
DistributionNormal DistributionName = "normal"
// DistributionPareto draws samples from a Pareto distribution
DistributionPareto DistributionName = "pareto"
// DistributionDefault instructs the controller to use an already evaluated distribution.
DistributionDefault DistributionName = "default"
)
type DistributionSpec struct {
// +kubebuilder:validation:Enum=constant;uniform;normal;pareto;default
Name DistributionName `json:"name"`
// +optional
*DistParamsPareto `json:"histogram,omitempty"`
}
// DistParamsPareto are parameters for the Pareto distribution.
type DistParamsPareto struct {
Scale float64 `json:"scale"`
Shape float64 `json:"shape"`
}
/*
Timeline Distribution
*/
type TimelineDistributionSpec struct {
// TotalDuration defines the total duration within which events will happen.
TotalDuration *metav1.Duration `json:"total"`
// DistributionSpec defines how the TotalDuration will be divided into time-based events.
DistributionSpec *DistributionSpec `json:"distribution"`
}
func (in TimelineDistributionSpec) Validate() error {
// Validate the duration.
if in.TotalDuration != nil {
return errors.New("empty duration field")
}
// Validate the distribution method.
if err := ValidateDistribution(in.DistributionSpec); err != nil {
return errors.Wrapf(err, "distribution error")
}
return nil
}
type Timeline []metav1.Time
// Next returns the next activation time, later than the given time.
func (in Timeline) Next(ref time.Time) time.Time {
for _, t := range in {
if t.After(ref) {
return t.Time
}
}
// bad hack. If there is no actual schedule, return something far in the future
// for the controller to keep running, but also to raise trigger to the test.
return time.Now().Add(12 * time.Hour)
}
func (in Timeline) String() string {
var out strings.Builder
out.WriteString("\n=== Timeline ===\n")
for _, node := range in {
out.WriteString(fmt.Sprintf("\n * %s", node.Time.Format(time.StampMilli)))
}
return out.String()
}
/*
Resource Distribution
*/
type ResourceDistributionSpec struct {
// TotalResources defines the total resources that will be distributed among the cluster's services.
TotalResources corev1.ResourceList `json:"total"`
// DistributionSpec defines how the TotalResources will be assigned to resources.
DistributionSpec *DistributionSpec `json:"distribution,omitempty"`
}
func (in ResourceDistributionSpec) Validate() error {
// Valida the type of resources.
for resourceName := range in.TotalResources {
switch resourceName {
case corev1.ResourceCPU, corev1.ResourceMemory:
continue
default:
return errors.Errorf("invalid resource '%s'", resourceName)
}
}
// Validate the distribution method.
if err := ValidateDistribution(in.DistributionSpec); err != nil {
return errors.Wrapf(err, "distribution error")
}
return nil
}
type ResourceDistribution []corev1.ResourceList
func (in ResourceDistribution) Table() (header []string, data [][]string) {
header = []string{
"CPU",
"Memory",
"Pods",
"Storage",
"Ephemeral",
}
for _, node := range in {
data = append(data, []string{
fmt.Sprintf("%.2f", node.Cpu().AsApproximateFloat64()),
fmt.Sprintf("%.2f", node.Memory().AsApproximateFloat64()),
fmt.Sprintf("%.2f", node.Pods().AsApproximateFloat64()),
fmt.Sprintf("%.2f", node.Storage().AsApproximateFloat64()),
fmt.Sprintf("%.2f", node.StorageEphemeral().AsApproximateFloat64()),
})
}
return header, data
}
func (in ResourceDistribution) String() string {
var out strings.Builder
out.WriteString("\n=== Resource Distribution ===\n")
for i, node := range in {
out.WriteString(fmt.Sprintf("\n=== node_%d ===", i))
out.WriteString(fmt.Sprintf("\n* CPU: %.2f", node.Cpu().AsApproximateFloat64()))
out.WriteString(fmt.Sprintf("\n* Memory: %.2f", node.Memory().AsApproximateFloat64()))
out.WriteString(fmt.Sprintf("\n* Pods: %.2f", node.Pods().AsApproximateFloat64()))
out.WriteString(fmt.Sprintf("\n* Storage: %.2f", node.Storage().AsApproximateFloat64()))
out.WriteString(fmt.Sprintf("\n* StorageEphemeral: %.2f", node.StorageEphemeral().AsApproximateFloat64()))
}
/*
sum := dist.Sum()
out.WriteString(fmt.Sprintf("\n -- TotalResources -- \nCPU:%.2f Memory:%.2f Pods:%.2f Storage:%.2f Ephemeral:%.2f",
sum.Cpu().AsApproximateFloat64(), sum.Memory().AsApproximateFloat64(), sum.Pods().AsApproximateFloat64(),
sum.Storage().AsApproximateFloat64(), sum.StorageEphemeral().AsApproximateFloat64()))
*/
return out.String()
}