From 792e2c58d671f39130ac028ab7ca647873fdbdd6 Mon Sep 17 00:00:00 2001 From: Dan Ramich Date: Tue, 10 Nov 2020 09:03:30 -0700 Subject: [PATCH] Reverting to 9ac2118268dbaf99b596df4017291cd698debd06 Removing new flag added to token command --- cmd/kubectl_token.go | 43 +++++++++++++------------------------------ main_test.go | 7 ------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/cmd/kubectl_token.go b/cmd/kubectl_token.go index 1b79d329..f445339e 100644 --- a/cmd/kubectl_token.go +++ b/cmd/kubectl_token.go @@ -17,7 +17,6 @@ import ( url2 "net/url" "os" "os/signal" - "path" "path/filepath" "strings" "time" @@ -110,11 +109,6 @@ func CredentialCommand() cli.Command { Name: "skip-verify", Usage: "Skip verification of the CACerts presented by the Server", }, - cli.StringFlag{ - Name: "cache-dir", - Usage: "The absolute path for the kubectl-token .cache directory", - EnvVar: "RANCHER_CACHE_DIR", - }, }, Subcommands: []cli.Command{ cli.Command{ @@ -147,13 +141,8 @@ func runCredential(ctx *cli.Context) error { } clusterID := ctx.String("cluster") - dir, err := getCacheDir(ctx) - if err != nil { - return err - } - cachedCredName := fmt.Sprintf("%s_%s", userID, clusterID) - cachedCred, err := loadCachedCredential(cachedCredName, dir) + cachedCred, err := loadCachedCredential(cachedCredName) if err != nil { customPrint(fmt.Errorf("LoadToken: %v", err)) } @@ -175,7 +164,7 @@ func runCredential(ctx *cli.Context) error { return err } - if err := cacheCredential(newCred, fmt.Sprintf("%s_%s", userID, clusterID), dir); err != nil { + if err := cacheCredential(newCred, fmt.Sprintf("%s_%s", userID, clusterID)); err != nil { customPrint(fmt.Errorf("CacheToken: %v", err)) } @@ -186,7 +175,7 @@ func deleteCachedCredential(ctx *cli.Context) error { if len(ctx.Args()) == 0 { return cli.ShowSubcommandHelp(ctx) } - dir, err := getCacheDir(ctx) + dir, err := os.Getwd() if err != nil { return err } @@ -206,7 +195,11 @@ func deleteCachedCredential(ctx *cli.Context) error { return nil } -func loadCachedCredential(key, dir string) (*ExecCredential, error) { +func loadCachedCredential(key string) (*ExecCredential, error) { + dir, err := os.Getwd() + if err != nil { + return nil, err + } cachePath := filepath.Join(dir, kubeConfigCache, fmt.Sprintf("%s%s", key, cachedFileExt)) f, err := os.Open(cachePath) if err != nil { @@ -229,12 +222,15 @@ func loadCachedCredential(key, dir string) (*ExecCredential, error) { return execCredential, nil } -func cacheCredential(cred *ExecCredential, id, dir string) error { +func cacheCredential(cred *ExecCredential, id string) error { // cache only if valid if cred.Status.Token == "" { return nil } - + dir, err := os.Getwd() + if err != nil { + return err + } cachePathDir := filepath.Join(dir, kubeConfigCache) if err := os.MkdirAll(cachePathDir, os.FileMode(0700)); err != nil { return err @@ -525,19 +521,6 @@ func generateKey() (string, error) { return string(token), nil } -func getCacheDir(ctx *cli.Context) (string, error) { - p := ctx.String("cache-dir") - if p == "" { - return os.Getwd() - } - - if !path.IsAbs(p) { - return "", fmt.Errorf("cache dir requires an absolute path") - } - - return p, nil -} - func getTLSConfig(input *LoginInput) (*tls.Config, error) { config := &tls.Config{} if input.skipVerify || input.caCerts == "" { diff --git a/main_test.go b/main_test.go index ece005e3..5bf6730a 100644 --- a/main_test.go +++ b/main_test.go @@ -27,7 +27,6 @@ func (m *MainTestSuite) TestParseArgs(c *check.C) { {"rancher", "run", "--debug", "-f=b"}, {"rancher", "run", "--debug", "-=b"}, {"rancher", "run", "--debug", "-"}, - {"rancher", "token", "--cache-dir=/path/to/cache"}, } r0, err := parseArgs(input[0]) if err != nil { @@ -62,10 +61,4 @@ func (m *MainTestSuite) TestParseArgs(c *check.C) { c.Fatal(err) } c.Assert(r5, check.DeepEquals, []string{"rancher", "run", "--debug", "-"}) - - r6, err := parseArgs(input[6]) - if err != nil { - c.Fatal(err) - } - c.Assert(r6, check.DeepEquals, []string{"rancher", "token", "--cache-dir=/path/to/cache"}) }