github.com/TeaOSLab/EdgeNode@v1.3.8/internal/configs/cluster_config.go (about)

     1  package configs
     2  
     3  import (
     4  	"github.com/iwind/TeaGo/Tea"
     5  	"gopkg.in/yaml.v3"
     6  	"os"
     7  )
     8  
     9  // ClusterConfig 集群配置
    10  type ClusterConfig struct {
    11  	OldRPC struct {
    12  		Endpoints     []string `yaml:"endpoints" json:"endpoints"`
    13  		DisableUpdate bool     `yaml:"disableUpdate" json:"disableUpdate"`
    14  	} `yaml:"rpc,omitempty" json:"rpc"`
    15  
    16  	RPCEndpoints     []string `yaml:"rpc.endpoints,flow" json:"rpc.endpoints"`
    17  	RPCDisableUpdate bool     `yaml:"rpc.disableUpdate" json:"rpc.disableUpdate"`
    18  
    19  	ClusterId string `yaml:"clusterId" json:"clusterId"`
    20  	Secret    string `yaml:"secret" json:"secret"`
    21  }
    22  
    23  func (this *ClusterConfig) Init() error {
    24  	// compatible with old
    25  	if len(this.RPCEndpoints) == 0 && len(this.OldRPC.Endpoints) > 0 {
    26  		this.RPCEndpoints = this.OldRPC.Endpoints
    27  		this.RPCDisableUpdate = this.OldRPC.DisableUpdate
    28  	}
    29  
    30  	return nil
    31  }
    32  
    33  func LoadClusterConfig() (*ClusterConfig, error) {
    34  	for _, filename := range []string{"api_cluster.yaml", "cluster.yaml"} {
    35  		data, err := os.ReadFile(Tea.ConfigFile(filename))
    36  		if err != nil {
    37  			if os.IsNotExist(err) {
    38  				continue
    39  			}
    40  			return nil, err
    41  		}
    42  
    43  		var config = &ClusterConfig{}
    44  		err = yaml.Unmarshal(data, config)
    45  		if err != nil {
    46  			return config, err
    47  		}
    48  
    49  		err = config.Init()
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  
    54  		return config, nil
    55  	}
    56  	return nil, os.ErrNotExist
    57  }