From fc695103eebf585caa64cb300e16435115630e4f Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Mon, 11 Sep 2017 17:09:35 +0800 Subject: [PATCH] charts, k8s: use logging package instead --- charts/genesis.go | 5 +++-- charts/static_nodes.go | 6 +++--- k8s/blockchain.go | 12 ++++++------ k8s/blockchain_example.go | 11 +++++++---- k8s/ethereum_example.go | 11 +++++------ k8s/utils.go | 12 +++++++----- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/charts/genesis.go b/charts/genesis.go index 2abb5e5d..96881423 100644 --- a/charts/genesis.go +++ b/charts/genesis.go @@ -18,7 +18,6 @@ package charts import ( "fmt" - "log" "os" "path/filepath" "strings" @@ -26,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/getamis/istanbul-tools/genesis" + "github.com/getamis/istanbul-tools/log" ) type GenesisChart struct { @@ -40,7 +40,8 @@ func NewGenesisChart(addrs []common.Address, gasLimit uint64) *GenesisChart { genesisPath := filepath.Join(chartPath, ".genesis") err := os.MkdirAll(genesisPath, 0700) if err != nil { - log.Fatal(err) + log.Error("Failed to create dir", "dir", genesisPath, "err", err) + return nil } chart := &GenesisChart{ diff --git a/charts/static_nodes.go b/charts/static_nodes.go index fcd4c09a..81f870be 100644 --- a/charts/static_nodes.go +++ b/charts/static_nodes.go @@ -18,12 +18,12 @@ package charts import ( "fmt" - "log" "os" "path/filepath" "strings" "github.com/getamis/istanbul-tools/common" + "github.com/getamis/istanbul-tools/log" ) type StaticNodesChart struct { @@ -38,11 +38,11 @@ func NewStaticNodesChart(nodekeys []string, ipAddrs []string) *StaticNodesChart staticNodesPath := filepath.Join(chartPath, ".static-nodes") err := os.MkdirAll(staticNodesPath, 0700) if err != nil { - log.Fatal(err) + log.Error("Failed to create dir", "dir", staticNodesPath, "err", err) } if len(nodekeys) != len(ipAddrs) { - log.Println("The number of nodekeys and the number of IP address should be equal") + log.Error("The number of nodekeys and the number of IP address should be equal", "nodekeys", len(nodekeys), "ips", len(ipAddrs)) return nil } diff --git a/k8s/blockchain.go b/k8s/blockchain.go index 40b3a9b5..5b55552a 100644 --- a/k8s/blockchain.go +++ b/k8s/blockchain.go @@ -19,12 +19,12 @@ package k8s import ( "errors" "fmt" - "log" "time" "github.com/getamis/istanbul-tools/charts" istcommon "github.com/getamis/istanbul-tools/common" "github.com/getamis/istanbul-tools/container" + "github.com/getamis/istanbul-tools/log" ) func NewBlockchain(numOfValidators int, gaslimit uint64, options ...Option) (bc *blockchain) { @@ -37,11 +37,11 @@ func NewBlockchain(numOfValidators int, gaslimit uint64, options ...Option) (bc } if err := bc.genesis.Install(false); err != nil { - log.Println(err) + log.Error("Failed to install genesis chart", "err", err) return nil } if err := bc.staticNodes.Install(false); err != nil { - log.Println(err) + log.Error("Failed to install static nodes chart", "err", err) bc.genesis.Uninstall() return nil } @@ -79,11 +79,11 @@ func (bc *blockchain) EnsureConsensusWorking(geths []container.Ethereum, t time. } func (bc *blockchain) AddValidators(numOfValidators int) ([]container.Ethereum, error) { - return nil, errors.New("Unsupported") + return nil, errors.New("unsupported") } func (bc *blockchain) RemoveValidators(candidates []container.Ethereum, processingTime time.Duration) error { - return errors.New("Unsupported") + return errors.New("unsupported") } func (bc *blockchain) Start(strong bool) error { @@ -108,7 +108,7 @@ func (bc *blockchain) Validators() []container.Ethereum { } func (bc *blockchain) CreateNodes(num int, options ...Option) (nodes []container.Ethereum, err error) { - return nil, errors.New("Unsupported") + return nil, errors.New("unsupported") } // ---------------------------------------------------------------------------- diff --git a/k8s/blockchain_example.go b/k8s/blockchain_example.go index e7258653..fe9d00a0 100644 --- a/k8s/blockchain_example.go +++ b/k8s/blockchain_example.go @@ -17,11 +17,12 @@ package k8s import ( - "testing" "time" + + "github.com/getamis/istanbul-tools/log" ) -func ExampleK8SBlockchain(t *testing.T) { +func ExampleK8SBlockchain() { chain := NewBlockchain( 4, 21000*1000, @@ -34,13 +35,15 @@ func ExampleK8SBlockchain(t *testing.T) { err := chain.Start(true) if err != nil { - t.Error(err) + log.Error("Failed to start chain", "err", err) + return } <-time.After(20 * time.Second) err = chain.Stop(false) if err != nil { - t.Error(err) + log.Error("Failed to stop chain", "err", err) + return } } diff --git a/k8s/ethereum_example.go b/k8s/ethereum_example.go index 5dbdd895..c585bdcd 100644 --- a/k8s/ethereum_example.go +++ b/k8s/ethereum_example.go @@ -17,25 +17,24 @@ package k8s import ( - "fmt" - "github.com/getamis/istanbul-tools/charts" "github.com/getamis/istanbul-tools/common" "github.com/getamis/istanbul-tools/genesis" + "github.com/getamis/istanbul-tools/log" ) func ExampleK8SEthereum() { _, nodekeys, addrs := common.GenerateKeys(1) genesisChart := charts.NewGenesisChart(addrs, genesis.InitGasLimit) if err := genesisChart.Install(false); err != nil { - fmt.Println(err) + log.Error("Failed to install genesis chart", "err", err) return } defer genesisChart.Uninstall() staticNodesChart := charts.NewStaticNodesChart(nodekeys, common.GenerateIPs(len(nodekeys))) if err := staticNodesChart.Install(false); err != nil { - fmt.Println(err) + log.Error("Failed to install static nodes chart", "err", err) return } defer staticNodesChart.Uninstall() @@ -52,13 +51,13 @@ func ExampleK8SEthereum() { err := geth.Start() if err != nil { - fmt.Println(err) + log.Error("Failed to start geth", "err", err) return } err = geth.Stop() if err != nil { - fmt.Println(err) + log.Error("Failed to stop geth", "err", err) return } } diff --git a/k8s/utils.go b/k8s/utils.go index dc644bd2..05937e16 100644 --- a/k8s/utils.go +++ b/k8s/utils.go @@ -17,7 +17,6 @@ package k8s import ( - "log" "os" "path/filepath" "sync" @@ -26,6 +25,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" + + "github.com/getamis/istanbul-tools/log" ) const ( @@ -38,19 +39,20 @@ const ( func k8sClient(podName string) *kubernetes.Clientset { config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(os.Getenv("HOME"), ".kube", "config")) if err != nil { - log.Fatalln(err) + log.Error("Failed to create Kubernetes config", "err", err) + return nil } for i := 0; i < healthCheckRetryCount; i++ { client, err := kubernetes.NewForConfig(config) if err != nil { - log.Println(err) + log.Error("Failed to create Kubernetes client from config", "config", config, "err", err) <-time.After(healthCheckRetryDelay) continue } _, err = client.CoreV1().Pods(defaultNamespace).Get(podName, metav1.GetOptions{}) if err != nil { - log.Println(err) + log.Error("Failed to get pod", "namespace", defaultNamespace, "pod", podName, "err", err) <-time.After(healthCheckRetryDelay) continue } else { @@ -58,7 +60,7 @@ func k8sClient(podName string) *kubernetes.Clientset { } } - log.Fatalln("Failed to retrieve kubernetes client") + log.Error("Failed to retrieve kubernetes client") return nil }