-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.go
217 lines (198 loc) · 7.06 KB
/
utils.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
/*
* Tencent is pleased to support the open source community by making TKEStack available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package util
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
"tkestack.io/galaxy/pkg/api/galaxy/constant"
)
// resolveDeploymentName return deployment name if pod is created by deployment
func resolveDeploymentName(pod *corev1.Pod) string {
if len(pod.OwnerReferences) == 1 && pod.OwnerReferences[0].Kind == "ReplicaSet" {
// assume pod belong to deployment for convenient
// can't parse deployment from pod name directly if deployment name is nearly 63 bytes
// e.g. if deployment name is dp1234567890dp1234567890dp1234567890dp1234567890dp1234567890dp1 (63 bytes)
// ReplicaSet name is dp1234567890dp1234567890dp1234567890dp1234567890dp1234567890dp1-69fd8dbc5c (74 bytes)
// pod name is generated like dp1234567890dp1234567890dp1234567890dp1234567890dp1234567848p74
ownerName := pod.OwnerReferences[0].Name
lastIndex := strings.LastIndex(ownerName, "-")
if lastIndex == -1 {
// parent app is replicatsets with a name that doesn't include '-' instead of deployment
return ownerName
}
// TODO for replicasets pods, this should return full ownerName. When we change this, be sure to fix release fip API too
return ownerName[:lastIndex]
}
return ""
}
type KeyObj struct {
// stores the key format in IPAM
// for deployment dp_namespace_deploymentName_podName,
// for pool pool__poolName_dp_namespace_deploymentName_podName, for statefulset
// sts_namespace_statefulsetName_podName
// If deployment name is 63 bytes, e.g. dp1234567890dp1234567890dp1234567890dp1234567890dp1234567890dp1
// deployment pod name will be 63 bytes with modified suffix, e.g.
// dp1234567890dp1234567890dp1234567890dp1234567890dp1234567848p74
// So we can't get deployment name from pod name and have to store deployment name with pod name
KeyInDB string
AppName string
AppTypePrefix string
PodName string
Namespace string
// the annotation value if pod has pool annotation
PoolName string
}
func NewKeyObj(appTypePrefix string, namespace, appName, podName, poolName string) *KeyObj {
k := &KeyObj{AppTypePrefix: appTypePrefix, AppName: appName, PodName: podName, Namespace: namespace,
PoolName: poolName}
k.genKey()
return k
}
func (k *KeyObj) Deployment() bool {
return k.AppTypePrefix == DeploymentPrefixKey
}
func (k *KeyObj) StatefulSet() bool {
return k.AppTypePrefix == StatefulsetPrefixKey
}
func (k *KeyObj) genKey() {
var prefix string
if k.PoolName != "" {
prefix = fmt.Sprintf("%s%s_", poolPrefix, k.PoolName)
if k.AppName == "" {
k.KeyInDB = prefix
return
}
}
if k.PoolName == "" && k.AppName == "" && k.Namespace == "" {
k.KeyInDB = ""
return
}
k.KeyInDB = fmt.Sprintf("%s%s%s_%s_%s", prefix, k.AppTypePrefix, k.Namespace, k.AppName, k.PodName)
}
// PoolPrefix returns the common key prefix in IPAM, for deployment dp_namespace_deploymentName_
// for pool pool__poolName_, for statefulset sts_namespace_statefulsetName_
// For now, if it is a statefulset pod, PoolPrefix is useless since we reserve ip by full pod name
// PoolPrefix is used by pool and deployment only.
func (k *KeyObj) PoolPrefix() string {
if k.PoolName != "" {
return fmt.Sprintf("%s%s_", poolPrefix, k.PoolName)
}
return fmt.Sprintf("%s%s_%s_", k.AppTypePrefix, k.Namespace, k.AppName)
}
func (k *KeyObj) PoolAppPrefix() string {
if k.PoolName != "" {
return fmt.Sprintf("%s%s_%s%s_%s_", poolPrefix, k.PoolName, k.AppTypePrefix, k.Namespace, k.AppName)
}
return k.PoolPrefix()
}
const (
// ip pool may be shared with other namespaces, so leave namespace empty
poolPrefix = "pool__"
DeploymentPrefixKey = "dp_"
StatefulsetPrefixKey = "sts_"
NoRefAppName = "NULL"
NoRefAppTypePrefix = "NULL_"
)
func FormatKey(pod *corev1.Pod) (*KeyObj, error) {
pool := constant.GetPool(pod.Annotations)
keyObj := &KeyObj{
PoolName: pool,
PodName: pod.Name,
Namespace: pod.Namespace}
if len(pod.OwnerReferences) == 0 {
keyObj.AppName = NoRefAppName
keyObj.AppTypePrefix = NoRefAppTypePrefix
} else {
if pod.OwnerReferences[0].Kind == "StatefulSet" {
keyObj.AppName = pod.OwnerReferences[0].Name
keyObj.AppTypePrefix = StatefulsetPrefixKey
} else if pod.OwnerReferences[0].Kind != "ReplicaSet" {
// some app type galaxy can't recognize
keyObj.AppName = pod.OwnerReferences[0].Name
keyObj.AppTypePrefix = GetAppTypePrefix(pod.OwnerReferences[0].Kind)
} else {
deploymentName := resolveDeploymentName(pod)
if deploymentName == "" {
return keyObj, fmt.Errorf("unsupported app type")
}
keyObj.AppName = deploymentName
// treat rs like deployment, share the same appPrefixKey
keyObj.AppTypePrefix = DeploymentPrefixKey
}
}
keyObj.genKey()
return keyObj, nil
}
func ParseKey(key string) *KeyObj {
//tapp_ns1_demo_demo-1
keyObj := &KeyObj{KeyInDB: key}
removedPoolKey := key
if strings.HasPrefix(key, poolPrefix) {
// pool__poolName_deployment_namespace_deploymentName_podName
// poolName and deployment_namespace_deploymentName_podName
parts := strings.SplitN(key[len(poolPrefix):], "_", 2)
if len(parts) != 2 {
return keyObj
}
keyObj.PoolName = parts[0]
removedPoolKey = parts[1]
}
keyObj.AppTypePrefix, keyObj.AppName, keyObj.PodName, keyObj.Namespace = resolvePodKey(removedPoolKey)
return keyObj
}
// resolvePodKey returns appname, podName, namespace
// "sts_kube-system_fip-bj_fip-bj-111": {"sts_", "fip-bj", "fip-bj-111", "kube-system"}
func resolvePodKey(key string) (string, string, string, string) {
// _ is not a valid char in appname
parts := strings.Split(key, "_")
if len(parts) == 4 {
return parts[0] + "_", parts[2], parts[3], parts[1]
}
return "", "", "", ""
}
func Join(name, namespace string) string {
return fmt.Sprintf("%s_%s", namespace, name)
}
func PodName(pod *corev1.Pod) string {
return fmt.Sprintf("%s_%s", pod.Namespace, pod.Name)
}
// GetAppTypePrefix formats an appTypePrefix for the given resource kind
func GetAppTypePrefix(kind string) string {
lower := strings.ToLower(kind)
if lower == "statefulset" || lower == "statefulsets" {
return StatefulsetPrefixKey
} else if lower == "replicaset" || lower == "deployment" {
return DeploymentPrefixKey
}
return lower + "_"
}
// GetAppType formats appTypePrefix to appType
func GetAppType(appTypePrefix string) string {
switch appTypePrefix {
case DeploymentPrefixKey:
return "deployment"
case StatefulsetPrefixKey:
return "statefulset"
default:
if len(appTypePrefix) > 0 {
return appTypePrefix[:len(appTypePrefix)-1]
} else {
return ""
}
}
}