...

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