From 266ba9ffdeeeeba8022ce94ae4e725d996c29363 Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Fri, 7 Apr 2017 16:19:50 -0400 Subject: [PATCH] validateClusterInfo: use clientcmdapi.NewCluster() Change validateClusterInfo to use clientcmdapi.NewCluster() instead of clientcmdapi.Cluster{} when comparing against the passed in clusterInfo. clusterInfo most likely will be a combination of clientcmdapi.NewCluster() merged with potential overrides. This is necessary because otherwise, the DeepEqual between what is supposed to be an empty Cluster and clusterInfo will fail, resulting in an error that doesn't allow fall-through to checking for in-cluster configuration. --- .../client-go/tools/clientcmd/validation.go | 3 ++- .../client-go/tools/clientcmd/validation_test.go | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/client-go/tools/clientcmd/validation.go b/staging/src/k8s.io/client-go/tools/clientcmd/validation.go index ceeeb042e8e..18cf2680a55 100644 --- a/staging/src/k8s.io/client-go/tools/clientcmd/validation.go +++ b/staging/src/k8s.io/client-go/tools/clientcmd/validation.go @@ -167,7 +167,8 @@ func ConfirmUsable(config clientcmdapi.Config, passedContextName string) error { func validateClusterInfo(clusterName string, clusterInfo clientcmdapi.Cluster) []error { validationErrors := make([]error, 0) - if reflect.DeepEqual(clientcmdapi.Cluster{}, clusterInfo) { + emptyCluster := clientcmdapi.NewCluster() + if reflect.DeepEqual(*emptyCluster, clusterInfo) { return []error{ErrEmptyCluster} } diff --git a/staging/src/k8s.io/client-go/tools/clientcmd/validation_test.go b/staging/src/k8s.io/client-go/tools/clientcmd/validation_test.go index ab7dc268519..6441f148376 100644 --- a/staging/src/k8s.io/client-go/tools/clientcmd/validation_test.go +++ b/staging/src/k8s.io/client-go/tools/clientcmd/validation_test.go @@ -187,7 +187,7 @@ func TestValidateEmptyContext(t *testing.T) { func TestValidateEmptyClusterInfo(t *testing.T) { config := clientcmdapi.NewConfig() - config.Clusters["empty"] = &clientcmdapi.Cluster{} + config.Clusters["empty"] = clientcmdapi.NewCluster() test := configValidationTest{ config: config, expectedErrorSubstring: []string{"cluster has no server defined"}, @@ -196,6 +196,19 @@ func TestValidateEmptyClusterInfo(t *testing.T) { test.testCluster("empty", t) test.testConfig(t) } + +func TestValidateClusterInfoErrEmptyCluster(t *testing.T) { + cluster := clientcmdapi.NewCluster() + errs := validateClusterInfo("", *cluster) + + if len(errs) != 1 { + t.Fatalf("unexpected errors: %v", errs) + } + if errs[0] != ErrEmptyCluster { + t.Errorf("unexpected error: %v", errs[0]) + } +} + func TestValidateMissingCAFileClusterInfo(t *testing.T) { config := clientcmdapi.NewConfig() config.Clusters["missing ca"] = &clientcmdapi.Cluster{