Commit f1b9c17c authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #44221 from ncdc/fix-validateClusterInfo-empty-cluster-check

Automatic merge from submit-queue (batch tested with PRs 43545, 44293, 44221, 43888) 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. https://github.com/kubernetes/kubernetes/pull/40508 changed `DirectClientConfig.getContext()` to start with a `clientcmdapi.NewCluster()` instead of the zero value for `clientcmdapi.Cluster`. This means that the `Extensions` map in the `Cluster` is initialized instead of `nil`, which breaks the `DeepEqual` test unless you compare `clusterInfo` against an initialized `clientcmdapi.NewCluster()`. cc @smarterclayton @sttts @vjsamuel @liggitt @deads2k @soltysh @fabianofranz @kubernetes/sig-api-machinery-pr-reviews
parents 86715941 266ba9ff
...@@ -167,7 +167,8 @@ func ConfirmUsable(config clientcmdapi.Config, passedContextName string) error { ...@@ -167,7 +167,8 @@ func ConfirmUsable(config clientcmdapi.Config, passedContextName string) error {
func validateClusterInfo(clusterName string, clusterInfo clientcmdapi.Cluster) []error { func validateClusterInfo(clusterName string, clusterInfo clientcmdapi.Cluster) []error {
validationErrors := make([]error, 0) validationErrors := make([]error, 0)
if reflect.DeepEqual(clientcmdapi.Cluster{}, clusterInfo) { emptyCluster := clientcmdapi.NewCluster()
if reflect.DeepEqual(*emptyCluster, clusterInfo) {
return []error{ErrEmptyCluster} return []error{ErrEmptyCluster}
} }
......
...@@ -187,7 +187,7 @@ func TestValidateEmptyContext(t *testing.T) { ...@@ -187,7 +187,7 @@ func TestValidateEmptyContext(t *testing.T) {
func TestValidateEmptyClusterInfo(t *testing.T) { func TestValidateEmptyClusterInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["empty"] = &clientcmdapi.Cluster{} config.Clusters["empty"] = clientcmdapi.NewCluster()
test := configValidationTest{ test := configValidationTest{
config: config, config: config,
expectedErrorSubstring: []string{"cluster has no server defined"}, expectedErrorSubstring: []string{"cluster has no server defined"},
...@@ -196,6 +196,19 @@ func TestValidateEmptyClusterInfo(t *testing.T) { ...@@ -196,6 +196,19 @@ func TestValidateEmptyClusterInfo(t *testing.T) {
test.testCluster("empty", t) test.testCluster("empty", t)
test.testConfig(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) { func TestValidateMissingCAFileClusterInfo(t *testing.T) {
config := clientcmdapi.NewConfig() config := clientcmdapi.NewConfig()
config.Clusters["missing ca"] = &clientcmdapi.Cluster{ config.Clusters["missing ca"] = &clientcmdapi.Cluster{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment