/
gateway.go
116 lines (97 loc) · 3.69 KB
/
gateway.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
/*
* 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 gateway
import (
"net/http"
"golang.org/x/oauth2"
genericapiserver "k8s.io/apiserver/pkg/server"
"tkestack.io/tke/pkg/apiserver/authentication/authenticator/oidc"
"tkestack.io/tke/pkg/gateway/api"
gatewayconfig "tkestack.io/tke/pkg/gateway/apis/config"
"tkestack.io/tke/pkg/gateway/assets"
"tkestack.io/tke/pkg/gateway/proxy"
"tkestack.io/tke/pkg/gateway/webtty"
)
// ExtraConfig contains the additional configuration of apiserver.
type ExtraConfig struct {
ServerName string
OAuthConfig *oauth2.Config
OIDCHttpClient *http.Client
OIDCAuthenticator *oidc.Authenticator
GatewayConfig *gatewayconfig.GatewayConfiguration
HeaderRequest bool
}
// Config contains the core configuration instance of server and additional
// configuration.
type Config struct {
GenericConfig *genericapiserver.RecommendedConfig
ExtraConfig ExtraConfig
}
type completedConfig struct {
GenericConfig genericapiserver.CompletedConfig
ExtraConfig *ExtraConfig
}
// CompletedConfig embed a private pointer of Config.
type CompletedConfig struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedConfig
}
// Gateway contains state for TKE gateway server.
type Gateway struct {
GenericAPIServer *genericapiserver.GenericAPIServer
}
// Complete fills in any fields not set that are required to have valid data.
// It's mutating the receiver.
func (cfg *Config) Complete() CompletedConfig {
c := completedConfig{
cfg.GenericConfig.Complete(),
&cfg.ExtraConfig,
}
return CompletedConfig{&c}
}
// New returns a new instance of APIServer from the given config.
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Gateway, error) {
s, err := c.GenericConfig.New(c.ExtraConfig.ServerName, delegationTarget)
if err != nil {
return nil, err
}
registerCallbackRoute(s.Handler.NonGoRestfulMux, c.ExtraConfig.OAuthConfig, c.ExtraConfig.OIDCHttpClient, c.ExtraConfig.GatewayConfig.DisableOIDCProxy)
if !c.ExtraConfig.GatewayConfig.DisableOIDCProxy {
if err := registerAuthRoute(s.Handler.NonGoRestfulMux, c.ExtraConfig.OIDCHttpClient, c.ExtraConfig.OIDCAuthenticator); err != nil {
return nil, err
}
}
if err := proxy.RegisterRoute(s.Handler.NonGoRestfulMux, c.ExtraConfig.GatewayConfig, c.ExtraConfig.OIDCAuthenticator); err != nil {
return nil, err
}
if err := api.RegisterRoute(s.Handler.GoRestfulContainer, c.ExtraConfig.GatewayConfig, c.ExtraConfig.OAuthConfig, c.ExtraConfig.OIDCHttpClient, c.ExtraConfig.OIDCAuthenticator, c.ExtraConfig.HeaderRequest); err != nil {
return nil, err
}
if err := webtty.RegisterRoute(s.Handler.NonGoRestfulMux, c.ExtraConfig.GatewayConfig); err != nil {
return nil, err
}
if !c.ExtraConfig.HeaderRequest {
assets.RegisterRoute(s.Handler.NonGoRestfulMux, c.ExtraConfig.OAuthConfig, c.ExtraConfig.GatewayConfig.DisableOIDCProxy)
} else {
assets.RegisterRoute(s.Handler.NonGoRestfulMux, nil, c.ExtraConfig.GatewayConfig.DisableOIDCProxy)
}
m := &Gateway{
GenericAPIServer: s,
}
return m, nil
}