@@ -17,13 +17,25 @@ limitations under the License.
17
17
package huaweicloud
18
18
19
19
import (
20
+ "fmt"
21
+ "io"
22
+ "os"
23
+
24
+ "gopkg.in/gcfg.v1"
25
+ huaweicloudsdkbasic "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
26
+ huaweicloudsdkconfig "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/config"
27
+ huaweicloudsdkas "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/services/as/v1"
28
+ huaweicloudsdkecs "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2"
29
+
20
30
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huawei-cloud-sdk-go/auth/aksk"
21
31
)
22
32
23
33
// CloudConfig is the cloud config file for huaweicloud.
24
34
type CloudConfig struct {
25
35
Global struct {
26
36
IdentityEndpoint string `gcfg:"identity-endpoint"` // example: "https://iam.cn-north-4.myhuaweicloud.com/v3.0"
37
+ ECSEndpoint string `gcfg:"ecs-endpoint"`
38
+ ASEndpoint string `gcfg:"as-endpoint"`
27
39
ProjectID string `gcfg:"project-id"`
28
40
AccessKey string `gcfg:"access-key"`
29
41
SecretKey string `gcfg:"secret-key"`
@@ -33,6 +45,69 @@ type CloudConfig struct {
33
45
}
34
46
}
35
47
48
+ func (c * CloudConfig ) getECSClient () * huaweicloudsdkecs.EcsClient {
49
+ // There are two types of services provided by HUAWEI CLOUD according to scope:
50
+ // - Regional services: most of services belong to this classification, such as ECS.
51
+ // - Global services: such as IAM, TMS, EPS.
52
+ // For Regional services' authentication, projectId is required.
53
+ // For global services' authentication, domainId is required.
54
+ // More details please refer to:
55
+ // https://github.com/huaweicloud/huaweicloud-sdk-go-v3/blob/0281b9734f0f95ed5565729e54d96e9820262426/README.md#use-go-sdk
56
+ credentials := huaweicloudsdkbasic .NewCredentialsBuilder ().
57
+ WithAk (c .Global .AccessKey ).
58
+ WithSk (c .Global .SecretKey ).
59
+ WithProjectId (c .Global .ProjectID ).
60
+ Build ()
61
+
62
+ client := huaweicloudsdkecs .EcsClientBuilder ().
63
+ WithEndpoint (c .Global .ECSEndpoint ).
64
+ WithCredential (credentials ).
65
+ WithHttpConfig (huaweicloudsdkconfig .DefaultHttpConfig ()).
66
+ Build ()
67
+
68
+ return huaweicloudsdkecs .NewEcsClient (client )
69
+ }
70
+
71
+ func (c * CloudConfig ) getASClient () * huaweicloudsdkas.AsClient {
72
+ credentials := huaweicloudsdkbasic .NewCredentialsBuilder ().
73
+ WithAk (c .Global .AccessKey ).
74
+ WithSk (c .Global .SecretKey ).
75
+ WithProjectId (c .Global .ProjectID ).
76
+ Build ()
77
+
78
+ client := huaweicloudsdkas .AsClientBuilder ().
79
+ WithEndpoint (c .Global .ASEndpoint ).
80
+ WithCredential (credentials ).
81
+ WithHttpConfig (huaweicloudsdkconfig .DefaultHttpConfig ()).
82
+ Build ()
83
+
84
+ return huaweicloudsdkas .NewAsClient (client )
85
+ }
86
+
87
+ func (c * CloudConfig ) validate () error {
88
+ if len (c .Global .ECSEndpoint ) == 0 {
89
+ return fmt .Errorf ("ECS endpoint missing from cloud configuration" )
90
+ }
91
+
92
+ if len (c .Global .ASEndpoint ) == 0 {
93
+ return fmt .Errorf ("AS endpoint missing from cloud configuration" )
94
+ }
95
+
96
+ if len (c .Global .AccessKey ) == 0 {
97
+ return fmt .Errorf ("access key missing from cloud coniguration" )
98
+ }
99
+
100
+ if len (c .Global .SecretKey ) == 0 {
101
+ return fmt .Errorf ("secret key missing from cloud configuration" )
102
+ }
103
+
104
+ if len (c .Global .ProjectID ) == 0 {
105
+ return fmt .Errorf ("project id missing from cloud configuration" )
106
+ }
107
+
108
+ return nil
109
+ }
110
+
36
111
// toAKSKOptions creates and returns a new instance of type aksk.AKSKOptions
37
112
func toAKSKOptions (cfg CloudConfig ) aksk.AKSKOptions {
38
113
opts := aksk.AKSKOptions {
@@ -46,3 +121,19 @@ func toAKSKOptions(cfg CloudConfig) aksk.AKSKOptions {
46
121
}
47
122
return opts
48
123
}
124
+
125
+ func readConf (confFile string ) (* CloudConfig , error ) {
126
+ var conf io.ReadCloser
127
+ conf , err := os .Open (confFile )
128
+ if err != nil {
129
+ return nil , fmt .Errorf ("failed to open configuration file: %s, error: %v" , confFile , err )
130
+ }
131
+ defer conf .Close ()
132
+
133
+ var cloudConfig CloudConfig
134
+ if err := gcfg .ReadInto (& cloudConfig , conf ); err != nil {
135
+ return nil , fmt .Errorf ("failed to read configuration file: %s, error: %v" , confFile , err )
136
+ }
137
+
138
+ return & cloudConfig , nil
139
+ }
0 commit comments