github.com/stakater/IngressMonitorController@v1.0.103/pkg/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"time"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  
    10  	yaml "gopkg.in/yaml.v2"
    11  )
    12  
    13  type Config struct {
    14  	Providers             []Provider    `yaml:"providers"`
    15  	EnableMonitorDeletion bool          `yaml:"enableMonitorDeletion"`
    16  	MonitorNameTemplate   string        `yaml:"monitorNameTemplate"`
    17  	ResyncPeriod          int           `yaml:"resyncPeriod,omitempty"`
    18  	CreationDelay         time.Duration `yaml:"creationDelay,omitempty"`
    19  }
    20  
    21  // UnmarshalYAML interface to deserialize specific types
    22  func (c *Config) UnmarshalYAML(data []byte) error {
    23  	type Alias Config
    24  	aux := struct {
    25  		CreationDelay string `yaml:"creationDelay,omitempty"`
    26  		*Alias
    27  	}{
    28  		Alias: (*Alias)(c),
    29  	}
    30  	if err := yaml.Unmarshal(data, &aux); err != nil {
    31  		return err
    32  	}
    33  
    34  	delay, err := time.ParseDuration(aux.CreationDelay)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	c.CreationDelay = delay
    39  
    40  	return nil
    41  }
    42  
    43  type Provider struct {
    44  	Name              string      `yaml:"name"`
    45  	ApiKey            string      `yaml:"apiKey"`
    46  	ApiToken          string      `yaml:"apiToken"`
    47  	ApiURL            string      `yaml:"apiURL"`
    48  	AlertContacts     string      `yaml:"alertContacts"`
    49  	AlertIntegrations string      `yaml:"alertIntegrations"`
    50  	Username          string      `yaml:"username"`
    51  	Password          string      `yaml:"password"`
    52  	AccountEmail      string      `yaml:"accountEmail"`
    53  	AppInsightsConfig AppInsights `yaml:"appInsightsConfig"`
    54  	GcloudConfig      Gcloud      `yaml:"gcloudConfig"`
    55  }
    56  
    57  type AppInsights struct {
    58  	Name          string        `yaml:"name"`
    59  	Location      string        `yaml:"location"`
    60  	ResourceGroup string        `yaml:"resourceGroup"`
    61  	Frequency     int32         `yaml:"frequency"`
    62  	GeoLocation   []interface{} `yaml:"geoLocation"`
    63  	EmailAction   EmailAction   `yaml:"emailAction"`
    64  	WebhookAction WebhookAction `yaml:"webhookAction"`
    65  }
    66  
    67  type Gcloud struct {
    68  	ProjectID string `yaml:"projectId"`
    69  }
    70  
    71  type EmailAction struct {
    72  	SendToServiceOwners bool     `yaml:"send_to_service_owners"`
    73  	CustomEmails        []string `yaml:"custom_emails"`
    74  }
    75  
    76  type WebhookAction struct {
    77  	ServiceURI string `yaml:"service_uri"`
    78  }
    79  
    80  func ReadConfig(filePath string) Config {
    81  	var config Config
    82  	// Read YML
    83  	log.Info("Reading YAML Configuration", filePath)
    84  	source, err := ioutil.ReadFile(filePath)
    85  	if err != nil {
    86  		log.Panic(err)
    87  	}
    88  
    89  	// Unmarshall
    90  	err = yaml.Unmarshal(source, &config)
    91  	if err != nil {
    92  		log.Panic(err)
    93  	}
    94  
    95  	return config
    96  }
    97  
    98  func GetControllerConfig() Config {
    99  	configFilePath := os.Getenv("CONFIG_FILE_PATH")
   100  	if len(configFilePath) == 0 {
   101  		configFilePath = "../../configs/testConfigs/test-config.yaml"
   102  	}
   103  
   104  	config := ReadConfig(configFilePath)
   105  
   106  	return config
   107  }