Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
eeaa4b64
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
eeaa4b64
编写于
7月 09, 2020
作者:
Z
zryfish
提交者:
GitHub
7月 09, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use more friendly error messages (#2364)
上级
b479195a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
47 addition
and
15 deletion
+47
-15
pkg/apiserver/dispatch/dispatch.go
pkg/apiserver/dispatch/dispatch.go
+2
-2
pkg/kapis/cluster/v1alpha1/handler.go
pkg/kapis/cluster/v1alpha1/handler.go
+27
-9
pkg/kapis/cluster/v1alpha1/handler_test.go
pkg/kapis/cluster/v1alpha1/handler_test.go
+16
-2
pkg/kapis/cluster/v1alpha1/register.go
pkg/kapis/cluster/v1alpha1/register.go
+2
-2
未找到文件。
pkg/apiserver/dispatch/dispatch.go
浏览文件 @
eeaa4b64
...
...
@@ -118,13 +118,13 @@ func (c *clusterDispatch) Dispatch(w http.ResponseWriter, req *http.Request, han
}
if
!
isClusterReady
(
cluster
)
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"cluster
is not ready"
),
http
.
StatusInternalServerError
)
http
.
Error
(
w
,
fmt
.
Sprintf
(
"cluster
%s is not ready"
,
cluster
.
Name
),
http
.
StatusInternalServerError
)
return
}
innCluster
:=
c
.
getInnerCluster
(
cluster
.
Name
)
if
innCluster
==
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"cluster
not ready"
),
http
.
StatusInternalServerError
)
http
.
Error
(
w
,
fmt
.
Sprintf
(
"cluster
%s is not ready"
,
cluster
.
Name
),
http
.
StatusInternalServerError
)
return
}
...
...
pkg/kapis/cluster/v1alpha1/handler.go
浏览文件 @
eeaa4b64
...
...
@@ -6,11 +6,13 @@ import (
"fmt"
"github.com/emicklei/go-restful"
"io"
"io/ioutil"
appsv1
"k8s.io/api/apps/v1"
corev1
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
v1
"k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/rest"
...
...
@@ -29,12 +31,10 @@ import (
const
(
defaultAgentImage
=
"kubesphere/tower:v1.0"
defaultTimeout
=
5
*
time
.
Second
defaultTimeout
=
10
*
time
.
Second
)
var
errClusterConnectionIsNotProxy
=
fmt
.
Errorf
(
"cluster is not using proxy connection"
)
var
errNon200Response
=
fmt
.
Errorf
(
"non-200 response returned from endpoint"
)
var
errInvalidResponse
=
fmt
.
Errorf
(
"invalid response from kubesphere apiserver"
)
type
handler
struct
{
serviceLister
v1
.
ServiceLister
...
...
@@ -45,7 +45,7 @@ type handler struct {
yamlPrinter
*
printers
.
YAMLPrinter
}
func
N
ewHandler
(
serviceLister
v1
.
ServiceLister
,
clusterLister
clusterlister
.
ClusterLister
,
proxyService
,
proxyAddress
,
agentImage
string
)
*
handler
{
func
n
ewHandler
(
serviceLister
v1
.
ServiceLister
,
clusterLister
clusterlister
.
ClusterLister
,
proxyService
,
proxyAddress
,
agentImage
string
)
*
handler
{
if
len
(
agentImage
)
==
0
{
agentImage
=
defaultAgentImage
...
...
@@ -222,7 +222,7 @@ func (h *handler) generateDefaultDeployment(cluster *v1alpha1.Cluster, w io.Writ
}
// ValidateCluster validate cluster kubeconfig and kubesphere apiserver address, check their accessibility
func
(
h
*
handler
)
V
alidateCluster
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
func
(
h
*
handler
)
v
alidateCluster
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
var
cluster
v1alpha1
.
Cluster
err
:=
request
.
ReadEntity
(
&
cluster
)
...
...
@@ -241,7 +241,7 @@ func (h *handler) ValidateCluster(request *restful.Request, response *restful.Re
return
}
err
=
validateKubeConfig
(
cluster
.
Spec
.
Connection
.
KubeConfig
)
err
=
h
.
validateKubeConfig
(
cluster
.
Spec
.
Connection
.
KubeConfig
)
if
err
!=
nil
{
api
.
HandleBadRequest
(
response
,
request
,
err
)
return
...
...
@@ -257,12 +257,25 @@ func (h *handler) ValidateCluster(request *restful.Request, response *restful.Re
}
// validateKubeConfig takes base64 encoded kubeconfig and check its validity
func
validateKubeConfig
(
kubeconfig
[]
byte
)
error
{
func
(
h
*
handler
)
validateKubeConfig
(
kubeconfig
[]
byte
)
error
{
config
,
err
:=
loadKubeConfigFromBytes
(
kubeconfig
)
if
err
!=
nil
{
return
err
}
clusters
,
err
:=
h
.
clusterLister
.
List
(
labels
.
Everything
())
if
err
!=
nil
{
return
err
}
// clusters with the exactly same KubernetesAPIEndpoint considered to be one
// MUST not import the same cluster twice
for
_
,
cluster
:=
range
clusters
{
if
len
(
cluster
.
Spec
.
Connection
.
KubernetesAPIEndpoint
)
!=
0
&&
cluster
.
Spec
.
Connection
.
KubernetesAPIEndpoint
==
config
.
Host
{
return
fmt
.
Errorf
(
"existing cluster %s with the exacty same server address, MUST not import the same cluster twice"
,
cluster
.
Name
)
}
}
config
.
Timeout
=
defaultTimeout
clientSet
,
err
:=
kubernetes
.
NewForConfig
(
config
)
...
...
@@ -327,14 +340,19 @@ func validateKubeSphereAPIServer(ksEndpoint string, kubeconfig []byte) (*version
return
nil
,
err
}
responseBytes
,
_
:=
ioutil
.
ReadAll
(
response
.
Body
)
responseBody
:=
string
(
responseBytes
)
response
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewBuffer
(
responseBytes
))
if
response
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
errNon200Response
return
nil
,
fmt
.
Errorf
(
"invalid response: %s , please make sure ks-apiserver.kubesphere-system.svc of member cluster is up and running"
,
responseBody
)
}
ver
:=
version
.
Info
{}
err
=
json
.
NewDecoder
(
response
.
Body
)
.
Decode
(
&
ver
)
if
err
!=
nil
{
return
nil
,
errInvalidResponse
return
nil
,
fmt
.
Errorf
(
"invalid response: %s , please make sure ks-apiserver.kubesphere-system.svc of member cluster is up and running"
,
responseBody
)
}
return
&
ver
,
nil
...
...
pkg/kapis/cluster/v1alpha1/handler_test.go
浏览文件 @
eeaa4b64
...
...
@@ -142,7 +142,7 @@ func TestGeranteAgentDeployment(t *testing.T) {
for
_
,
testCase
:=
range
testCases
{
t
.
Run
(
testCase
.
description
,
func
(
t
*
testing
.
T
)
{
h
:=
N
ewHandler
(
informersFactory
.
KubernetesSharedInformerFactory
()
.
Core
()
.
V1
()
.
Services
()
.
Lister
(),
h
:=
n
ewHandler
(
informersFactory
.
KubernetesSharedInformerFactory
()
.
Core
()
.
V1
()
.
Services
()
.
Lister
(),
informersFactory
.
KubeSphereSharedInformerFactory
()
.
Cluster
()
.
V1alpha1
()
.
Clusters
()
.
Lister
(),
proxyService
,
""
,
...
...
@@ -215,6 +215,20 @@ users:
`
func
TestValidateKubeConfig
(
t
*
testing
.
T
)
{
k8sclient
:=
k8sfake
.
NewSimpleClientset
(
service
)
ksclient
:=
fake
.
NewSimpleClientset
(
cluster
)
informersFactory
:=
informers
.
NewInformerFactories
(
k8sclient
,
ksclient
,
nil
,
nil
,
nil
,
nil
)
informersFactory
.
KubernetesSharedInformerFactory
()
.
Core
()
.
V1
()
.
Services
()
.
Informer
()
.
GetIndexer
()
.
Add
(
service
)
informersFactory
.
KubeSphereSharedInformerFactory
()
.
Cluster
()
.
V1alpha1
()
.
Clusters
()
.
Informer
()
.
GetIndexer
()
.
Add
(
cluster
)
h
:=
newHandler
(
informersFactory
.
KubernetesSharedInformerFactory
()
.
Core
()
.
V1
()
.
Services
()
.
Lister
(),
informersFactory
.
KubeSphereSharedInformerFactory
()
.
Cluster
()
.
V1alpha1
()
.
Clusters
()
.
Lister
(),
proxyService
,
""
,
agentImage
)
config
,
err
:=
loadKubeConfigFromBytes
([]
byte
(
base64EncodedKubeConfig
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -247,7 +261,7 @@ func TestValidateKubeConfig(t *testing.T) {
_
=
env
.
Stop
()
}()
err
=
validateKubeConfig
([]
byte
(
base64EncodedKubeConfig
))
err
=
h
.
validateKubeConfig
([]
byte
(
base64EncodedKubeConfig
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
pkg/kapis/cluster/v1alpha1/register.go
浏览文件 @
eeaa4b64
...
...
@@ -24,7 +24,7 @@ func AddToContainer(container *restful.Container,
agentImage
string
)
error
{
webservice
:=
runtime
.
NewWebService
(
GroupVersion
)
h
:=
N
ewHandler
(
k8sInformers
.
Core
()
.
V1
()
.
Services
()
.
Lister
(),
ksInformers
.
Cluster
()
.
V1alpha1
()
.
Clusters
()
.
Lister
(),
proxyService
,
proxyAddress
,
agentImage
)
h
:=
n
ewHandler
(
k8sInformers
.
Core
()
.
V1
()
.
Services
()
.
Lister
(),
ksInformers
.
Cluster
()
.
V1alpha1
()
.
Clusters
()
.
Lister
(),
proxyService
,
proxyAddress
,
agentImage
)
// returns deployment yaml for cluster agent
webservice
.
Route
(
webservice
.
GET
(
"/clusters/{cluster}/agent/deployment"
)
.
...
...
@@ -36,7 +36,7 @@ func AddToContainer(container *restful.Container,
webservice
.
Route
(
webservice
.
POST
(
"/clusters/validation"
)
.
Doc
(
""
)
.
Param
(
webservice
.
BodyParameter
(
"cluster"
,
"cluster specification"
))
.
To
(
h
.
V
alidateCluster
)
.
To
(
h
.
v
alidateCluster
)
.
Returns
(
http
.
StatusOK
,
api
.
StatusOK
,
nil
))
container
.
Add
(
webservice
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录