/
loadbalancer.go
120 lines (109 loc) · 3.59 KB
/
loadbalancer.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
package pkg
import (
"context"
"fmt"
"gitserver/kubernetes/inspur-cloud-controller-manager/cloud-controller-manager/pkg/common"
"k8s.io/api/core/v1"
corev1lister "k8s.io/client-go/listers/core/v1"
)
var (
ErrorNotFoundInCloud = fmt.Errorf("Cannot find lb in incloud")
ErrorSlbIdNotDefined = fmt.Errorf("Could not find Service SLB Id ")
)
type LoadBalancer struct {
//service *corev1.Service
//Type int
//TCPPorts []int
//NodePorts []int
//Nodes []*corev1.Node
//Name string
//clusterName string
RegionId string `json:"regionId"`
CreatedTime string `json:"createdTime"`
ExpiredTime string `json:"expiredTime"`
SpecificationId string `json:"specificationId"`
SpecificationName string `json:"specificationName"`
SlbId string `json:"slbId"`
SlbName string `json:"slbName"`
Scheme string `json:"scheme"`
BusinessIp string `json:"businessIp"`
VpcId string `json:"vpcId"`
VpcName string `json:"vpcName"`
SubnetId string `json:"subnetId"`
EipId string `json:"eipId"`
EipAddress string `json:"eipAddress"`
ListenerCount int `json:"listenerCount"`
SlbType string `json:"slbType"`
State string `json:"state"`
UserId string `json:"userId"`
}
type LoadBalancerStatus struct {
K8sLoadBalancerStatus *v1.LoadBalancerStatus
}
type NewLoadBalancerOption struct {
NodeLister corev1lister.NodeLister
K8sNodes []*v1.Node
K8sService *v1.Service
Context context.Context
ClusterName string
}
//GetLoadBalancer by slbid,use incloud api to get lb in cloud, return err if not found
func GetLoadBalancer(config *InCloud, service *v1.Service) (*LoadBalancer, error) {
slbid := getServiceAnnotation(service, common.ServiceAnnotationInternalSlbId, "")
if slbid == "" {
return nil, ErrorSlbIdNotDefined
}
token, error := getKeyCloakToken(config.RequestedSubject, config.TokenClientID, config.ClientSecret, config.KeycloakUrl, config)
if error != nil {
return nil, error
}
lb, err := describeLoadBalancer(config.LbUrlPre, token, slbid)
if err != nil {
return nil, err
}
if lb == nil {
return nil, ErrorNotFoundInCloud
}
return lb, nil
}
func ModifyLoadBalancer(config *InCloud, service *v1.Service, slbName string) (*SlbResponse, error) {
slbid := getServiceAnnotation(service, common.ServiceAnnotationInternalSlbId, "")
if slbid == "" {
return nil, ErrorSlbIdNotDefined
}
token, error := getKeyCloakToken(config.RequestedSubject, config.TokenClientID, config.ClientSecret, config.KeycloakUrl, config)
if error != nil {
return nil, error
}
slbResponse, err := modifyLoadBalancer(config.LbUrlPre, token, slbid, slbName)
if err != nil {
return nil, err
}
if slbResponse == nil {
return nil, ErrorNotFoundInCloud
}
return slbResponse, nil
}
func DeleteLoadBalancer(config *InCloud, service *v1.Service) error {
slbid := getServiceAnnotation(service, common.ServiceAnnotationInternalSlbId, "")
if slbid == "" {
return ErrorSlbIdNotDefined
}
token, error := getKeyCloakToken(config.RequestedSubject, config.TokenClientID, config.ClientSecret, config.KeycloakUrl, config)
if error != nil {
return error
}
error = deleteLoadBalancer(config.LbUrlPre, token, slbid)
return error
}
// GetNodesInstanceIDs return resource ids for listener to create backends
func (lb *LoadBalancer) GetNodesInstanceIDs() []string {
//if len(lb.Nodes) == 0 {
// return nil
//}
result := make([]string, 0)
//for _, node := range lb.Nodes {
// result = append(result, instance.NodeNameToInstanceID(node.Name, lb.nodeLister))
//}
return result
}