...

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  	"os"
    20  	"strings"
    21  
    22  	"github.com/pkg/errors"
    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.ClusterScoped {
    55  		if strings.TrimSpace(config.TargetNamespace) == "" {
    56  			return errors.New("no target namespace specified with namespace scoped mode")
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62