github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/k8s/pkg/helm/reorg/reorg.go (about)

     1  package reorg
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/rs/zerolog/log"
    10  
    11  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/client"
    12  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/config"
    13  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/environment"
    14  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/projectpath"
    15  )
    16  
    17  const (
    18  	URLsKey            = "geth"
    19  	TXNodesAppLabel    = "geth-ethereum-geth"
    20  	MinerNodesAppLabel = "geth-ethereum-miner-node" // #nosec G101
    21  )
    22  
    23  type Props struct {
    24  	NetworkName string `envconfig:"network_name"`
    25  	NetworkType string `envconfig:"network_type"`
    26  	Values      map[string]interface{}
    27  }
    28  
    29  type Chart struct {
    30  	Name    string
    31  	Path    string
    32  	Version string
    33  	Props   *Props
    34  	Values  *map[string]interface{}
    35  }
    36  
    37  func (m Chart) IsDeploymentNeeded() bool {
    38  	return true
    39  }
    40  
    41  func (m Chart) GetName() string {
    42  	return m.Name
    43  }
    44  
    45  func (m Chart) GetProps() interface{} {
    46  	return m.Props
    47  }
    48  
    49  func (m Chart) GetPath() string {
    50  	return m.Path
    51  }
    52  
    53  func (m Chart) GetVersion() string {
    54  	return m.Version
    55  }
    56  
    57  func (m Chart) GetValues() *map[string]interface{} {
    58  	return m.Values
    59  }
    60  
    61  func (m Chart) ExportData(e *environment.Environment) error {
    62  	urls := make([]string, 0)
    63  	minerPods, err := e.Client.ListPods(e.Cfg.Namespace, fmt.Sprintf("app=%s-ethereum-miner-node", m.Props.NetworkName))
    64  	if err != nil {
    65  		return err
    66  	}
    67  	txPods, err := e.Client.ListPods(e.Cfg.Namespace, fmt.Sprintf("app=%s-ethereum-geth", m.Props.NetworkName))
    68  	if err != nil {
    69  		return err
    70  	}
    71  	if len(txPods.Items) > 0 {
    72  		for i := range txPods.Items {
    73  			podName := fmt.Sprintf("%s-ethereum-geth:%d", m.Props.NetworkName, i)
    74  			txNodeLocalWS, err := e.Fwd.FindPort(podName, "geth", "ws-rpc").As(client.LocalConnection, client.WS)
    75  			if err != nil {
    76  				return err
    77  			}
    78  			txNodeInternalWs, err := e.Fwd.FindPort(podName, "geth", "ws-rpc").As(client.RemoteConnection, client.WS)
    79  			if err != nil {
    80  				return err
    81  			}
    82  			if e.Cfg.InsideK8s {
    83  				urls = append(urls, txNodeInternalWs)
    84  				log.Info().Str("URL", txNodeInternalWs).Msgf("Geth network (TX Node) - %d", i)
    85  			} else {
    86  				urls = append(urls, txNodeLocalWS)
    87  				log.Info().Str("URL", txNodeLocalWS).Msgf("Geth network (TX Node) - %d", i)
    88  			}
    89  		}
    90  	}
    91  
    92  	if len(minerPods.Items) > 0 {
    93  		for i := range minerPods.Items {
    94  			podName := fmt.Sprintf("%s-ethereum-miner-node:%d", m.Props.NetworkName, i)
    95  			minerNodeLocalWS, err := e.Fwd.FindPort(podName, "geth-miner", "ws-rpc-miner").As(client.LocalConnection, client.WS)
    96  			if err != nil {
    97  				return err
    98  			}
    99  			minerNodeInternalWs, err := e.Fwd.FindPort(podName, "geth-miner", "ws-rpc-miner").As(client.RemoteConnection, client.WS)
   100  			if err != nil {
   101  				return err
   102  			}
   103  			if e.Cfg.InsideK8s {
   104  				urls = append(urls, minerNodeInternalWs)
   105  				log.Info().Str("URL", minerNodeInternalWs).Msgf("Geth network (Miner Node) - %d", i)
   106  			} else {
   107  				urls = append(urls, minerNodeLocalWS)
   108  				log.Info().Str("URL", minerNodeLocalWS).Msgf("Geth network (Miner Node) - %d", i)
   109  			}
   110  		}
   111  	}
   112  
   113  	e.URLs[m.Props.NetworkName] = urls
   114  	return nil
   115  }
   116  
   117  func defaultProps() *Props {
   118  	internalRepo := os.Getenv(config.EnvVarInternalDockerRepo)
   119  	gethRepo := "ethereum/client-go"
   120  	bootnodeRepo := "jpoon/bootnode-registrar"
   121  	if internalRepo != "" {
   122  		gethRepo = fmt.Sprintf("%s/ethereum/client-go", internalRepo)
   123  		bootnodeRepo = fmt.Sprintf("%s/jpoon/bootnode-registrar", internalRepo)
   124  	}
   125  	return &Props{
   126  		NetworkName: "geth",
   127  		NetworkType: "geth-reorg",
   128  		Values: map[string]interface{}{
   129  			"imagePullPolicy": "IfNotPresent",
   130  			"bootnode": map[string]interface{}{
   131  				"replicas": "2",
   132  				"image": map[string]interface{}{
   133  					"repository": gethRepo,
   134  					"tag":        "alltools-v1.10.25",
   135  				},
   136  			},
   137  			"bootnodeRegistrar": map[string]interface{}{
   138  				"replicas": "1",
   139  				"image": map[string]interface{}{
   140  					"repository": bootnodeRepo,
   141  					"tag":        "v1.0.0",
   142  				},
   143  			},
   144  			"geth": map[string]interface{}{
   145  				"image": map[string]interface{}{
   146  					"repository": gethRepo,
   147  					"tag":        "v1.10.25",
   148  				},
   149  				"tx": map[string]interface{}{
   150  					"replicas": "1",
   151  					"service": map[string]interface{}{
   152  						"type": "ClusterIP",
   153  					},
   154  					"resources": map[string]interface{}{
   155  						"requests": map[string]interface{}{
   156  							"cpu":    "2",
   157  							"memory": "2Gi",
   158  						},
   159  						"limits": map[string]interface{}{
   160  							"cpu":    "2",
   161  							"memory": "2Gi",
   162  						},
   163  					},
   164  				},
   165  				"miner": map[string]interface{}{
   166  					"replicas": "2",
   167  					"account": map[string]interface{}{
   168  						"secret": "",
   169  					},
   170  					"resources": map[string]interface{}{
   171  						"requests": map[string]interface{}{
   172  							"cpu":    "2",
   173  							"memory": "2Gi",
   174  						},
   175  						"limits": map[string]interface{}{
   176  							"cpu":    "2",
   177  							"memory": "2Gi",
   178  						},
   179  					},
   180  				},
   181  				"genesis": map[string]interface{}{
   182  					"networkId": "1337",
   183  				},
   184  			},
   185  		},
   186  	}
   187  }
   188  
   189  func New(props *Props) environment.ConnectedChart {
   190  	return NewVersioned("", props)
   191  }
   192  
   193  // NewVersioned enables choosing a specific helm chart version
   194  func NewVersioned(helmVersion string, props *Props) environment.ConnectedChart {
   195  	targetProps := defaultProps()
   196  	config.MustMerge(targetProps, props)
   197  	config.MustMerge(&targetProps.Values, props.Values)
   198  	chartPath := "chainlink-qa/ethereum"
   199  	if b, err := strconv.ParseBool(os.Getenv(config.EnvVarLocalCharts)); err == nil && b {
   200  		chartPath = fmt.Sprintf("%s/geth-reorg", projectpath.ChartsRoot)
   201  	}
   202  	return Chart{
   203  		Name:    strings.ReplaceAll(strings.ToLower(targetProps.NetworkName), " ", "-"), // name cannot contain spaces
   204  		Path:    chartPath,
   205  		Values:  &targetProps.Values,
   206  		Props:   targetProps,
   207  		Version: helmVersion,
   208  	}
   209  }