...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package config
15
16 import (
17 "fmt"
18 "os"
19 "strings"
20
21 ctrl "sigs.k8s.io/controller-runtime"
22 "sigs.k8s.io/controller-runtime/pkg/log/zap"
23
24 "github.com/chaos-mesh/chaos-mesh/pkg/config"
25 )
26
27
28 var ControllerCfg *config.ChaosControllerConfig
29
30 var log = ctrl.Log.WithName("config")
31
32 func init() {
33 conf, err := config.EnvironChaosController()
34 if err != nil {
35 ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
36 log.Error(err, "Chaos Controller: invalid environment configuration")
37 os.Exit(1)
38 }
39
40 err = validate(&conf)
41 if err != nil {
42 ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
43 log.Error(err, "Chaos Controller: invalid configuration")
44 os.Exit(1)
45 }
46
47 ControllerCfg = &conf
48 }
49
50 func validate(config *config.ChaosControllerConfig) error {
51
52 if config.WatcherConfig == nil {
53 return fmt.Errorf("required WatcherConfig is missing")
54 }
55
56 if config.ClusterScoped != config.WatcherConfig.ClusterScoped {
57 return fmt.Errorf("K8sConfigMapWatcher config ClusterScoped is not same with controller-manager ClusterScoped. k8s configmap watcher: %t, controller manager: %t", config.WatcherConfig.ClusterScoped, config.ClusterScoped)
58 }
59
60 if !config.ClusterScoped {
61 if strings.TrimSpace(config.TargetNamespace) == "" {
62 return fmt.Errorf("no target namespace specified with namespace scoped mode")
63 }
64
65 if config.TargetNamespace != config.WatcherConfig.TargetNamespace {
66 return fmt.Errorf("K8sConfigMapWatcher config TargertNamespace is not same with controller-manager TargetNamespace. k8s configmap watcher: %s, controller manager: %s", config.WatcherConfig.TargetNamespace, config.TargetNamespace)
67 }
68 }
69
70 return nil
71 }
72