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

     1  package ethereum
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/rs/zerolog/log"
     9  
    10  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/client"
    11  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/config"
    12  	"github.com/smartcontractkit/chainlink-testing-framework/libs/k8s/environment"
    13  	"github.com/smartcontractkit/chainlink-testing-framework/libs/utils/projectpath"
    14  )
    15  
    16  type Props struct {
    17  	NetworkName string   `envconfig:"network_name"`
    18  	Simulated   bool     `envconfig:"network_simulated"`
    19  	HttpURLs    []string `envconfig:"http_url"`
    20  	WsURLs      []string `envconfig:"ws_url"`
    21  	Values      map[string]interface{}
    22  }
    23  
    24  type HelmProps struct {
    25  	Name    string
    26  	Path    string
    27  	Version string
    28  	Values  *map[string]interface{}
    29  }
    30  
    31  type Chart struct {
    32  	HelmProps *HelmProps
    33  	Props     *Props
    34  }
    35  
    36  func (m Chart) IsDeploymentNeeded() bool {
    37  	return m.Props.Simulated
    38  }
    39  
    40  func (m Chart) GetProps() interface{} {
    41  	return m.Props
    42  }
    43  
    44  func (m Chart) GetName() string {
    45  	return m.HelmProps.Name
    46  }
    47  
    48  func (m Chart) GetPath() string {
    49  	return m.HelmProps.Path
    50  }
    51  
    52  func (m Chart) GetVersion() string {
    53  	return m.HelmProps.Version
    54  }
    55  
    56  func (m Chart) GetValues() *map[string]interface{} {
    57  	return m.HelmProps.Values
    58  }
    59  
    60  func (m Chart) ExportData(e *environment.Environment) error {
    61  	if m.Props.Simulated {
    62  		gethLocalHttp, err := e.Fwd.FindPort("geth:0", "geth-network", "http-rpc").As(client.LocalConnection, client.HTTP)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		gethInternalHttp, err := e.Fwd.FindPort("geth:0", "geth-network", "http-rpc").As(client.RemoteConnection, client.HTTP)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		gethLocalWs, err := e.Fwd.FindPort("geth:0", "geth-network", "ws-rpc").As(client.LocalConnection, client.WS)
    71  		if err != nil {
    72  			return err
    73  		}
    74  		gethInternalWs, err := e.Fwd.FindPort("geth:0", "geth-network", "ws-rpc").As(client.RemoteConnection, client.WS)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		if e.Cfg.InsideK8s {
    79  			services, err := e.Client.ListServices(e.Cfg.Namespace, fmt.Sprintf("app=%s", m.HelmProps.Name))
    80  			if err != nil {
    81  				return err
    82  			}
    83  			e.URLs[m.Props.NetworkName] = []string{fmt.Sprintf("ws://%s:8546", services.Items[0].Name)}
    84  			e.URLs[m.Props.NetworkName+"_http"] = []string{fmt.Sprintf("http://%s:8544", services.Items[0].Name)}
    85  		} else {
    86  			e.URLs[m.Props.NetworkName] = []string{gethLocalWs}
    87  			e.URLs[m.Props.NetworkName+"_http"] = []string{gethLocalHttp}
    88  		}
    89  
    90  		// For cases like starknet we need the internalHttp address to set up the L1<>L2 interaction
    91  		e.URLs[m.Props.NetworkName+"_internal"] = []string{gethInternalWs}
    92  		e.URLs[m.Props.NetworkName+"_internal_http"] = []string{gethInternalHttp}
    93  
    94  		log.Info().Str("Name", "Geth").Str("URLs", gethLocalWs).Msg("Geth network")
    95  	} else {
    96  		e.URLs[m.Props.NetworkName] = m.Props.WsURLs
    97  		log.Info().Str("Name", m.Props.NetworkName).Strs("URLs", m.Props.WsURLs).Msg("Ethereum network")
    98  	}
    99  	return nil
   100  }
   101  
   102  func defaultProps() *Props {
   103  	internalRepo := os.Getenv(config.EnvVarInternalDockerRepo)
   104  	gethImage := "ethereum/client-go"
   105  	if internalRepo != "" {
   106  		gethImage = fmt.Sprintf("%s/ethereum/client-go", internalRepo)
   107  	}
   108  	return &Props{
   109  		NetworkName: "Simulated Geth",
   110  		Simulated:   true,
   111  		Values: map[string]interface{}{
   112  			"replicas": "1",
   113  			"geth": map[string]interface{}{
   114  				"image": map[string]interface{}{
   115  					"image":   gethImage,
   116  					"version": "v1.10.25",
   117  				},
   118  			},
   119  			"resources": map[string]interface{}{
   120  				"requests": map[string]interface{}{
   121  					"cpu":    "1000m",
   122  					"memory": "768Mi",
   123  				},
   124  				"limits": map[string]interface{}{
   125  					"cpu":    "1000m",
   126  					"memory": "768Mi",
   127  				},
   128  			},
   129  		},
   130  	}
   131  }
   132  
   133  func New(props *Props) environment.ConnectedChart {
   134  	return NewVersioned("", props)
   135  }
   136  
   137  // NewVersioned enables choosing a specific helm chart version
   138  func NewVersioned(helmVersion string, props *Props) environment.ConnectedChart {
   139  	targetProps := defaultProps()
   140  	if props == nil {
   141  		props = targetProps
   142  	}
   143  	config.MustMerge(targetProps, props)
   144  	config.MustMerge(&targetProps.Values, props.Values)
   145  	targetProps.Simulated = props.Simulated // Mergo has issues with boolean merging for simulated networks
   146  	if targetProps.Simulated {
   147  		chartPath := "chainlink-qa/geth"
   148  		if b, err := strconv.ParseBool(os.Getenv(config.EnvVarLocalCharts)); err == nil && b {
   149  			chartPath = fmt.Sprintf("%s/geth", projectpath.ChartsRoot)
   150  		}
   151  		return Chart{
   152  			HelmProps: &HelmProps{
   153  				Name:   "geth",
   154  				Path:   chartPath,
   155  				Values: &targetProps.Values,
   156  			},
   157  			Props: targetProps,
   158  		}
   159  	}
   160  	return Chart{
   161  		Props: targetProps,
   162  		HelmProps: &HelmProps{
   163  			Version: helmVersion,
   164  		},
   165  	}
   166  }