first version implement certs/genesis passing

This commit is contained in:
Fabio Barone
2021-11-08 13:51:04 -05:00
parent 6666f7a724
commit 1d1dcb26b3
7 changed files with 116 additions and 27 deletions
+2 -1
View File
@@ -20,7 +20,7 @@ RUN go mod download
copy network/ network/
COPY api/ api/
COPY k8s/ k8s/
COPY client/ client/
COPY constants/ constants/
COPY examples/ examples/
# Build
@@ -30,5 +30,6 @@ RUN GOOS=linux GOARCH=amd64 go build -o simplenet ./examples/k8s/main.go
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/base-debian11
COPY --from=builder /workspace/simplenet /
COPY --from=builder /workspace/examples/ /examples/
ENTRYPOINT ["/simplenet"]
+17
View File
@@ -0,0 +1,17 @@
{
"network_config": {
"node_count": 5,
"name": "custom-test-net",
"log_level": "debug"
},
"k8s_config": {
"provide_files": true,
"namespace": "default",
"deployment_spec": "avalanchego-test-validator",
"kind": "Avalanchego",
"api_version": "chain.avax.network/v1alpha1",
"image": "avaplatform/avalanchego",
"tag": "v1.6.4",
"log_level_key": "AVAGO_LOG_LEVEL"
}
}
+40 -15
View File
@@ -2,7 +2,9 @@ package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
@@ -11,7 +13,20 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"
)
const DefaultNetworkTimeout = 120 * time.Second
const (
// DefaultNetworkTimeout to wait network to come up until deemed failed
DefaultNetworkTimeout = 120 * time.Second
confFileName = "/conf.json"
)
// TODO: shouldn't we think of something like Viper for loading config file?
var confPath = os.ExpandEnv("$GOPATH")
type allConfig struct {
NetworkConfig network.Config `json:"network_config"`
K8sConfig k8s.Config `json:"k8s_config"`
}
func main() {
// Create the logger
@@ -27,20 +42,26 @@ func main() {
os.Exit(1)
}
config := network.Config{
NodeCount: 5,
LogLevel: "debug",
Name: "test-val",
ImplSpecificConfig: k8s.Config{
Namespace: "dev",
DeploymentSpec: "avalanchego-test-validator",
Kind: "Avalanchego",
APIVersion: "chain.avax.network/v1alpha1",
Image: "avaplatform/avalanchego",
Tag: "v1.6.4",
LogLevelKey: "AVAGO_LOG_LEVEL",
},
configDir := fmt.Sprintf("%s/src/github.com/ava-labs/avalanche-network-runner-local/examples/k8s", confPath)
if confPath == "" {
configDir = "./examples/k8s"
}
confFile, err := ioutil.ReadFile(configDir + confFileName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var rconfig allConfig
err = json.Unmarshal(confFile, &rconfig)
if err != nil {
os.Exit(1)
}
// TODO maybe do config validation
config := rconfig.NetworkConfig
config.ImplSpecificConfig = rconfig.K8sConfig
timeout, cancel := context.WithTimeout(context.Background(), DefaultNetworkTimeout)
defer cancel()
@@ -50,7 +71,11 @@ func main() {
log.Fatal("Error creating network: %s", err)
os.Exit(1)
}
defer adapter.Stop(timeout)
defer func() {
if err := adapter.Stop(timeout); err != nil {
log.Error("Error stopping network (ignored): %s", err)
}
}()
log.Info("Network created. Booting...")
+1
View File
@@ -13,6 +13,7 @@ require (
github.com/hashicorp/go-hclog v0.15.0 // indirect
github.com/hashicorp/go-plugin v1.4.0 // indirect
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/labstack/gommon v0.3.0
github.com/magiconair/properties v1.8.4 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/oklog/run v1.1.0 // indirect
+3
View File
@@ -496,6 +496,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -743,8 +744,10 @@ github.com/tyler-smith/go-bip39 v1.0.2 h1:+t3w+KwLXO6154GNJY+qUtIxLTmFjfUmpguQT1
github.com/tyler-smith/go-bip39 v1.0.2/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
+48 -7
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
@@ -15,6 +16,7 @@ import (
"github.com/ava-labs/avalanchego/ids"
avagoconst "github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/labstack/gommon/log"
"golang.org/x/sync/errgroup"
k8sapi "github.com/ava-labs/avalanchego-operator/api/v1alpha1"
@@ -60,13 +62,14 @@ type networkImpl struct {
// Config encapsulates kubernetes specific options
type Config struct {
Namespace string // The kubernetes Namespace
DeploymentSpec string // Identifies this network in the cluster
Kind string // Identifies the object Kind for the operator
APIVersion string // The APIVersion of the kubernetes object
Image string // The docker image to use
Tag string // The docker tag to use
LogLevelKey string // The key for the log level value
ProvideFiles bool `json:"provide_files"` // If true, upload certs and genesis, otherwise have k8s generate them
Namespace string `json:"namespace"` // The kubernetes Namespace
DeploymentSpec string `json:"deployment_spec"` // Identifies this network in the cluster
Kind string `json:"kind"` // Identifies the object Kind for the operator
APIVersion string `json:"api_version"` // The APIVersion of the kubernetes object
Image string `json:"image"` // The docker image to use
Tag string `json:"tag"` // The docker tag to use
LogLevelKey string `json:"log_level_key"` // The key for the log level value
}
// TODO should this just be a part of NewNetwork?
@@ -290,6 +293,12 @@ func (a *networkImpl) GetNode(id string) (node.Node, error) {
func (a *networkImpl) createDeploymentFromConfig() *k8sapi.Avalanchego {
// Returns a new network whose initial state is specified in the config
var certs []k8sapi.Certificate
var genesis string
if a.k8sConfig.ProvideFiles {
certs, genesis = a.readFiles()
}
newChain := &k8sapi.Avalanchego{
TypeMeta: metav1.TypeMeta{
Kind: a.k8sConfig.Kind,
@@ -310,6 +319,8 @@ func (a *networkImpl) createDeploymentFromConfig() *k8sapi.Avalanchego {
Value: a.config.LogLevel,
},
},
Certificates: certs,
Genesis: genesis,
},
}
@@ -344,6 +355,36 @@ func (a *networkImpl) buildNodeMapping() error {
return nil
}
func (a *networkImpl) readFiles() ([]k8sapi.Certificate, string) {
certs := make([]k8sapi.Certificate, a.config.NodeCount)
configDir := "./examples/common/configs"
genesisFile, err := os.ReadFile(fmt.Sprintf("%s/genesis.json", configDir))
if err != nil {
log.Fatal("%s", err)
os.Exit(1)
}
for i := 0; i < a.config.NodeCount; i++ {
log.Info("reading config %d", i)
nodeConfigDir := fmt.Sprintf("%s/node%d", configDir, i)
stakingKey, err := os.ReadFile(fmt.Sprintf("%s/staking.key", nodeConfigDir))
if err != nil {
log.Fatal("%s", err)
os.Exit(1)
}
stakingCert, err := os.ReadFile(fmt.Sprintf("%s/staking.crt", nodeConfigDir))
if err != nil {
log.Fatal("%s", err)
os.Exit(1)
}
cert := k8sapi.Certificate{
Cert: string(stakingCert),
Key: string(stakingKey),
}
certs[i] = cert
}
return certs, string(genesisFile)
}
func (a *networkImpl) String() string {
s := strings.Builder{}
_, _ = s.WriteString("****************************************************************************************************")
+5 -4
View File
@@ -37,20 +37,21 @@ type Network interface {
// TODO add methods
}
// Config encapsulates general config params for a network
type Config struct {
// Configuration specific to a particular implementation of a network.
ImplSpecificConfig interface{}
// How many nodes in the network
NodeCount int
NodeCount int `json:"node_count"`
// Config for each node
NodeConfigs []node.Config
// Log level for the whole network
LogLevel string
LogLevel string `json:"log_level"`
// Name for the network
Name string
Name string `json:"name"`
}
// TODO enforce that all nodes have same genesis.
// Validate TODO enforce that all nodes have same genesis.
func (c *Config) Validate() error {
for i, nodeConfig := range c.NodeConfigs {
if err := nodeConfig.Validate(); err != nil {