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