Repository: kubesphere/ks-devops
Pull request #1035 (2024-02-28 08:26:50)

Fix nil in log
<!-- 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 feature
/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
-->

### What this PR does / why we need it:

### 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
- [ ] Already added the RBAC markers for the new controllers

### 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
Fix nil in log
```


ks-ci-bot: @chilianyi: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our [release note process](https://git.k8s.io/community/contributors/guide/release-notes.md) to remove it.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
ks-ci-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please ask for approval from **chilianyi** after the PR has been reviewed.

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

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

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

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":["chilianyi"]} -->
yudong2015: /lgtm
/approve
ks-ci-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubesphere/ks-devops/pull/1035#issuecomment-1968496989" title="Approved">yudong2015</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)~~ [yudong2015]

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/controllers/argocd/multi-cluster-controller.go b/controllers/argocd/multi-cluster-controller.go
index 455bb30..4ef8c72 100644
--- a/controllers/argocd/multi-cluster-controller.go
+++ b/controllers/argocd/multi-cluster-controller.go
@@ -155,7 +155,7 @@ func getArgoClusterConfigObject(configText string) (object *ClusterConfig) {
 	rawDecodedconfig, _ := base64.StdEncoding.DecodeString(configText)
 
 	var kubeconfig *config
-	if kubeconfig = parseKubeConfig(rawDecodedconfig); kubeconfig == nil {
+	if kubeconfig, _ = parseKubeConfig(rawDecodedconfig); kubeconfig == nil {
 		return
 	}
 
diff --git a/controllers/argocd/types.go b/controllers/argocd/types.go
index 63069fd..86681c9 100644
--- a/controllers/argocd/types.go
+++ b/controllers/argocd/types.go
@@ -19,7 +19,6 @@ limitations under the License.
 package argocd
 
 import (
-	"fmt"
 	"gopkg.in/yaml.v2"
 )
 
@@ -58,11 +57,12 @@ type TLSClientConfig struct {
 	CAData []byte `json:"caData,omitempty"`
 }
 
-func parseKubeConfig(data []byte) (cfg *config) {
-	cfg = &config{}
-	err := yaml.Unmarshal(data, cfg)
-	fmt.Println(err)
-	return
+func parseKubeConfig(data []byte) (*config, error) {
+	cfg := &config{}
+	if err := yaml.Unmarshal(data, cfg); err != nil {
+		return nil, err
+	}
+	return cfg, nil
 }
 
 type config struct {


Test patch:
diff --git a/controllers/argocd/types_test.go b/controllers/argocd/types_test.go
index d4724ed..cef9101 100644
--- a/controllers/argocd/types_test.go
+++ b/controllers/argocd/types_test.go
@@ -63,7 +63,9 @@ users:
 	}}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			assert.Equalf(t, tt.want, parseKubeConfig(tt.args.data), "parseKubeConfig(%v)", tt.args.data)
+			config, err := parseKubeConfig(tt.args.data)
+			assert.Nil(t, err)
+			assert.Equalf(t, tt.want, config, "parseKubeConfig(%v)", tt.args.data)
 		})
 	}
 }