...

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

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

     1  // Copyright 2021 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  // ControllerCfg is a global variable to keep the configuration for Chaos Controller
    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