github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infra/aliyun/ali_vpc.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aliyun 16 17 import ( 18 "errors" 19 20 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" 21 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses" 22 "github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" 23 24 "github.com/sealerio/sealer/utils" 25 ) 26 27 type VpcManager struct { 28 Config Config 29 Client *vpc.Client 30 } 31 32 func (a *AliProvider) RetryVpcRequest(request requests.AcsRequest, response responses.AcsResponse) error { 33 return utils.Retry(TryTimes, TrySleepTime, func() error { 34 err := a.VpcClient.DoAction(request, response) 35 if err != nil { 36 return err 37 } 38 return nil 39 }) 40 } 41 42 func (a *AliProvider) CreateVPC() error { 43 request := vpc.CreateCreateVpcRequest() 44 request.Scheme = Scheme 45 request.RegionId = a.Config.RegionID 46 //response, err := d.Client.CreateVpc(request) 47 response := vpc.CreateCreateVpcResponse() 48 err := a.RetryVpcRequest(request, response) 49 if err != nil { 50 return err 51 } 52 a.Cluster.Annotations[VpcID] = response.VpcId 53 a.Cluster.Annotations[AliRegionID] = a.Config.RegionID 54 return nil 55 } 56 57 func (a *AliProvider) DeleteVPC() error { 58 request := vpc.CreateDeleteVpcRequest() 59 request.Scheme = Scheme 60 request.VpcId = a.Cluster.Annotations[VpcID] 61 62 //response, err := d.Client.DeleteVpc(request) 63 response := vpc.CreateDeleteVpcResponse() 64 return a.RetryVpcRequest(request, response) 65 } 66 67 func (a *AliProvider) CreateVSwitch() error { 68 request := vpc.CreateCreateVSwitchRequest() 69 request.Scheme = Scheme 70 request.ZoneId = a.Cluster.Annotations[ZoneID] 71 request.CidrBlock = CidrBlock 72 request.VpcId = a.Cluster.GetAnnotationsByKey(VpcID) 73 request.RegionId = a.Config.RegionID 74 //response, err := d.Client.CreateVSwitch(request) 75 response := vpc.CreateCreateVSwitchResponse() 76 err := a.RetryVpcRequest(request, response) 77 if err != nil { 78 return err 79 } 80 a.Cluster.Annotations[VSwitchID] = response.VSwitchId 81 return nil 82 } 83 84 func (a *AliProvider) DeleteVSwitch() error { 85 request := vpc.CreateDeleteVSwitchRequest() 86 request.Scheme = Scheme 87 request.VSwitchId = a.Cluster.Annotations[VSwitchID] 88 89 //response, err := d.Client.DeleteVSwitch(request) 90 response := vpc.CreateDeleteVSwitchResponse() 91 return a.RetryVpcRequest(request, response) 92 } 93 94 func (a *AliProvider) GetZoneID() error { 95 request := vpc.CreateDescribeZonesRequest() 96 request.Scheme = Scheme 97 response := vpc.CreateDescribeZonesResponse() 98 err := a.RetryVpcRequest(request, response) 99 if err != nil { 100 return err 101 } 102 if len(response.Zones.Zone) == 0 { 103 return errors.New("not available ZoneID ") 104 } 105 a.Cluster.Annotations[ZoneID] = response.Zones.Zone[0].ZoneId 106 107 return nil 108 } 109 110 func (a *AliProvider) BindEipForMaster0() error { 111 instances, err := a.GetInstancesInfo(Master, JustGetInstanceInfo) 112 if err != nil { 113 return err 114 } 115 if len(instances) == 0 { 116 return errors.New("can not find master0 ") 117 } 118 master0 := instances[0] 119 eIP, eIPID, err := a.AllocateEipAddress() 120 if err != nil { 121 return err 122 } 123 err = a.AssociateEipAddress(master0.InstanceID, eIPID) 124 if err != nil { 125 return err 126 } 127 a.Cluster.Annotations[Eip] = eIP 128 a.Cluster.Annotations[EipID] = eIPID 129 a.Cluster.Annotations[Master0ID] = master0.InstanceID 130 a.Cluster.Annotations[Master0InternalIP] = master0.PrimaryIPAddress 131 return nil 132 } 133 134 func (a *AliProvider) AllocateEipAddress() (eIP, eIPID string, err error) { 135 request := vpc.CreateAllocateEipAddressRequest() 136 request.Scheme = Scheme 137 request.Bandwidth = Bandwidth 138 request.InternetChargeType = InternetChargeType 139 //response, err := d.Client.AllocateEipAddress(request) 140 response := vpc.CreateAllocateEipAddressResponse() 141 err = a.RetryVpcRequest(request, response) 142 if err != nil { 143 return "", "", err 144 } 145 return response.EipAddress, response.AllocationId, nil 146 } 147 148 func (a *AliProvider) AssociateEipAddress(instanceID, eipID string) error { 149 request := vpc.CreateAssociateEipAddressRequest() 150 request.Scheme = Scheme 151 request.InstanceId = instanceID 152 request.AllocationId = eipID 153 154 //response, err := d.Client.AssociateEipAddress(request) 155 response := vpc.CreateAssociateEipAddressResponse() 156 return a.RetryVpcRequest(request, response) 157 } 158 159 func (a *AliProvider) UnassociateEipAddress() error { 160 request := vpc.CreateUnassociateEipAddressRequest() 161 request.Scheme = Scheme 162 request.AllocationId = a.Cluster.Annotations[EipID] 163 request.Force = requests.NewBoolean(true) 164 //response, err := d.Client.UnassociateEipAddress(request) 165 response := vpc.CreateUnassociateEipAddressResponse() 166 return a.RetryVpcRequest(request, response) 167 } 168 169 func (a *AliProvider) ReleaseEipAddress() error { 170 err := a.UnassociateEipAddress() 171 if err != nil { 172 return err 173 } 174 request := vpc.CreateReleaseEipAddressRequest() 175 request.Scheme = Scheme 176 request.AllocationId = a.Cluster.Annotations[EipID] 177 //response, err := d.Client.ReleaseEipAddress(request) 178 response := vpc.CreateReleaseEipAddressResponse() 179 return a.RetryVpcRequest(request, response) 180 }