/
common_types.go
48 lines (38 loc) · 1011 Bytes
/
common_types.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
package v1alpha1
import "gopkg.in/inf.v0"
// NamespacedName contains the name of a object and its namespace
type NamespacedName struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
// SloSpec defines what is the percentage
type SloSpec struct {
// TargetAvailabilityPercent defines the percent number to be used
TargetAvailabilityPercent string `json:"targetAvailabilityPercent"`
}
func (s SloSpec) IsValid() (bool, string) {
if s.TargetAvailabilityPercent == "" {
return false, ""
}
d, sucess := new(inf.Dec).SetString(s.TargetAvailabilityPercent)
// value is not parsable
if !sucess {
return false, ""
}
// will be 90
ninty := inf.NewDec(9, -1)
// is lower than lower bound
if d.Cmp(ninty) <= 0 {
return false, ""
}
// will be 100
hundred := inf.NewDec(1, -2)
// is higher than upper bound
if d.Cmp(hundred) >= 0 {
return false, ""
}
// will be 1/100
oneHundredth := inf.NewDec(1, 2)
res := d.Mul(d, oneHundredth).String()
return true, res
}