-
Notifications
You must be signed in to change notification settings - Fork 11
/
utility.go
272 lines (243 loc) · 6.49 KB
/
utility.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package util
import (
"bytes"
"encoding/json"
"fmt"
"math"
"net/url"
"reflect"
"sort"
"strconv"
"strings"
"github.com/cloud-barista/cb-dragonfly/pkg/types"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func StructToMap(i interface{}) (values url.Values) {
values = url.Values{}
iVal := reflect.ValueOf(i).Elem()
typ := iVal.Type()
for i := 0; i < iVal.NumField(); i++ {
f := iVal.Field(i)
// You ca use tags here...
// tag := typ.Field(i).Tag.Get("tagname")
// Convert each type into a string for the url.Values string map
var v string
switch f.Interface().(type) {
case int, int8, int16, int32, int64:
v = strconv.FormatInt(f.Int(), 10)
case uint, uint8, uint16, uint32, uint64:
v = strconv.FormatUint(f.Uint(), 10)
case float32:
v = strconv.FormatFloat(f.Float(), 'f', 4, 32)
case float64:
v = strconv.FormatFloat(f.Float(), 'f', 4, 64)
case []byte:
v = string(f.Bytes())
case string:
v = f.String()
}
values.Set(typ.Field(i).Name, v)
}
return
}
func ToMap(val interface{}) (map[string]interface{}, error) {
// Convert struct to bytes
byteBuffer := new(bytes.Buffer)
if err := json.NewEncoder(byteBuffer).Encode(val); err != nil {
return nil, err
}
// Convert bytes to map
byteData := byteBuffer.Bytes()
resultMap := map[string]interface{}{}
if err := json.Unmarshal(byteData, &resultMap); err != nil {
return nil, err
}
return resultMap, nil
}
func GetFields(val reflect.Value) []string {
var fieldArr []string
t := val.Type()
for i := 0; i < t.NumField(); i++ {
fieldArr = append(fieldArr, t.Field(i).Tag.Get("json"))
}
return fieldArr
}
func SplitOneStringToTopicsSlice(topicsStrings string) []string {
return strings.Split(topicsStrings, "^")[1:]
}
func MergeTopicsToOneString(topicsSlice []string) string {
var combinedTopicString string
for _, topic := range topicsSlice {
combinedTopicString = fmt.Sprintf("%s^%s", combinedTopicString, topic)
}
return combinedTopicString
}
func CalculateNumberOfCollector(topicCount int, maxHostCount int) int {
collectorCount := topicCount / maxHostCount
if topicCount%maxHostCount != 0 {
collectorCount += 1
}
return collectorCount
}
func GetAllTopicBySort(topicsSlice []string) []string {
if len(topicsSlice) == 0 {
return []string{}
}
sort.Slice(topicsSlice, func(i, j int) bool {
return topicsSlice[i] < topicsSlice[j]
})
return topicsSlice
}
func MakeCollectorTopicMap(allTopics []string, maxHostCount int) (map[int][]string, []int) {
if len(allTopics) == 0 {
return map[int][]string{}, []int{}
}
collectorTopicMap := map[int][]string{}
collectorTopicCnt := []int{}
allTopicsLen := len(allTopics)
startIdx := 0
endIdx := 0
collectorCount := CalculateNumberOfCollector(allTopicsLen, maxHostCount)
for collectorCountIdx := 0; collectorCountIdx < collectorCount; collectorCountIdx++ {
if allTopicsLen < maxHostCount {
endIdx = len(allTopics)
} else {
endIdx = (collectorCountIdx + 1) * maxHostCount
}
aTopics := allTopics[startIdx:endIdx]
collectorTopicMap[collectorCountIdx] = aTopics
collectorTopicCnt = append(collectorTopicCnt, len(aTopics))
startIdx = endIdx
allTopicsLen -= maxHostCount
}
return collectorTopicMap, collectorTopicCnt
}
func GetCspCollectorIdx(topic string) (collectorIdx int) {
topicSplit := strings.Split(topic, "_")
cspType := strings.ToUpper(topicSplit[len(topicSplit)-1])
switch cspType {
case types.Alibaba:
collectorIdx = 0
break
case types.Aws:
collectorIdx = 1
break
case types.Azure:
collectorIdx = 2
break
case types.Cloudit:
collectorIdx = 3
break
case types.Cloudtwin:
collectorIdx = 4
break
case types.Docker:
collectorIdx = 5
break
case types.Gcp:
collectorIdx = 6
break
case types.Openstack:
collectorIdx = 7
break
}
return
}
func Unique(slice []string, sortOption bool) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
if sortOption {
sort.Strings(list)
}
return list
}
func ReturnDiffTopicList(a, b []string) (diff []string) {
m := make(map[string]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
return
}
func Int32Ptr(i int32) *int32 { return &i }
func Int64Ptr(i int64) *int64 { return &i }
func DeploymentTemplate(collectorCreateOrder int, collectorUUID string, env []apiv1.EnvVar) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s%d-%s", types.DeploymentName, collectorCreateOrder, collectorUUID),
Labels: map[string]string{types.LabelKey: collectorUUID},
},
Spec: appsv1.DeploymentSpec{
Replicas: Int32Ptr(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
types.LabelKey: collectorUUID,
},
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
types.LabelKey: collectorUUID,
},
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: fmt.Sprintf("%s%d-%s", types.DeploymentName, collectorCreateOrder, collectorUUID),
Image: types.CollectorImage,
Ports: []apiv1.ContainerPort{},
Env: env,
VolumeMounts: []apiv1.VolumeMount{
{
Name: "config-volume",
MountPath: "/go/src/github.com/cloud-barista/cb-dragonfly/conf",
},
},
SecurityContext: &apiv1.SecurityContext{
RunAsUser: Int64Ptr(0),
},
},
},
Volumes: []apiv1.Volume{
{
Name: "config-volume",
VolumeSource: apiv1.VolumeSource{
ConfigMap: &apiv1.ConfigMapVolumeSource{
LocalObjectReference: apiv1.LocalObjectReference{
Name: "cb-dragonfly-config",
},
},
},
},
},
ServiceAccountName: "cb-dragonfly",
},
},
},
}
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func ToFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}
func CheckMCK8SType(serviceType string) bool {
return strings.EqualFold(serviceType, types.MCK8S) || strings.EqualFold(serviceType, types.KUBERNETES) || strings.EqualFold(serviceType, types.K8S)
}
func CheckMCISType(serviceType string) bool {
return strings.EqualFold(serviceType, types.MCIS) || strings.EqualFold(serviceType, types.VM)
}