fix cluster import validation bug (#2223)

This commit is contained in:
zryfish
2020-06-18 10:54:20 +08:00
committed by GitHub
parent 0ac6d069dc
commit b661e0d1b6
3 changed files with 14 additions and 16 deletions

View File

@@ -61,7 +61,10 @@ func NewHandler(serviceLister v1.ServiceLister, clusterLister clusterlister.Clus
} }
} }
func (h *handler) GenerateAgentDeployment(request *restful.Request, response *restful.Response) { // generateAgentDeployment will return a deployment yaml for proxy connection type cluster
// ProxyPublishAddress takes high precedence over proxyPublishService, use proxyPublishService ingress
// address only when proxyPublishAddress is not provided.
func (h *handler) generateAgentDeployment(request *restful.Request, response *restful.Response) {
clusterName := request.PathParameter("cluster") clusterName := request.PathParameter("cluster")
cluster, err := h.clusterLister.Get(clusterName) cluster, err := h.clusterLister.Get(clusterName)
@@ -113,7 +116,7 @@ func (h *handler) populateProxyAddress() error {
service, err := h.serviceLister.Services(namespace).Get(parts[0]) service, err := h.serviceLister.Services(namespace).Get(parts[0])
if err != nil { if err != nil {
return err return fmt.Errorf("service %s not found in namespace %s", parts[0], namespace)
} }
if len(service.Spec.Ports) == 0 { if len(service.Spec.Ports) == 0 {
@@ -184,9 +187,10 @@ func (h *handler) generateDefaultDeployment(cluster *v1alpha1.Cluster, w io.Writ
fmt.Sprintf("--name=%s", cluster.Name), fmt.Sprintf("--name=%s", cluster.Name),
fmt.Sprintf("--token=%s", cluster.Spec.Connection.Token), fmt.Sprintf("--token=%s", cluster.Spec.Connection.Token),
fmt.Sprintf("--proxy-server=%s", h.proxyAddress), fmt.Sprintf("--proxy-server=%s", h.proxyAddress),
fmt.Sprintf("--keepalive=30s"), fmt.Sprintf("--keepalive=10s"),
fmt.Sprintf("--kubesphere-service=ks-apiserver.kubesphere-system.svc:80"), fmt.Sprintf("--kubesphere-service=ks-apiserver.kubesphere-system.svc:80"),
fmt.Sprintf("--kubernetes-service=kubernetes.default.svc:443"), fmt.Sprintf("--kubernetes-service=kubernetes.default.svc:443"),
fmt.Sprintf("--v=0"),
}, },
Image: h.agentImage, Image: h.agentImage,
Resources: corev1.ResourceRequirements{ Resources: corev1.ResourceRequirements{
@@ -221,12 +225,12 @@ func (h *handler) ValidateCluster(request *restful.Request, response *restful.Re
} }
if cluster.Spec.Connection.Type != v1alpha1.ConnectionTypeDirect { if cluster.Spec.Connection.Type != v1alpha1.ConnectionTypeDirect {
api.HandleBadRequest(response, request, fmt.Errorf("cluster connection type is not direct")) api.HandleBadRequest(response, request, fmt.Errorf("cluster connection type MUST be direct"))
return return
} }
if len(cluster.Spec.Connection.KubeConfig) == 0 || len(cluster.Spec.Connection.KubeSphereAPIEndpoint) == 0 { if len(cluster.Spec.Connection.KubeConfig) == 0 {
api.HandleBadRequest(response, request, fmt.Errorf("cluster kubeconfig and kubesphere endpoint should not be empty")) api.HandleBadRequest(response, request, fmt.Errorf("cluster kubeconfig MUST NOT be empty"))
return return
} }
@@ -236,13 +240,6 @@ func (h *handler) ValidateCluster(request *restful.Request, response *restful.Re
return return
} }
// kubesphere apiserver endpoint not provided, that's allowed
// Cluster dispatcher will use kube-apiserver proxy instead
if len(cluster.Spec.Connection.KubeSphereAPIEndpoint) == 0 {
response.WriteHeader(http.StatusOK)
return
}
_, err = validateKubeSphereAPIServer(cluster.Spec.Connection.KubeSphereAPIEndpoint, cluster.Spec.Connection.KubeConfig) _, err = validateKubeSphereAPIServer(cluster.Spec.Connection.KubeSphereAPIEndpoint, cluster.Spec.Connection.KubeConfig)
if err != nil { if err != nil {
api.HandleBadRequest(response, request, fmt.Errorf("unable validate kubesphere endpoint, %v", err)) api.HandleBadRequest(response, request, fmt.Errorf("unable validate kubesphere endpoint, %v", err))
@@ -286,7 +283,7 @@ func loadKubeConfigFromBytes(kubeconfig []byte) (*rest.Config, error) {
} }
// validateKubeSphereAPIServer uses version api to check the accessibility // validateKubeSphereAPIServer uses version api to check the accessibility
// If ksEndpoint is empty, use // If kubesphere apiserver endpoint is not provided, use kube-apiserver proxy instead
func validateKubeSphereAPIServer(ksEndpoint string, kubeconfig []byte) (*version.Info, error) { func validateKubeSphereAPIServer(ksEndpoint string, kubeconfig []byte) (*version.Info, error) {
if len(ksEndpoint) == 0 && len(kubeconfig) == 0 { if len(ksEndpoint) == 0 && len(kubeconfig) == 0 {
return nil, fmt.Errorf("neither kubesphere api endpoint nor kubeconfig was provided") return nil, fmt.Errorf("neither kubesphere api endpoint nor kubeconfig was provided")

View File

@@ -89,9 +89,10 @@ spec:
- --name=gondor - --name=gondor
- --token=randomtoken - --token=randomtoken
- --proxy-server=http://139.198.121.121:8080 - --proxy-server=http://139.198.121.121:8080
- --keepalive=30s - --keepalive=10s
- --kubesphere-service=ks-apiserver.kubesphere-system.svc:80 - --kubesphere-service=ks-apiserver.kubesphere-system.svc:80
- --kubernetes-service=kubernetes.default.svc:443 - --kubernetes-service=kubernetes.default.svc:443
- --v=0
image: kubesphere/tower:v1.0 image: kubesphere/tower:v1.0
name: agent name: agent
resources: resources:

View File

@@ -30,7 +30,7 @@ func AddToContainer(container *restful.Container,
webservice.Route(webservice.GET("/clusters/{cluster}/agent/deployment"). webservice.Route(webservice.GET("/clusters/{cluster}/agent/deployment").
Doc("Return deployment yaml for cluster agent."). Doc("Return deployment yaml for cluster agent.").
Param(webservice.PathParameter("cluster", "Name of the cluster.").Required(true)). Param(webservice.PathParameter("cluster", "Name of the cluster.").Required(true)).
To(h.GenerateAgentDeployment). To(h.generateAgentDeployment).
Returns(http.StatusOK, api.StatusOK, nil)) Returns(http.StatusOK, api.StatusOK, nil))
webservice.Route(webservice.POST("/clusters/validation"). webservice.Route(webservice.POST("/clusters/validation").