This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
/
alicloud_clientmgr.go
133 lines (114 loc) · 3.25 KB
/
alicloud_clientmgr.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
package alicloud
import (
"sync"
"time"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs"
"github.com/denverdino/aliyungo/metadata"
"github.com/denverdino/aliyungo/slb"
"github.com/golang/glog"
"github.com/patrickmn/go-cache"
"k8s.io/apimachinery/pkg/util/wait"
)
var ROLE_NAME = "KubernetesMasterRole"
var TOKEN_RESYNC_PERIOD = 5 * time.Minute
type TokenAuth struct {
lock sync.RWMutex
auth metadata.RoleAuth
active bool
}
func (token *TokenAuth) authid() (string, string, string) {
token.lock.RLock()
defer token.lock.RUnlock()
return token.auth.AccessKeyId,
token.auth.AccessKeySecret,
token.auth.SecurityToken
}
type ClientMgr struct {
stop <-chan struct{}
token *TokenAuth
meta *metadata.MetaData
routes *RoutesClient
loadbalancer *LoadBalancerClient
instance *InstancerClient
}
func NewClientMgr(key, secret string) (*ClientMgr, error) {
token := &TokenAuth{
auth: metadata.RoleAuth{
AccessKeyId: key,
AccessKeySecret: secret,
},
active: false,
}
m := metadata.NewMetaData(nil)
if key == "" || secret == "" {
if rolename, err := m.RoleName(); err != nil {
return nil, err
} else {
ROLE_NAME = rolename
role, err := m.RamRoleToken(ROLE_NAME)
if err != nil {
return nil, err
}
glog.V(2).Infof("alicloud: clientmgr, using role=[%s] with initial token=[%+v]", ROLE_NAME, role)
token.auth = role
token.active = true
}
}
keyid, sec, tok := token.authid()
ecsclient := ecs.NewECSClientWithSecurityToken(keyid, sec, tok, DEFAULT_REGION)
ecsclient.SetUserAgent(KUBERNETES_ALICLOUD_IDENTITY)
slbclient := slb.NewSLBClientWithSecurityToken(keyid, sec, tok, DEFAULT_REGION)
slbclient.SetUserAgent(KUBERNETES_ALICLOUD_IDENTITY)
mgr := &ClientMgr{
stop: make(<-chan struct{}, 1),
token: token,
meta: m,
instance: &InstancerClient{
c: ecsclient,
},
loadbalancer: &LoadBalancerClient{
c: slbclient,
},
routes: &RoutesClient{
client: ecsclient,
routers: cache.New(defaultCacheExpiration, defaultCacheExpiration),
vpcs: cache.New(defaultCacheExpiration, defaultCacheExpiration),
},
}
if !token.active {
// use key and secret
glog.Infof("alicloud: clientmgr, use accesskeyid and accesskeysecret mode to authenticate user. without token")
return mgr, nil
}
go wait.Until(func() {
// refresh client token periodically
token.lock.Lock()
defer token.lock.Unlock()
role, err := mgr.meta.RamRoleToken(ROLE_NAME)
if err != nil {
glog.Errorf("alicloud: clientmgr, error get ram role token [%s]\n", err.Error())
return
}
token.auth = role
ecsclient.WithSecurityToken(role.SecurityToken).
WithAccessKeyId(role.AccessKeyId).
WithAccessKeySecret(role.AccessKeySecret)
slbclient.WithSecurityToken(role.SecurityToken).
WithAccessKeyId(role.AccessKeyId).
WithAccessKeySecret(role.AccessKeySecret)
}, time.Duration(TOKEN_RESYNC_PERIOD), mgr.stop)
return mgr, nil
}
func (c *ClientMgr) Instances(region common.Region) *InstancerClient {
return c.instance
}
func (c *ClientMgr) Routes(region common.Region) *RoutesClient {
return c.routes
}
func (c *ClientMgr) LoadBalancers(region common.Region) *LoadBalancerClient {
return c.loadbalancer
}
func (c *ClientMgr) MetaData() *metadata.MetaData {
return c.meta
}