...

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

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/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  	"time"
    20  
    21  	"github.com/kelseyhightower/envconfig"
    22  	"github.com/pkg/errors"
    23  )
    24  
    25  // ChaosDashboardConfig defines the configuration for Chaos Dashboard
    26  type ChaosDashboardConfig struct {
    27  	ListenHost           string                   `envconfig:"LISTEN_HOST" default:"0.0.0.0" json:"listen_host"`
    28  	ListenPort           int                      `envconfig:"LISTEN_PORT" default:"2333" json:"listen_port"`
    29  	MetricHost           string                   `envconfig:"METRIC_HOST" default:"0.0.0.0" json:"-"`
    30  	MetricPort           int                      `envconfig:"METRIC_PORT" default:"2334" json:"-"`
    31  	EnableLeaderElection bool                     `envconfig:"ENABLE_LEADER_ELECTION" json:"-"`
    32  	Database             *DatabaseConfig          `json:"-"`
    33  	PersistTTL           *TTLConfigWithStringTime `json:"-"`
    34  	// ClusterScoped means control Chaos Object in cluster level(all namespace).
    35  	ClusterScoped bool `envconfig:"CLUSTER_SCOPED" default:"true" json:"cluster_mode"`
    36  	// TargetNamespace is the target namespace to injecting chaos.
    37  	// It only works with ClusterScoped is false.
    38  	TargetNamespace string `envconfig:"TARGET_NAMESPACE" default:"" json:"target_namespace"`
    39  	// EnableFilterNamespace will filter namespace with annotation. Only the pods/containers in namespace
    40  	// annotated with `chaos-mesh.org/inject=enabled` will be injected.
    41  	EnableFilterNamespace bool `envconfig:"ENABLE_FILTER_NAMESPACE" default:"false"`
    42  	// SecurityMode will use the token login by the user if set to true
    43  	SecurityMode bool `envconfig:"SECURITY_MODE" default:"true" json:"security_mode"`
    44  	// GcpSecurityMode will use the gcloud authentication to login to GKE user
    45  	GcpSecurityMode bool   `envconfig:"GCP_SECURITY_MODE" default:"false" json:"gcp_security_mode"`
    46  	GcpClientId     string `envconfig:"GCP_CLIENT_ID" default:"" json:"-"`
    47  	GcpClientSecret string `envconfig:"GCP_CLIENT_SECRET" default:"" json:"-"`
    48  
    49  	RootUrl string `envconfig:"ROOT_URL" default:"http://localhost:2333" json:"root_path"`
    50  
    51  	// enableProfiling is a flag to enable pprof in controller-manager and chaos-daemon
    52  	EnableProfiling bool `envconfig:"ENABLE_PROFILING" default:"true" json:"-"`
    53  
    54  	// After v2.5, the DNS server is created by default.
    55  	DNSServerCreate bool   `envconfig:"DNS_SERVER_CREATE" default:"true" json:"dns_server_create"`
    56  	Version         string `json:"version"`
    57  
    58  	// The QPS config for kubernetes client
    59  	QPS float32 `envconfig:"QPS" default:"200" json:"-"`
    60  	// The Burst config for kubernetes client
    61  	Burst int `envconfig:"BURST" default:"300" json:"-"`
    62  }
    63  
    64  // DatabaseConfig defines the configuration for databases
    65  type DatabaseConfig struct {
    66  	Driver string `envconfig:"DATABASE_DRIVER"     default:"sqlite3"`
    67  	// Datasource is the connection string for database.
    68  	// For sqlite3, it is the path of the database file.
    69  	// For mysql, it is the DSN (https://github.com/go-sql-driver/mysql#dsn-data-source-name).
    70  	Datasource string `envconfig:"DATABASE_DATASOURCE" default:"core.sqlite"`
    71  }
    72  
    73  // TTLConfig defines all the TTL-related configurations.
    74  type TTLConfig struct {
    75  	// ResyncPeriod defines the period of cleaning data.
    76  	ResyncPeriod time.Duration
    77  
    78  	// TTL of events.
    79  	EventTTL time.Duration
    80  	// TTL of experiments.
    81  	ExperimentTTL time.Duration
    82  	// TTL of schedules.
    83  	ScheduleTTL time.Duration
    84  	// TTL of workflows.
    85  	WorkflowTTL time.Duration
    86  }
    87  
    88  // TTLConfigWithStringTime defines all the TTL-related configurations with string type time.
    89  type TTLConfigWithStringTime struct {
    90  	ResyncPeriod string `envconfig:"CLEAN_SYNC_PERIOD" default:"12h"`
    91  
    92  	EventTTL      string `envconfig:"TTL_EVENT"         default:"168h"` // one week
    93  	ExperimentTTL string `envconfig:"TTL_EXPERIMENT"    default:"336h"` // two weeks
    94  	ScheduleTTL   string `envconfig:"TTL_SCHEDULE"      default:"336h"`
    95  	WorkflowTTL   string `envconfig:"TTL_WORKFLOW"      default:"336h"`
    96  }
    97  
    98  func (config *TTLConfigWithStringTime) Parse() (*TTLConfig, error) {
    99  	syncPeriod, err := time.ParseDuration(config.ResyncPeriod)
   100  	if err != nil {
   101  		return nil, errors.Wrap(err, "parse configuration sync period")
   102  	}
   103  
   104  	eventTTL, err := time.ParseDuration(config.EventTTL)
   105  	if err != nil {
   106  		return nil, errors.Wrap(err, "parse configuration TTL for event")
   107  	}
   108  
   109  	experimentTTL, err := time.ParseDuration(config.ExperimentTTL)
   110  	if err != nil {
   111  		return nil, errors.Wrap(err, "parse configuration TTL for experiment")
   112  	}
   113  
   114  	scheduleTTL, err := time.ParseDuration(config.ScheduleTTL)
   115  	if err != nil {
   116  		return nil, errors.Wrap(err, "parse configuration TTL for schedule")
   117  	}
   118  
   119  	workflowTTL, err := time.ParseDuration(config.WorkflowTTL)
   120  	if err != nil {
   121  		return nil, errors.Wrap(err, "parse configuration TTL for workflow")
   122  	}
   123  
   124  	return &TTLConfig{
   125  		ResyncPeriod:  syncPeriod,
   126  		EventTTL:      eventTTL,
   127  		ExperimentTTL: experimentTTL,
   128  		ScheduleTTL:   scheduleTTL,
   129  		WorkflowTTL:   workflowTTL,
   130  	}, nil
   131  }
   132  
   133  // GetChaosDashboardEnv gets all env variables related to dashboard.
   134  func GetChaosDashboardEnv() (*ChaosDashboardConfig, error) {
   135  	cfg := ChaosDashboardConfig{}
   136  	err := envconfig.Process("", &cfg)
   137  	return &cfg, err
   138  }
   139