github.com/argoproj/argo-events@v1.9.1/controllers/config.go (about)

     1  package controllers
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/fsnotify/fsnotify"
     8  	"github.com/spf13/viper"
     9  )
    10  
    11  type GlobalConfig struct {
    12  	EventBus *EventBusConfig `json:"eventBus"`
    13  }
    14  
    15  type EventBusConfig struct {
    16  	NATS      *StanConfig      `json:"nats"`
    17  	JetStream *JetStreamConfig `json:"jetstream"`
    18  }
    19  
    20  type StanConfig struct {
    21  	Versions []StanVersion `json:"versions"`
    22  }
    23  
    24  type StanVersion struct {
    25  	Version              string `json:"version"`
    26  	NATSStreamingImage   string `json:"natsStreamingImage"`
    27  	MetricsExporterImage string `json:"metricsExporterImage"`
    28  }
    29  
    30  type JetStreamConfig struct {
    31  	Settings     string             `json:"settings"`
    32  	StreamConfig string             `json:"streamConfig"`
    33  	Versions     []JetStreamVersion `json:"versions"`
    34  }
    35  
    36  type JetStreamVersion struct {
    37  	Version              string `json:"version"`
    38  	NatsImage            string `json:"natsImage"`
    39  	ConfigReloaderImage  string `json:"configReloaderImage"`
    40  	MetricsExporterImage string `json:"metricsExporterImage"`
    41  	StartCommand         string `json:"startCommand"`
    42  }
    43  
    44  func (g *GlobalConfig) supportedSTANVersions() []string {
    45  	result := []string{}
    46  	if g.EventBus == nil || g.EventBus.NATS == nil {
    47  		return result
    48  	}
    49  	for _, v := range g.EventBus.NATS.Versions {
    50  		result = append(result, v.Version)
    51  	}
    52  	return result
    53  }
    54  
    55  func (g *GlobalConfig) supportedJetStreamVersions() []string {
    56  	result := []string{}
    57  	if g.EventBus == nil || g.EventBus.JetStream == nil {
    58  		return result
    59  	}
    60  	for _, v := range g.EventBus.JetStream.Versions {
    61  		result = append(result, v.Version)
    62  	}
    63  	return result
    64  }
    65  
    66  func (g *GlobalConfig) GetSTANVersion(version string) (*StanVersion, error) {
    67  	if g.EventBus == nil || g.EventBus.NATS == nil {
    68  		return nil, fmt.Errorf("\"eventBus.nats\" not found in the configuration")
    69  	}
    70  	if len(g.EventBus.NATS.Versions) == 0 {
    71  		return nil, fmt.Errorf("nats streaming version configuration not found")
    72  	}
    73  	for _, r := range g.EventBus.NATS.Versions {
    74  		if r.Version == version {
    75  			return &r, nil
    76  		}
    77  	}
    78  	return nil, fmt.Errorf("unsupported version %q, supported versions: %q", version, strings.Join(g.supportedSTANVersions(), ","))
    79  }
    80  
    81  func (g *GlobalConfig) GetJetStreamVersion(version string) (*JetStreamVersion, error) {
    82  	if g.EventBus == nil || g.EventBus.JetStream == nil {
    83  		return nil, fmt.Errorf("\"eventBus.jetstream\" not found in the configuration")
    84  	}
    85  	if len(g.EventBus.JetStream.Versions) == 0 {
    86  		return nil, fmt.Errorf("jetstream version configuration not found")
    87  	}
    88  	for _, r := range g.EventBus.JetStream.Versions {
    89  		if r.Version == version {
    90  			return &r, nil
    91  		}
    92  	}
    93  	return nil, fmt.Errorf("unsupported version %q, supported versions: %q", version, strings.Join(g.supportedJetStreamVersions(), ","))
    94  }
    95  
    96  func LoadConfig(onErrorReloading func(error)) (*GlobalConfig, error) {
    97  	v := viper.New()
    98  	v.SetConfigName("controller-config")
    99  	v.SetConfigType("yaml")
   100  	v.AddConfigPath("/etc/argo-events")
   101  	err := v.ReadInConfig()
   102  	if err != nil {
   103  		return nil, fmt.Errorf("failed to load configuration file. %w", err)
   104  	}
   105  	r := &GlobalConfig{}
   106  	err = v.Unmarshal(r)
   107  	if err != nil {
   108  		return nil, fmt.Errorf("failed unmarshal configuration file. %w", err)
   109  	}
   110  	v.WatchConfig()
   111  	v.OnConfigChange(func(e fsnotify.Event) {
   112  		err = v.Unmarshal(r)
   113  		if err != nil {
   114  			onErrorReloading(err)
   115  		}
   116  	})
   117  	return r, nil
   118  }
   119  
   120  func ValidateConfig(config *GlobalConfig) error {
   121  	if len(config.supportedJetStreamVersions()) == 0 {
   122  		return fmt.Errorf("no jetstream versions were provided in the controller config")
   123  	}
   124  
   125  	if len(config.supportedSTANVersions()) == 0 {
   126  		return fmt.Errorf("no stan versions were provided in the controller config")
   127  	}
   128  
   129  	return nil
   130  }