sigs.k8s.io/external-dns@v0.14.1/provider/tencentcloud/cloudapi/clientset.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cloudapi 18 19 import ( 20 "fmt" 21 "sync" 22 23 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" 24 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" 25 dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323" 26 privatedns "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028" 27 "go.uber.org/ratelimit" 28 ) 29 30 type TencentClientSetService interface { 31 PrivateDnsCli(action string) *privatedns.Client 32 DnsPodCli(action string) *dnspod.Client 33 } 34 35 func NewTencentClientSetService(region string, rate int, secretId string, secretKey string, internetEndpoint bool) *defaultTencentClientSetService { 36 p := &defaultTencentClientSetService{ 37 Region: region, 38 RateLimit: rate, 39 } 40 cred := common.NewCredential(secretId, secretKey) 41 42 privatednsProf := profile.NewClientProfile() 43 if !internetEndpoint { 44 privatednsProf.HttpProfile.Endpoint = "privatedns.internal.tencentcloudapi.com" 45 } 46 p.privateDnsClient, _ = privatedns.NewClient(cred, region, privatednsProf) 47 48 dnsPodProf := profile.NewClientProfile() 49 if !internetEndpoint { 50 dnsPodProf.HttpProfile.Endpoint = "dnspod.internal.tencentcloudapi.com" 51 } 52 p.dnsPodClient, _ = dnspod.NewClient(cred, region, dnsPodProf) 53 54 return p 55 } 56 57 type defaultTencentClientSetService struct { 58 Region string 59 RateLimit int 60 RateLimitSyncMap sync.Map 61 62 privateDnsClient *privatedns.Client 63 dnsPodClient *dnspod.Client 64 } 65 66 func (p *defaultTencentClientSetService) checkRateLimit(request, method string) { 67 action := fmt.Sprintf("%s_%s", request, method) 68 if rl, ok := p.RateLimitSyncMap.LoadOrStore(action, ratelimit.New(p.RateLimit, ratelimit.WithoutSlack)); ok { 69 rl.(ratelimit.Limiter).Take() 70 } 71 } 72 73 func (p *defaultTencentClientSetService) PrivateDnsCli(action string) *privatedns.Client { 74 p.checkRateLimit("privateDns", action) 75 return p.privateDnsClient 76 } 77 78 func (p *defaultTencentClientSetService) DnsPodCli(action string) *dnspod.Client { 79 p.checkRateLimit("dnsPod", action) 80 return p.dnsPodClient 81 }