...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/config/config.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/config

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    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  // ControllerCfg is a global variable to keep the configuration for Chaos Controller
    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