forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arenaconfig.go
305 lines (271 loc) · 8.33 KB
/
arenaconfig.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package config
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"os"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/util"
config "github.com/kubeflow/arena/pkg/util/config"
homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
extclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
RecommendedConfigPathEnvVar = "ARENA_CONFIG"
DefaultArenaConfigPath = "~/.arena/config"
GlobalConfigmapName = "arena-config"
AdminUserKeyInConfigmap = "adminUsers"
)
var arenaClient *ArenaConfiger
var errInitArenaClient error
var once sync.Once
// InitArenaConfiger initilize
func InitArenaConfiger(args types.ArenaClientArgs) (*ArenaConfiger, error) {
once.Do(func() {
arenaClient, errInitArenaClient = newArenaConfiger(args)
})
return arenaClient, errInitArenaClient
}
// GetArenaConfiger returns the arena configer,it must be invoked after invoking function InitArenaConfiger(...)
func GetArenaConfiger() *ArenaConfiger {
if arenaClient == nil {
err := fmt.Errorf("ArenaClient is not initilized,but you want to get it")
log.Errorf("Arena Client is not initilized,please use function InitArenaClient(...) to init it")
panic(err)
}
return arenaClient
}
type tokenRetriever struct {
rountTripper http.RoundTripper
token string
}
func (t *tokenRetriever) RoundTrip(req *http.Request) (*http.Response, error) {
header := req.Header.Get("authorization")
switch {
case strings.HasPrefix(header, "Bearer "):
t.token = strings.ReplaceAll(header, "Bearer ", "")
}
return t.rountTripper.RoundTrip(req)
}
type User struct {
name string
id string
}
func (u User) GetName() string {
return u.name
}
func (u User) GetId() string {
return u.id
}
type ArenaConfiger struct {
restConfig *rest.Config
clientConfig clientcmd.ClientConfig
clientset *kubernetes.Clientset
apiExtensionClientset *extclientset.Clientset
user User
adminUsers []User
namespace string
arenaNamespace string
configs map[string]string
isDaemonMode bool
clusterInstalledCRDs []string
isolateUserInNamespace bool
tokenRetriever *tokenRetriever
}
func newArenaConfiger(args types.ArenaClientArgs) (*ArenaConfiger, error) {
tr := &tokenRetriever{}
arenaConfigs, err := loadArenaConifg()
if err != nil {
return nil, err
}
clientConfig, restConfig, clientSet, err := initKubeClient(args.Kubeconfig)
if err != nil {
return nil, err
}
restConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
tr.rountTripper = rt
return tr
})
apiExtensionClientSet, err := extclientset.NewForConfig(restConfig)
if err != nil {
return nil, err
}
/*
crdNames, err := getClusterInstalledCRDs(apiExtensionClientSet)
if err != nil {
return nil, err
}
*/
namespace := updateNamespace(args.Namespace, arenaConfigs, clientConfig)
log.Debugf("current namespace is %v", namespace)
userName, err := getUserName(namespace, clientConfig, restConfig, clientSet, tr)
if err != nil {
return nil, err
}
log.Debugf("succeed to get user name: %v from client-go", *userName)
userId := util.Md5(*userName)
log.Debugf("the user id is %v", userId)
data := getGlobalConfigFromConfigmap(args.ArenaNamespace, clientSet)
adminUsers := getAdminUserFromConfigmap(data)
i, err := isolateUserInNamespace(namespace, clientSet)
if err != nil {
return nil, err
}
log.Debugf("enable isolate user in namespace %v: %v", namespace, i)
return &ArenaConfiger{
restConfig: restConfig,
clientConfig: clientConfig,
clientset: clientSet,
apiExtensionClientset: apiExtensionClientSet,
namespace: namespace,
arenaNamespace: args.ArenaNamespace,
configs: arenaConfigs,
isDaemonMode: args.IsDaemonMode,
clusterInstalledCRDs: []string{},
user: User{name: *userName, id: userId},
adminUsers: adminUsers,
isolateUserInNamespace: i,
tokenRetriever: tr,
}, nil
}
// GetClientConfig returns the kubernetes ClientConfig
func (a *ArenaConfiger) GetClientConfig() clientcmd.ClientConfig {
return a.clientConfig
}
// GetRestConfig returns the kubernetes RestConfig
func (a *ArenaConfiger) GetRestConfig() *rest.Config {
return a.restConfig
}
// GetClientSet returns the kubernetes ClientSet
func (a *ArenaConfiger) GetClientSet() *kubernetes.Clientset {
return a.clientset
}
func (a *ArenaConfiger) GetAPIExtensionClientSet() *extclientset.Clientset {
return a.apiExtensionClientset
}
// GetArenaNamespace returns the kubernetes namespace which some operators exists in
func (a *ArenaConfiger) GetArenaNamespace() string {
return a.arenaNamespace
}
// GetNamespace returns the namespace of user assigns
func (a *ArenaConfiger) GetNamespace() string {
return a.namespace
}
// GetConfigsFromConfigFile returns the configs read from config file
func (a *ArenaConfiger) GetConfigsFromConfigFile() map[string]string {
return a.configs
}
func (a *ArenaConfiger) IsDaemonMode() bool {
return a.isDaemonMode
}
func (a *ArenaConfiger) GetClusterInstalledCRDs() []string {
return a.clusterInstalledCRDs
}
func (a *ArenaConfiger) GetUser() User {
return a.user
}
func (a *ArenaConfiger) IsIsolateUserInNamespace() bool {
return a.isolateUserInNamespace
}
func (a *ArenaConfiger) GetAdminUsers() []User {
return a.adminUsers
}
func (a *ArenaConfiger) IsAdminUser() bool {
for _, admin := range a.adminUsers {
if a.user.GetId() == admin.GetId() {
return true
}
}
return false
}
func getGlobalConfigFromConfigmap(namespace string, client *kubernetes.Clientset) map[string]string {
data := map[string]string{}
configmap, err := client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), GlobalConfigmapName, metav1.GetOptions{})
if err != nil {
log.Debugf("failed to get arena global configmap %v,reason: %v", GlobalConfigmapName, err)
return data
}
return configmap.Data
}
func getAdminUserFromConfigmap(data map[string]string) []User {
users := []User{}
val, ok := data[AdminUserKeyInConfigmap]
if !ok {
return users
}
userNames := strings.Split(val, ",")
for _, name := range userNames {
name = strings.Trim(name, " ")
if name == "" {
continue
}
userId := util.Md5(name)
u := User{name: name, id: userId}
log.Debugf("found admin user: %v", u)
users = append(users, u)
}
return users
}
// loadArenaConifg returns configs in map
func loadArenaConifg() (map[string]string, error) {
arenaConfigs := map[string]string{}
log.Debugf("start to init arena config")
validateFile := func(file string) bool {
if file == "" {
return false
}
_, err := os.Stat(file)
if err != nil {
log.Debugf("failed to get state of file %v,reason: %v,skip to handle it", file, err)
return false
}
return true
}
configFileName := os.Getenv(RecommendedConfigPathEnvVar)
defaultConfigFile, err := homedir.Expand(DefaultArenaConfigPath)
if err != nil {
return arenaConfigs, err
}
// if config file path read from env is invalid,read it from default path
if !validateFile(configFileName) {
configFileName = defaultConfigFile
}
// if config file is invalid,return null
if !validateFile(configFileName) {
return arenaConfigs, nil
}
arenaConfigs = config.ReadConfigFile(configFileName)
log.Debugf("arena configs: %v", arenaConfigs)
return arenaConfigs, nil
}
func updateNamespace(namespace string, arenaConfigs map[string]string, clientConfig clientcmd.ClientConfig) string {
if namespace != "" {
return namespace
}
log.Debugf("we need to update the namespace")
if n, ok := arenaConfigs["namespace"]; ok {
log.Debugf("read namespace %v from arena configuration file", n)
return n
}
n, _, err := clientConfig.Namespace()
if err == nil {
log.Debugf("read namespace %v from kubeconfig", n)
return n
}
log.Debugf("failed to read namespace from kubeconfig,we set the default namespace with 'default'")
return "default"
}
func isolateUserInNamespace(namespaceName string, clientSet *kubernetes.Clientset) (bool, error) {
namespace, err := clientSet.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
if err != nil {
return false, err
}
return namespace.Labels[types.MultiTenantIsolationLabel] == "true", nil
}