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

     1  // Copyright 2015 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  package manager
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/golang/glog"
    21  	"github.com/prometheus/client_golang/prometheus"
    22  	"k8s.io/heapster/events/core"
    23  )
    24  
    25  var (
    26  	// Last time of eventer housekeep since unix epoch in seconds
    27  	lastHousekeepTimestamp = prometheus.NewGauge(
    28  		prometheus.GaugeOpts{
    29  			Namespace: "eventer",
    30  			Subsystem: "manager",
    31  			Name:      "last_time_seconds",
    32  			Help:      "Last time of eventer housekeep since unix epoch in seconds.",
    33  		})
    34  
    35  	// Time of latest scrape operation
    36  	LatestScrapeTime = time.Now()
    37  )
    38  
    39  func init() {
    40  	prometheus.MustRegister(lastHousekeepTimestamp)
    41  }
    42  
    43  type Manager interface {
    44  	Start()
    45  	Stop()
    46  }
    47  
    48  type realManager struct {
    49  	source    core.EventSource
    50  	sink      core.EventSink
    51  	frequency time.Duration
    52  	stopChan  chan struct{}
    53  }
    54  
    55  func NewManager(source core.EventSource, sink core.EventSink, frequency time.Duration) (Manager, error) {
    56  	manager := realManager{
    57  		source:    source,
    58  		sink:      sink,
    59  		frequency: frequency,
    60  		stopChan:  make(chan struct{}),
    61  	}
    62  
    63  	return &manager, nil
    64  }
    65  
    66  func (rm *realManager) Start() {
    67  	go rm.Housekeep()
    68  }
    69  
    70  func (rm *realManager) Stop() {
    71  	rm.stopChan <- struct{}{}
    72  }
    73  
    74  func (rm *realManager) Housekeep() {
    75  	for {
    76  		// Try to infovke housekeep at fixed time.
    77  		now := time.Now()
    78  		start := now.Truncate(rm.frequency)
    79  		end := start.Add(rm.frequency)
    80  		timeToNextSync := end.Sub(now)
    81  
    82  		select {
    83  		case <-time.After(timeToNextSync):
    84  			rm.housekeep()
    85  		case <-rm.stopChan:
    86  			rm.sink.Stop()
    87  			return
    88  		}
    89  	}
    90  }
    91  
    92  func (rm *realManager) housekeep() {
    93  	defer func() {
    94  		lastHousekeepTimestamp.Set(float64(time.Now().Unix()))
    95  	}()
    96  
    97  	LatestScrapeTime = time.Now()
    98  
    99  	// No parallelism. Assumes that the events are pushed to Heapster. Add parallelism
   100  	// when this stops to be true.
   101  	events := rm.source.GetNewEvents()
   102  	glog.V(0).Infof("Exporting %d events", len(events.Events))
   103  	rm.sink.ExportEvents(events)
   104  }