...

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

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

     1  // Copyright 2020 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 config
    15  
    16  import (
    17  	"time"
    18  
    19  	"github.com/kelseyhightower/envconfig"
    20  
    21  	"github.com/chaos-mesh/chaos-mesh/pkg/ttlcontroller"
    22  )
    23  
    24  // ChaosDashboardConfig defines the configuration for Chaos Dashboard
    25  type ChaosDashboardConfig struct {
    26  	ListenHost           string            `envconfig:"LISTEN_HOST" default:"0.0.0.0" json:"listen_host"`
    27  	ListenPort           int               `envconfig:"LISTEN_PORT" default:"2333" json:"listen_port"`
    28  	MetricAddress        string            `envconfig:"METRIC_ADDRESS" json:"-"`
    29  	EnableLeaderElection bool              `envconfig:"ENABLE_LEADER_ELECTION" json:"-"`
    30  	Database             *DatabaseConfig   `json:"-"`
    31  	PersistTTL           *PersistTTLConfig `json:"-"`
    32  	// ClusterScoped means control Chaos Object in cluster level(all namespace),
    33  	ClusterScoped bool `envconfig:"CLUSTER_SCOPED" default:"true" json:"cluster_mode"`
    34  	// TargetNamespace is the target namespace to injecting chaos.
    35  	// It only works with ClusterScoped is false;
    36  	TargetNamespace string `envconfig:"TARGET_NAMESPACE" default:"" json:"target_namespace"`
    37  	// EnableFilterNamespace will filter namespace with annotation. Only the pods/containers in namespace
    38  	// annotated with `chaos-mesh.org/inject=enabled` will be injected
    39  	EnableFilterNamespace bool `envconfig:"ENABLE_FILTER_NAMESPACE" default:"false"`
    40  
    41  	// SecurityMode will use the token login by the user if set to true
    42  	SecurityMode    bool   `envconfig:"SECURITY_MODE" default:"true" json:"security_mode"`
    43  	DNSServerCreate bool   `envconfig:"DNS_SERVER_CREATE" default:"false" json:"dns_server_create"`
    44  	Version         string `json:"version"`
    45  }
    46  
    47  // PersistTTLConfig defines the configuration of ttl
    48  type PersistTTLConfig struct {
    49  	SyncPeriod string `envconfig:"CLEAN_SYNC_PERIOD" default:"12h"`
    50  	Event      string `envconfig:"TTL_EVENT"       default:"168h"` // one week
    51  	Experiment string `envconfig:"TTL_EXPERIMENT"  default:"336h"` // two weeks
    52  }
    53  
    54  // DatabaseConfig defines the configuration for databases
    55  type DatabaseConfig struct {
    56  	// Archive Chaos Experiments to DB
    57  	Archive    bool
    58  	Driver     string `envconfig:"DATABASE_DRIVER"     default:"sqlite3"`
    59  	Datasource string `envconfig:"DATABASE_DATASOURCE" default:"core.sqlite"`
    60  	Secret     string `envconfig:"DATABASE_SECRET"`
    61  }
    62  
    63  // EnvironChaosDashboard returns the settings from the environment.
    64  func EnvironChaosDashboard() (*ChaosDashboardConfig, error) {
    65  	cfg := ChaosDashboardConfig{}
    66  	err := envconfig.Process("", &cfg)
    67  	return &cfg, err
    68  }
    69  
    70  // ParsePersistTTLConfig parse PersistTTLConfig to persistTTLConfigParsed.
    71  func ParsePersistTTLConfig(config *PersistTTLConfig) (*ttlcontroller.TTLconfig, error) {
    72  	SyncPeriod, err := time.ParseDuration(config.SyncPeriod)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	Event, err := time.ParseDuration(config.Event)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	Experiment, err := time.ParseDuration(config.Experiment)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return &ttlcontroller.TTLconfig{
    88  		DatabaseTTLResyncPeriod: SyncPeriod,
    89  		EventTTL:                Event,
    90  		ArchiveExperimentTTL:    Experiment,
    91  	}, nil
    92  }
    93