Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from hello2mao/safe-delete-eip
Browse files Browse the repository at this point in the history
Safe delete eip
  • Loading branch information
hello2mao authored Feb 27, 2019
2 parents 1a788e8 + cf6b4cf commit 9d84f52
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 11 deletions.
37 changes: 26 additions & 11 deletions pkg/cloud-provider/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/kubernetes/pkg/cloudprovider"

"k8s.io/cloud-provider-baiducloud/pkg/cloud-sdk/blb"
"fmt"
)

// LoadBalancer returns a balancer interface. Also returns true if the interface is supported, false otherwise.
Expand Down Expand Up @@ -157,18 +158,32 @@ func (bc *Baiducloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName
}

// delete EIP
if lb.PublicIp != "" {
if len(service.Spec.LoadBalancerIP) != 0 {
glog.V(3).Infof("[%v %v] EnsureLoadBalancerDeleted: LoadBalancerIP is set, not delete EIP.", serviceName, clusterName)
glog.V(2).Infof("[%v %v] EnsureLoadBalancerDeleted: delete %v FINISH", serviceName, clusterName, serviceName)
return nil
}
glog.V(3).Infof("[%v %v] EnsureLoadBalancerDeleted: Start delete EIP: %s", serviceName, clusterName, lb.PublicIp)
err = bc.deleteEIP(lb.PublicIp)
if err != nil {
return err
}
if result.LoadBalancerInternalVpc == "true" {
glog.V(3).Infof("[%v %v] EnsureLoadBalancerDeleted: use LoadBalancerInternalVpc, no EIP to delete", service.Namespace, service.Name, lb.Address)
glog.V(2).Infof("[%v %v] EnsureLoadBalancerDeleted: delete %v FINISH", serviceName, clusterName, serviceName)
return nil
}
if len(service.Spec.LoadBalancerIP) != 0 {
glog.V(3).Infof("[%v %v] EnsureLoadBalancerDeleted: LoadBalancerIP is set, not delete EIP.", serviceName, clusterName)
glog.V(2).Infof("[%v %v] EnsureLoadBalancerDeleted: delete %v FINISH", serviceName, clusterName, serviceName)
return nil
}
glog.V(3).Infof("[%v %v] EnsureLoadBalancerDeleted: Start delete EIP: %s", serviceName, clusterName, lb.PublicIp)
var targetEip string
if len(service.Status.LoadBalancer.Ingress) != 0 { // P0: use service EXTERNAL_IP
targetEip = service.Status.LoadBalancer.Ingress[0].IP
}
if len(targetEip) == 0 { // P1: use BLB public ip
targetEip = lb.PublicIp
}
if len(targetEip) == 0 { // get none EIP
return fmt.Errorf("EnsureLoadBalancerDeleted failed: can not get a EIP to delete")
}
err = bc.deleteEIP(targetEip)
if err != nil {
return err
}

glog.V(2).Infof("[%v %v] EnsureLoadBalancerDeleted: delete %v FINISH", serviceName, clusterName, serviceName)
return nil
}
25 changes: 25 additions & 0 deletions test/create-and-delete/create-and-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

startTime=`date +%Y%m%d-%H:%M`
startTime_s=`date +%s`

for i in `seq 1 2`;
do
kubectl create -f ./test/create-and-delete/nginx.yaml
status="Check: "
while true
do
if [ `kubectl get service |grep "nginx-service"|grep -v "pending"|wc -l` -eq 5 ]; then
break
fi
sleep 10
status=$status"."
echo $status
done
kubectl delete -f ./test/create-and-delete/nginx.yaml
done

endTime=`date +%Y%m%d-%H:%M`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "Test Finish: create-and-delete" "$startTime ---> $endTime" "Total: $sumTime s"
87 changes: 87 additions & 0 deletions test/create-and-delete/nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service-1
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- name: nginx-port
port: 80
targetPort: 80
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service-2
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- name: nginx-port
port: 80
targetPort: 80
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service-3
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- name: nginx-port
port: 80
targetPort: 80
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service-4
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- name: nginx-port
port: 80
targetPort: 80
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service-5
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- name: nginx-port
port: 80
targetPort: 80
protocol: TCP
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
57 changes: 57 additions & 0 deletions test/feature-test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

startTime=`date +%Y%m%d-%H:%M`
startTime_s=`date +%s`

echo "Test Nginx"
kubectl create -f ./docs/controllers/service/examples/nginx.yaml
status="Check: "
while true
do
if [ `kubectl get service |grep "nginx-service"|grep -v "pending"|wc -l` -eq 1 ]; then
break
fi
sleep 10
status=$status"."
echo $status
done
kubectl delete -f ./docs/controllers/service/examples/nginx.yaml
echo "Test Nginx success"
echo ""

echo "Test BLB allocate vip"
kubectl create -f ./docs/controllers/service/examples/nginx-BLB-allocate-vip.yaml
status="Check: "
while true
do
if [ `kubectl get service |grep "nginx-service-blb-allocate-vip"|grep -v "pending"|wc -l` -eq 1 ]; then
break
fi
sleep 10
status=$status"."
echo $status
done
kubectl delete -f ./docs/controllers/service/examples/nginx-BLB-allocate-vip.yaml
echo "Test BLB allocate vip success"
echo ""

echo "Test BLB support internal vpc"
kubectl create -f ./docs/controllers/service/examples/nginx-BLB-support-internal-vpc.yaml
status="Check: "
while true
do
if [ `kubectl get service |grep "nginx-service-blb-internal-vpc"|grep -v "pending"|wc -l` -eq 1 ]; then
break
fi
sleep 10
status=$status"."
echo $status
done
kubectl delete -f ./docs/controllers/service/examples/nginx-BLB-support-internal-vpc.yaml
echo "Test BLB support internal vpc success"
echo ""

endTime=`date +%Y%m%d-%H:%M`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "Test Finish: feature-test" "$startTime ---> $endTime" "Total: $sumTime s"
4 changes: 4 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

./test/feature-test/run.sh
./test/create-and-delete/create-and-delete.sh

0 comments on commit 9d84f52

Please sign in to comment.