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

     1  package controllers
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	aptosv1alpha1 "github.com/kotalco/kotal/apis/aptos/v1alpha1"
     8  	aptosClients "github.com/kotalco/kotal/clients/aptos"
     9  	"github.com/kotalco/kotal/controllers/shared"
    10  	"gopkg.in/yaml.v2"
    11  	"k8s.io/apimachinery/pkg/types"
    12  	"sigs.k8s.io/controller-runtime/pkg/client"
    13  )
    14  
    15  type Waypoint struct {
    16  	FromConfig string `yaml:"from_config,omitempty"`
    17  	FromFile   string `yaml:"from_file,omitempty"`
    18  }
    19  
    20  type Execution struct {
    21  	GenesisFileLocation string `yaml:"genesis_file_location"`
    22  }
    23  
    24  type Base struct {
    25  	Role     string   `yaml:"role"`
    26  	DataDir  string   `yaml:"data_dir"`
    27  	Waypoint Waypoint `yaml:"waypoint"`
    28  }
    29  
    30  type Identity struct {
    31  	Type   string `yaml:"type"`
    32  	Key    string `yaml:"key"`
    33  	PeerId string `yaml:"peer_id"`
    34  }
    35  
    36  type Peer struct {
    37  	Addresses []string `yaml:"addresses"`
    38  	Role      string   `yaml:"role"`
    39  }
    40  
    41  type Network struct {
    42  	NetworkId       string          `yaml:"network_id"`
    43  	DiscoveryMethod string          `yaml:"discovery_method"`
    44  	ListenAddress   string          `yaml:"listen_address"`
    45  	Identity        Identity        `yaml:"identity,omitempty"`
    46  	Seeds           map[string]Peer `yaml:"seeds,omitempty"`
    47  }
    48  
    49  type API struct {
    50  	Enabled bool   `yaml:"enabled"`
    51  	Address string `yaml:"address"`
    52  }
    53  
    54  type InspectionService struct {
    55  	Port uint `yaml:"port"`
    56  }
    57  
    58  type Config struct {
    59  	Base              Base              `yaml:"base"`
    60  	Execution         Execution         `yaml:"execution"`
    61  	FullNodeNetworks  []Network         `yaml:"full_node_networks,omitempty"`
    62  	API               API               `yaml:"api"`
    63  	InspectionService InspectionService `yaml:"inspection_service"`
    64  }
    65  
    66  // ConfigFromSpec generates config.toml file from node spec
    67  func ConfigFromSpec(node *aptosv1alpha1.Node, client client.Client) (config string, err error) {
    68  	var role string
    69  	if node.Spec.Validator {
    70  		role = "validator"
    71  	} else {
    72  		role = "full_node"
    73  	}
    74  
    75  	var nodePrivateKey string
    76  	var identity Identity
    77  	if node.Spec.NodePrivateKeySecretName != "" {
    78  		key := types.NamespacedName{
    79  			Name:      node.Spec.NodePrivateKeySecretName,
    80  			Namespace: node.Namespace,
    81  		}
    82  
    83  		if nodePrivateKey, err = shared.GetSecret(context.Background(), client, key, "key"); err != nil {
    84  			return
    85  		}
    86  
    87  		identity = Identity{
    88  			Type:   "from_config",
    89  			Key:    nodePrivateKey,
    90  			PeerId: node.Spec.PeerId,
    91  		}
    92  
    93  	}
    94  
    95  	seeds := map[string]Peer{}
    96  
    97  	if len(node.Spec.SeedPeers) != 0 {
    98  		for _, peer := range node.Spec.SeedPeers {
    99  			seeds[peer.ID] = Peer{
   100  				Addresses: peer.Addresses,
   101  				Role:      "Upstream",
   102  			}
   103  		}
   104  	}
   105  
   106  	homeDir := aptosClients.NewClient(node).HomeDir()
   107  	configDir := shared.PathConfig(homeDir)
   108  	dataDir := shared.PathData(homeDir)
   109  
   110  	waypoint := Waypoint{}
   111  	var genesisFileLocation string
   112  
   113  	if node.Spec.Waypoint == "" {
   114  		waypoint.FromFile = fmt.Sprintf("%s/kotal_waypoint.txt", shared.PathData(homeDir))
   115  	} else {
   116  		waypoint.FromConfig = node.Spec.Waypoint
   117  	}
   118  
   119  	if node.Spec.GenesisConfigmapName == "" {
   120  		genesisFileLocation = fmt.Sprintf("%s/kotal_genesis.blob", dataDir)
   121  	} else {
   122  		genesisFileLocation = fmt.Sprintf("%s/genesis.blob", configDir)
   123  	}
   124  
   125  	c := Config{
   126  		Base: Base{
   127  			Role:     role,
   128  			DataDir:  shared.PathData(homeDir),
   129  			Waypoint: waypoint,
   130  		},
   131  		Execution: Execution{
   132  			GenesisFileLocation: genesisFileLocation,
   133  		},
   134  		FullNodeNetworks: []Network{
   135  			{
   136  				NetworkId:       "public",
   137  				DiscoveryMethod: "onchain",
   138  				ListenAddress:   fmt.Sprintf("/ip4/%s/tcp/%d", shared.Host(true), node.Spec.P2PPort),
   139  				Identity:        identity,
   140  				Seeds:           seeds,
   141  			},
   142  		},
   143  		API: API{
   144  			Enabled: node.Spec.API,
   145  			Address: fmt.Sprintf("%s:%d", shared.Host(node.Spec.API), node.Spec.APIPort),
   146  		},
   147  		InspectionService: InspectionService{
   148  			Port: node.Spec.MetricsPort,
   149  		},
   150  	}
   151  
   152  	data, err := yaml.Marshal(&c)
   153  	if err != nil {
   154  		return
   155  	}
   156  
   157  	config = string(data)
   158  	return
   159  }