-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer apiVersion from
KUBERNETES_EXEC_INFO
environment variable (#1162
) * Infer apiVersion from KUBERNETES_EXEC_INFO * Test client.authentication.k8s.io/v1 * Set --exec-interactive-mode * Set --exec-interactive-mode=Never * Fix comments
- Loading branch information
Showing
10 changed files
with
304 additions
and
57 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
mocks/github.com/int128/kubelogin/pkg/credentialplugin/reader_mock/mock_Interface.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package reader provides a loader for the credential plugin. | ||
package reader | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/google/wire" | ||
"github.com/int128/kubelogin/pkg/credentialplugin" | ||
"k8s.io/client-go/pkg/apis/clientauthentication" | ||
) | ||
|
||
var Set = wire.NewSet( | ||
wire.Struct(new(Reader), "*"), | ||
wire.Bind(new(Interface), new(*Reader)), | ||
) | ||
|
||
type Interface interface { | ||
Read() (credentialplugin.Input, error) | ||
} | ||
|
||
type Reader struct{} | ||
|
||
// Read parses the environment variable KUBERNETES_EXEC_INFO. | ||
// If the environment variable is not given by kubectl, Read returns a zero value. | ||
func (r Reader) Read() (credentialplugin.Input, error) { | ||
execInfo := os.Getenv("KUBERNETES_EXEC_INFO") | ||
if execInfo == "" { | ||
return credentialplugin.Input{}, nil | ||
} | ||
var execCredential clientauthentication.ExecCredential | ||
if err := json.Unmarshal([]byte(execInfo), &execCredential); err != nil { | ||
return credentialplugin.Input{}, fmt.Errorf("invalid KUBERNETES_EXEC_INFO: %w", err) | ||
} | ||
return credentialplugin.Input{ | ||
ClientAuthenticationAPIVersion: execCredential.APIVersion, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package reader | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/int128/kubelogin/pkg/credentialplugin" | ||
) | ||
|
||
func TestReader_Read(t *testing.T) { | ||
var reader Reader | ||
|
||
t.Run("KUBERNETES_EXEC_INFO is empty", func(t *testing.T) { | ||
input, err := reader.Read() | ||
if err != nil { | ||
t.Errorf("Read returned error: %v", err) | ||
} | ||
want := credentialplugin.Input{} | ||
if diff := cmp.Diff(want, input); diff != "" { | ||
t.Errorf("input mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
t.Run("KUBERNETES_EXEC_INFO is invalid JSON", func(t *testing.T) { | ||
t.Setenv("KUBERNETES_EXEC_INFO", "invalid") | ||
_, err := reader.Read() | ||
if err == nil { | ||
t.Errorf("Read wants error but no error") | ||
} | ||
}) | ||
t.Run("KUBERNETES_EXEC_INFO is v1", func(t *testing.T) { | ||
t.Setenv( | ||
"KUBERNETES_EXEC_INFO", | ||
`{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1","spec":{"interactive":true}}`, | ||
) | ||
input, err := reader.Read() | ||
if err != nil { | ||
t.Errorf("Read returned error: %v", err) | ||
} | ||
want := credentialplugin.Input{ClientAuthenticationAPIVersion: "client.authentication.k8s.io/v1"} | ||
if diff := cmp.Diff(want, input); diff != "" { | ||
t.Errorf("input mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.