//monitorNewDeployment loops, checking the new deployment metrics against those from the old deployment, rolling back if need be
monitorNewDeployment loops, checking the new deployment metrics against those from the old deployment, rolling back if need be
// monitorNewDeployment loops checking the new deployment metrics against those from the old deployment rolling back if need be
func (c *Barrelman) monitorNewDeployment(appName string, oldDepl, newDepl *appsv1.Deployment, deploymentMetadata *v1alpha1.DeploymentMetadata,
	oldMonitor *v1alpha1.DeploymentMonitor, monitorNotFound bool, strategy string) {
	glog.Infof("Beginning to monitor new deployment %v vs. old deployment %v, strategy %s",
		newDepl.Spec.Template.Spec.Containers[0].Image, oldDepl.Spec.Template.Spec.Containers[0].Image, strategy)
	glog.Infof("Beginning to monitor new env %v vs. old env %v",
		newDepl.Spec.Template.Spec.Containers[0].Env, oldDepl.Spec.Template.Spec.Containers[0].Env)
	var podNames [][]string
	var err error
	if !(strategy == m.StrategyContinuous || strategy == m.StrategyHpa) {
		podNames, err = c.getPodNames(oldDepl, newDepl, c.kubeclientset)
		if err != nil {
			glog.Infof("Get pod names error %v %s", newDepl, err)
			return
		}
		glog.Infof("Found pod names: %s, %d", podNames[0], len(podNames))
	}
	var jobId string
	var phase string
	if oldMonitor == nil {
		var oldMonitor2, er = c.foremastClientset.DeploymentV1alpha1().DeploymentMonitors(newDepl.Namespace).Get(newDepl.Name, metav1.GetOptions{})
		if er == nil {
			oldMonitor = oldMonitor2
		}
	}
	if (strategy == m.StrategyContinuous || strategy == m.StrategyHpa) || (oldMonitor != nil && !oldMonitor.Spec.Continuous) { 
		analystClient, err := a.NewClient(nil, deploymentMetadata.Spec.Analyst.Endpoint)
		if err != nil {
			glog.Infof("Creating foremast service request error %v %s", newDepl, err)
			return
		}
		var metrics = deploymentMetadata.Spec.Metrics
		var metricAliases []string = nil
		if strategy == m.StrategyHpa { 
			if oldMonitor.Spec.HpaScoreTemplate == "" {
				glog.Infof("No HpaScore Template ignore %v", oldMonitor)
				return
			}
			for _, t := range deploymentMetadata.Spec.HpaScoreTemplates {
				if oldMonitor.Spec.HpaScoreTemplate == t.Name {
					metricAliases = t.Metrics
					break
				}
			}
		}
		jobId, err = analystClient.StartAnalyzing(newDepl.Namespace, appName, podNames, deploymentMetadata.Spec.Metrics.Endpoint, metrics, watchTime, strategy, metricAliases)
		if err != nil {
			glog.Infof("Starting analyzing error, try it again: %v", err)
			jobId, err = analystClient.StartAnalyzing(newDepl.Namespace, appName, podNames, deploymentMetadata.Spec.Metrics.Endpoint, metrics, watchTime, strategy, metricAliases)
			if err != nil {
				glog.Infof("Tried twice to analyzing error: %v", err)
				return
			}
		}
		phase = v1alpha1.MonitorPhaseRunning
	} else { 
		jobId = oldMonitor.Status.JobId
		phase = oldMonitor.Status.Phase
	}
	if oldMonitor != nil {
		var t = time.Now()
		var start = t.Format(time.RFC3339)
		t = t.Add(waitUntilMax * time.Minute)
		var waitUntil = t.Format(time.RFC3339)
		oldMonitor.SetGroupVersionKind(schema.GroupVersionKind{
			Group:   "deployment.foremast.ai",
			Kind:    "DeploymentMonitor",
			Version: "v1alpha1",
		})
		oldMonitor.Namespace = newDepl.Namespace
		oldMonitor.Name = newDepl.Name
		if oldMonitor.Annotations == nil {
			oldMonitor.Annotations = map[string]string{}
		}
		var oldRevision = oldMonitor.Spec.RollbackRevision
		if strategy == m.StrategyRollingUpdate {
			oldRevision, _ = deploymentutil.Revision(oldDepl)
		}
		oldMonitor.Annotations[DeploymentName] = newDepl.Name
		var remediationOption = v1alpha1.RemediationNone
		if oldMonitor.Spec.Remediation.Option != "" {
			remediationOption = oldMonitor.Spec.Remediation.Option
		}
		oldMonitor.Spec = v1alpha1.DeploymentMonitorSpec{
			Selector:  newDepl.Spec.Selector,
			Analyst:   deploymentMetadata.Spec.Analyst,
			StartTime: start,
			WaitUntil: waitUntil,
			Metrics:   deploymentMetadata.Spec.Metrics,
			Logs:      deploymentMetadata.Spec.Logs,
			Remediation: v1alpha1.RemediationAction{
				Option: remediationOption,
			},
			Continuous:       oldMonitor.Spec.Continuous,
			HpaScoreTemplate: oldMonitor.Spec.HpaScoreTemplate,
			RollbackRevision: oldRevision,
		}
		var HpaScoreEnabled = oldMonitor.Status.HpaScoreEnabled
		oldMonitor.Status = v1alpha1.DeploymentMonitorStatus{
			JobId:           jobId,
			Phase:           phase,
			Timestamp:       start,
			HpaScoreEnabled: HpaScoreEnabled,
		}
	}
	if monitorNotFound { 
		_, err = c.foremastClientset.DeploymentV1alpha1().DeploymentMonitors(newDepl.Namespace).Create(oldMonitor)
		if err != nil {
			glog.Infof("Creating DeploymentMonitor error: %v", err)
		} else {
			glog.Infof("Created DeploymentMonitor %v", oldMonitor)
		}
	} else {
		_, err = c.foremastClientset.DeploymentV1alpha1().DeploymentMonitors(newDepl.Namespace).Update(oldMonitor)
		if err != nil {
			glog.Infof("Updating DeploymentMonitor error: %v", err)
		} else {
			glog.Infof("Updated DeploymentMonitor %v", oldMonitor)
		}
	}
}
func ( c * Barrelman ) monitorNewDeployment ( appName string , oldDepl , newDepl * appsv1 . Deployment , deploymentMetadata * v1alpha1 . DeploymentMetadata , oldMonitor * v1alpha1 . DeploymentMonitor , monitorNotFound bool , strategy string ) { glog . Infof ( " " , newDepl . Spec . Template . Spec . Containers [ 0 ] . Image , oldDepl . Spec . Template . Spec . Containers [ 0 ] . Image , strategy ) glog . Infof ( " " , newDepl . Spec . Template . Spec . Containers [ 0 ] . Env , oldDepl . Spec . Template . Spec . Containers [ 0 ] . Env ) var podNames [ ] [ ] string var err error if ! ( strategy == m . StrategyContinuous || strategy == m . StrategyHpa ) { podNames , err = c . getPodNames ( oldDepl , newDepl , c . kubeclientset ) if err != nil { glog . Infof ( " " , newDepl , err ) return } glog . Infof ( " " , podNames [ 0 ] , len ( podNames ) ) } var jobId string var phase string if oldMonitor == nil { var oldMonitor2 , er = c . foremastClientset . DeploymentV1alpha1 ( ) . DeploymentMonitors ( newDepl . Namespace ) . Get ( newDepl . Name , metav1 . GetOptions { } ) if er == nil { oldMonitor = oldMonitor2 } } if ( strategy == m . StrategyContinuous || strategy == m . StrategyHpa ) || ( oldMonitor != nil && ! oldMonitor . Spec . Continuous ) { analystClient , err := a . NewClient ( nil , deploymentMetadata . Spec . Analyst . Endpoint ) if err != nil { glog . Infof ( " " , newDepl , err ) return } var metrics = deploymentMetadata . Spec . Metrics var metricAliases [ ] string = nil if strategy == m . StrategyHpa { if oldMonitor . Spec . HpaScoreTemplate == " " { glog . Infof ( " " , oldMonitor ) return } for _ , t := range deploymentMetadata . Spec . HpaScoreTemplates { if oldMonitor . Spec . HpaScoreTemplate == t . Name { metricAliases = t . Metrics break } } } jobId , err = analystClient . StartAnalyzing ( newDepl . Namespace , appName , podNames , deploymentMetadata . Spec . Metrics . Endpoint , metrics , watchTime , strategy , metricAliases ) if err != nil { glog . Infof ( " " , err ) jobId , err = analystClient . StartAnalyzing ( newDepl . Namespace , appName , podNames , deploymentMetadata . Spec . Metrics . Endpoint , metrics , watchTime , strategy , metricAliases ) if err != nil { glog . Infof ( " " , err ) return } } phase = v1alpha1 . MonitorPhaseRunning } else { jobId = oldMonitor . Status . JobId phase = oldMonitor . Status . Phase } if oldMonitor != nil { var t = time . Now ( ) var start = t . Format ( time . RFC3339 ) t = t . Add ( waitUntilMax * time . Minute ) var waitUntil = t . Format ( time . RFC3339 ) oldMonitor . SetGroupVersionKind ( schema . GroupVersionKind { Group : " " , Kind : " " , Version : " " , } ) oldMonitor . Namespace = newDepl . Namespace oldMonitor . Name = newDepl . Name if oldMonitor . Annotations == nil { oldMonitor . Annotations = map [ string ] string { } } var oldRevision = oldMonitor . Spec . RollbackRevision if strategy == m . StrategyRollingUpdate { oldRevision , _ = deploymentutil . Revision ( oldDepl ) } oldMonitor . Annotations [ DeploymentName ] = newDepl . Name var remediationOption = v1alpha1 . RemediationNone if oldMonitor . Spec . Remediation . Option != " " { remediationOption = oldMonitor . Spec . Remediation . Option } oldMonitor . Spec = v1alpha1 . DeploymentMonitorSpec { Selector : newDepl . Spec . Selector , Analyst : deploymentMetadata . Spec . Analyst , StartTime : start , WaitUntil : waitUntil , Metrics : deploymentMetadata . Spec . Metrics , Logs : deploymentMetadata . Spec . Logs , Remediation : v1alpha1 . RemediationAction { Option : remediationOption , } , Continuous : oldMonitor . Spec . Continuous , HpaScoreTemplate : oldMonitor . Spec . HpaScoreTemplate , RollbackRevision : oldRevision , } var HpaScoreEnabled = oldMonitor . Status . HpaScoreEnabled oldMonitor . Status = v1alpha1 . DeploymentMonitorStatus { JobId : jobId , Phase : phase , Timestamp : start , HpaScoreEnabled : HpaScoreEnabled , } } if monitorNotFound { _ , err = c . foremastClientset . DeploymentV1alpha1 ( ) . DeploymentMonitors ( newDepl . Namespace ) . Create ( oldMonitor ) if err != nil { glog . Infof ( " " , err ) } else { glog . Infof ( " " , oldMonitor ) } } else { _ , err = c . foremastClientset . DeploymentV1alpha1 ( ) . DeploymentMonitors ( newDepl . Namespace ) . Update ( oldMonitor ) if err != nil { glog . Infof ( " " , err ) } else { glog . Infof ( " " , oldMonitor ) } } }
monitorNewDeployment loops, checking the new deployment metrics against those from the old deployment, rolling back if need be
monitorNewDeployment loops checking the new deployment metrics against those from the old deployment rolling back if need be
//begin monitoring new deployment //Only generates job when the continuous monitoring is not running //Select metrics by hpaScoreTemplate //Take the old jobId //Create deployment analysis object if it doesn't exist // default is 0 //Not found