Skip to content

Commit

Permalink
fix create context
Browse files Browse the repository at this point in the history
  • Loading branch information
pow-devops2020 committed May 31, 2020
1 parent 82c5fe2 commit fd405f4
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": ["list"]
"args": ["ctx", "testnonadmin", "--namespace", "nonadmin2", "--role", "nonadmin"]
}
]
}
73 changes: 51 additions & 22 deletions pkg/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package actions

import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/docker/machine/libmachine/log"
"github.com/urfave/cli"
"github.com/verchol/kubectx/pkg/config"
configtools "github.com/verchol/kubectx/pkg/config"
Expand All @@ -17,9 +19,8 @@ import (
func Commands(app *cli.App) {
app.Commands = []cli.Command{
{
Name: "version",
Aliases: []string{"version"},
Usage: "create a new config",
Name: "version",
Usage: "create a new config",
Action: func(c *cli.Context) {
color.Green("cli info is %v\n", app.Version)
},
Expand Down Expand Up @@ -67,7 +68,7 @@ func Commands(app *cli.App) {
Usage: "used to validate cluster connectivity",
},
},
Action: configtools.HandleSetContext,
Action: ListActionsHandler,
},
{
Name: "newcontext",
Expand Down Expand Up @@ -112,10 +113,24 @@ func Commands(app *cli.App) {
}, cli.BoolFlag{
Name: "cache",
Usage: "take information from local cache file ",
}, cli.BoolFlag{
Name: "verbose",
Required: false,
Usage: "enable printing extra debuggin information",
},
}
app.Before = func(c *cli.Context) error {
if c.GlobalBool("verbose") {
log.SetOutput(os.Stdout)
} else {
log.SetOutput(ioutil.Discard)
}

return nil
}
app.Action = configtools.DefaultActionHandler

app.Action = configtools.SetContextAction
//app.Action = configtools.HandleSetContext
}

//CreqteContextAction ...
Expand All @@ -128,18 +143,16 @@ func CreateContextAction(c *cli.Context) error {
ns := c.String("namespace")
role := c.String("role")

if ns != "" {
role = "nonAdmin"
}

if role == "" || role == "admin" {
if role == "Admin" || role == "admin" {
role = "Admin"
} else {
role = "nonAdmin"
}
//_ := c.Bool("create")
//_ := c.String("permission")

log.Info(contextName, ns)
log.Info("verbs %v\n", c.StringSlice("verbs"))
log.Printf(contextName, ns)
log.Printf("verbs %v\n", c.StringSlice("verbs"))

config, err := configtools.LoadConfig()
if err != nil {
Expand All @@ -162,7 +175,7 @@ func TestClusterAction(c *cli.Context) error {
context = c.Args().First()
}

fmt.Printf("context is %v\n", context)
log.Printf("context is %v\n", context)

config, err := config.LoadConfig()
if err != nil {
Expand All @@ -189,16 +202,17 @@ func TestClusterAction(c *cli.Context) error {
}
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
panic(err)
log.Fatal(err)

}

works, err := configtools.ValidateCluster(waitingPeriod, namespace, clientSet)
_, validationErr := configtools.ValidateCluster(waitingPeriod, namespace, clientSet)
Red := color.New(color.FgRed).SprintFunc()
Green := color.New(color.FgGreen).SprintFunc()

cache, err := configtools.NewLocalCache()
cache, cacheerr := configtools.NewLocalCache()

if err != nil {
if cacheerr != nil {
panic(err)
}
kubeContext := configtools.KubeContext{}
Expand All @@ -213,22 +227,37 @@ func TestClusterAction(c *cli.Context) error {
}
kubeContext.Status = configtools.ClusterAvailable

if !works {
fmt.Printf("context %v is not available \n%v : %v\n", Green(context), Red("error:"), err)
if validationErr != nil {
fmt.Printf("context %v is not available \n%v : %v\n", Red(context), Red("error:"), validationErr)
kubeContext.Status = configtools.ClusterNotAvailable

} else {
fmt.Printf("context %v is available\n", Green(context))
kubeContext.Status = configtools.ClusterAvailable
}

fmt.Printf("adding entry %v %v\n", kubeContext.Name, kubeContext.Status)
log.Printf("adding entry %v %v\n", kubeContext.Name, kubeContext.Status)

cache.AddEntry(context, &kubeContext)
cache.Flash()
return err
}
func ListActionsHandler(c *cli.Context) error {

config, err := config.LoadConfig()
if err != nil {
return err
}

flags := configtools.FlagOptions{Validate: false, Cache: c.GlobalBool("cache")}
if flags.Cache {
configtools.ListContextFromCache()
return nil
}
configtools.ListContexts(config, flags)

return nil
}
func SetContextNamespaceAction(c *cli.Context) error {

ns := c.Args().First()
Expand All @@ -248,7 +277,7 @@ func SetContextNamespaceAction(c *cli.Context) error {

err = clientcmd.ModifyConfig(config.ConfigAccess(), rawConfig, false)

fmt.Printf("context %v was updated with namespace %v \n", currentCtxName, ns)
log.Printf("context %v was updated with namespace %v \n", currentCtxName, ns)

return err
}
8 changes: 4 additions & 4 deletions pkg/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
)
Expand Down Expand Up @@ -76,13 +76,13 @@ func NewLocalCache(cacheOpt ...string) (*LocalCache, error) {
//Create ..
func (c *LocalCache) Create() error {
_, err := os.Stat(c.cachePath)
fmt.Printf("cachefile %v err %v", c.cachePath, err)
log.Printf("cachefile %v err %v", c.cachePath, err)

if err == nil || !os.IsNotExist(err) {
return err
}

fmt.Printf("\ncreating cache file %v\n\n", LocalCacheFile)
log.Printf("\ncreating cache file %v\n\n", LocalCacheFile)
err = os.MkdirAll(path.Dir(LocalCacheFile), os.ModePerm)
if err != nil {
return err
Expand All @@ -107,7 +107,7 @@ func (c *LocalCache) loadCache() (map[string]*KubeContext, error) {
}

func (c *LocalCache) Flash() (*LocalCache, error) {
fmt.Printf("flashing cache\n")
log.Printf("flashing cache\n")
bytes, err := json.Marshal(c.cache)

if err != nil {
Expand Down
Loading

0 comments on commit fd405f4

Please sign in to comment.