...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/webhook/config/watcher/config.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/webhook/config/watcher

     1  // Copyright 2019 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 watcher
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  )
    19  
    20  // Config is a configuration struct for the Watcher type
    21  type Config struct {
    22  	// ClusterScoped means control Chaos Object in cluster level(all namespace);
    23  	ClusterScoped bool `envconfig:"CLUSTER_SCOPED" default:"true"`
    24  	// TemplateNamespace is the namespace which holds the template configmap.
    25  	// If controller-manager is running with in-cluster mode. If is set to empty string, it will be overwrite to namespace which the pod belongs.
    26  	TemplateNamespace string `envconfig:"TEMPLATE_NAMESPACE" default:""`
    27  	// TargetNamespace means configmaps in this namespace will be controlled by this controller.
    28  	// It SHOULD be the same with TargetNamespace in config.ChaosControllerConfig while clusterScoped is false.
    29  	TargetNamespace string `envconfig:"TARGET_NAMESPACE" default:""`
    30  	// TemplateLabels is label pairs used to discover common templates in Kubernetes. These should be key1:value[,key2:val2,...]
    31  	TemplateLabels map[string]string `envconfig:"TEMPLATE_LABELS"`
    32  	// ConfigLabels is label pairs used to discover ConfigMaps in Kubernetes. These should be key1:value[,key2:val2,...]
    33  	ConfigLabels map[string]string `envconfig:"CONFIGMAP_LABELS"`
    34  }
    35  
    36  // NewConfig returns a new initialized Config
    37  func NewConfig() *Config {
    38  	return &Config{
    39  		ClusterScoped:     true,
    40  		TemplateNamespace: "",
    41  		TargetNamespace:   "",
    42  		TemplateLabels:    map[string]string{},
    43  		ConfigLabels:      map[string]string{},
    44  	}
    45  }
    46  
    47  // Verify will verify the parameter configuration is correct
    48  func (c *Config) Verify() error {
    49  	if len(c.TemplateLabels) == 0 {
    50  		return errors.New("envconfig:\"TEMPLATE_LABELS\" template labels must be set")
    51  	}
    52  	if len(c.ConfigLabels) == 0 {
    53  		return errors.New("envconfig:\"CONFIGMAP_LABELS\" conf labels must be set")
    54  	}
    55  	if !c.ClusterScoped && len(c.TargetNamespace) == 0 {
    56  		return errors.New("envconfig:\"TARGET_NAMESPACE\" conf labels must be set while CLUSTER_SCOPED is false")
    57  	}
    58  	return nil
    59  }
    60