/
config.go
128 lines (112 loc) · 4.99 KB
/
config.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package config
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/util/sets"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/filters"
serverstorage "k8s.io/apiserver/pkg/server/storage"
versionedclientset "tkestack.io/tke/api/client/clientset/versioned"
versionedinformers "tkestack.io/tke/api/client/informers/externalversions"
generatedopenapi "tkestack.io/tke/api/openapi"
"tkestack.io/tke/api/platform"
"tkestack.io/tke/cmd/tke-platform-api/app/options"
"tkestack.io/tke/pkg/apiserver/authentication"
"tkestack.io/tke/pkg/apiserver/authorization"
"tkestack.io/tke/pkg/apiserver/debug"
"tkestack.io/tke/pkg/apiserver/handler"
"tkestack.io/tke/pkg/apiserver/openapi"
"tkestack.io/tke/pkg/apiserver/storage"
"tkestack.io/tke/pkg/apiserver/util"
"tkestack.io/tke/pkg/auth/filter"
"tkestack.io/tke/pkg/platform/apiserver"
)
const (
license = "Apache 2.0"
title = "Tencent Kubernetes Engine Platform API"
)
// Config is the running configuration structure of the TKE platform apiserver.
type Config struct {
ServerName string
GenericAPIServerConfig *genericapiserver.Config
VersionedSharedInformerFactory versionedinformers.SharedInformerFactory
StorageFactory *serverstorage.DefaultStorageFactory
PrivilegedUsername string
}
// CreateConfigFromOptions creates a running configuration instance based
// on a given TKE platform apiserver command line or configuration file option.
func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config, error) {
genericAPIServerConfig := genericapiserver.NewConfig(platform.Codecs)
genericAPIServerConfig.LongRunningFunc = filters.BasicLongRunningRequestCheck(
sets.NewString("watch", "proxy"),
sets.NewString("attach", "exec", "proxy", "log", "portforward"),
)
genericAPIServerConfig.MergedResourceConfig = apiserver.DefaultAPIResourceConfigSource()
genericAPIServerConfig.EnableIndex = false
genericAPIServerConfig.EnableProfiling = false
if err := util.SetupAuditConfig(genericAPIServerConfig, opts.Audit); err != nil {
return nil, err
}
if err := opts.Generic.ApplyTo(genericAPIServerConfig); err != nil {
return nil, err
}
if err := opts.SecureServing.ApplyTo(&genericAPIServerConfig.SecureServing, &genericAPIServerConfig.LoopbackClientConfig); err != nil {
return nil, err
}
openapi.SetupOpenAPI(genericAPIServerConfig, generatedopenapi.GetOpenAPIDefinitions, title, license, opts.Generic.ExternalHost, opts.Generic.ExternalPort)
// storageFactory
storageFactoryConfig := storage.NewFactoryConfig(platform.Codecs, platform.Scheme)
storageFactoryConfig.APIResourceConfig = genericAPIServerConfig.MergedResourceConfig
completedStorageFactoryConfig, err := storageFactoryConfig.Complete(opts.ETCD)
if err != nil {
return nil, err
}
storageFactory, err := completedStorageFactoryConfig.New()
if err != nil {
return nil, err
}
if err := opts.ETCD.ApplyWithStorageFactoryTo(storageFactory, genericAPIServerConfig); err != nil {
return nil, err
}
// client config
genericAPIServerConfig.LoopbackClientConfig.ContentConfig.ContentType = "application/vnd.kubernetes.protobuf"
kubeClientConfig := genericAPIServerConfig.LoopbackClientConfig
clientgoExternalClient, err := versionedclientset.NewForConfig(kubeClientConfig)
if err != nil {
return nil, fmt.Errorf("failed to create real external clientset: %v", err)
}
clusterInspector := filter.NewClusterInspector(clientgoExternalClient.PlatformV1(), opts.Authentication.PrivilegedUsername)
genericAPIServerConfig.BuildHandlerChainFunc = handler.BuildHandlerChain(nil, nil, []filter.Inspector{clusterInspector})
versionedInformers := versionedinformers.NewSharedInformerFactory(clientgoExternalClient, 10*time.Minute)
debug.SetupDebug(genericAPIServerConfig, opts.Debug)
if err := authentication.SetupAuthentication(genericAPIServerConfig, opts.Authentication); err != nil {
return nil, err
}
if err := authorization.SetupAuthorization(genericAPIServerConfig, opts.Authorization); err != nil {
return nil, err
}
return &Config{
ServerName: serverName,
GenericAPIServerConfig: genericAPIServerConfig,
VersionedSharedInformerFactory: versionedInformers,
StorageFactory: storageFactory,
PrivilegedUsername: opts.Authentication.PrivilegedUsername,
}, nil
}