-
Notifications
You must be signed in to change notification settings - Fork 90
/
limitrange.go
96 lines (79 loc) · 2.25 KB
/
limitrange.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
package kotsadm
import (
"context"
"fmt"
"os"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsadm/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func maybeGetNamespaceLimitRanges(clientset *kubernetes.Clientset, namespace string) (*corev1.LimitRange, error) {
limitRanges, err := clientset.CoreV1().LimitRanges(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to list limit ranges")
}
if len(limitRanges.Items) == 0 {
return nil, nil
}
return &limitRanges.Items[0], nil
}
func getSize(deployOptions types.DeployOptions, label string, size resource.Quantity) (resource.Quantity, error) {
if deployOptions.LimitRange != nil {
var allowedMax *resource.Quantity
var allowedMin *resource.Quantity
for _, limit := range deployOptions.LimitRange.Spec.Limits {
if limit.Type == corev1.LimitTypePersistentVolumeClaim {
max, ok := limit.Max["storage"]
if ok {
allowedMax = &max
}
min, ok := limit.Min["storage"]
if ok {
allowedMin = &min
}
}
}
newSize := promptForSizeIfNotBetween(label, &size, allowedMin, allowedMax)
if newSize == nil {
os.Exit(-1)
}
size = *newSize
}
return size, nil
}
func promptForSizeIfNotBetween(label string, desired *resource.Quantity, min *resource.Quantity, max *resource.Quantity) *resource.Quantity {
actualSize := desired
if max != nil {
if max.Cmp(*desired) == -1 {
/// desired is too big
actualSize = max
}
}
if min != nil {
if min.Cmp(*desired) == 1 {
/// desired is too small, yeap, you read that right
actualSize = min
}
}
if actualSize.Cmp(*desired) == 0 {
return desired
}
prompt := promptui.Prompt{
Label: fmt.Sprintf("The storage request for %s is not acceptable for the current namespace. KOTS recommends a size of %s, but will attempt to proceed with %s to meet the namespace limits. Do you want to continue", label, desired.String(), actualSize.String()),
IsConfirm: true,
}
for {
_, err := prompt.Run()
if err != nil {
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
continue
}
return actualSize
}
}