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