Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(operator): Use Async Gauges for active KLC Entities #206

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions operator/api/v1alpha1/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,12 @@ const PostDeploymentEvaluationCheckType CheckType = "post-eval"
type KeptnMeters struct {
TaskCount syncint64.Counter
TaskDuration syncfloat64.Histogram
TaskActive syncint64.UpDownCounter
DeploymentCount syncint64.Counter
DeploymentDuration syncfloat64.Histogram
DeploymentActive syncint64.UpDownCounter
AppCount syncint64.Counter
AppDuration syncfloat64.Histogram
AppActive syncint64.UpDownCounter
EvaluationCount syncint64.Counter
EvaluationDuration syncfloat64.Histogram
EvaluationActive syncint64.UpDownCounter
}

const (
Expand Down Expand Up @@ -151,3 +147,8 @@ func GenerateEvaluationName(checkType CheckType, evalName string) string {
randomId := rand.Intn(99_999-10_000) + 10000
return fmt.Sprintf("%s-%s-%d", checkType, TruncateString(evalName, 27), randomId)
}

type GaugeValue struct {
Value int64
Attributes []attribute.KeyValue
}
31 changes: 24 additions & 7 deletions operator/controllers/keptnappversion/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return reconcile.Result{}, fmt.Errorf("could not fetch KeptnappVersion: %+v", err)
}

if !appVersion.IsStartTimeSet() {
// metrics: increment active app counter
r.Meters.AppActive.Add(ctx, 1, appVersion.GetActiveMetricsAttributes()...)
appVersion.SetStartTime()
}
appVersion.SetStartTime()

traceContextCarrier := propagation.MapCarrier(appVersion.Annotations)
ctx = otel.GetTextMapPropagator().Extract(ctx, traceContextCarrier)
Expand Down Expand Up @@ -145,8 +141,6 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ
// AppVersion is completed at this place

if !appVersion.IsEndTimeSet() {
// metrics: decrement active app counter
r.Meters.AppActive.Add(ctx, -1, appVersion.GetActiveMetricsAttributes()...)
appVersion.Status.CurrentPhase = common.PhaseCompleted.ShortName
appVersion.SetEndTime()
}
Expand Down Expand Up @@ -207,3 +201,26 @@ func (r *KeptnAppVersionReconciler) handlePhase(ctx context.Context, appVersion
}
return ctrl.Result{Requeue: true, RequeueAfter: 5 * time.Second}, nil
}

func (r *KeptnAppVersionReconciler) GetActiveApps(ctx context.Context) ([]common.GaugeValue, error) {
appInstances := &klcv1alpha1.KeptnAppVersionList{}
err := r.List(ctx, appInstances)
if err != nil {
return nil, fmt.Errorf("could not retrieve app versions: %w", err)
}

res := []common.GaugeValue{}

for _, appInstance := range appInstances.Items {
gaugeValue := int64(0)
if !appInstance.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: appInstance.GetActiveMetricsAttributes(),
})
}

return res, nil
}
35 changes: 25 additions & 10 deletions operator/controllers/keptnevaluation/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ

semconv.AddAttributeFromEvaluation(span, *evaluation)

if !evaluation.IsStartTimeSet() {
// metrics: increment active evaluation counter
r.Meters.EvaluationActive.Add(ctx, 1, evaluation.GetActiveMetricsAttributes()...)
evaluation.SetStartTime()
}
evaluation.SetStartTime()

if evaluation.Status.RetryCount >= evaluation.Spec.Retries {
r.recordEvent("Warning", evaluation, "ReconcileTimeOut", "retryCount exceeded")
Expand Down Expand Up @@ -182,11 +178,7 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ
func (r *KeptnEvaluationReconciler) updateFinishedEvaluationMetrics(ctx context.Context, evaluation *klcv1alpha1.KeptnEvaluation, span trace.Span) error {
r.recordEvent("Normal", evaluation, string(evaluation.Status.OverallStatus), "the evaluation has "+string(evaluation.Status.OverallStatus))

if !evaluation.IsEndTimeSet() {
// metrics: decrement active evaluation counter
r.Meters.EvaluationActive.Add(ctx, -1, evaluation.GetActiveMetricsAttributes()...)
evaluation.SetEndTime()
}
evaluation.SetEndTime()

err := r.Client.Status().Update(ctx, evaluation)
if err != nil {
Expand Down Expand Up @@ -329,3 +321,26 @@ func (r *KeptnEvaluationReconciler) checkValue(objective klcv1alpha1.Objective,
func (r *KeptnEvaluationReconciler) recordEvent(eventType string, evaluation *klcv1alpha1.KeptnEvaluation, shortReason string, longReason string) {
r.Recorder.Event(evaluation, eventType, shortReason, fmt.Sprintf("%s / Namespace: %s, Name: %s, WorkloadVersion: %s ", longReason, evaluation.Namespace, evaluation.Name, evaluation.Spec.WorkloadVersion))
}

func (r *KeptnEvaluationReconciler) GetActiveEvaluations(ctx context.Context) ([]common.GaugeValue, error) {
evaluations := &klcv1alpha1.KeptnEvaluationList{}
err := r.List(ctx, evaluations)
if err != nil {
return nil, fmt.Errorf("could not retrieve workload instances: %w", err)
}

res := []common.GaugeValue{}

for _, evaluation := range evaluations.Items {
gaugeValue := int64(0)
if !evaluation.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: evaluation.GetActiveMetricsAttributes(),
})
}

return res, nil
}
36 changes: 25 additions & 11 deletions operator/controllers/keptntask/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

semconv.AddAttributeFromTask(span, *task)

if !task.IsStartTimeSet() {
// metrics: increment active task counter
r.Meters.TaskActive.Add(ctx, 1, task.GetActiveMetricsAttributes()...)
task.SetStartTime()
}
task.SetStartTime()

err := r.Client.Status().Update(ctx, task)
if err != nil {
Expand Down Expand Up @@ -117,12 +113,7 @@ func (r *KeptnTaskReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
r.Log.Info("Finished Reconciling KeptnTask")

// Task is completed at this place

if !task.IsEndTimeSet() {
// metrics: decrement active task counter
r.Meters.TaskActive.Add(ctx, -1, task.GetActiveMetricsAttributes()...)
task.SetEndTime()
}
task.SetEndTime()

err = r.Client.Status().Update(ctx, task)
if err != nil {
Expand Down Expand Up @@ -175,3 +166,26 @@ func (r *KeptnTaskReconciler) JobExists(ctx context.Context, task klcv1alpha1.Ke

return false, nil
}

func (r *KeptnTaskReconciler) GetActiveTasks(ctx context.Context) ([]common.GaugeValue, error) {
tasks := &klcv1alpha1.KeptnTaskList{}
err := r.List(ctx, tasks)
if err != nil {
return nil, fmt.Errorf("could not retrieve workload instances: %w", err)
}

res := []common.GaugeValue{}

for _, task := range tasks.Items {
gaugeValue := int64(0)
if !task.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: task.GetActiveMetricsAttributes(),
})
}

return res, nil
}
31 changes: 24 additions & 7 deletions operator/controllers/keptnworkloadinstance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr

semconv.AddAttributeFromWorkloadInstance(span, *workloadInstance)

if !workloadInstance.IsStartTimeSet() {
// metrics: increment active deployment counter
r.Meters.DeploymentActive.Add(ctx, 1, workloadInstance.GetActiveMetricsAttributes()...)
workloadInstance.SetStartTime()
}
workloadInstance.SetStartTime()

//Wait for pre-evaluation checks of App
phase := common.PhaseAppPreEvaluation
Expand Down Expand Up @@ -173,8 +169,6 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr

// WorkloadInstance is completed at this place
if !workloadInstance.IsEndTimeSet() {
// metrics: decrement active deployment counter
r.Meters.DeploymentActive.Add(ctx, -1, workloadInstance.GetActiveMetricsAttributes()...)
workloadInstance.Status.CurrentPhase = common.PhaseCompleted.ShortName
workloadInstance.SetEndTime()
}
Expand All @@ -200,6 +194,29 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr
return ctrl.Result{}, nil
}

func (r *KeptnWorkloadInstanceReconciler) GetActiveDeployments(ctx context.Context) ([]common.GaugeValue, error) {
workloadInstances := &klcv1alpha1.KeptnWorkloadInstanceList{}
err := r.List(ctx, workloadInstances)
if err != nil {
return nil, fmt.Errorf("could not retrieve workload instances: %w", err)
}

res := []common.GaugeValue{}

for _, workloadInstance := range workloadInstances.Items {
gaugeValue := int64(0)
if !workloadInstance.IsEndTimeSet() {
gaugeValue = int64(1)
}
res = append(res, common.GaugeValue{
Value: gaugeValue,
Attributes: workloadInstance.GetActiveMetricsAttributes(),
})
}

return res, nil
}

func (r *KeptnWorkloadInstanceReconciler) handlePhase(ctx context.Context, workloadInstance *klcv1alpha1.KeptnWorkloadInstance, phase common.KeptnPhaseType, span trace.Span, phaseFailed func() bool, reconcilePhase func() (common.KeptnState, error)) (ctrl.Result, error) {
r.Log.Info(phase.LongName + " not finished")
oldPhase := workloadInstance.Status.CurrentPhase
Expand Down
Loading