未验证 提交 98f44fac 编写于 作者: Z zryfish 提交者: GitHub

fix generation bug (#2043)

上级 933207d2
...@@ -309,6 +309,16 @@ func (c *ClusterController) syncCluster(key string) error { ...@@ -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.KubernetesAPIEndpoint = fmt.Sprintf("https://%s:%d", service.Spec.ClusterIP, kubernetesPort)
cluster.Spec.Connection.KubeSphereAPIEndpoint = fmt.Sprintf("http://%s:%d", service.Spec.ClusterIP, kubespherePort) 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) { if !reflect.DeepEqual(oldCluster.Spec, cluster.Spec) {
cluster, err = c.clusterClient.Update(cluster) cluster, err = c.clusterClient.Update(cluster)
if err != nil { if err != nil {
......
...@@ -23,6 +23,8 @@ const ( ...@@ -23,6 +23,8 @@ const (
defaultAgentImage = "kubesphere/tower:v1.0" defaultAgentImage = "kubesphere/tower:v1.0"
) )
var ErrClusterConnectionIsNotProxy = fmt.Errorf("cluster is not using proxy connection")
type handler struct { type handler struct {
serviceLister v1.ServiceLister serviceLister v1.ServiceLister
clusterLister clusterlister.ClusterLister clusterLister clusterlister.ClusterLister
...@@ -62,6 +64,11 @@ func (h *handler) GenerateAgentDeployment(request *restful.Request, response *re ...@@ -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 // use service ingress address
if len(h.proxyAddress) == 0 { if len(h.proxyAddress) == 0 {
err = h.populateProxyAddress() err = h.populateProxyAddress()
...@@ -126,6 +133,10 @@ func (h *handler) populateProxyAddress() error { ...@@ -126,6 +133,10 @@ func (h *handler) populateProxyAddress() error {
func (h *handler) generateDefaultDeployment(cluster *v1alpha1.Cluster, w io.Writer) error { func (h *handler) generateDefaultDeployment(cluster *v1alpha1.Cluster, w io.Writer) error {
if cluster.Spec.Connection.Type == v1alpha1.ConnectionTypeDirect {
return ErrClusterConnectionIsNotProxy
}
agent := appsv1.Deployment{ agent := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{ TypeMeta: metav1.TypeMeta{
Kind: "Deployment", Kind: "Deployment",
......
...@@ -98,23 +98,57 @@ func TestGeranteAgentDeployment(t *testing.T) { ...@@ -98,23 +98,57 @@ func TestGeranteAgentDeployment(t *testing.T) {
informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Informer().GetIndexer().Add(service) informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Informer().GetIndexer().Add(service)
informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster) informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster)
h := NewHandler(informersFactory.KubernetesSharedInformerFactory().Core().V1().Services().Lister(), directConnectionCluster := cluster.DeepCopy()
informersFactory.KubeSphereSharedInformerFactory().Cluster().V1alpha1().Clusters().Lister(), directConnectionCluster.Spec.Connection.Type = v1alpha1.ConnectionTypeDirect
proxyService,
"", var testCases = []struct {
agentImage) description string
expectingError bool
var buf bytes.Buffer expectedError error
cluster *v1alpha1.Cluster
err := h.populateProxyAddress() expected string
if err != nil { }{
t.Error(err) {
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) for _, testCase := range testCases {
if diff := cmp.Diff(buf.String(), expected); len(diff) != 0 {
t.Error(diff) 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) { func TestInnerGenerateAgentDeployment(t *testing.T) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册