Skip to content

Commit 3b178a4

Browse files
Merge pull request #77 from containerum/chkit-v3
Chkit v3
2 parents a8a468a + 35a5a7c commit 3b178a4

109 files changed

Lines changed: 4229 additions & 1159 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gopkg.lock

Lines changed: 33 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
non-go = true
3131

3232
[[constraint]]
33-
version = "~0.9.0"
33+
version = "~0.10.0"
3434
name = "git.containerum.net/ch/kube-client"
3535

36-
[[constraint]]
37-
name = "k8s.io/apimachinery"
38-
version = "kubernetes-1.9.3"
36+
#[[constraint]]
37+
# name = "k8s.io/apimachinery"
38+
# version = "kubernetes-1.9.3"
3939

4040
[[constraint]]
4141
branch = "master"

Makefile

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
.PHONY: genkey build test clean release single_release
22

3-
CMD_DIR:=./cmd/chkit
3+
CMD_DIR:=cmd/chkit
4+
CLI_DIR:=pkg/cli
45
#get current package, assuming it`s in GOPATH sources
5-
PACKAGE := $(shell go list -f '{{.ImportPath}}' $(CMD_DIR))
6-
6+
PACKAGE := $(shell go list -f '{{.ImportPath}}' ./$(CLI_DIR))
7+
PACKAGE := $(PACKAGE:%/$(CLI_DIR)=%)
78
SIGNING_KEY_DIR:=~/.config/containerum/.chkit-sign
89
PRIVATE_KEY_FILE:=privkey.pem
910
PUBLIC_KEY_FILE:=pubkey.pem
@@ -17,11 +18,11 @@ VERSION?=$(LATEST_TAG:v%=%)
1718
# make directory and store path to variable
1819
BUILDS_DIR:=$(PWD)/build
1920
EXECUTABLE:=chkit
20-
DEV_LDFLAGS=-X $(PACKAGE)/cmd.Version=$(VERSION) \
21-
-X $(PACKAGE)/pkg/cli/mode.API_ADDR=$(CONTAINERUM_API)
22-
RELEASE_LDFLAGS=-X $(PACKAGE)/cmd.Version=$(VERSION) \
21+
DEV_LDFLAGS=-X '$(PACKAGE)/$(CLI_DIR)/mode.API_ADDR=$(CONTAINERUM_API)' \
22+
-X '$(PACKAGE)/$(CLI_DIR).VERSION=v$(VERSION)'
23+
RELEASE_LDFLAGS=-X $(PACKAGE)/$(CLI_DIR).VERSION=v$(VERSION) \
2324
-X $(PACKAGE)/pkg/update.PublicKeyB64=\'$(shell base64 -w 0 $(SIGNING_KEY_DIR)/$(PUBLIC_KEY_FILE))\'\
24-
-X $(PACKAGE)/pkg/cli/mode.API_ADDR=$(CONTAINERUM_API)
25+
-X $(PACKAGE)/$(CLI_DIR)/mode.API_ADDR=$(CONTAINERUM_API)
2526

2627
genkey:
2728
@echo "Generating private/public ECDSA keys to sign"
@@ -33,7 +34,7 @@ genkey:
3334
# go has build artifacts caching so soruce tracking not needed
3435
build:
3536
@echo "Building chkit for current OS/architecture, without signing"
36-
@go build $(CMD_DIR) -v -ldflags="$(RELEASE_LDFLAGS)" -o $(BUILDS_DIR)/$(EXECUTABLE)
37+
@go build -v -ldflags="$(RELEASE_LDFLAGS)" -o $(BUILDS_DIR)/$(EXECUTABLE) ./$(CMD_DIR)
3738

3839
test:
3940
@echo "Running tests"
@@ -63,8 +64,8 @@ $(eval ifeq ($(1),windows)
6364
else
6465
temp_executable=$(temp_build_dir)/$(EXECUTABLE)
6566
endif)
66-
@echo go build -tags="dev" -ldflags="$(RELEASE_LDFLAGS)" -v -o $(temp_executable)
67-
@GOOS=$(1) GOARCH=$(2) go build -tags="dev" -ldflags="$(RELEASE_LDFLAGS)" -v -o $(temp_executable)
67+
@echo go build -tags="dev" -ldflags="$(RELEASE_LDFLAGS)" -v -o $(temp_executable) ./$(CMD_DIR)
68+
@GOOS=$(1) GOARCH=$(2) go build -tags="dev" -ldflags="$(RELEASE_LDFLAGS)" -v -o $(temp_executable) ./$(CMD_DIR)
6869
@$(call create_checksum,$(temp_executable))
6970
@$(call create_signature,$(temp_executable))
7071
$(eval ifeq ($(1),windows)
@@ -89,9 +90,10 @@ single_release:
8990
dev:
9091
$(eval VERSION=$(LATEST_TAG:v%=%)+dev)
9192
@echo building $(VERSION)
92-
@go build -v --tags="dev" -ldflags="$(DEV_LDFLAGS)" $(CMD_DIR)
93+
@echo $(PACKAGE)
94+
go build -v --tags="dev" --ldflags="$(DEV_LDFLAGS)" ./$(CMD_DIR)
9395

9496
mock:
9597
$(eval VERSION=$(LATEST_TAG:v%=%)+mock)
9698
@echo building $(VERSION)
97-
@go build -v -tags="dev mock" -ldflags="$(DEV_LDFLAGS)"
99+
@go build -v --tags="dev mock" -ldflags="$(DEV_LDFLAGS)" ./$(CMD_DIR)

cmd/chkit/chkit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func main() {
11-
if err := cli.Root.Execute(); err != nil {
11+
if err := cli.Run(); err != nil {
1212
fmt.Printf("%v\n", err)
1313
os.Exit(1)
1414
}

pkg/cli/clisetup/client.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ package clisetup
33
import (
44
"github.com/containerum/chkit/pkg/cli/mode"
55
"github.com/containerum/chkit/pkg/client"
6-
. "github.com/containerum/chkit/pkg/context"
6+
"github.com/containerum/chkit/pkg/context"
77
"github.com/sirupsen/logrus"
88
)
99

10-
func SetupClient() error {
10+
func SetupClient(ctx *context.Context, debugRequests bool) error {
1111
var err error
1212
if mode.DEBUG && !mode.MOCK {
13-
logrus.WithField("operation", "SetupClient").Debugf("Using test API: %q", GlobalContext.Client.APIaddr)
14-
if Config.DebugRequests {
13+
logrus.WithField("operation", "SetupClient").Debugf("Using test API: %q", ctx.Client.APIaddr)
14+
if debugRequests {
1515
logrus.Debugf("verbose requests logs")
16-
GlobalContext.Client.Log = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
16+
ctx.Client.Log = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
1717
}
18-
err = GlobalContext.Client.Init(chClient.WithTestAPI)
18+
err = ctx.Client.Init(chClient.WithTestAPI)
1919
} else if mode.DEBUG && mode.MOCK {
2020
logrus.Debugf("Using mock API")
21-
err = GlobalContext.Client.Init(chClient.WithMock)
21+
err = ctx.Client.Init(chClient.WithMock)
2222
} else {
23-
logrus.Debugf("Using production API: %v", GlobalContext.Client.APIaddr)
24-
err = GlobalContext.Client.Init(chClient.WithCommonAPI)
23+
logrus.Debugf("Using production API: %v", ctx.Client.APIaddr)
24+
err = ctx.Client.Init(chClient.WithCommonAPI)
2525
}
2626
if err != nil {
2727
return err

pkg/cli/clisetup/config.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@ import (
1111
"github.com/sirupsen/logrus"
1212
)
1313

14-
func SetupConfig() error {
15-
config := context.GlobalContext.Client.Config
16-
config.Fingerprint = fingerpint.Fingerprint()
17-
tokens, err := configuration.LoadTokens()
14+
func SetupConfig(ctx *context.Context) error {
15+
ctx.Client.Fingerprint = fingerpint.Fingerprint()
16+
tokens, err := configuration.LoadTokens(ctx)
1817
if err != nil && !os.IsNotExist(err) {
1918
return ErrUnableToLoadTokens.Wrap(err)
2019
} else if os.IsNotExist(err) {
21-
if err = configuration.SaveTokens(model.Tokens{}); err != nil {
20+
if err = configuration.SaveTokens(ctx, model.Tokens{}); err != nil {
2221
logrus.WithError(ErrUnableToSaveTokens.Wrap(err)).Errorf("unable to setup config")
2322
return ErrUnableToSaveTokens.Wrap(err)
2423
}
2524
}
26-
config.Tokens = tokens
27-
if _, err := url.Parse(config.APIaddr); err != nil {
28-
logrus.Debugf("invalid API url: %q", config.APIaddr)
25+
ctx.Client.Tokens = tokens
26+
if _, err := url.Parse(ctx.Client.APIaddr); err != nil {
27+
logrus.Debugf("invalid API url: %q", ctx.Client.APIaddr)
2928
return ErrInvalidAPIurl.Wrap(err)
3029
}
31-
if config.Password == "" || config.Username == "" {
30+
if ctx.Client.Password == "" || ctx.Client.Username == "" {
3231
logrus.Debugf("invalid username or pass")
3332
return ErrInvalidUserInfo
3433
}
35-
context.GlobalContext.Client.Config = config
3634
return nil
3735
}

pkg/cli/clisetup/defaultns.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package clisetup
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/containerum/chkit/pkg/context"
7+
"github.com/containerum/chkit/pkg/util/activekit"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
func GetDefaultNS(ctx *context.Context, force bool) error {
12+
logrus.Debugf("getting user namespaces list")
13+
list, err := ctx.Client.GetNamespaceList()
14+
if err != nil {
15+
logrus.WithError(err).Errorf("unable to get user namespace list")
16+
fmt.Printf("Unable to get default namespace\n")
17+
return err
18+
}
19+
if len(list) == 0 {
20+
fmt.Printf("You have no namespaces!\n")
21+
} else if force {
22+
ctx.Namespace = list[0].Label
23+
return nil
24+
} else {
25+
var menu []*activekit.MenuItem
26+
for _, ns := range list {
27+
menu = append(menu, &activekit.MenuItem{
28+
Label: ns.Label,
29+
Action: func(ns string) func() error {
30+
return func() error {
31+
ctx.Namespace = ns
32+
ctx.Changed = true
33+
return nil
34+
}
35+
}(ns.Label),
36+
})
37+
}
38+
_, err := (&activekit.Menu{
39+
Title: "Select default namespace",
40+
Items: menu,
41+
}).Run()
42+
return err
43+
}
44+
return nil
45+
}
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package login
1+
package clisetup
22

33
import (
44
"bufio"
@@ -7,36 +7,47 @@ import (
77
"strings"
88
"syscall"
99

10+
"github.com/containerum/chkit/pkg/chkitErrors"
1011
"github.com/containerum/chkit/pkg/context"
1112
"golang.org/x/crypto/ssh/terminal"
1213
)
1314

14-
func Login() error {
15+
var (
16+
// ErrUnableToReadPassword -- unable to read password
17+
ErrUnableToReadPassword chkitErrors.Err = "unable to read password"
18+
// ErrUnableToReadUsername -- unable to read username
19+
ErrUnableToReadUsername chkitErrors.Err = "unable to read username"
20+
// ErrInvalidPassword -- invalid password
21+
ErrInvalidPassword chkitErrors.Err = "invalid password"
22+
// ErrInvalidUsername -- invalid username
23+
ErrInvalidUsername chkitErrors.Err = "invalid username"
24+
)
25+
26+
func InteractiveLogin(ctx *context.Context) error {
1527
var err error
1628
var username, pass string
1729

18-
if strings.TrimSpace(context.GlobalContext.Client.Username) == "" {
30+
if strings.TrimSpace(ctx.Client.Username) == "" {
1931
username, err = readLogin()
2032
if err != nil {
2133
return err
2234
}
2335
if strings.TrimSpace(username) == "" {
2436
return ErrInvalidUsername
2537
}
26-
context.GlobalContext.Client.Username = username
38+
ctx.Client.Username = username
2739
}
2840

29-
if strings.TrimSpace(context.GlobalContext.Client.Password) == "" {
41+
if strings.TrimSpace(ctx.Client.Password) == "" {
3042
pass, err = readPassword()
3143
if err != nil {
3244
return err
3345
}
3446
if strings.TrimSpace(pass) == "" {
3547
return ErrInvalidPassword
3648
}
37-
context.GlobalContext.Client.Password = pass
49+
ctx.Client.Password = pass
3850
}
39-
4051
return nil
4152
}
4253

pkg/cli/clisetup/logs.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package clisetup
2+
3+
import (
4+
"os"
5+
"path"
6+
"time"
7+
8+
"github.com/containerum/chkit/pkg/configdir"
9+
"github.com/containerum/chkit/pkg/configuration"
10+
"github.com/containerum/chkit/pkg/context"
11+
"github.com/sirupsen/logrus"
12+
)
13+
14+
func SetupLogs(ctx *context.Context) error {
15+
logrus.SetLevel(logrus.DebugLevel)
16+
logrus.SetFormatter(&logrus.TextFormatter{
17+
FullTimestamp: true,
18+
TimestampFormat: time.RFC1123,
19+
})
20+
logFile := path.Join(configdir.LogDir(), configuration.LogFileName())
21+
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
22+
if err != nil {
23+
logrus.Fatalf("error while creating log file: %v", err)
24+
}
25+
logrus.SetOutput(file)
26+
return nil
27+
}

0 commit comments

Comments
 (0)