Skip to content

Commit

Permalink
Enable kubectl version lower than 1.22 to work though without trigger…
Browse files Browse the repository at this point in the history
…ing exit under devicelogin mode (#152)

Co-authored-by: Jing Gao <jing@JingMS>
  • Loading branch information
everjing and Jing Gao authored Nov 18, 2022
1 parent ed605ad commit 9cfbe19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/token/execCredentialPlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"time"

"github.com/Azure/go-autorest/autorest/adal"
Expand Down Expand Up @@ -37,7 +38,9 @@ func New(o *Options) (ExecCredentialPlugin, error) {
if err := json.Unmarshal([]byte(env), &execCredential); err != nil {
return nil, fmt.Errorf("cannot convert to ExecCredential: %w", err)
}
if !execCredential.Spec.Interactive && o.LoginMethod == DeviceCodeLogin {
klog.V(10).Infof("KUBERNETES_EXEC_INFO is: %s", env)

if !strings.Contains(env, `"spec":{}`) && !execCredential.Spec.Interactive && o.LoginMethod == DeviceCodeLogin {
return nil, fmt.Errorf("devicelogin is not supported if interactiveMode is 'never'")
}
}
Expand Down
22 changes: 21 additions & 1 deletion pkg/token/execCredentialPlugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestDeviceloginAndNonInteractive(t *testing.T) {
}
}

func TestKUBERNETES_EXEC_INFOIsEmpty(t *testing.T) {
func TestKUBERNETES_EXEC_INFO(t *testing.T) {
testData := []struct {
name string
execInfoEnvTest string
Expand All @@ -189,6 +189,26 @@ func TestKUBERNETES_EXEC_INFOIsEmpty(t *testing.T) {
TenantID: "tenantID",
},
},
{
name: "KUBERNETES_EXEC_INFO.spec is empty for apiVersion of v1beta1",
execInfoEnvTest: `{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1beta1","spec":{}}`,
options: Options{
LoginMethod: DeviceCodeLogin,
ClientID: "clientID",
ServerID: "serverID",
TenantID: "tenantID",
},
},
{
name: "KUBERNETES_EXEC_INFO.spec is empty for apiVersion of v1",
execInfoEnvTest: `{"kind":"ExecCredential","apiVersion":"client.authentication.k8s.io/v1","spec":{}}`,
options: Options{
LoginMethod: DeviceCodeLogin,
ClientID: "clientID",
ServerID: "serverID",
TenantID: "tenantID",
},
},
}

for _, data := range testData {
Expand Down

0 comments on commit 9cfbe19

Please sign in to comment.