Repository: kubesphere/ks-devops
Pull request #474 (2022-02-23 06:52:45)

Add an API for getting the argo cluster list
<!-- Thanks for sending a pull request! Here are some tips for you:

1. If you want **faster** PR reviews, read how: https://github.com/kubesphere/community/blob/master/developer-guide/development/the-pr-author-guide-to-getting-through-code-review.md
2. In case you want to know how your PR got reviewed, read: https://github.com/kubesphere/community/blob/master/developer-guide/development/code-review-guide.md
3. Here are some coding conventions followed by the KubeSphere community: https://github.com/kubesphere/community/blob/master/developer-guide/development/coding-conventions.md
4. Additional open-source best practice: https://github.com/LinuxSuRen/open-source-best-practice
-->

### What type of PR is this?
<!-- 
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind design
/kind chore

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->
/kind feature

### What this PR does / why we need it:
This is part of #63 

### Which issue(s) this PR fixes:
<!--
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
Please leave it or change # to be None if there is no corresponding issue that exists
-->
Fixes #

### Special notes for reviewers:
<!--
You can use the following command to let the DevOps SIG members help you to review your PR.
/cc @kubesphere/sig-devops 
And please avoid cc any individual.
-->
Please check the following list before waiting reviewers:

- [ ] Already committed the CRD files to [the Helm Chart](https://github.com/kubesphere-sigs/ks-devops-helm-chart/) if you created some new CRDs
- [ ] Already [added the permission](https://github.com/kubesphere/ks-installer/blob/9e063b085a0e43fdb3d0d9e3e7f4149146f14b9c/roles/ks-core/prepare/files/ks-init/role-templates.yaml) for the new API
  see also #473
- [ ] Already added the RBAC markers for the new controllers

Please see the following screenshot of the tests:

![image](https://user-images.githubusercontent.com/1450685/155273382-0ece9408-e171-4d10-8149-1dc8c9319302.png)

### Does this PR introduce a user-facing change??
<!--
If no, just write "None" in the release-note block below.
If yes, a release note is required:
Enter your extended-release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

Please keep the note be same as your PR title if you believe it should be in the release notes.
-->
```release-note
None
```


ks-ci-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubesphere/ks-devops/pull/474#" title="Author self-approved">LinuxSuRen</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubesphere%2Fks-devops).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubesphere/ks-devops/blob/master/OWNERS)~~ [LinuxSuRen]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) Report
> Merging [#474](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (58e43f9) into [master](https://codecov.io/gh/kubesphere/ks-devops/commit/12018e7d66089999bbd016cb1a3cd380ab4eeda0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (12018e7) will **increase** coverage by `0.06%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/kubesphere/ks-devops/pull/474/graphs/tree.svg?width=650&height=150&src=pr&token=qSK4RBbyNg&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)

```diff
@@            Coverage Diff             @@
##           master     #474      +/-   ##
==========================================
+ Coverage   76.89%   76.96%   +0.06%     
==========================================
  Files         130      132       +2     
  Lines       22556    22676     +120     
==========================================
+ Hits        17345    17453     +108     
- Misses       5024     5034      +10     
- Partials      187      189       +2     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `76.96% <100.00%> (+0.06%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) | Coverage Δ | |
|---|---|---|
| [pkg/kapis/devops/v1alpha1/argocd/handler.go](https://codecov.io/gh/kubesphere/ks-devops/pull/474/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL2thcGlzL2Rldm9wcy92MWFscGhhMS9hcmdvY2QvaGFuZGxlci5nbw==) | `100.00% <100.00%> (ø)` | |
| [pkg/kapis/devops/v1alpha1/argocd/route.go](https://codecov.io/gh/kubesphere/ks-devops/pull/474/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL2thcGlzL2Rldm9wcy92MWFscGhhMS9hcmdvY2Qvcm91dGUuZ28=) | `100.00% <100.00%> (ø)` | |
| [controllers/argocd/types.go](https://codecov.io/gh/kubesphere/ks-devops/pull/474/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvYXJnb2NkL3R5cGVzLmdv) | `100.00% <0.00%> (ø)` | |
| [controllers/argocd/multi-cluster-controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/474/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvYXJnb2NkL211bHRpLWNsdXN0ZXItY29udHJvbGxlci5nbw==) | `87.50% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Last update [794724a...58e43f9](https://codecov.io/gh/kubesphere/ks-devops/pull/474?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere).

ks-ci-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubesphere/ks-devops/pull/474#pullrequestreview-890730227" title="LGTM">JohnNiang</a>*, *<a href="https://github.com/kubesphere/ks-devops/pull/474#" title="Author self-approved">LinuxSuRen</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubesphere%2Fks-devops).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubesphere/ks-devops/blob/master/OWNERS)~~ [LinuxSuRen]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/kapis/devops/v1alpha1/argocd/handler.go b/pkg/kapis/devops/v1alpha1/argocd/handler.go
index 2c30c6a..5aff249 100644
--- a/pkg/kapis/devops/v1alpha1/argocd/handler.go
+++ b/pkg/kapis/devops/v1alpha1/argocd/handler.go
@@ -18,6 +18,7 @@ package argocd
 import (
 	"context"
 	"github.com/emicklei/go-restful"
+	v1 "k8s.io/api/core/v1"
 	"k8s.io/apimachinery/pkg/types"
 	"kubesphere.io/devops/pkg/api/devops/v1alpha1"
 	"kubesphere.io/devops/pkg/kapis"
@@ -29,7 +30,7 @@ func (h *handler) applicationList(req *restful.Request, res *restful.Response) {
 	namespace := getPathParameter(req, kapisv1alpha1.DevopsPathParameter)
 
 	applicationList := &v1alpha1.ApplicationList{}
-	err := h.genericClient.List(context.Background(), applicationList, client.InNamespace(namespace))
+	err := h.List(context.Background(), applicationList, client.InNamespace(namespace))
 	response(req, res, applicationList, err)
 }
 
@@ -38,7 +39,7 @@ func (h *handler) getApplication(req *restful.Request, res *restful.Response) {
 	name := getPathParameter(req, pathParameterApplication)
 
 	application := &v1alpha1.Application{}
-	err := h.genericClient.Get(context.Background(), types.NamespacedName{
+	err := h.Get(context.Background(), types.NamespacedName{
 		Namespace: namespace,
 		Name:      name,
 	}, application)
@@ -50,12 +51,12 @@ func (h *handler) delApplication(req *restful.Request, res *restful.Response) {
 	name := getPathParameter(req, pathParameterApplication)
 
 	application := &v1alpha1.Application{}
-	err := h.genericClient.Get(context.Background(), types.NamespacedName{
+	err := h.Get(context.Background(), types.NamespacedName{
 		Namespace: namespace,
 		Name:      name,
 	}, application)
 	if err == nil {
-		err = h.genericClient.Delete(context.Background(), application)
+		err = h.Delete(context.Background(), application)
 	}
 	response(req, res, application, err)
 }
@@ -67,7 +68,7 @@ func (h *handler) createApplication(req *restful.Request, res *restful.Response)
 	application := &v1alpha1.Application{}
 	if err = req.ReadEntity(application); err == nil {
 		application.Namespace = namespace
-		err = h.genericClient.Create(context.Background(), application)
+		err = h.Create(context.Background(), application)
 	}
 
 	response(req, res, application, err)
@@ -78,18 +79,45 @@ func (h *handler) updateApplication(req *restful.Request, res *restful.Response)
 	name := getPathParameter(req, pathParameterApplication)
 
 	application := &v1alpha1.Application{}
-	err := h.genericClient.Get(context.Background(), types.NamespacedName{
+	err := h.Get(context.Background(), types.NamespacedName{
 		Namespace: namespace,
 		Name:      name,
 	}, application)
 	if err == nil {
 		if err = req.ReadEntity(application); err == nil {
-			err = h.genericClient.Update(context.Background(), application)
+			err = h.Update(context.Background(), application)
 		}
 	}
 	response(req, res, application, err)
 }
 
+func (h *handler) getClusters(req *restful.Request, res *restful.Response) {
+	ctx := context.Background()
+
+	secrets := &v1.SecretList{}
+	err := h.List(ctx, secrets, client.MatchingLabels{
+		"argocd.argoproj.io/secret-type": "cluster",
+	})
+
+	argoClusters := make([]v1alpha1.ApplicationDestination, 0)
+	if err == nil && secrets != nil && len(secrets.Items) > 0 {
+		for i := range secrets.Items {
+			secret := secrets.Items[i]
+
+			name := string(secret.Data["name"])
+			server := string(secret.Data["server"])
+
+			if name != "" && server != "" {
+				argoClusters = append(argoClusters, v1alpha1.ApplicationDestination{
+					Server: server,
+					Name:   name,
+				})
+			}
+		}
+	}
+	response(req, res, argoClusters, err)
+}
+
 func getPathParameter(req *restful.Request, param *restful.Parameter) string {
 	return req.PathParameter(param.Data().Name)
 }
@@ -103,11 +131,11 @@ func response(req *restful.Request, res *restful.Response, object interface{}, e
 }
 
 type handler struct {
-	genericClient client.Client
+	client.Client
 }
 
 func newHandler(options *kapisv1alpha1.Options) *handler {
 	return &handler{
-		genericClient: options.GenericClient,
+		Client: options.GenericClient,
 	}
 }
diff --git a/pkg/kapis/devops/v1alpha1/argocd/route.go b/pkg/kapis/devops/v1alpha1/argocd/route.go
index 724f256..3bca682 100644
--- a/pkg/kapis/devops/v1alpha1/argocd/route.go
+++ b/pkg/kapis/devops/v1alpha1/argocd/route.go
@@ -65,4 +65,9 @@ func RegisterRoutes(service *restful.WebService, options *kapisv1alpha1.Options)
 		Reads(v1alpha1.Application{}).
 		Doc("Update a particular application").
 		Returns(http.StatusOK, api.StatusOK, v1alpha1.Application{}))
+
+	service.Route(service.GET("/clusters").
+		To(handler.getClusters).
+		Doc("Get the clusters list").
+		Returns(http.StatusOK, api.StatusOK, []v1alpha1.ApplicationDestination{}))
 }


Test patch:
diff --git a/pkg/kapis/devops/v1alpha1/argocd/route_test.go b/pkg/kapis/devops/v1alpha1/argocd/route_test.go
index dfbb381..eae8de9 100644
--- a/pkg/kapis/devops/v1alpha1/argocd/route_test.go
+++ b/pkg/kapis/devops/v1alpha1/argocd/route_test.go
@@ -21,6 +21,7 @@ import (
 	"github.com/emicklei/go-restful"
 	"github.com/stretchr/testify/assert"
 	"io"
+	v1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"kubesphere.io/devops/pkg/api/devops/v1alpha1"
@@ -54,7 +55,7 @@ func TestRegisterRoutes(t *testing.T) {
 			options: &kapisv1alpha1.Options{GenericClient: fake.NewFakeClientWithScheme(schema)},
 		},
 		verify: func(t *testing.T, service *restful.WebService) {
-			assert.Equal(t, 5, len(service.Routes()))
+			assert.Equal(t, 6, len(service.Routes()))
 		},
 	}}
 	for _, tt := range tests {
@@ -68,6 +69,8 @@ func TestRegisterRoutes(t *testing.T) {
 func TestAPIs(t *testing.T) {
 	schema, err := v1alpha1.SchemeBuilder.Register().Build()
 	assert.Nil(t, err)
+	err = v1.SchemeBuilder.AddToScheme(schema)
+	assert.Nil(t, err)
 
 	app := v1alpha1.Application{
 		ObjectMeta: metav1.ObjectMeta{
@@ -76,6 +79,38 @@ func TestAPIs(t *testing.T) {
 		},
 	}
 
+	nonArgoClusterSecret := v1.Secret{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "non-argo-cluster",
+			Namespace: "ns",
+		},
+	}
+	invalidArgoClusterSecret := v1.Secret{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "invalid-argo-cluster",
+			Namespace: "ns",
+			Labels: map[string]string{
+				"argocd.argoproj.io/secret-type": "cluster",
+			},
+		},
+		Data: map[string][]byte{
+			"server": []byte("server"),
+		},
+	}
+	validArgoClusterSecret := v1.Secret{
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      "argo-cluster",
+			Namespace: "ns",
+			Labels: map[string]string{
+				"argocd.argoproj.io/secret-type": "cluster",
+			},
+		},
+		Data: map[string][]byte{
+			"name":   []byte("name"),
+			"server": []byte("server"),
+		},
+	}
+
 	type request struct {
 		method string
 		uri    string
@@ -229,6 +264,44 @@ func TestAPIs(t *testing.T) {
 			assert.Equal(t, "good", project)
 			assert.Nil(t, err)
 		},
+	}, {
+		name: "get clusters, no expected data",
+		request: request{
+			method: http.MethodGet,
+			uri:    "/clusters",
+		},
+		k8sclient:    fake.NewFakeClientWithScheme(schema, nonArgoClusterSecret.DeepCopy()),
+		responseCode: http.StatusOK,
+		verify: func(t *testing.T, body []byte) {
+			assert.Equal(t, "[]", string(body))
+		},
+	}, {
+		name: "get clusters, have invalid data",
+		request: request{
+			method: http.MethodGet,
+			uri:    "/clusters",
+		},
+		k8sclient:    fake.NewFakeClientWithScheme(schema, invalidArgoClusterSecret.DeepCopy()),
+		responseCode: http.StatusOK,
+		verify: func(t *testing.T, body []byte) {
+			assert.Equal(t, "[]", string(body))
+		},
+	}, {
+		name: "get clusters, have the expected data",
+		request: request{
+			method: http.MethodGet,
+			uri:    "/clusters",
+		},
+		k8sclient:    fake.NewFakeClientWithScheme(schema, validArgoClusterSecret.DeepCopy()),
+		responseCode: http.StatusOK,
+		verify: func(t *testing.T, body []byte) {
+			assert.Equal(t, `[
+ {
+  "server": "server",
+  "name": "name"
+ }
+]`, string(body))
+		},
 	}}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {