mv annotation ref-count from configmap to pods

This commit is contained in:
wencaiwulue
2021-09-05 09:53:31 +08:00
parent abf4c3ef55
commit cf5ccdb8cd
2 changed files with 26 additions and 28 deletions

View File

@@ -26,12 +26,12 @@ func AddCleanUpResourceHandler(client *kubernetes.Clientset, namespace string, s
go func() {
<-stopChan
log.Info("prepare to exit, cleaning up")
cleanUpTrafficManagerIfRefCountIsZero(client, namespace)
for _, ipNet := range ip {
if err := ReleaseIpToDHCP(client, namespace, ipNet); err != nil {
log.Errorf("failed to release ip to dhcp, err: %v", err)
}
}
cleanUpTrafficManagerIfRefCountIsZero(client, namespace)
wg := sync.WaitGroup{}
for _, service := range strings.Split(services, ",") {
if len(service) > 0 {
@@ -62,32 +62,29 @@ func deletePod(client *kubernetes.Clientset, namespace, podName string) {
// vendor/k8s.io/kubectl/pkg/polymorphichelpers/rollback.go:99
func updateRefCount(client *kubernetes.Clientset, namespace, name string, increment int) {
err := retry.OnError(
retry.DefaultRetry,
func(err error) bool { return err != nil },
func() error {
configMap, err := client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, v1.GetOptions{})
if err != nil {
log.Errorf("update ref-count failed, increment: %d, error: %v", increment, err)
return err
}
curCount := 0
if ref := configMap.GetAnnotations()["ref-count"]; len(ref) > 0 {
curCount, err = strconv.Atoi(ref)
}
patch, _ := json.Marshal([]interface{}{
map[string]interface{}{
"op": "replace",
"path": "/metadata/annotations/ref-count",
"value": strconv.Itoa(curCount + increment),
},
})
_, err = client.CoreV1().ConfigMaps(namespace).
Patch(context.TODO(), util.TrafficManager, types.JSONPatchType, patch, v1.PatchOptions{})
if err := retry.OnError(retry.DefaultRetry, func(err error) bool {
return err != nil
}, func() error {
pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), name, v1.GetOptions{})
if err != nil {
log.Errorf("update ref-count failed, increment: %d, error: %v", increment, err)
return err
},
)
if err != nil {
}
curCount := 0
if ref := pod.GetAnnotations()["ref-count"]; len(ref) > 0 {
curCount, err = strconv.Atoi(ref)
}
patch, _ := json.Marshal([]interface{}{
map[string]interface{}{
"op": "replace",
"path": "/metadata/annotations/ref-count",
"value": strconv.Itoa(curCount + increment),
},
})
_, err = client.CoreV1().Pods(namespace).
Patch(context.TODO(), util.TrafficManager, types.JSONPatchType, patch, v1.PatchOptions{})
return err
}); err != nil {
log.Errorf("update ref count error, error: %v", err)
} else {
log.Info("update ref count successfully")
@@ -96,12 +93,12 @@ func updateRefCount(client *kubernetes.Clientset, namespace, name string, increm
func cleanUpTrafficManagerIfRefCountIsZero(client *kubernetes.Clientset, namespace string) {
updateRefCount(client, namespace, util.TrafficManager, -1)
configMap, err := client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), util.TrafficManager, v1.GetOptions{})
pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), util.TrafficManager, v1.GetOptions{})
if err != nil {
log.Error(err)
return
}
refCount, err := strconv.Atoi(configMap.GetAnnotations()["ref-count"])
refCount, err := strconv.Atoi(pod.GetAnnotations()["ref-count"])
if err != nil {
log.Error(err)
return

View File

@@ -29,6 +29,7 @@ func CreateServerOutbound(clientset *kubernetes.Clientset, namespace string, ser
)
if err3 == nil && i != 0 && firstPod != nil {
updateRefCount(clientset, namespace, firstPod.Name, 1)
return firstPod, nil
}
args := []string{