github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/eventer.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     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  //go:generate ./hooks/run_extpoints.sh
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"os"
    25  	"runtime"
    26  	"strconv"
    27  	"strings"
    28  	"time"
    29  
    30  	"github.com/golang/glog"
    31  	"k8s.io/apiserver/pkg/util/logs"
    32  	"k8s.io/heapster/common/flags"
    33  	"k8s.io/heapster/events/api"
    34  	"k8s.io/heapster/events/manager"
    35  	"k8s.io/heapster/events/sinks"
    36  	"k8s.io/heapster/events/sources"
    37  	"k8s.io/heapster/version"
    38  )
    39  
    40  var (
    41  	argFrequency   = flag.Duration("frequency", 30*time.Second, "The resolution at which Eventer pushes events to sinks")
    42  	argMaxProcs    = flag.Int("max_procs", 0, "max number of CPUs that can be used simultaneously. Less than 1 for default (number of cores)")
    43  	argSources     flags.Uris
    44  	argSinks       flags.Uris
    45  	argVersion     bool
    46  	argHealthzIP   = flag.String("healthz-ip", "0.0.0.0", "ip eventer health check service uses")
    47  	argHealthzPort = flag.Uint("healthz-port", 8084, "port eventer health check listens on")
    48  )
    49  
    50  func main() {
    51  	quitChannel := make(chan struct{}, 0)
    52  
    53  	flag.Var(&argSources, "source", "source(s) to read events from")
    54  	flag.Var(&argSinks, "sink", "external sink(s) that receive events")
    55  	flag.BoolVar(&argVersion, "version", false, "print version info and exit")
    56  	flag.Parse()
    57  
    58  	if argVersion {
    59  		fmt.Println(version.VersionInfo())
    60  		os.Exit(0)
    61  	}
    62  
    63  	logs.InitLogs()
    64  	defer logs.FlushLogs()
    65  
    66  	setMaxProcs()
    67  
    68  	glog.Infof(strings.Join(os.Args, " "))
    69  	glog.Infof("Eventer version %v", version.HeapsterVersion)
    70  	if err := validateFlags(); err != nil {
    71  		glog.Fatal(err)
    72  	}
    73  
    74  	// sources
    75  	if len(argSources) != 1 {
    76  		glog.Fatal("Wrong number of sources specified")
    77  	}
    78  	sourceFactory := sources.NewSourceFactory()
    79  	sources, err := sourceFactory.BuildAll(argSources)
    80  	if err != nil {
    81  		glog.Fatalf("Failed to create sources: %v", err)
    82  	}
    83  	if len(sources) != 1 {
    84  		glog.Fatal("Requires exactly 1 source")
    85  	}
    86  
    87  	// sinks
    88  	sinksFactory := sinks.NewSinkFactory()
    89  	sinkList := sinksFactory.BuildAll(argSinks)
    90  	if len([]flags.Uri(argSinks)) != 0 && len(sinkList) == 0 {
    91  		glog.Fatal("No available sink to use")
    92  	}
    93  
    94  	for _, sink := range sinkList {
    95  		glog.Infof("Starting with %s sink", sink.Name())
    96  	}
    97  	sinkManager, err := sinks.NewEventSinkManager(sinkList, sinks.DefaultSinkExportEventsTimeout, sinks.DefaultSinkStopTimeout)
    98  	if err != nil {
    99  		glog.Fatalf("Failed to create sink manager: %v", err)
   100  	}
   101  
   102  	// main manager
   103  	manager, err := manager.NewManager(sources[0], sinkManager, *argFrequency)
   104  	if err != nil {
   105  		glog.Fatalf("Failed to create main manager: %v", err)
   106  	}
   107  
   108  	manager.Start()
   109  	glog.Infof("Starting eventer")
   110  
   111  	go startHTTPServer()
   112  
   113  	<-quitChannel
   114  }
   115  
   116  func startHTTPServer() {
   117  	glog.Info("Starting eventer http service")
   118  
   119  	glog.Fatal(http.ListenAndServe(net.JoinHostPort(*argHealthzIP, strconv.Itoa(int(*argHealthzPort))), nil))
   120  }
   121  
   122  func validateFlags() error {
   123  	var minFrequency = 5 * time.Second
   124  
   125  	if *argFrequency < minFrequency {
   126  		return fmt.Errorf("frequency needs to be no less than %s, supplied %s", minFrequency,
   127  			*argFrequency)
   128  	}
   129  
   130  	if *argFrequency > api.MaxEventsScrapeDelay {
   131  		return fmt.Errorf("frequency needs to be no greater than %s, supplied %s",
   132  			api.MaxEventsScrapeDelay, *argFrequency)
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  func setMaxProcs() {
   139  	// Allow as many threads as we have cores unless the user specified a value.
   140  	var numProcs int
   141  	if *argMaxProcs < 1 {
   142  		numProcs = runtime.NumCPU()
   143  	} else {
   144  		numProcs = *argMaxProcs
   145  	}
   146  	runtime.GOMAXPROCS(numProcs)
   147  
   148  	// Check if the setting was successful.
   149  	actualNumProcs := runtime.GOMAXPROCS(0)
   150  	if actualNumProcs != numProcs {
   151  		glog.Warningf("Specified max procs of %d but using %d", numProcs, actualNumProcs)
   152  	}
   153  }