config.go 7.9 KB
Newer Older
1 2 3 4 5
package config

import (
	"fmt"
	"github.com/spf13/viper"
6
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
J
Jeff 已提交
7
	"kubesphere.io/kubesphere/pkg/simple/client/alerting"
Z
zryfish 已提交
8
	"kubesphere.io/kubesphere/pkg/simple/client/cache"
R
runzexia 已提交
9
	"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
10 11
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
	"kubesphere.io/kubesphere/pkg/simple/client/ldap"
G
Guangzhe Huang 已提交
12
	"kubesphere.io/kubesphere/pkg/simple/client/logging/elasticsearch"
G
Guangzhe Huang 已提交
13
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring/prometheus"
14
	"kubesphere.io/kubesphere/pkg/simple/client/network"
J
Jeff 已提交
15
	"kubesphere.io/kubesphere/pkg/simple/client/notification"
16
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
17
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
18 19
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
J
Jeff 已提交
20 21
	"reflect"
	"strings"
22 23
)

J
Jeff 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// Package config saves configuration for running KubeSphere components
//
// Config can be configured from command line flags and configuration file.
// Command line flags hold higher priority than configuration file. But if
// component Endpoint/Host/APIServer was left empty, all of that component
// command line flags will be ignored, use configuration file instead.
// For example, we have configuration file
//
// mysql:
//   host: mysql.kubesphere-system.svc
//   username: root
//   password: password
//
// At the same time, have command line flags like following:
//
// --mysql-host mysql.openpitrix-system.svc --mysql-username king --mysql-password 1234
//
// We will use `king:1234@mysql.openpitrix-system.svc` from command line flags rather
// than `root:password@mysql.kubesphere-system.svc` from configuration file,
// cause command line has higher priority. But if command line flags like following:
//
// --mysql-username root --mysql-password password
//
47
// we will `root:password@mysql.kubesphere-system.svc` as input, cause
J
Jeff 已提交
48 49 50
// mysql-host is missing in command line flags, all other mysql command line flags
// will be ignored.

51
const (
J
Jeff 已提交
52
	// DefaultConfigurationName is the default name of configuration
53
	defaultConfigurationName = "kubesphere"
J
Jeff 已提交
54 55

	// DefaultConfigurationPath the default location of the configuration file
56
	defaultConfigurationPath = "/etc/kubesphere"
57 58
)

59
// Config defines everything needed for apiserver to deal with external services
60
type Config struct {
61 62 63 64
	DevopsOptions         *jenkins.Options                   `json:"devops,omitempty" yaml:"devops,omitempty" mapstructure:"devops"`
	SonarQubeOptions      *sonarqube.Options                 `json:"sonarqube,omitempty" yaml:"sonarQube,omitempty" mapstructure:"sonarqube"`
	KubernetesOptions     *k8s.KubernetesOptions             `json:"kubernetes,omitempty" yaml:"kubernetes,omitempty" mapstructure:"kubernetes"`
	ServiceMeshOptions    *servicemesh.Options               `json:"servicemesh,omitempty" yaml:"servicemesh,omitempty" mapstructure:"servicemesh"`
65
	NetworkOptions        *network.Options                   `json:"network,omitempty" yaml:"network,omitempty" mapstructure:"network"`
66 67 68 69 70 71 72
	LdapOptions           *ldap.Options                      `json:"ldap,omitempty" yaml:"ldap,omitempty" mapstructure:"ldap"`
	RedisOptions          *cache.Options                     `json:"redis,omitempty" yaml:"redis,omitempty" mapstructure:"redis"`
	S3Options             *s3.Options                        `json:"s3,omitempty" yaml:"s3,omitempty" mapstructure:"s3"`
	OpenPitrixOptions     *openpitrix.Options                `json:"openpitrix,omitempty" yaml:"openpitrix,omitempty" mapstructure:"openpitrix"`
	MonitoringOptions     *prometheus.Options                `json:"monitoring,omitempty" yaml:"monitoring,omitempty" mapstructure:"monitoring"`
	LoggingOptions        *elasticsearch.Options             `json:"logging,omitempty" yaml:"logging,omitempty" mapstructure:"logging"`
	AuthenticationOptions *authoptions.AuthenticationOptions `json:"authentication,omitempty" yaml:"authentication,omitempty" mapstructure:"authentication"`
J
Jeff 已提交
73 74
	// Options used for enabling components, not actually used now. Once we switch Alerting/Notification API to kubesphere,
	// we can add these options to kubesphere command lines
Z
zryfish 已提交
75 76
	AlertingOptions     *alerting.Options     `json:"alerting,omitempty" yaml:"alerting,omitempty" mapstructure:"alerting"`
	NotificationOptions *notification.Options `json:"notification,omitempty" yaml:"notification,omitempty" mapstructure:"notification"`
77 78
}

79 80
// newConfig creates a default non-empty Config
func New() *Config {
81
	return &Config{
82 83 84 85
		DevopsOptions:         jenkins.NewDevopsOptions(),
		SonarQubeOptions:      sonarqube.NewSonarQubeOptions(),
		KubernetesOptions:     k8s.NewKubernetesOptions(),
		ServiceMeshOptions:    servicemesh.NewServiceMeshOptions(),
86
		NetworkOptions:        network.NewNetworkOptions(),
87 88 89 90 91 92 93 94 95
		LdapOptions:           ldap.NewOptions(),
		RedisOptions:          cache.NewRedisOptions(),
		S3Options:             s3.NewS3Options(),
		OpenPitrixOptions:     openpitrix.NewOptions(),
		MonitoringOptions:     prometheus.NewPrometheusOptions(),
		AlertingOptions:       alerting.NewAlertingOptions(),
		NotificationOptions:   notification.NewNotificationOptions(),
		LoggingOptions:        elasticsearch.NewElasticSearchOptions(),
		AuthenticationOptions: authoptions.NewAuthenticateOptions(),
96 97 98
	}
}

99 100 101 102 103
// TryLoadFromDisk loads configuration from default location after server startup
// return nil error if configuration file not exists
func TryLoadFromDisk() (*Config, error) {
	viper.SetConfigName(defaultConfigurationName)
	viper.AddConfigPath(defaultConfigurationPath)
104

105 106
	// Load from current working directory, only used for debugging
	viper.AddConfigPath(".")
107

108 109 110 111 112 113
	if err := viper.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			return nil, err
		} else {
			return nil, fmt.Errorf("error parsing configuration file %s", err)
		}
J
Jeff 已提交
114 115
	}

116
	conf := New()
117

118 119
	if err := viper.Unmarshal(conf); err != nil {
		return nil, err
J
Jeff 已提交
120 121
	}

122 123
	return conf, nil
}
124

125 126
// convertToMap simply converts config to map[string]bool
// to hide sensitive information
127 128
func (conf *Config) ToMap() map[string]bool {
	conf.stripEmptyOptions()
129
	result := make(map[string]bool, 0)
130

131 132
	if conf == nil {
		return result
133 134
	}

135
	c := reflect.Indirect(reflect.ValueOf(conf))
136

137 138 139 140 141
	for i := 0; i < c.NumField(); i++ {
		name := strings.Split(c.Type().Field(i).Tag.Get("json"), ",")[0]
		if strings.HasPrefix(name, "-") {
			continue
		}
142

143 144 145 146 147
		if c.Field(i).IsNil() {
			result[name] = false
		} else {
			result[name] = true
		}
148
	}
149 150

	return result
151
}
J
Jeff 已提交
152

Z
zryfish 已提交
153
// Remove invalid options before serializing to json or yaml
154
func (conf *Config) stripEmptyOptions() {
J
Jeff 已提交
155

Z
zryfish 已提交
156
	if conf.RedisOptions != nil && conf.RedisOptions.Host == "" {
157
		conf.RedisOptions = nil
J
Jeff 已提交
158 159
	}

160 161
	if conf.DevopsOptions != nil && conf.DevopsOptions.Host == "" {
		conf.DevopsOptions = nil
J
Jeff 已提交
162 163
	}

164 165 166
	if conf.MonitoringOptions != nil && conf.MonitoringOptions.Endpoint == "" &&
		conf.MonitoringOptions.SecondaryEndpoint == "" {
		conf.MonitoringOptions = nil
J
Jeff 已提交
167 168
	}

169 170
	if conf.SonarQubeOptions != nil && conf.SonarQubeOptions.Host == "" {
		conf.SonarQubeOptions = nil
J
Jeff 已提交
171 172
	}

173 174
	if conf.LdapOptions != nil && conf.LdapOptions.Host == "" {
		conf.LdapOptions = nil
J
Jeff 已提交
175 176
	}

177 178
	if conf.OpenPitrixOptions != nil && conf.OpenPitrixOptions.IsEmpty() {
		conf.OpenPitrixOptions = nil
J
Jeff 已提交
179 180
	}

181 182 183 184
	if conf.NetworkOptions != nil && conf.NetworkOptions.WeaveScopeHost == "" {
		conf.NetworkOptions = nil
	}

185 186 187 188
	if conf.ServiceMeshOptions != nil && conf.ServiceMeshOptions.IstioPilotHost == "" &&
		conf.ServiceMeshOptions.ServicemeshPrometheusHost == "" &&
		conf.ServiceMeshOptions.JaegerQueryHost == "" {
		conf.ServiceMeshOptions = nil
J
Jeff 已提交
189 190
	}

191 192
	if conf.S3Options != nil && conf.S3Options.Endpoint == "" {
		conf.S3Options = nil
J
Jeff 已提交
193 194
	}

195 196
	if conf.AlertingOptions != nil && conf.AlertingOptions.Endpoint == "" {
		conf.AlertingOptions = nil
J
Jeff 已提交
197 198
	}

199 200
	if conf.LoggingOptions != nil && conf.LoggingOptions.Host == "" {
		conf.LoggingOptions = nil
J
Jeff 已提交
201 202
	}

203 204
	if conf.NotificationOptions != nil && conf.NotificationOptions.Endpoint == "" {
		conf.NotificationOptions = nil
J
Jeff 已提交
205 206
	}

J
Jeff 已提交
207
}