Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pow-devops2020 committed May 20, 2020
2 parents 5c467d2 + f990256 commit 0a78ed4
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"mode": "auto",
"program": "${fileDirname}",
"env": {},
<<<<<<< HEAD
"args": ["list", "kind-weave"]
=======
"args": ["list"]
//"args": ["test", "--timeout", "20", "helm3demo"]
>>>>>>> f990256f609702d8198bb5802e442218e5574adb
}
]
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/docker/machine v0.16.2
github.com/fatih/color v1.8.0
github.com/imdario/mergo v0.3.8 // indirect
github.com/kyokomi/emoji v2.2.2+incompatible // indirect
github.com/micro/cli v0.2.0 // indirect
github.com/olekukonko/tablewriter v0.0.4
github.com/urfave/cli v1.22.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kyokomi/emoji v2.2.2+incompatible h1:gaQFbK2+uSxOR4iGZprJAbpmtqTrHhSdgOyIMD6Oidc=
github.com/kyokomi/emoji v2.2.2+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA=
github.com/labbsr0x/bindman-dns-webhook v1.0.2/go.mod h1:p6b+VCXIR8NYKpDr8/dg1HKfQoRHCdcsROXKvmoehKA=
github.com/labbsr0x/goh v1.0.1/go.mod h1:8K2UhVoaWXcCU7Lxoa2omWnC8gyW8px7/lmO61c027w=
github.com/linode/linodego v0.10.0/go.mod h1:cziNP7pbvE3mXIPneHj0oRY8L1WtGEIKlZ8LANE4eXA=
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var (

func init() {
app.Name = "Create Kubeconfig CLI"
app.Usage = "An example how to create kube config"
app.Author = "verchol"
app.Usage = "kubernetes context management utility"
app.Author = "Oleg Verhovsky"
app.Version = version
}

Expand All @@ -34,7 +34,7 @@ func main() {
err := app.Run(os.Args)

if err != nil {
os.Exit(1)
panic(err)
}
return

Expand Down
44 changes: 35 additions & 9 deletions pkg/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ func Commands(app *cli.App) {
Name: "validate",
Usage: "used to validate cluster connectivity",
},
cli.BoolFlag{
Name: "nocache",
Usage: "used to reinitiate conncectivity status",
},
},
Action: configtools.HandleSetContext,
},
Expand Down Expand Up @@ -103,8 +99,8 @@ func Commands(app *cli.App) {
Name: "validate",
Usage: "used to validate cluster connectivity",
}, cli.BoolFlag{
Name: "nocache",
Usage: "used to reinitiate conncectivity status",
Name: "cache",
Usage: "take information from local cache file ",
},
}

Expand Down Expand Up @@ -173,6 +169,12 @@ func CreateContextAction(c *cli.Context) error {
func TestClusterAction(c *cli.Context) error {
context := c.String("context")

if context == "" {
context = c.Args().First()
}

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

config, err := config.LoadConfig()
if err != nil {
panic(err)
Expand Down Expand Up @@ -204,12 +206,36 @@ func TestClusterAction(c *cli.Context) error {
works, err := configtools.ValidateCluster(waitingPeriod, namespace, clientSet)
Red := color.New(color.FgRed).SprintFunc()
Green := color.New(color.FgGreen).SprintFunc()

cache, err := configtools.NewLocalCache()

if err != nil {
panic(err)
}
kubeContext := configtools.KubeContext{}
kubeContext.Name = context
ns, _, _ := tempConfig.Namespace()
kubeContext.Namespace = ns
rawConfig, _ := config.RawConfig()
auth := rawConfig.Contexts[context].AuthInfo
authProvider := rawConfig.AuthInfos[auth].AuthProvider
if authProvider != nil {
kubeContext.AuthProvider = authProvider.Name
}
kubeContext.Status = configtools.ClusterAvailable

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

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

fmt.Printf("context %v is available\n", Green(context))
fmt.Printf("adding entry %v %v\n", kubeContext.Name, kubeContext.Status)

return nil
cache.AddEntry(context, &kubeContext)
cache.Flash()
return err
}
141 changes: 141 additions & 0 deletions pkg/config/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package config

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
)

//Cache ..
type ClusterStatus int

const (
ClusterAvailable ClusterStatus = 1
ClusterNotAvailable ClusterStatus = 2
ClusterNotTested ClusterStatus = 0
)

type KubeContext struct {
Name string
Namespace string
Status ClusterStatus
AuthProvider string
LastUpdated string
CurrentContext bool
}
type Cache interface {
Create() error
//Empty() error
//Delete() error
AddEntry(entry string, c *KubeContext)
GetEntry(entry string) (*KubeContext, error)
Reset() bool
}

const LocalCacheFileName = ".status-test-cache"

var LocalCacheFile string

const LocalCacheSize = 100

type LocalCache struct {
cachePath string
cache map[string]*KubeContext
}

func init() {
homedir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
LocalCacheFile = path.Join(homedir, ".kubecli", LocalCacheFileName)
}

//NewLocalCache ...
func NewLocalCache(cacheOpt ...string) (*LocalCache, error) {
var cachePath string
if len(cacheOpt) == 0 || cacheOpt[0] == string("") {
cachePath = LocalCacheFile
} else {
cachePath = cacheOpt[0]
}

c := &LocalCache{cachePath: cachePath}
_, err := c.loadCache()
if err != nil {
return c, err
}
//c.cache = cache
return c, nil
}

//Create ..
func (c *LocalCache) Create() error {
_, err := os.Stat(c.cachePath)
fmt.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)
_, err = os.Create(LocalCacheFile)

return err
}
func (c *LocalCache) loadCache() (map[string]*KubeContext, error) {
err := c.Create()
if err != nil {
panic(err)
}
cache := make(map[string]*KubeContext, LocalCacheSize)
bytes, err := ioutil.ReadFile(LocalCacheFile)
json.Unmarshal(bytes, &cache)
if err != nil {
return nil, err
}
c.cache = cache
return cache, nil
}

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

if err != nil {
panic(err)
}
err = ioutil.WriteFile(c.cachePath, bytes, 0644)

return c, err
}
func (c *LocalCache) AddEntry(entry string, ctx *KubeContext) *LocalCache {

c.cache[entry] = ctx

return c
}

func (c *LocalCache) GetEntry(entry string) (*KubeContext, error) {
cacheObj, err := c.loadCache()
if err != nil {
return nil, err
}
kubeContext, ok := cacheObj[entry]
if !ok {
err = errors.New("missing key")
}

return kubeContext, err
}

//Reset ...
func (c *LocalCache) Reset() bool {
c.cache = make(map[string]*KubeContext, LocalCacheSize)
_, err := c.Flash()

return err != nil
}
Loading

0 comments on commit 0a78ed4

Please sign in to comment.