-
Notifications
You must be signed in to change notification settings - Fork 335
/
location.go
115 lines (99 loc) · 3.96 KB
/
location.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
/*
* 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 util
import (
"context"
"fmt"
"net/http"
"net/url"
"path"
"tkestack.io/tke/pkg/apiserver/authentication"
"tkestack.io/tke/pkg/platform/apiserver/filter"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/endpoints/request"
restclient "k8s.io/client-go/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/api/platform"
platformv1 "tkestack.io/tke/api/platform/v1"
clusterprovider "tkestack.io/tke/pkg/platform/provider/cluster"
)
// APIServerLocationByCluster returns a URL and transport which one can use to
// send traffic for the specified cluster api server.
func APIServerLocationByCluster(ctx context.Context, cluster *platformv1.Cluster) (*url.URL, http.RoundTripper, string, error) {
username, tenantID := authentication.UsernameAndTenantID(ctx)
if len(tenantID) > 0 && cluster.Spec.TenantID != tenantID {
return nil, nil, "", errors.NewNotFound(platform.Resource("clusters"), cluster.ObjectMeta.Name)
}
if cluster.Status.Phase != platformv1.ClusterRunning {
return nil, nil, "", errors.NewServiceUnavailable(fmt.Sprintf("cluster %s status is abnormal", cluster.ObjectMeta.Name))
}
if cluster.Status.Locked != nil && *cluster.Status.Locked {
return nil, nil, "", errors.NewForbidden(platform.Resource("clusters"), cluster.ObjectMeta.Name, fmt.Errorf("cluster is been locked"))
}
provider, err := clusterprovider.GetProvider(cluster.Spec.Type)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
restconfig, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
transport, err := restclient.TransportFor(restconfig)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
address, err := clusterAddress(cluster)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
token := restconfig.BearerToken
// Otherwise, return the requested scheme and port, and the proxy transport
return &url.URL{
Scheme: "https",
Host: fmt.Sprintf("%v:%v", address.Host, address.Port),
Path: address.Path,
}, transport, token, nil
}
// APIServerLocation returns a URL and transport which one can use to send
// traffic for the specified kube api server.
func APIServerLocation(ctx context.Context, platformClient platforminternalclient.PlatformInterface) (*url.URL, http.RoundTripper, string, error) {
clusterName := filter.ClusterFrom(ctx)
if clusterName == "" {
return nil, nil, "", errors.NewBadRequest("clusterName is required")
}
requestInfo, ok := request.RequestInfoFrom(ctx)
if !ok {
return nil, nil, "", errors.NewBadRequest("unable to get request info from context")
}
cluster, err := platformClient.Clusters().Get(ctx, clusterName, metav1.GetOptions{})
if err != nil {
return nil, nil, "", err
}
clusterv1 := &platformv1.Cluster{}
err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
location, transport, token, err := APIServerLocationByCluster(ctx, clusterv1)
if err != nil {
return nil, nil, "", err
}
location.Path = path.Join(location.Path, requestInfo.Path)
return location, transport, token, nil
}