github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/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  	"os"
    23  	"runtime"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/golang/glog"
    28  	"k8s.io/heapster/common/flags"
    29  	"k8s.io/heapster/events/manager"
    30  	"k8s.io/heapster/events/sinks"
    31  	"k8s.io/heapster/events/sources"
    32  	"k8s.io/heapster/version"
    33  	"k8s.io/kubernetes/pkg/util/logs"
    34  )
    35  
    36  var (
    37  	argFrequency = flag.Duration("frequency", 30*time.Second, "The resolution at which Eventer pushes events to sinks")
    38  	argMaxProcs  = flag.Int("max_procs", 0, "max number of CPUs that can be used simultaneously. Less than 1 for default (number of cores)")
    39  	argSources   flags.Uris
    40  	argSinks     flags.Uris
    41  	argVersion   bool
    42  )
    43  
    44  func main() {
    45  	quitChannel := make(chan struct{}, 0)
    46  
    47  	flag.Var(&argSources, "source", "source(s) to read events from")
    48  	flag.Var(&argSinks, "sink", "external sink(s) that receive events")
    49  	flag.BoolVar(&argVersion, "version", false, "print version info and exit")
    50  	flag.Parse()
    51  
    52  	if argVersion {
    53  		fmt.Println(version.VersionInfo())
    54  		os.Exit(0)
    55  	}
    56  
    57  	logs.InitLogs()
    58  	defer logs.FlushLogs()
    59  
    60  	setMaxProcs()
    61  
    62  	glog.Infof(strings.Join(os.Args, " "))
    63  	glog.Infof("Eventer version %v", version.HeapsterVersion)
    64  	if err := validateFlags(); err != nil {
    65  		glog.Fatal(err)
    66  	}
    67  
    68  	// sources
    69  	if len(argSources) != 1 {
    70  		glog.Fatal("Wrong number of sources specified")
    71  	}
    72  	sourceFactory := sources.NewSourceFactory()
    73  	sources, err := sourceFactory.BuildAll(argSources)
    74  	if err != nil {
    75  		glog.Fatalf("Failed to create sources: %v", err)
    76  	}
    77  	if len(sources) != 1 {
    78  		glog.Fatal("Requires exactly 1 source")
    79  	}
    80  
    81  	// sinks
    82  	sinksFactory := sinks.NewSinkFactory()
    83  	sinkList := sinksFactory.BuildAll(argSinks)
    84  	if len([]flags.Uri(argSinks)) != 0 && len(sinkList) == 0 {
    85  		glog.Fatal("No available sink to use")
    86  	}
    87  
    88  	for _, sink := range sinkList {
    89  		glog.Infof("Starting with %s sink", sink.Name())
    90  	}
    91  	sinkManager, err := sinks.NewEventSinkManager(sinkList, sinks.DefaultSinkExportEventsTimeout, sinks.DefaultSinkStopTimeout)
    92  	if err != nil {
    93  		glog.Fatalf("Failed to create sink manager: %v", err)
    94  	}
    95  
    96  	// main manager
    97  	manager, err := manager.NewManager(sources[0], sinkManager, *argFrequency)
    98  	if err != nil {
    99  		glog.Fatalf("Failed to create main manager: %v", err)
   100  	}
   101  	manager.Start()
   102  
   103  	glog.Infof("Starting eventer")
   104  	<-quitChannel
   105  }
   106  
   107  func validateFlags() error {
   108  	if *argFrequency < 5*time.Second {
   109  		return fmt.Errorf("frequency needs to be greater than 5 seconds - %d", *argFrequency)
   110  	}
   111  	return nil
   112  }
   113  
   114  func setMaxProcs() {
   115  	// Allow as many threads as we have cores unless the user specified a value.
   116  	var numProcs int
   117  	if *argMaxProcs < 1 {
   118  		numProcs = runtime.NumCPU()
   119  	} else {
   120  		numProcs = *argMaxProcs
   121  	}
   122  	runtime.GOMAXPROCS(numProcs)
   123  
   124  	// Check if the setting was successful.
   125  	actualNumProcs := runtime.GOMAXPROCS(0)
   126  	if actualNumProcs != numProcs {
   127  		glog.Warningf("Specified max procs of %d but using %d", numProcs, actualNumProcs)
   128  	}
   129  }