charts, k8s: use logging package instead

This commit is contained in:
Alan Chen 2017-09-11 17:09:35 +08:00
parent a32379213c
commit fc695103ee
6 changed files with 31 additions and 26 deletions

View File

@ -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{

View File

@ -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
}

View File

@ -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")
}
// ----------------------------------------------------------------------------

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}