Skip to content

Commit

Permalink
status support
Browse files Browse the repository at this point in the history
  • Loading branch information
verchol committed Apr 16, 2020
1 parent 927939e commit 0b50c2c
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": ["list", "--cache"]
"args": ["list"]
//"args": ["test", "--timeout", "20", "helm3demo"]
}
]
}
4 changes: 4 additions & 0 deletions dist/checksums.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
480f0432084fb7494e88a1a2b74da1be0daab5071d575f48c6e7d9070f4e4393 kubecli_v1.0.9-next_Linux_i386.tar.gz
3ffaf9509cea9520694c6908dbe1e8c4333502fe99a69e61ed4a0bcabb81692b kubecli_v1.0.9-next_Linux_x86_64.tar.gz
3175eba6759ed5ecf9085fe4dcf556e3639a8ff498f34831a9c1985c26bbb361 kubecli_v1.0.9-next_Darwin_x86_64.tar.gz
3a20f62d49bc452ea55a58a5f14fb9676f2d00f3e96e99a6e9b179b951f93302 kubecli_v1.0.9-next_Darwin_i386.tar.gz
105 changes: 105 additions & 0 deletions dist/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
project_name: kubecli
release:
github:
owner: verchol
name: kubecli
name_template: '{{.Tag}}'
brew:
name: kubecli
github:
owner: verchol
name: homebrew-kubecli
commit_author:
name: Oleg Verhovsky
email: [email protected]
folder: Formula
install: bin.install "kubecli"
description: kubecli --help
homepage: http://docs.codefresh.io
scoop:
name: kubecli
commit_author:
name: goreleaserbot
email: [email protected]
url_template: https://github.com/verchol/kubecli/releases/download/{{ .Tag }}/{{
.ArtifactName }}
builds:
- id: kubecli
goos:
- linux
- darwin
goarch:
- amd64
- "386"
goarm:
- "6"
targets:
- linux_amd64
- linux_386
- darwin_amd64
- darwin_386
main: .
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
binary: kubecli
env:
- CGO_ENABLED=0
lang: go
archives:
- id: default
builds:
- kubecli
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm
}}v{{ .Arm }}{{ end }}'
replacements:
"386": i386
amd64: x86_64
darwin: Darwin
linux: Linux
windows: Windows
format: tar.gz
files:
- licence*
- LICENCE*
- license*
- LICENSE*
- readme*
- README*
- changelog*
- CHANGELOG*
nfpm:
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm
}}v{{ .Arm }}{{ end }}'
bindir: /usr/local/bin
snapcraft:
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm
}}v{{ .Arm }}{{ end }}'
snapshot:
name_template: '{{ .Tag }}-next'
checksum:
name_template: checksums.txt
algorithm: sha256
changelog:
filters:
exclude:
- '^docs:'
- '^test:'
sort: asc
dist: dist
sign:
cmd: gpg
args:
- --output
- $signature
- --detach-sig
- $artifact
signature: ${artifact}.sig
artifacts: none
env_files:
github_token: ~/.config/goreleaser/github_token
before:
hooks:
- echo before hook
- go generate ./...
github_urls:
download: https://github.com
Binary file added dist/darwin_386/kubecli
Binary file not shown.
Binary file added dist/darwin_amd64/kubecli
Binary file not shown.
Binary file added dist/kubecli_v1.0.9-next_Darwin_i386.tar.gz
Binary file not shown.
Binary file added dist/kubecli_v1.0.9-next_Darwin_x86_64.tar.gz
Binary file not shown.
Binary file added dist/kubecli_v1.0.9-next_Linux_i386.tar.gz
Binary file not shown.
Binary file added dist/kubecli_v1.0.9-next_Linux_x86_64.tar.gz
Binary file not shown.
Binary file added dist/linux_386/kubecli
Binary file not shown.
Binary file added dist/linux_amd64/kubecli
Binary file not shown.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

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

var app = cli.NewApp()
Expand Down
23 changes: 17 additions & 6 deletions pkg/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,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 @@ -181,7 +187,7 @@ func TestClusterAction(c *cli.Context) error {
Green := color.New(color.FgGreen).SprintFunc()

cache, err := configtools.NewLocalCache()
defer cache.Flash()

if err != nil {
panic(err)
}
Expand All @@ -195,15 +201,20 @@ func TestClusterAction(c *cli.Context) error {
if authProvider != nil {
kubeContext.AuthProvider = authProvider.Name
}
kubeContext.Status = works
kubeContext.Status = configtools.ClusterAvailable

cache.AddEntry(context, &kubeContext)
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
}
28 changes: 18 additions & 10 deletions pkg/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ import (
)

//Cache ..
type ClusterStatus int

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

type KubeContext struct {
Name string
Namespace string
Status bool
AuthProvider string
LastUpdated string
Name string
Namespace string
Status ClusterStatus
AuthProvider string
LastUpdated string
CurrentContext bool
}
type Cache interface {
Create() error
Expand Down Expand Up @@ -66,14 +74,14 @@ func NewLocalCache(cacheOpt ...string) (*LocalCache, error) {

//Create ..
func (c *LocalCache) Create() error {
fileInfo, err := os.Stat(c.cachePath)
_, err := os.Stat(c.cachePath)
fmt.Printf("cachefile %v err %v", c.cachePath, err)

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

fmt.Printf("creating cache file %v", LocalCacheFile)
fmt.Printf("\ncreating cache file %v\n\n", LocalCacheFile)
_, err = os.Create(LocalCacheFile)

return err
Expand All @@ -94,7 +102,7 @@ func (c *LocalCache) loadCache() (map[string]*KubeContext, error) {
}

func (c *LocalCache) Flash() (*LocalCache, error) {

fmt.Printf("flashing cache\n")
bytes, err := json.Marshal(c.cache)

if err != nil {
Expand Down
58 changes: 33 additions & 25 deletions pkg/config/configtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,14 @@ func UpdateCache(data map[string]*KubeContext) error {
if err != nil {
return err
}

for i, _ := range data {
if data[i].Status == ClusterNotTested {
_, ok := c.cache[i]
if ok {
data[i].Status = c.cache[i].Status
}
}
}
c.cache = data
_, err = c.Flash()

Expand All @@ -237,7 +244,6 @@ func ListContexts(config clientcmd.ClientConfig, flags FlagOptions) {
if err != nil {
panic(err)
}
green := color.New(color.FgGreen).SprintFunc()

data := make(map[string]*KubeContext, 100)
headers := []string{"Contexts", "IsAvailable", "AuthProvider"}
Expand All @@ -248,12 +254,6 @@ func ListContexts(config clientcmd.ClientConfig, flags FlagOptions) {

currentContextName := fmt.Sprintf("%v", name)

if rawConfig.CurrentContext == name {
//fmt.Printf("current context %v is active = %v\n", green(name), (pods != -1))
currentContextName = fmt.Sprintf("%v", green(name))

}

cfg, err := clientcmd.NewDefaultClientConfig(rawConfig, &clientcmd.ConfigOverrides{CurrentContext: name}).ClientConfig()
authProvider := "default"

Expand All @@ -268,20 +268,22 @@ func ListContexts(config clientcmd.ClientConfig, flags FlagOptions) {
log.Debug("[auth %v]\n", authProvider)
// auth := fmt.Sprintf("[%s]", authProvider)
pods = 1
var status ClusterStatus
status = ClusterNotTested

if flags.Validate {
pods = testCluster(config, name)
}
var status bool

if pods != -1 {
if pods != -1 {

status = true
} else {

status = false
status = ClusterAvailable
} else {
status = ClusterNotAvailable
}
}
namespace, _, _ := config.Namespace()
kubeContext := &KubeContext{currentContextName, namespace, status, authProvider, time.Now().String()}
isCurrentContext := (rawConfig.CurrentContext == name)
kubeContext := &KubeContext{currentContextName, namespace,
status, authProvider, time.Now().String(), isCurrentContext}
data[currentContextName] = kubeContext

}
Expand All @@ -306,8 +308,8 @@ func (a Contexts) Less(i, j int) bool {
}

func printTable(header []string, data map[string]*KubeContext) {
happyIcon := "\u2714"
sadIcon := "\u2716"
//happyIcon := "\u2714"
//sadIcon := "\u2716"
red := color.New(color.FgRed).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()

Expand All @@ -323,13 +325,19 @@ func printTable(header []string, data map[string]*KubeContext) {
for _, name := range names {
ctx := data[name]
var statusStr string
if ctx.Status {

statusStr = fmt.Sprintf("%s", green(happyIcon))
} else {
statusStr = fmt.Sprintf("%s", red(sadIcon))
contextName := ctx.Name
switch ctx.Status {
case ClusterAvailable:
statusStr = fmt.Sprintf("%s", green("Yes"))
case ClusterNotAvailable:
statusStr = fmt.Sprintf("%s", red("No"))
case ClusterNotTested:
statusStr = "N/A"
}
if ctx.CurrentContext {
contextName = green(contextName)
}
v := []string{ctx.Name, statusStr, ctx.AuthProvider}
v := []string{contextName, statusStr, ctx.AuthProvider}
table.Append(v)
}
table.Render() // Send output
Expand Down

0 comments on commit 0b50c2c

Please sign in to comment.