diff --git a/pkg/controller/cluster/cluster_controller.go b/pkg/controller/cluster/cluster_controller.go index 3d0e7044f..fe0200da8 100644 --- a/pkg/controller/cluster/cluster_controller.go +++ b/pkg/controller/cluster/cluster_controller.go @@ -309,6 +309,16 @@ func (c *ClusterController) syncCluster(key string) error { cluster.Spec.Connection.KubernetesAPIEndpoint = fmt.Sprintf("https://%s:%d", service.Spec.ClusterIP, kubernetesPort) cluster.Spec.Connection.KubeSphereAPIEndpoint = fmt.Sprintf("http://%s:%d", service.Spec.ClusterIP, kubespherePort) + initializedCondition := clusterv1alpha1.ClusterCondition{ + Type: clusterv1alpha1.ClusterInitialized, + Status: v1.ConditionTrue, + Reason: string(clusterv1alpha1.ClusterInitialized), + Message: "Cluster has been initialized", + LastUpdateTime: metav1.Now(), + LastTransitionTime: metav1.Now(), + } + c.updateClusterCondition(cluster, initializedCondition) + if !reflect.DeepEqual(oldCluster.Spec, cluster.Spec) { cluster, err = c.clusterClient.Update(cluster) if err != nil { diff --git a/pkg/kapis/cluster/v1alpha1/handler.go b/pkg/kapis/cluster/v1alpha1/handler.go index a0335b40b..f74da7882 100644 --- a/pkg/kapis/cluster/v1alpha1/handler.go +++ b/pkg/kapis/cluster/v1alpha1/handler.go @@ -23,6 +23,8 @@ const ( defaultAgentImage = "kubesphere/tower:v1.0" ) +var ErrClusterConnectionIsNotProxy = fmt.Errorf("cluster is not using proxy connection") + type handler struct { serviceLister v1.ServiceLister clusterLister clusterlister.ClusterLister @@ -62,6 +64,11 @@ func (h *handler) GenerateAgentDeployment(request *restful.Request, response *re } } + if cluster.Spec.Connection.Type != v1alpha1.ConnectionTypeProxy { + api.HandleNotFound(response, request, fmt.Errorf("cluster %s is not using proxy connection", cluster.Name)) + return + } + // use service ingress address if len(h.proxyAddress) == 0 { err = h.populateProxyAddress() @@ -126,6 +133,10 @@ func (h *handler) populateProxyAddress() error { func (h *handler) generateDefaultDeployment(cluster *v1alpha1.Cluster, w io.Writer) error { + if cluster.Spec.Connection.Type == v1alpha1.ConnectionTypeDirect { + return ErrClusterConnectionIsNotProxy + } + agent := appsv1.Deployment{ TypeMeta: metav1.TypeMeta{ Kind: "Deployment", diff --git a/pkg/kapis/cluster/v1alpha1/handler_test.go b/pkg/kapis/cluster/v1alpha1/handler_test.go index cc37e9db3..4467757d4 100644 --- a/pkg/kapis/cluster/v1alpha1/handler_test.go +++ b/pkg/kapis/cluster/v1alpha1/handler_test.go @@ -98,23 +98,57 @@ func TestGeranteAgentDeployment(t *testing.T) { 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) + directConnectionCluster := cluster.DeepCopy() + directConnectionCluster.Spec.Connection.Type = v1alpha1.ConnectionTypeDirect - var buf bytes.Buffer - - err := h.populateProxyAddress() - if err != nil { - t.Error(err) + var testCases = []struct { + description string + expectingError bool + expectedError error + cluster *v1alpha1.Cluster + expected string + }{ + { + description: "test normal case", + expectingError: false, + expected: expected, + cluster: cluster, + }, + { + description: "test direct connection cluster", + expectingError: true, + expectedError: ErrClusterConnectionIsNotProxy, + cluster: directConnectionCluster, + }, } - err = h.generateDefaultDeployment(cluster, &buf) - if diff := cmp.Diff(buf.String(), expected); len(diff) != 0 { - t.Error(diff) + for _, testCase := range testCases { + + t.Run(testCase.description, func(t *testing.T) { + h := NewHandler(informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Lister(), + informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Lister(), + proxyService, + "", + agentImage) + + var buf bytes.Buffer + + err := h.populateProxyAddress() + if err != nil { + t.Error(err) + } + + err = h.generateDefaultDeployment(testCase.cluster, &buf) + if testCase.expectingError { + if err == nil { + t.Fatalf("expecting error %v, got nil", testCase.expectedError) + } else if err != testCase.expectedError { + t.Fatalf("expecting error %v, got %v", testCase.expectedError, err) + } + } + }) } + } func TestInnerGenerateAgentDeployment(t *testing.T) {