config.go 11.0 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

17 18 19 20 21
package config

import (
	"fmt"
	"github.com/spf13/viper"
22
	networkv1alpha1 "kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
23
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
H
hongming 已提交
24
	authorizationoptions "kubesphere.io/kubesphere/pkg/apiserver/authorization/options"
J
Jeff 已提交
25
	"kubesphere.io/kubesphere/pkg/simple/client/alerting"
26
	"kubesphere.io/kubesphere/pkg/simple/client/auditing"
Z
zryfish 已提交
27
	"kubesphere.io/kubesphere/pkg/simple/client/cache"
R
runzexia 已提交
28
	"kubesphere.io/kubesphere/pkg/simple/client/devops/jenkins"
29
	"kubesphere.io/kubesphere/pkg/simple/client/events"
30
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
R
root 已提交
31
	"kubesphere.io/kubesphere/pkg/simple/client/kubeedge"
32
	"kubesphere.io/kubesphere/pkg/simple/client/ldap"
33
	"kubesphere.io/kubesphere/pkg/simple/client/logging"
G
Guangzhe Huang 已提交
34
	"kubesphere.io/kubesphere/pkg/simple/client/monitoring/prometheus"
H
hongming 已提交
35
	"kubesphere.io/kubesphere/pkg/simple/client/multicluster"
36
	"kubesphere.io/kubesphere/pkg/simple/client/network"
J
Jeff 已提交
37
	"kubesphere.io/kubesphere/pkg/simple/client/notification"
38
	"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
Z
zryfish 已提交
39
	"kubesphere.io/kubesphere/pkg/simple/client/s3"
40 41
	"kubesphere.io/kubesphere/pkg/simple/client/servicemesh"
	"kubesphere.io/kubesphere/pkg/simple/client/sonarqube"
J
Jeff 已提交
42 43
	"reflect"
	"strings"
44 45
)

J
Jeff 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// 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
//
69
// we will `root:password@mysql.kubesphere-system.svc` as input, cause
J
Jeff 已提交
70 71 72
// mysql-host is missing in command line flags, all other mysql command line flags
// will be ignored.

73
const (
J
Jeff 已提交
74
	// DefaultConfigurationName is the default name of configuration
75
	defaultConfigurationName = "kubesphere"
J
Jeff 已提交
76 77

	// DefaultConfigurationPath the default location of the configuration file
78
	defaultConfigurationPath = "/etc/kubesphere"
79 80
)

81
// Config defines everything needed for apiserver to deal with external services
82
type Config struct {
H
hongming 已提交
83 84 85 86 87 88 89 90 91 92
	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"`
	NetworkOptions        *network.Options                           `json:"network,omitempty" yaml:"network,omitempty" mapstructure:"network"`
	LdapOptions           *ldap.Options                              `json:"-,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"`
93
	LoggingOptions        *logging.Options                           `json:"logging,omitempty" yaml:"logging,omitempty" mapstructure:"logging"`
H
hongming 已提交
94
	AuthenticationOptions *authoptions.AuthenticationOptions         `json:"authentication,omitempty" yaml:"authentication,omitempty" mapstructure:"authentication"`
H
hongming 已提交
95
	AuthorizationOptions  *authorizationoptions.AuthorizationOptions `json:"authorization,omitempty" yaml:"authorization,omitempty" mapstructure:"authorization"`
H
hongming 已提交
96
	MultiClusterOptions   *multicluster.Options                      `json:"multicluster,omitempty" yaml:"multicluster,omitempty" mapstructure:"multicluster"`
97 98
	EventsOptions         *events.Options                            `json:"events,omitempty" yaml:"events,omitempty" mapstructure:"events"`
	AuditingOptions       *auditing.Options                          `json:"auditing,omitempty" yaml:"auditing,omitempty" mapstructure:"auditing"`
99 100
	AlertingOptions       *alerting.Options                          `json:"alerting,omitempty" yaml:"alerting,omitempty" mapstructure:"alerting"`
	NotificationOptions   *notification.Options                      `json:"notification,omitempty" yaml:"notification,omitempty" mapstructure:"notification"`
R
root 已提交
101
	KubeEdgeOptions       *kubeedge.Options                          `json:"kubeedge,omitempty" yaml:"kubeedge,omitempty" mapstructure:"kubeedge"`
102 103
}

104 105
// newConfig creates a default non-empty Config
func New() *Config {
106
	return &Config{
107 108 109 110
		DevopsOptions:         jenkins.NewDevopsOptions(),
		SonarQubeOptions:      sonarqube.NewSonarQubeOptions(),
		KubernetesOptions:     k8s.NewKubernetesOptions(),
		ServiceMeshOptions:    servicemesh.NewServiceMeshOptions(),
111
		NetworkOptions:        network.NewNetworkOptions(),
112 113 114 115 116 117 118
		LdapOptions:           ldap.NewOptions(),
		RedisOptions:          cache.NewRedisOptions(),
		S3Options:             s3.NewS3Options(),
		OpenPitrixOptions:     openpitrix.NewOptions(),
		MonitoringOptions:     prometheus.NewPrometheusOptions(),
		AlertingOptions:       alerting.NewAlertingOptions(),
		NotificationOptions:   notification.NewNotificationOptions(),
119
		LoggingOptions:        logging.NewLoggingOptions(),
120
		AuthenticationOptions: authoptions.NewAuthenticateOptions(),
H
hongming 已提交
121
		AuthorizationOptions:  authorizationoptions.NewAuthorizationOptions(),
H
hongming 已提交
122
		MultiClusterOptions:   multicluster.NewOptions(),
123 124
		EventsOptions:         events.NewEventsOptions(),
		AuditingOptions:       auditing.NewAuditingOptions(),
R
root 已提交
125
		KubeEdgeOptions:       kubeedge.NewKubeEdgeOptions(),
126 127 128
	}
}

129 130 131 132 133
// 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)
134

135 136
	// Load from current working directory, only used for debugging
	viper.AddConfigPath(".")
137

138 139 140 141 142 143
	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 已提交
144 145
	}

146
	conf := New()
147

148 149
	if err := viper.Unmarshal(conf); err != nil {
		return nil, err
J
Jeff 已提交
150 151
	}

152 153
	return conf, nil
}
154

155 156
// convertToMap simply converts config to map[string]bool
// to hide sensitive information
157 158
func (conf *Config) ToMap() map[string]bool {
	conf.stripEmptyOptions()
159
	result := make(map[string]bool, 0)
160

161 162
	if conf == nil {
		return result
163 164
	}

165
	c := reflect.Indirect(reflect.ValueOf(conf))
166

167 168 169 170 171
	for i := 0; i < c.NumField(); i++ {
		name := strings.Split(c.Type().Field(i).Tag.Get("json"), ",")[0]
		if strings.HasPrefix(name, "-") {
			continue
		}
172

173 174 175
		if name == "network" {
			ippoolName := "network.ippool"
			nsnpName := "network"
176
			networkTopologyName := "network.topology"
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
			if conf.NetworkOptions == nil {
				result[nsnpName] = false
				result[ippoolName] = false
			} else {
				if conf.NetworkOptions.EnableNetworkPolicy {
					result[nsnpName] = true
				} else {
					result[nsnpName] = false
				}

				if conf.NetworkOptions.IPPoolType == networkv1alpha1.IPPoolTypeNone {
					result[ippoolName] = false
				} else {
					result[ippoolName] = true
				}
192 193 194 195 196 197

				if conf.NetworkOptions.WeaveScopeHost == "" {
					result[networkTopologyName] = false
				} else {
					result[networkTopologyName] = true
				}
198 199 200 201
			}
			continue
		}

L
LiHui 已提交
202 203 204 205 206 207 208 209
		if name == "openpitrix" {
			if conf.OpenPitrixOptions.IsEmpty() {
				result["openpitrix.appstore"] = false
			} else {
				result["openpitrix.appstore"] = true
			}
		}

210 211 212 213 214
		if c.Field(i).IsNil() {
			result[name] = false
		} else {
			result[name] = true
		}
215
	}
216 217

	return result
218
}
J
Jeff 已提交
219

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

Z
zryfish 已提交
223
	if conf.RedisOptions != nil && conf.RedisOptions.Host == "" {
224
		conf.RedisOptions = nil
J
Jeff 已提交
225 226
	}

227 228
	if conf.DevopsOptions != nil && conf.DevopsOptions.Host == "" {
		conf.DevopsOptions = nil
J
Jeff 已提交
229 230
	}

231
	if conf.MonitoringOptions != nil && conf.MonitoringOptions.Endpoint == "" {
232
		conf.MonitoringOptions = nil
J
Jeff 已提交
233 234
	}

235 236
	if conf.SonarQubeOptions != nil && conf.SonarQubeOptions.Host == "" {
		conf.SonarQubeOptions = nil
J
Jeff 已提交
237 238
	}

239 240
	if conf.LdapOptions != nil && conf.LdapOptions.Host == "" {
		conf.LdapOptions = nil
J
Jeff 已提交
241 242
	}

243 244
	if conf.OpenPitrixOptions != nil && conf.OpenPitrixOptions.IsEmpty() {
		conf.OpenPitrixOptions = nil
J
Jeff 已提交
245 246
	}

247
	if conf.NetworkOptions != nil && conf.NetworkOptions.IsEmpty() {
248 249 250
		conf.NetworkOptions = nil
	}

251 252 253 254
	if conf.ServiceMeshOptions != nil && conf.ServiceMeshOptions.IstioPilotHost == "" &&
		conf.ServiceMeshOptions.ServicemeshPrometheusHost == "" &&
		conf.ServiceMeshOptions.JaegerQueryHost == "" {
		conf.ServiceMeshOptions = nil
J
Jeff 已提交
255 256
	}

257 258
	if conf.S3Options != nil && conf.S3Options.Endpoint == "" {
		conf.S3Options = nil
J
Jeff 已提交
259 260
	}

261 262
	if conf.AlertingOptions != nil && conf.AlertingOptions.Endpoint == "" &&
		conf.AlertingOptions.PrometheusEndpoint == "" && conf.AlertingOptions.ThanosRulerEndpoint == "" {
263
		conf.AlertingOptions = nil
J
Jeff 已提交
264 265
	}

266 267
	if conf.LoggingOptions != nil && conf.LoggingOptions.Host == "" {
		conf.LoggingOptions = nil
J
Jeff 已提交
268 269
	}

270 271
	if conf.NotificationOptions != nil && conf.NotificationOptions.Endpoint == "" {
		conf.NotificationOptions = nil
J
Jeff 已提交
272 273
	}

Z
zryfish 已提交
274 275 276
	if conf.MultiClusterOptions != nil && !conf.MultiClusterOptions.Enable {
		conf.MultiClusterOptions = nil
	}
J
junotx 已提交
277 278 279 280

	if conf.EventsOptions != nil && conf.EventsOptions.Host == "" {
		conf.EventsOptions = nil
	}
R
root 已提交
281 282 283 284

	if conf.AuditingOptions != nil && conf.AuditingOptions.Host == "" {
		conf.AuditingOptions = nil
	}
R
root 已提交
285 286 287 288

	if conf.KubeEdgeOptions != nil && conf.KubeEdgeOptions.Endpoint == "" {
		conf.KubeEdgeOptions = nil
	}
J
Jeff 已提交
289
}