/
cloud.go
134 lines (110 loc) · 3.43 KB
/
cloud.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
package tencentcloud
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"github.com/dbdd4us/qcloudapi-sdk-go/cvm"
"github.com/dbdd4us/qcloudapi-sdk-go/ccs"
"github.com/dbdd4us/qcloudapi-sdk-go/common"
"k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/controller"
)
const (
providerName = "tencentcloud"
)
var (
CloudInstanceNotFound = errors.New("tencentcloud instance not found")
)
func init() {
cloudprovider.RegisterCloudProvider(providerName, NewCloud)
}
func NewCloud(config io.Reader) (cloudprovider.Interface, error) {
var c Config
if config != nil {
cfg, err := ioutil.ReadAll(config)
if err != nil {
return nil, err
}
if err := json.Unmarshal(cfg, &c); err != nil {
return nil, err
}
}
if c.Region == "" {
c.Region = os.Getenv("TENCENTCLOUD_CLOUD_CONTROLLER_MANAGER_REGION")
}
if c.SecretId == "" {
c.SecretId = os.Getenv("TENCENTCLOUD_CLOUD_CONTROLLER_MANAGER_SECRET_ID")
}
if c.SecretKey == "" {
c.SecretKey = os.Getenv("TENCENTCLOUD_CLOUD_CONTROLLER_MANAGER_SECRET_KEY")
}
if c.ClusterRouteTable == "" {
c.ClusterRouteTable = os.Getenv("TENCENTCLOUD_CLOUD_CONTROLLER_MANAGER_CLUSTER_ROUTE_TABLE")
}
return &Cloud{config: c}, nil
}
type Cloud struct {
config Config
kubeClient kubernetes.Interface
cvm *cvm.Client
ccs *ccs.Client
}
type Config struct {
Region string `json:"region"`
SecretId string `json:"secret_id"`
SecretKey string `json:"secret_key"`
ClusterRouteTable string `json:"cluster_route_table"`
}
// Initialize provides the cloud with a kubernetes client builder and may spawn goroutines
// to perform housekeeping activities within the cloud provider.
func (cloud *Cloud) Initialize(clientBuilder controller.ControllerClientBuilder) {
cloud.kubeClient = clientBuilder.ClientOrDie("tencentcloud-cloud-provider")
cvmClient, err := cvm.NewClient(
common.Credential{SecretId: cloud.config.SecretId, SecretKey: cloud.config.SecretKey},
common.Opts{Region: cloud.config.Region},
)
if err != nil {
panic(err)
}
cloud.cvm = cvmClient
ccsClient, err := ccs.NewClient(
common.Credential{SecretId: cloud.config.SecretId, SecretKey: cloud.config.SecretKey},
common.Opts{Region: cloud.config.Region},
)
if err != nil {
panic(err)
}
cloud.ccs = ccsClient
return
}
// LoadBalancer returns a balancer interface. Also returns true if the interface is supported, false otherwise.
func (cloud *Cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
return nil, false
}
// Instances returns an instances interface. Also returns true if the interface is supported, false otherwise.
func (cloud *Cloud) Instances() (cloudprovider.Instances, bool) {
return cloud, true
}
// Zones returns a zones interface. Also returns true if the interface is supported, false otherwise.
func (cloud *Cloud) Zones() (cloudprovider.Zones, bool) {
return nil, false
}
// Clusters returns a clusters interface. Also returns true if the interface is supported, false otherwise.
func (cloud *Cloud) Clusters() (cloudprovider.Clusters, bool) {
return nil, false
}
// Routes returns a routes interface along with whether the interface is supported.
func (cloud *Cloud) Routes() (cloudprovider.Routes, bool) {
return cloud, true
}
// ProviderName returns the cloud provider ID.
func (cloud *Cloud) ProviderName() string {
return providerName
}
// HasClusterID returns true if a ClusterID is required and set
func (cloud *Cloud) HasClusterID() bool {
return false
}