Skip to content

Commit

Permalink
Merge pull request #1627 from headlamp-k8s/fix-empty-home
Browse files Browse the repository at this point in the history
Revert "backend: human readable errors for kubeconfig"
  • Loading branch information
joaquimrocha authored Dec 20, 2023
2 parents f61c42b + ce02a2f commit d6fc4d5
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 163 deletions.
16 changes: 8 additions & 8 deletions backend/cmd/headlamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,18 @@ func TestDynamicClusters(t *testing.T) {
{
Name: &newCluster,
Server: &newClusterServer,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
{
Name: &newCluster2,
Server: &newCluster2Server,
InsecureSkipTLSVerify: true,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
{
Name: &newCluster3,
Server: &newCluster3Server,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
},
expectedState: http.StatusCreated,
Expand All @@ -196,12 +196,12 @@ func TestDynamicClusters(t *testing.T) {
{
Name: &newCluster,
Server: &newClusterServer,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
{
Name: &newCluster, // same name will override
Server: &newCluster2Server,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
},
expectedState: http.StatusCreated,
Expand All @@ -213,12 +213,12 @@ func TestDynamicClusters(t *testing.T) {
{
Name: nil,
Server: &newClusterServer,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
{
Name: &newCluster,
Server: nil,
CertificateAuthorityData: []byte("YWJjZGUK"),
CertificateAuthorityData: []byte("abcde"),
},
},
expectedState: http.StatusBadRequest,
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestDynamicClusters(t *testing.T) {
t.Fatal(err)
}

assert.Equal(t, tc.expectedState, r.Code)
assert.Equal(t, r.Code, tc.expectedState)

// Verify if the created cluster matches what we asked to be created
if r.Code == http.StatusCreated {
Expand Down
82 changes: 0 additions & 82 deletions backend/pkg/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"k8s.io/client-go/tools/clientcmd/api"
)

// errorCheck contains a condition and a message.
type errorCheck struct {
Condition func(error) bool
Message string
}

// TODO: Use a different way to avoid name clashes with other clusters.
const InClusterContextName = "main"

Expand Down Expand Up @@ -201,11 +195,6 @@ func LoadContextsFromFile(kubeConfigPath string, source int) ([]Context, error)

config, err := clientcmd.LoadFromFile(kubeConfigPath)
if err != nil {
return nil, handleErrors(err)
}

// Validate kubeconfig
if err := clientcmd.Validate(*config); err != nil {
return nil, err
}

Expand Down Expand Up @@ -353,74 +342,3 @@ func LoadAndStoreKubeConfigs(kubeConfigStore ContextStore, kubeConfigs string, s

return nil
}

// getErrorChecks returns a list of error checks.
// These are generally the errors when config is not loaded properly.
func getErrorChecks() []errorCheck {
return []errorCheck{
{
Condition: func(e error) bool {
return strings.Contains(e.Error(), "illegal base64 data")
},
Message: "invalid client certificate or client key or certificate authority data in kubeconfig",
},
{
Condition: func(e error) bool {
return strings.Contains(e.Error(), "client-certificate-data")
},
Message: "invalid client certificate data in kubeconfig",
},
{
Condition: func(e error) bool {
return strings.Contains(e.Error(), "client-key-data")
},
Message: "invalid client key data in kubeconfig",
},
{
Condition: func(e error) bool {
return strings.Contains(e.Error(), "certificate-authority-data")
},
Message: "invalid certificate authority data in kubeconfig",
},
{
Condition: func(e error) bool {
return (strings.Contains(e.Error(), "client-certificate") &&
!strings.Contains(e.Error(), "client-certificate-data"))
},
Message: "invalid client certificate path in kubeconfig",
},
{
Condition: func(e error) bool {
return (strings.Contains(e.Error(), "client-key") && !strings.Contains(e.Error(), "client-key-data"))
},
Message: "invalid client key path in kubeconfig",
},
{
Condition: func(e error) bool {
return (strings.Contains(e.Error(), "certificate-authority") &&
!strings.Contains(e.Error(), "certificate-authority-data"))
},
Message: "invalid certificate authority path in kubeconfig",
},
// Add more checks as needed
}
}

// handleErrors handles errors based on the given error checks.
// It returns a single error message with all the error messages.
func handleErrors(err error) error {
var errorMessages []string

errorChecks := getErrorChecks()
for _, check := range errorChecks {
if check.Condition(err) {
errorMessages = append(errorMessages, check.Message)
}
}

if len(errorMessages) > 0 {
return fmt.Errorf("[%s]", strings.Join(errorMessages, ", "))
}

return err
}
16 changes: 0 additions & 16 deletions backend/pkg/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ func TestLoadContextsFromKubeConfigFile(t *testing.T) {
_, err := kubeconfig.LoadContextsFromFile(kubeConfigFile, kubeconfig.KubeConfig)
require.Error(t, err)
})

t.Run("invalid_file without any context", func(t *testing.T) {
kubeConfigFile := "./test_data/invalid_kubeconfig_without_context"

_, err := kubeconfig.LoadContextsFromFile(kubeConfigFile, kubeconfig.KubeConfig)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid configuration: context was not found")
})

t.Run("invalid_file client cert data value", func(t *testing.T) {
kubeConfigFile := "./test_data/invalid_kubeconfig"

_, err := kubeconfig.LoadContextsFromFile(kubeConfigFile, kubeconfig.KubeConfig)
require.Error(t, err)
require.Contains(t, err.Error(), "[invalid certificate authority data in kubeconfig]")
})
}

func TestContext(t *testing.T) {
Expand Down
28 changes: 0 additions & 28 deletions backend/pkg/kubeconfig/test_data/invalid_kubeconfig

This file was deleted.

This file was deleted.

0 comments on commit d6fc4d5

Please sign in to comment.