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

fix generation bug (#2043)

上级 933207d2
......@@ -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 {
......
......@@ -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",
......
......@@ -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)
var buf bytes.Buffer
err := h.populateProxyAddress()
if err != nil {
t.Error(err)
directConnectionCluster := cluster.DeepCopy()
directConnectionCluster.Spec.Connection.Type = v1alpha1.ConnectionTypeDirect
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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册