-
Notifications
You must be signed in to change notification settings - Fork 11
/
helper.go
125 lines (104 loc) · 2.88 KB
/
helper.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
package kubernetes
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ghodss/yaml"
"github.com/loft-sh/log"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
limitsPrefix = "limits."
requestsPrefix = "requests."
)
func parseResources(resourceString string, log log.Logger) corev1.ResourceRequirements {
resourcesSplitted := strings.Split(resourceString, ",")
requests := corev1.ResourceList{}
limits := corev1.ResourceList{}
for _, resourceName := range resourcesSplitted {
resourceName = strings.TrimSpace(resourceName)
// requests
if strings.HasPrefix(resourceName, requestsPrefix) {
strippedResource := strings.TrimPrefix(resourceName, requestsPrefix)
name, quantity, err := parseResource(strippedResource)
if err != nil {
log.Error(err.Error())
continue
}
requests[corev1.ResourceName(name)] = quantity
}
// limits
if strings.HasPrefix(resourceName, limitsPrefix) {
strippedResource := strings.TrimPrefix(resourceName, limitsPrefix)
name, quantity, err := parseResource(strippedResource)
if err != nil {
log.Error(err.Error())
continue
}
limits[corev1.ResourceName(name)] = quantity
}
}
return corev1.ResourceRequirements{
Limits: limits,
Requests: requests,
}
}
func getPodTemplate(manifest string) (*corev1.Pod, error) {
_, err := os.Stat(manifest)
if err != nil {
if os.IsNotExist(err) {
// try to parse inline template
pod := &corev1.Pod{}
err = yaml.Unmarshal([]byte(manifest), pod)
if err != nil {
return nil, errors.Wrap(err, "unmarshal pod template")
}
return pod, nil
}
return nil, err
}
p, err := filepath.Abs(manifest)
if err != nil {
return nil, err
}
body, err := os.ReadFile(p)
if err != nil {
return nil, err
}
pod := &corev1.Pod{}
err = yaml.Unmarshal(body, pod)
if err != nil {
return nil, errors.Wrap(err, "unmarshal pod template")
}
return pod, nil
}
func parseLabels(str string) (map[string]string, error) {
if str == "" {
return nil, nil
}
labels := strings.Split(str, ",")
retMap := map[string]string{}
for _, label := range labels {
label = strings.TrimSpace(label)
splitted := strings.SplitN(label, "=", 2)
if len(splitted) != 2 {
return nil, fmt.Errorf("invalid label '%s', expected format label=value", label)
}
retMap[splitted[0]] = splitted[1]
}
return retMap, nil
}
func parseResource(resourceName string) (string, resource.Quantity, error) {
splittedResource := strings.SplitN(resourceName, "=", 2)
if len(splittedResource) != 2 {
return "", resource.Quantity{}, fmt.Errorf("error parsing resource %s: expected form resource=quantity", resourceName)
}
quantity, err := resource.ParseQuantity(splittedResource[1])
if err != nil {
return "", resource.Quantity{}, fmt.Errorf("error parsing resource %s: %w", resourceName, err)
}
return splittedResource[0], quantity, nil
}