Skip to content

Commit

Permalink
Merge pull request #1087 from silvin-lubecki/check-kubernetes-config
Browse files Browse the repository at this point in the history
Check allNamespace config value while loading configuration file
  • Loading branch information
thaJeztah authored May 28, 2018
2 parents 537e67d + fb34ffc commit 05f04bb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cli/config/configfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configfile
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (configFile *ConfigFile) LoadFromReader(configData io.Reader) error {
ac.ServerAddress = addr
configFile.AuthConfigs[addr] = ac
}
return nil
return checkKubernetesConfiguration(configFile.Kubernetes)
}

// ContainsAuth returns whether there is authentication configured
Expand Down Expand Up @@ -318,3 +319,17 @@ func (configFile *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig,
func (configFile *ConfigFile) GetFilename() string {
return configFile.Filename
}

func checkKubernetesConfiguration(kubeConfig *KubernetesConfig) error {
if kubeConfig == nil {
return nil
}
switch kubeConfig.AllNamespaces {
case "":
case "enabled":
case "disabled":
default:
return fmt.Errorf("invalid 'kubernetes.allNamespaces' value, should be 'enabled' or 'disabled': %s", kubeConfig.AllNamespaces)
}
return nil
}
42 changes: 42 additions & 0 deletions cli/config/configfile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,45 @@ func TestGetAllCredentialsCredHelperOverridesDefaultStore(t *testing.T) {
assert.Check(t, is.Equal(1, testCredsStore.(*mockNativeStore).GetAllCallCount))
assert.Check(t, is.Equal(0, testCredHelper.(*mockNativeStore).GetAllCallCount))
}

func TestCheckKubernetesConfigurationRaiseAnErrorOnInvalidValue(t *testing.T) {
testCases := []struct {
name string
config *KubernetesConfig
expectError bool
}{
{
"no kubernetes config is valid",
nil,
false,
},
{
"enabled is valid",
&KubernetesConfig{AllNamespaces: "enabled"},
false,
},
{
"disabled is valid",
&KubernetesConfig{AllNamespaces: "disabled"},
false,
},
{
"empty string is valid",
&KubernetesConfig{AllNamespaces: ""},
false,
},
{
"other value is invalid",
&KubernetesConfig{AllNamespaces: "unknown"},
true,
},
}
for _, test := range testCases {
err := checkKubernetesConfiguration(test.config)
if test.expectError {
assert.Assert(t, err != nil, test.name)
} else {
assert.NilError(t, err, test.name)
}
}
}

0 comments on commit 05f04bb

Please sign in to comment.