github.com/kotalco/kotal@v0.3.0/controllers/chainlink/config.go (about)

     1  package controllers
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/kotalco/kotal/controllers/shared"
    10  	"k8s.io/apimachinery/pkg/types"
    11  	"sigs.k8s.io/controller-runtime/pkg/client"
    12  
    13  	"github.com/BurntSushi/toml"
    14  	chainlinkv1alpha1 "github.com/kotalco/kotal/apis/chainlink/v1alpha1"
    15  )
    16  
    17  // Chainlink node configuration is documented here
    18  // https://github.com/smartcontractkit/chainlink/blob/develop/docs/CONFIG.md
    19  
    20  type WebServer struct {
    21  	AllowOrigins  string
    22  	SecureCookies bool
    23  	HTTPPort      uint
    24  	TLS           WebServerTLS
    25  }
    26  
    27  type WebServerTLS struct {
    28  	HTTPSPort uint
    29  	CertPath  string
    30  	KeyPath   string
    31  }
    32  
    33  type EVM struct {
    34  	ChainID             string
    35  	LinkContractAddress string
    36  	Nodes               []Node
    37  }
    38  
    39  type Node struct {
    40  	Name    string
    41  	WSURL   string
    42  	HTTPURL string
    43  }
    44  
    45  type P2PV2 struct {
    46  	ListenAddresses []string
    47  }
    48  
    49  type P2P struct {
    50  	V2 P2PV2
    51  }
    52  
    53  type Log struct {
    54  	Level string
    55  }
    56  
    57  type Config struct {
    58  	RootDir   string
    59  	P2P       P2P
    60  	Log       Log
    61  	WebServer WebServer
    62  	// WebServerTLS WebServerTLS `toml:"WebServer.TLS,omitempty"`
    63  	EVM   []EVM
    64  	Nodes []Node `toml:"EVM.Nodes"`
    65  }
    66  
    67  // ConfigFromSpec generates config.toml file from node spec
    68  func ConfigFromSpec(node *chainlinkv1alpha1.Node, homeDir string) (config string, err error) {
    69  	c := &Config{}
    70  
    71  	c.RootDir = shared.PathData(homeDir)
    72  
    73  	c.Log = Log{
    74  		Level: string(node.Spec.Logging),
    75  	}
    76  
    77  	c.EVM = []EVM{
    78  		{
    79  			ChainID:             fmt.Sprintf("%d", node.Spec.EthereumChainId),
    80  			LinkContractAddress: node.Spec.LinkContractAddress,
    81  			Nodes: []Node{
    82  				{
    83  					Name:    "node",
    84  					WSURL:   node.Spec.EthereumWSEndpoint,
    85  					HTTPURL: strings.Join(node.Spec.EthereumHTTPEndpoints, ","),
    86  				},
    87  			},
    88  		},
    89  	}
    90  
    91  	c.P2P = P2P{
    92  		V2: P2PV2{
    93  			ListenAddresses: []string{
    94  				fmt.Sprintf("%s:%d", shared.Host(true), node.Spec.P2PPort),
    95  			},
    96  		},
    97  	}
    98  
    99  	c.WebServer = WebServer{
   100  		AllowOrigins:  strings.Join(node.Spec.CORSDomains, ","),
   101  		SecureCookies: node.Spec.SecureCookies,
   102  		HTTPPort:      node.Spec.APIPort,
   103  	}
   104  
   105  	if node.Spec.CertSecretName != "" {
   106  		c.WebServer.TLS = WebServerTLS{
   107  			HTTPSPort: node.Spec.TLSPort,
   108  			KeyPath:   fmt.Sprintf("%s/tls.key", shared.PathSecrets(homeDir)),
   109  			CertPath:  fmt.Sprintf("%s/tls.crt", shared.PathSecrets(homeDir)),
   110  		}
   111  	}
   112  
   113  	var buff bytes.Buffer
   114  	enc := toml.NewEncoder(&buff)
   115  	err = enc.Encode(c)
   116  	if err != nil {
   117  		return
   118  	}
   119  
   120  	config = buff.String()
   121  
   122  	return
   123  }
   124  
   125  type SecretsConfig struct {
   126  	Database Database
   127  	Password Password
   128  }
   129  
   130  type Database struct {
   131  	URL string
   132  }
   133  
   134  type Password struct {
   135  	Keystore string
   136  }
   137  
   138  // SecretsFromSpec generates config.toml file from node spec
   139  func SecretsFromSpec(node *chainlinkv1alpha1.Node, homeDir string, client client.Client) (config string, err error) {
   140  	c := &SecretsConfig{}
   141  
   142  	c.Database = Database{
   143  		URL: node.Spec.DatabaseURL,
   144  	}
   145  
   146  	key := types.NamespacedName{
   147  		Name:      node.Spec.KeystorePasswordSecretName,
   148  		Namespace: node.Namespace,
   149  	}
   150  
   151  	var password string
   152  	if password, err = shared.GetSecret(context.Background(), client, key, "password"); err != nil {
   153  		return
   154  	}
   155  
   156  	c.Password = Password{
   157  		Keystore: password,
   158  	}
   159  
   160  	var buff bytes.Buffer
   161  	enc := toml.NewEncoder(&buff)
   162  	err = enc.Encode(c)
   163  	if err != nil {
   164  		return
   165  	}
   166  
   167  	config = buff.String()
   168  
   169  	return
   170  }