From 1b26e3ffd2ddbab49a4fdd86951ff8ad1868a68f Mon Sep 17 00:00:00 2001 From: chilianyi Date: Wed, 28 Feb 2024 16:46:52 +0800 Subject: [PATCH] Fix nil in log (#1035) --- controllers/argocd/multi-cluster-controller.go | 2 +- controllers/argocd/types.go | 12 ++++++------ controllers/argocd/types_test.go | 4 +++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/controllers/argocd/multi-cluster-controller.go b/controllers/argocd/multi-cluster-controller.go index 455bb304..4ef8c721 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 63069fdb..86681c95 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 { diff --git a/controllers/argocd/types_test.go b/controllers/argocd/types_test.go index d4724ed8..cef9101d 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) }) } }