/
ip.go
159 lines (130 loc) · 3.59 KB
/
ip.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
package ucloud
import (
"fmt"
"github.com/golang/glog"
//"github.com/ucloud/ucloud-sdk-go/service/uhost"
"github.com/ucloud/ucloud-sdk-go/service/unet"
"kubeup.com/archon/pkg/cluster"
"kubeup.com/archon/pkg/util"
)
type EIP struct {
ID string `k8s:"eip-id"`
}
type EIPOptions struct {
OperatorName string `k8s:"eip-operator-name"`
Bandwidth int `k8s:"eip-bandwidth"`
ChargeType string `k8s:"eip-charge-type"`
PayMode string `k8s:"eip-pay-mode"`
SharedBandwidthID string `k8s:"eip-shared-bandwidth-id"`
}
func (p *UCloud) EnsurePublicIP(clusterName string, instance *cluster.Instance) (status *cluster.InstanceStatus, err error) {
eip := EIP{}
err = util.MapToStruct(instance.Annotations, &eip, UCloudAnnotationPrefix)
if err != nil {
return
}
if eip.ID != "" {
status = &instance.Status
return
}
defer func() {
if err != nil && eip.ID != "" {
err2 := p.ucloud.ReleaseEipAddress(eip.ID)
if err2 != nil {
glog.Errorf("Error rolling back eip address: %v", err2)
}
}
}()
// Default EIP options
eo := EIPOptions{
OperatorName: EIPOperatorBGP,
Bandwidth: 2,
PayMode: EIPPayModeTraffic,
ChargeType: EIPChargeTypeDynamic,
}
err = util.MapToStruct(instance.Annotations, &eo, UCloudAnnotationPrefix)
if err != nil {
return
}
if eo.SharedBandwidthID != "" {
eo.Bandwidth = 0
eo.PayMode = EIPPayModeSharedBandwidth
}
eip0, err := p.ucloud.AllocateEipAddress(&unet.AllocateEIPParams{
OperatorName: eo.OperatorName,
Bandwidth: eo.Bandwidth,
ChargeType: eo.ChargeType,
// TODO Fix ucloud api
PayMode: eo.PayMode,
ShareBandwidthId: eo.SharedBandwidthID,
Name: fmt.Sprintf("EIP-%s", instance.Name),
})
if err != nil {
err = fmt.Errorf("Error allocating EIP: %s", err.Error())
return
}
eip.ID = eip0.EIPId
if instance.Annotations == nil {
instance.Annotations = make(map[string]string)
}
err = util.StructToMap(eip, instance.Annotations, UCloudAnnotationPrefix)
if err != nil {
err = fmt.Errorf("Error allocating EIP: %s", err.Error())
return
}
status = &instance.Status
// TODO: Handle duplet ip
status.PublicIP = (*eip0.EIPAddr)[0].IP
return &instance.Status, nil
}
func (p *UCloud) EnsurePublicIPDeleted(clusterName string, instance *cluster.Instance) (err error) {
if instance.Annotations == nil {
return
}
eip := EIP{}
err = util.MapToStruct(instance.Annotations, &eip, UCloudAnnotationPrefix)
if err != nil {
return
}
if eip.ID != "" {
//p.unet.WaitForEip(common.Region(networkSpec.Region), eip.AllocationID, unet.EipStatusAvailable, 0)
err = p.ucloud.ReleaseEipAddress(eip.ID)
if err != nil {
err = fmt.Errorf("Error releasing EIP: %s", err.Error())
return
}
eip.ID = ""
err = util.StructToMap(eip, instance.Annotations, UCloudAnnotationPrefix)
if err != nil {
err = fmt.Errorf("Error releasing EIP: %s", err.Error())
return
}
instance.Status.PublicIP = ""
}
return nil
}
func (p *UCloud) EnsurePrivateIP(clusterName string, instance *cluster.Instance) (status *cluster.InstanceStatus, err error) {
if instance.Status.InstanceID != "" {
status = &instance.Status
return
}
status, err = p.createInstance(clusterName, instance)
if err == nil {
if status.PublicIP == "" {
status.PublicIP = instance.Status.PublicIP
}
instance.Status = *status
}
return
}
func (p *UCloud) EnsurePrivateIPDeleted(clusterName string, instance *cluster.Instance) (err error) {
if instance.Status.InstanceID == "" {
return
}
err = p.deleteInstance(instance.Status.InstanceID)
if err != nil {
return
}
instance.Status.InstanceID = ""
return
}