// NewDeploymentController returns a new sample controller
NewDeploymentController returns a new sample controller
// NewDeploymentController returns a new sample controller
func NewHpaController(kubeclientset kubernetes.Interface, foremastClientset clientset.Interface,
	hpaInfomer hpainformers.HorizontalPodAutoscalerInformer, barrelman *Barrelman) *HpaController {
	glog.V(4).Info("Creating event broadcaster:" + ForemastHPA)
	eventBroadcaster := record.NewBroadcaster()
	eventBroadcaster.StartLogging(glog.Infof)
	eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")})
	recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: ForemastHPA})
	controller := &HpaController{
		kubeclientset:     kubeclientset,
		foremastClientset: foremastClientset,
		recorder:          recorder,
		barrelman:         barrelman,
	}
	glog.Info("Setting up event handlers for HpaController")
	glog.Info("Creating HpaController")
	hpaInfomer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			newHpa := obj.(*asv2.HorizontalPodAutoscaler)
			controller.updateDeploymentMonitor(newHpa)
		},
		UpdateFunc: func(old, new interface{}) {
			oldHpa := old.(*asv2.HorizontalPodAutoscaler)
			newHpa := new.(*asv2.HorizontalPodAutoscaler)
			controller.updateDeploymentMonitor(newHpa)
			if oldHpa.Status.DesiredReplicas != newHpa.Status.DesiredReplicas && len(newHpa.Spec.Metrics) > 0 {
				var l = len(newHpa.Spec.Metrics)
				for i := 0; i < l; i++ {
					var m = newHpa.Spec.Metrics[i]
					if m.Type == "Object" && m.Object.Metric.Name == "namespace_app_pod_hpa_score" {
						monitor := controller.getDeploymentMonitor(newHpa)
						alertContent := HpaAlertContent{}
						alertContent.Timestamp = time.Now().Format(time.RFC1123)
						alertContent.Application = monitor.Annotations[DeploymentName]
						alertContent.Namespace = monitor.Namespace
						alertContent.Action = "up"
						alertContent.Old = strconv.Itoa(int(oldHpa.Status.CurrentReplicas))
						hpaEntries := []d.HpaLogEntry{}
						sort.Slice(monitor.Status.HpaLogs, func(i, j int) bool { 
							return monitor.Status.HpaLogs[i].Timestamp > monitor.Status.HpaLogs[j].Timestamp
						})
						current := strconv.FormatInt(time.Now().Unix(), 10)
						logCount := 4 
						if newHpa.Status.DesiredReplicas < oldHpa.Status.CurrentReplicas {
							logCount = 6
							alertContent.Action = "down"
						}
						for _, l := range monitor.Status.HpaLogs {
							if current >= l.Timestamp {
								logTime, _ := strconv.ParseFloat(l.Timestamp, 10)
								l.Timestamp = time.Unix(int64(logTime), 0).Format(time.RFC1123)
								hpaEntries = append(hpaEntries, l)
								if alertContent.New == "" {
									alertContent.New = strconv.Itoa(int(newHpa.Status.DesiredReplicas))
								}
								if len(hpaEntries) >= logCount {
									break
								}
							}
						}
						alertContent.HpaLogEntry = hpaEntries
						tmpl := template.Must(template.New("letter").Parse(letter))
						var buf bytes.Buffer
						tmpl.Execute(&buf, alertContent)
						glog.Infof("%v", buf.String())
					}
				}
			}
		},
		DeleteFunc: func(obj interface{}) {
			hpa := obj.(*asv2.HorizontalPodAutoscaler)
			if hpa == nil {
				return
			}
			monitor := controller.getDeploymentMonitor(hpa)
			if monitor != nil {
				monitor.Spec.HpaScoreTemplate = ""
				controller.foremastClientset.DeploymentV1alpha1().DeploymentMonitors(hpa.Namespace).Update(monitor)
				glog.V(4).Infof("Updating deployment monitor error, while HPA got deleted: %s", monitor.GetName())
			}
		},
	})
	return controller
}
func NewHpaController ( kubeclientset kubernetes . Interface , foremastClientset clientset . Interface , hpaInfomer hpainformers . HorizontalPodAutoscalerInformer , barrelman * Barrelman ) * HpaController { glog . V ( 4 ) . Info ( " " + ForemastHPA ) eventBroadcaster := record . NewBroadcaster ( ) eventBroadcaster . StartLogging ( glog . Infof ) eventBroadcaster . StartRecordingToSink ( & typedcorev1 . EventSinkImpl { Interface : kubeclientset . CoreV1 ( ) . Events ( " " ) } ) recorder := eventBroadcaster . NewRecorder ( scheme . Scheme , corev1 . EventSource { Component : ForemastHPA } ) controller := & HpaController { kubeclientset : kubeclientset , foremastClientset : foremastClientset , recorder : recorder , barrelman : barrelman , } glog . Info ( " " ) glog . Info ( " " ) hpaInfomer . Informer ( ) . AddEventHandler ( cache . ResourceEventHandlerFuncs { AddFunc : func ( obj interface { } ) { newHpa := obj . ( * asv2 . HorizontalPodAutoscaler ) controller . updateDeploymentMonitor ( newHpa ) } , UpdateFunc : func ( old , new interface { } ) { oldHpa := old . ( * asv2 . HorizontalPodAutoscaler ) newHpa := new . ( * asv2 . HorizontalPodAutoscaler ) controller . updateDeploymentMonitor ( newHpa ) if oldHpa . Status . DesiredReplicas != newHpa . Status . DesiredReplicas && len ( newHpa . Spec . Metrics ) > 0 { var l = len ( newHpa . Spec . Metrics ) for i := 0 ; i < l ; i ++ { var m = newHpa . Spec . Metrics [ i ] if m . Type == " " && m . Object . Metric . Name == " " { monitor := controller . getDeploymentMonitor ( newHpa ) alertContent := HpaAlertContent { } alertContent . Timestamp = time . Now ( ) . Format ( time . RFC1123 ) alertContent . Application = monitor . Annotations [ DeploymentName ] alertContent . Namespace = monitor . Namespace alertContent . Action = " " alertContent . Old = strconv . Itoa ( int ( oldHpa . Status . CurrentReplicas ) ) hpaEntries := [ ] d . HpaLogEntry { } sort . Slice ( monitor . Status . HpaLogs , func ( i , j int ) bool { return monitor . Status . HpaLogs [ i ] . Timestamp > monitor . Status . HpaLogs [ j ] . Timestamp } ) current := strconv . FormatInt ( time . Now ( ) . Unix ( ) , 10 ) logCount := 4 if newHpa . Status . DesiredReplicas < oldHpa . Status . CurrentReplicas { logCount = 6 alertContent . Action = " " } for _ , l := range monitor . Status . HpaLogs { if current >= l . Timestamp { logTime , _ := strconv . ParseFloat ( l . Timestamp , 10 ) l . Timestamp = time . Unix ( int64 ( logTime ) , 0 ) . Format ( time . RFC1123 ) hpaEntries = append ( hpaEntries , l ) if alertContent . New == " " { alertContent . New = strconv . Itoa ( int ( newHpa . Status . DesiredReplicas ) ) } if len ( hpaEntries ) >= logCount { break } } } alertContent . HpaLogEntry = hpaEntries tmpl := template . Must ( template . New ( " " ) . Parse ( letter ) ) var buf bytes . Buffer tmpl . Execute ( & buf , alertContent ) glog . Infof ( " " , buf . String ( ) ) } } } } , DeleteFunc : func ( obj interface { } ) { hpa := obj . ( * asv2 . HorizontalPodAutoscaler ) if hpa == nil { return } monitor := controller . getDeploymentMonitor ( hpa ) if monitor != nil { monitor . Spec . HpaScoreTemplate = " " controller . foremastClientset . DeploymentV1alpha1 ( ) . DeploymentMonitors ( hpa . Namespace ) . Update ( monitor ) glog . V ( 4 ) . Infof ( " " , monitor . GetName ( ) ) } } , } ) return controller }
NewDeploymentController returns a new sample controller
NewDeploymentController returns a new sample controller
// Create event broadcaster // Create event broadcaster // If there is desiredReplicas changed //TODO check the status from CRD and get the hpalog from CRD // desc // default scaling up log count // find most recently 4 scaling down logs // write to log //TODO foremast-brain