...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/chaos-daemon/main.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/chaos-daemon

     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 main
    17  
    18  import (
    19  	"flag"
    20  	stdlog "log"
    21  	"os"
    22  
    23  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    24  
    25  	"github.com/prometheus/client_golang/prometheus"
    26  	"github.com/prometheus/client_golang/prometheus/collectors"
    27  	ctrl "sigs.k8s.io/controller-runtime"
    28  
    29  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon"
    30  	"github.com/chaos-mesh/chaos-mesh/pkg/fusedev"
    31  	"github.com/chaos-mesh/chaos-mesh/pkg/version"
    32  )
    33  
    34  var (
    35  	conf = &chaosdaemon.Config{Host: "0.0.0.0"}
    36  
    37  	printVersion bool
    38  )
    39  
    40  func init() {
    41  	flag.BoolVar(&printVersion, "version", false, "print version information and exit")
    42  	flag.IntVar(&conf.GRPCPort, "grpc-port", 31767, "the port which grpc server listens on")
    43  	flag.IntVar(&conf.HTTPPort, "http-port", 31766, "the port which http server listens on")
    44  	flag.StringVar(&conf.Runtime, "runtime", "docker", "current container runtime")
    45  	flag.StringVar(&conf.CaCert, "ca", "", "ca certificate of grpc server")
    46  	flag.StringVar(&conf.Cert, "cert", "", "certificate of grpc server")
    47  	flag.StringVar(&conf.Key, "key", "", "key of grpc server")
    48  	flag.BoolVar(&conf.Profiling, "pprof", false, "enable pprof")
    49  
    50  	flag.Parse()
    51  }
    52  
    53  func main() {
    54  	version.PrintVersionInfo("Chaos-daemon")
    55  
    56  	if printVersion {
    57  		os.Exit(0)
    58  	}
    59  	rootLogger, err := log.NewDefaultZapLogger()
    60  	if err != nil {
    61  		stdlog.Fatal("failed to create root logger", err)
    62  	}
    63  	rootLogger = rootLogger.WithName("chaos-daemon.daemon-server")
    64  	log.ReplaceGlobals(rootLogger)
    65  	ctrl.SetLogger(rootLogger)
    66  
    67  	reg := prometheus.NewRegistry()
    68  	reg.MustRegister(
    69  		// Use collectors as prometheus functions deprecated
    70  		collectors.NewGoCollector(),
    71  		collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
    72  	)
    73  
    74  	rootLogger.Info("grant access to /dev/fuse")
    75  	err = fusedev.GrantAccess()
    76  	if err != nil {
    77  		rootLogger.Error(err, "fail to grant access to /dev/fuse")
    78  	}
    79  
    80  	if err = chaosdaemon.StartServer(conf, reg, rootLogger); err != nil {
    81  		rootLogger.Error(err, "failed to start chaos-daemon server")
    82  		os.Exit(1)
    83  	}
    84  }
    85