From 79db08953323bd7bddc062714d63a0ab45eb23d1 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 14:56:36 +0100 Subject: [PATCH 01/23] adjust linter settings Signed-off-by: Moritz Wiesinger --- .golangci.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 17f107a0a8..e9b26081c3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,6 +10,9 @@ linters: - dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f()) - nilnil # Checks that there is no simultaneous return of nil error and an invalid value. - noctx # noctx finds sending http request without context.Context + - gocyclo # measure cyclomatic complexity + - gocognit # measure cognitive complexity + - funlen # limit function length issues: exclude-rules: @@ -18,4 +21,14 @@ issues: text: '//+kubebuilder' - linters: - containedctx + - gocyclo path: _test\.go + +linters-settings: + gocyclo: + min-complexity: 10 + gocognit: + min-complexity: 20 + funlen: + lines: 120 + statements: 120 From b1cb161c172a063cf79d70454a3c3f57d9cdd38c Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:18:08 +0100 Subject: [PATCH 02/23] refactoring Signed-off-by: Moritz Wiesinger --- .../controllers/keptnevaluation/controller.go | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/operator/controllers/keptnevaluation/controller.go b/operator/controllers/keptnevaluation/controller.go index 1d7447354e..018c8ba004 100644 --- a/operator/controllers/keptnevaluation/controller.go +++ b/operator/controllers/keptnevaluation/controller.go @@ -79,9 +79,10 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ // taking down all associated K8s resources is handled by K8s r.Log.Info("KeptnEvaluation resource not found. Ignoring since object must be deleted") return ctrl.Result{}, nil + } else { + r.Log.Error(err, "Failed to get the KeptnEvaluation") + return ctrl.Result{}, nil } - r.Log.Error(err, "Failed to get the KeptnEvaluation") - return ctrl.Result{}, nil } traceContextCarrier := propagation.MapCarrier(evaluation.Annotations) @@ -91,19 +92,10 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ defer span.End() evaluation.SetSpanAttributes(span) - evaluation.SetStartTime() if evaluation.Status.RetryCount >= evaluation.Spec.Retries { - r.recordEvent("Warning", evaluation, "ReconcileTimeOut", "retryCount exceeded") - err := controllererrors.ErrRetryCountExceeded - span.SetStatus(codes.Error, err.Error()) - evaluation.Status.OverallStatus = apicommon.StateFailed - err2 := r.updateFinishedEvaluationMetrics(ctx, evaluation, span) - if err2 != nil { - r.Log.Error(err2, "failed to update finished evaluation metrics") - } - return ctrl.Result{}, nil + return r.recordTooManyRetries(ctx, evaluation, span) } if !evaluation.Status.OverallStatus.IsSucceeded() { @@ -116,9 +108,10 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ if errors.IsNotFound(err) { r.Log.Info(err.Error() + ", ignoring error since object must be deleted") return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil + } else { + r.Log.Error(err, "Failed to retrieve a resource") + return ctrl.Result{}, nil } - r.Log.Error(err, "Failed to retrieve a resource") - return ctrl.Result{}, nil } statusSummary := apicommon.StatusSummary{} @@ -150,7 +143,6 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ } else { evaluation.Status.OverallStatus = apicommon.StateProgressing } - } if !evaluation.Status.OverallStatus.IsSucceeded() { @@ -165,15 +157,24 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ r.recordEvent("Normal", evaluation, "NotFinished", "has not finished") return ctrl.Result{Requeue: true, RequeueAfter: evaluation.Spec.RetryInterval.Duration}, nil - } r.Log.Info("Finished Reconciling KeptnEvaluation") - err := r.updateFinishedEvaluationMetrics(ctx, evaluation, span) return ctrl.Result{}, err +} +func (r *KeptnEvaluationReconciler) recordTooManyRetries(ctx context.Context, evaluation *klcv1alpha1.KeptnEvaluation, span trace.Span) (ctrl.Result, error) { + r.recordEvent("Warning", evaluation, "ReconcileTimeOut", "retryCount exceeded") + err := controllererrors.ErrRetryCountExceeded + span.SetStatus(codes.Error, err.Error()) + evaluation.Status.OverallStatus = apicommon.StateFailed + err2 := r.updateFinishedEvaluationMetrics(ctx, evaluation, span) + if err2 != nil { + r.Log.Error(err2, "failed to update finished evaluation metrics") + } + return ctrl.Result{}, nil } func (r *KeptnEvaluationReconciler) updateFinishedEvaluationMetrics(ctx context.Context, evaluation *klcv1alpha1.KeptnEvaluation, span trace.Span) error { From 87543b98c71aabaea6c7d2ded109fadfd3237848 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:22:01 +0100 Subject: [PATCH 03/23] update linter config Signed-off-by: Moritz Wiesinger --- .golangci.yml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e9b26081c3..b5a457723d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,16 +3,16 @@ run: go: '1.19' linters: enable: - - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification - - gci # Gci controls golang package import order and makes it always deterministic. - - errorlint # errorlint is a linter that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. - - containedctx # containedctx is a linter that detects struct contained context.Context field - - dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f()) - - nilnil # Checks that there is no simultaneous return of nil error and an invalid value. - - noctx # noctx finds sending http request without context.Context - - gocyclo # measure cyclomatic complexity +# - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification +# - gci # Gci controls golang package import order and makes it always deterministic. +# - errorlint # errorlint is a linter that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. +# - containedctx # containedctx is a linter that detects struct contained context.Context field +# - dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f()) +# - nilnil # Checks that there is no simultaneous return of nil error and an invalid value. +# - noctx # noctx finds sending http request without context.Context +# - gocyclo # measure cyclomatic complexity - gocognit # measure cognitive complexity - - funlen # limit function length +# - funlen # limit function length issues: exclude-rules: @@ -22,6 +22,7 @@ issues: - linters: - containedctx - gocyclo + - gocognit path: _test\.go linters-settings: From 5a42bdd2b3fe573ff5ec1de8607abfa97c9ed2d3 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:26:01 +0100 Subject: [PATCH 04/23] styling Signed-off-by: Moritz Wiesinger --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index b5a457723d..59c42a224e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,7 +10,7 @@ linters: # - dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f()) # - nilnil # Checks that there is no simultaneous return of nil error and an invalid value. # - noctx # noctx finds sending http request without context.Context -# - gocyclo # measure cyclomatic complexity +# - gocyclo # measure cyclomatic complexity - gocognit # measure cognitive complexity # - funlen # limit function length From 02baf6141da77b0570d278926c48062d311ab05b Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:26:04 +0100 Subject: [PATCH 05/23] refactoring Signed-off-by: Moritz Wiesinger --- .../controllers/keptnevaluation/controller.go | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/operator/controllers/keptnevaluation/controller.go b/operator/controllers/keptnevaluation/controller.go index 018c8ba004..ccb61df516 100644 --- a/operator/controllers/keptnevaluation/controller.go +++ b/operator/controllers/keptnevaluation/controller.go @@ -122,19 +122,7 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ evaluation.Status.EvaluationStatus = make(map[string]klcv1alpha1.EvaluationStatusItem) } - for _, query := range evaluationDefinition.Spec.Objectives { - if _, ok := evaluation.Status.EvaluationStatus[query.Name]; !ok { - evaluation.AddEvaluationStatus(query) - } - if evaluation.Status.EvaluationStatus[query.Name].Status.IsSucceeded() { - statusSummary = apicommon.UpdateStatusSummary(apicommon.StateSucceeded, statusSummary) - newStatus[query.Name] = evaluation.Status.EvaluationStatus[query.Name] - continue - } - statusItem := r.queryEvaluation(query, *evaluationProvider) - statusSummary = apicommon.UpdateStatusSummary(statusItem.Status, statusSummary) - newStatus[query.Name] = *statusItem - } + statusSummary = r.updateEvaluationStatuses(evaluationDefinition, evaluation, statusSummary, newStatus, evaluationProvider) evaluation.Status.RetryCount++ evaluation.Status.EvaluationStatus = newStatus @@ -165,6 +153,23 @@ func (r *KeptnEvaluationReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } +func (r *KeptnEvaluationReconciler) updateEvaluationStatuses(evaluationDefinition *klcv1alpha1.KeptnEvaluationDefinition, evaluation *klcv1alpha1.KeptnEvaluation, statusSummary apicommon.StatusSummary, newStatus map[string]klcv1alpha1.EvaluationStatusItem, evaluationProvider *klcv1alpha1.KeptnEvaluationProvider) apicommon.StatusSummary { + for _, query := range evaluationDefinition.Spec.Objectives { + if _, ok := evaluation.Status.EvaluationStatus[query.Name]; !ok { + evaluation.AddEvaluationStatus(query) + } + if evaluation.Status.EvaluationStatus[query.Name].Status.IsSucceeded() { + statusSummary = apicommon.UpdateStatusSummary(apicommon.StateSucceeded, statusSummary) + newStatus[query.Name] = evaluation.Status.EvaluationStatus[query.Name] + continue + } + statusItem := r.queryEvaluation(query, *evaluationProvider) + statusSummary = apicommon.UpdateStatusSummary(statusItem.Status, statusSummary) + newStatus[query.Name] = *statusItem + } + return statusSummary +} + func (r *KeptnEvaluationReconciler) recordTooManyRetries(ctx context.Context, evaluation *klcv1alpha1.KeptnEvaluation, span trace.Span) (ctrl.Result, error) { r.recordEvent("Warning", evaluation, "ReconcileTimeOut", "retryCount exceeded") err := controllererrors.ErrRetryCountExceeded From cea0a7f63fe8d71d10f65cf9b296f1407c0b678a Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:38:42 +0100 Subject: [PATCH 06/23] refactor main Signed-off-by: Moritz Wiesinger --- operator/main.go | 218 ++++++++++++++++++++++++++--------------------- 1 file changed, 122 insertions(+), 96 deletions(-) diff --git a/operator/main.go b/operator/main.go index b81a8900f2..abd99074f3 100644 --- a/operator/main.go +++ b/operator/main.go @@ -23,6 +23,7 @@ import ( "log" "net/http" "os" + "sigs.k8s.io/controller-runtime/pkg/manager" "time" "github.com/kelseyhightower/envconfig" @@ -257,94 +258,13 @@ func main() { Log: ctrl.Log.WithName("Mutating Webhook"), }}) } - taskReconciler := &keptntask.KeptnTaskReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnTask Controller"), - Recorder: mgr.GetEventRecorderFor("keptntask-controller"), - Meters: meters, - Tracer: otel.Tracer("keptn/operator/task"), - } - if err = (taskReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnTask") - os.Exit(1) - } - - taskDefinitionReconciler := &keptntaskdefinition.KeptnTaskDefinitionReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnTaskDefinition Controller"), - Recorder: mgr.GetEventRecorderFor("keptntaskdefinition-controller"), - } - if err = (taskDefinitionReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnTaskDefinition") - os.Exit(1) - } - - appReconciler := &keptnapp.KeptnAppReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnApp Controller"), - Recorder: mgr.GetEventRecorderFor("keptnapp-controller"), - Tracer: otel.Tracer("keptn/operator/app"), - } - if err = (appReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnApp") - os.Exit(1) - } - - workloadReconciler := &keptnworkload.KeptnWorkloadReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnWorkload Controller"), - Recorder: mgr.GetEventRecorderFor("keptnworkload-controller"), - Tracer: otel.Tracer("keptn/operator/workload"), - } - if err = (workloadReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnWorkload") - os.Exit(1) - } - - workloadInstanceReconciler := &keptnworkloadinstance.KeptnWorkloadInstanceReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnWorkloadInstance Controller"), - Recorder: mgr.GetEventRecorderFor("keptnworkloadinstance-controller"), - Meters: meters, - Tracer: otel.Tracer("keptn/operator/workloadinstance"), - SpanHandler: spanHandler, - } - if err = (workloadInstanceReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnWorkloadInstance") - os.Exit(1) - } - - appVersionReconciler := &keptnappversion.KeptnAppVersionReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnAppVersion Controller"), - Recorder: mgr.GetEventRecorderFor("keptnappversion-controller"), - Tracer: otel.Tracer("keptn/operator/appversion"), - Meters: meters, - SpanHandler: spanHandler, - } - if err = (appVersionReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnAppVersion") - os.Exit(1) - } - - evaluationReconciler := &keptnevaluation.KeptnEvaluationReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - Log: ctrl.Log.WithName("KeptnEvaluation Controller"), - Recorder: mgr.GetEventRecorderFor("keptnevaluation-controller"), - Tracer: otel.Tracer("keptn/operator/evaluation"), - Meters: meters, - } - if err = (evaluationReconciler).SetupWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create controller", "controller", "KeptnEvaluation") - os.Exit(1) - } + setupTaskReconciler(mgr, meters) + setupTaskDefinitionReconciler(mgr) + setupAppReconciler(mgr) + setupWorkloadReconciler(mgr) + setupWorkloadInstanceReconciler(mgr, meters, spanHandler) + setupAppVersionReconciler(mgr, meters, spanHandler) + setupEvaluationReconciler(mgr, meters) //+kubebuilder:scaffold:builder err = meter.RegisterCallback( @@ -422,13 +342,126 @@ func main() { for _, val := range workloadDeploymentDuration { workloadDeploymentDurationGauge.Observe(ctx, val.Value, val.Attributes...) } - }) if err != nil { fmt.Println("Failed to register callback") panic(err) } + initHealthProbes(mgr) + + setupLog.Info("starting manager") + setupLog.Info("Keptn lifecycle operator is alive") + if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { + setupLog.Error(err, "problem running manager") + os.Exit(1) + } +} + +func setupEvaluationReconciler(mgr manager.Manager, meters common.KeptnMeters) { + evaluationReconciler := &keptnevaluation.KeptnEvaluationReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnEvaluation Controller"), + Recorder: mgr.GetEventRecorderFor("keptnevaluation-controller"), + Tracer: otel.Tracer("keptn/operator/evaluation"), + Meters: meters, + } + if err := (evaluationReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnEvaluation") + os.Exit(1) + } +} + +func setupAppVersionReconciler(mgr manager.Manager, meters common.KeptnMeters, spanHandler *controllercommon.SpanHandler) { + appVersionReconciler := &keptnappversion.KeptnAppVersionReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnAppVersion Controller"), + Recorder: mgr.GetEventRecorderFor("keptnappversion-controller"), + Tracer: otel.Tracer("keptn/operator/appversion"), + Meters: meters, + SpanHandler: spanHandler, + } + if err := (appVersionReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnAppVersion") + os.Exit(1) + } +} + +func setupWorkloadInstanceReconciler(mgr manager.Manager, meters common.KeptnMeters, spanHandler *controllercommon.SpanHandler) { + workloadInstanceReconciler := &keptnworkloadinstance.KeptnWorkloadInstanceReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnWorkloadInstance Controller"), + Recorder: mgr.GetEventRecorderFor("keptnworkloadinstance-controller"), + Meters: meters, + Tracer: otel.Tracer("keptn/operator/workloadinstance"), + SpanHandler: spanHandler, + } + if err := (workloadInstanceReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnWorkloadInstance") + os.Exit(1) + } +} + +func setupWorkloadReconciler(mgr manager.Manager) { + workloadReconciler := &keptnworkload.KeptnWorkloadReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnWorkload Controller"), + Recorder: mgr.GetEventRecorderFor("keptnworkload-controller"), + Tracer: otel.Tracer("keptn/operator/workload"), + } + if err := (workloadReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnWorkload") + os.Exit(1) + } +} + +func setupAppReconciler(mgr manager.Manager) { + appReconciler := &keptnapp.KeptnAppReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnApp Controller"), + Recorder: mgr.GetEventRecorderFor("keptnapp-controller"), + Tracer: otel.Tracer("keptn/operator/app"), + } + if err := (appReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnApp") + os.Exit(1) + } +} + +func setupTaskDefinitionReconciler(mgr manager.Manager) { + taskDefinitionReconciler := &keptntaskdefinition.KeptnTaskDefinitionReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnTaskDefinition Controller"), + Recorder: mgr.GetEventRecorderFor("keptntaskdefinition-controller"), + } + if err := (taskDefinitionReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnTaskDefinition") + os.Exit(1) + } +} + +func setupTaskReconciler(mgr manager.Manager, meters common.KeptnMeters) { + taskReconciler := &keptntask.KeptnTaskReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Log: ctrl.Log.WithName("KeptnTask Controller"), + Recorder: mgr.GetEventRecorderFor("keptntask-controller"), + Meters: meters, + Tracer: otel.Tracer("keptn/operator/task"), + } + if err := (taskReconciler).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "KeptnTask") + os.Exit(1) + } +} + +func initHealthProbes(mgr manager.Manager) { if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) @@ -437,13 +470,6 @@ func main() { setupLog.Error(err, "unable to set up ready check") os.Exit(1) } - - setupLog.Info("starting manager") - setupLog.Info("Keptn lifecycle operator is alive") - if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { - setupLog.Error(err, "problem running manager") - os.Exit(1) - } } func getOTelTracerProviderOptions(env envConfig) ([]trace.TracerProviderOption, error) { From c83b76340a499b5a6bd237fae1f96dddff8774bc Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:40:32 +0100 Subject: [PATCH 07/23] extract constant Signed-off-by: Moritz Wiesinger --- operator/main.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/operator/main.go b/operator/main.go index abd99074f3..f5de6588fe 100644 --- a/operator/main.go +++ b/operator/main.go @@ -85,6 +85,7 @@ type envConfig struct { } func main() { + const unableToStartOTelError = "unable to start OTel" var env envConfig if err := envconfig.Process("", &env); err != nil { log.Fatalf("Failed to process env var: %s", err) @@ -110,71 +111,72 @@ func main() { provider := metric.NewMeterProvider(metric.WithReader(exporter)) meter := provider.Meter("keptn/task") deploymentCount, err := meter.SyncInt64().Counter("keptn.deployment.count", instrument.WithDescription("a simple counter for Keptn Deployments")) + if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } deploymentDuration, err := meter.SyncFloat64().Histogram("keptn.deployment.duration", instrument.WithDescription("a histogram of duration for Keptn Deployments"), instrument.WithUnit(unit.Unit("s"))) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } deploymentActiveGauge, err := meter.AsyncInt64().Gauge("keptn.deployment.active", instrument.WithDescription("a gauge keeping track of the currently active Keptn Deployments")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } taskCount, err := meter.SyncInt64().Counter("keptn.task.count", instrument.WithDescription("a simple counter for Keptn Tasks")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } taskDuration, err := meter.SyncFloat64().Histogram("keptn.task.duration", instrument.WithDescription("a histogram of duration for Keptn Tasks"), instrument.WithUnit(unit.Unit("s"))) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } taskActiveGauge, err := meter.AsyncInt64().Gauge("keptn.task.active", instrument.WithDescription("a simple counter of active Keptn Tasks")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } appCount, err := meter.SyncInt64().Counter("keptn.app.count", instrument.WithDescription("a simple counter for Keptn Apps")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } appDuration, err := meter.SyncFloat64().Histogram("keptn.app.duration", instrument.WithDescription("a histogram of duration for Keptn Apps"), instrument.WithUnit(unit.Unit("s"))) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } appActiveGauge, err := meter.AsyncInt64().Gauge("keptn.app.active", instrument.WithDescription("a simple counter of active Keptn Apps")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } evaluationCount, err := meter.SyncInt64().Counter("keptn.evaluation.count", instrument.WithDescription("a simple counter for Keptn Evaluations")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } evaluationDuration, err := meter.SyncFloat64().Histogram("keptn.evaluation.duration", instrument.WithDescription("a histogram of duration for Keptn Evaluations"), instrument.WithUnit(unit.Unit("s"))) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } evaluationActiveGauge, err := meter.AsyncInt64().Gauge("keptn.evaluation.active", instrument.WithDescription("a simple counter of active Keptn Evaluations")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } appDeploymentIntervalGauge, err := meter.AsyncFloat64().Gauge("keptn.app.deploymentinterval", instrument.WithDescription("a gauge of the interval between deployments")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } appDeploymentDurationGauge, err := meter.AsyncFloat64().Gauge("keptn.app.deploymentduration", instrument.WithDescription("a gauge of the duration of deployments")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } workloadDeploymentIntervalGauge, err := meter.AsyncFloat64().Gauge("keptn.deployment.deploymentinterval", instrument.WithDescription("a gauge of the interval between deployments")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } workloadDeploymentDurationGauge, err := meter.AsyncFloat64().Gauge("keptn.deployment.deploymentduration", instrument.WithDescription("a gauge of the duration of deployments")) if err != nil { - setupLog.Error(err, "unable to start OTel") + setupLog.Error(err, unableToStartOTelError) } meters := common.KeptnMeters{ From a2b5b46bbbfb5307cc337a5ef4a2e564438c6d8d Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:46:12 +0100 Subject: [PATCH 08/23] extract setup of keptn meters Signed-off-by: Moritz Wiesinger --- operator/main.go | 96 ++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/operator/main.go b/operator/main.go index f5de6588fe..b51e6d75c8 100644 --- a/operator/main.go +++ b/operator/main.go @@ -20,6 +20,7 @@ import ( "context" "flag" "fmt" + metric2 "go.opentelemetry.io/otel/metric" "log" "net/http" "os" @@ -74,6 +75,8 @@ var ( buildVersion string ) +const unableToStartOTelError = "unable to start OTel" + func init() { utilruntime.Must(clientgoscheme.AddToScheme(scheme)) utilruntime.Must(lifecyclev1alpha1.AddToScheme(scheme)) @@ -85,7 +88,6 @@ type envConfig struct { } func main() { - const unableToStartOTelError = "unable to start OTel" var env envConfig if err := envconfig.Process("", &env); err != nil { log.Fatalf("Failed to process env var: %s", err) @@ -110,51 +112,21 @@ func main() { // } provider := metric.NewMeterProvider(metric.WithReader(exporter)) meter := provider.Meter("keptn/task") - deploymentCount, err := meter.SyncInt64().Counter("keptn.deployment.count", instrument.WithDescription("a simple counter for Keptn Deployments")) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } - deploymentDuration, err := meter.SyncFloat64().Histogram("keptn.deployment.duration", instrument.WithDescription("a histogram of duration for Keptn Deployments"), instrument.WithUnit(unit.Unit("s"))) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } + meters := setupKeptnMeters(meter) + deploymentActiveGauge, err := meter.AsyncInt64().Gauge("keptn.deployment.active", instrument.WithDescription("a gauge keeping track of the currently active Keptn Deployments")) if err != nil { setupLog.Error(err, unableToStartOTelError) } - taskCount, err := meter.SyncInt64().Counter("keptn.task.count", instrument.WithDescription("a simple counter for Keptn Tasks")) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } - taskDuration, err := meter.SyncFloat64().Histogram("keptn.task.duration", instrument.WithDescription("a histogram of duration for Keptn Tasks"), instrument.WithUnit(unit.Unit("s"))) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } taskActiveGauge, err := meter.AsyncInt64().Gauge("keptn.task.active", instrument.WithDescription("a simple counter of active Keptn Tasks")) if err != nil { setupLog.Error(err, unableToStartOTelError) } - appCount, err := meter.SyncInt64().Counter("keptn.app.count", instrument.WithDescription("a simple counter for Keptn Apps")) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } - appDuration, err := meter.SyncFloat64().Histogram("keptn.app.duration", instrument.WithDescription("a histogram of duration for Keptn Apps"), instrument.WithUnit(unit.Unit("s"))) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } appActiveGauge, err := meter.AsyncInt64().Gauge("keptn.app.active", instrument.WithDescription("a simple counter of active Keptn Apps")) if err != nil { setupLog.Error(err, unableToStartOTelError) } - evaluationCount, err := meter.SyncInt64().Counter("keptn.evaluation.count", instrument.WithDescription("a simple counter for Keptn Evaluations")) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } - evaluationDuration, err := meter.SyncFloat64().Histogram("keptn.evaluation.duration", instrument.WithDescription("a histogram of duration for Keptn Evaluations"), instrument.WithUnit(unit.Unit("s"))) - if err != nil { - setupLog.Error(err, unableToStartOTelError) - } evaluationActiveGauge, err := meter.AsyncInt64().Gauge("keptn.evaluation.active", instrument.WithDescription("a simple counter of active Keptn Evaluations")) if err != nil { setupLog.Error(err, unableToStartOTelError) @@ -179,17 +151,6 @@ func main() { setupLog.Error(err, unableToStartOTelError) } - meters := common.KeptnMeters{ - TaskCount: taskCount, - TaskDuration: taskDuration, - DeploymentCount: deploymentCount, - DeploymentDuration: deploymentDuration, - AppCount: appCount, - AppDuration: appDuration, - EvaluationCount: evaluationCount, - EvaluationDuration: evaluationDuration, - } - // Start the prometheus HTTP server and pass the exporter Collector to it // TODO uncomment after bumping OTel version to 1.11.2 or higher //go serveMetrics() @@ -360,6 +321,53 @@ func main() { } } +func setupKeptnMeters(meter metric2.Meter) common.KeptnMeters { + taskCount, err := meter.SyncInt64().Counter("keptn.task.count", instrument.WithDescription("a simple counter for Keptn Tasks")) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + taskDuration, err := meter.SyncFloat64().Histogram("keptn.task.duration", instrument.WithDescription("a histogram of duration for Keptn Tasks"), instrument.WithUnit(unit.Unit("s"))) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + deploymentCount, err := meter.SyncInt64().Counter("keptn.deployment.count", instrument.WithDescription("a simple counter for Keptn Deployments")) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + deploymentDuration, err := meter.SyncFloat64().Histogram("keptn.deployment.duration", instrument.WithDescription("a histogram of duration for Keptn Deployments"), instrument.WithUnit(unit.Unit("s"))) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + appCount, err := meter.SyncInt64().Counter("keptn.app.count", instrument.WithDescription("a simple counter for Keptn Apps")) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + appDuration, err := meter.SyncFloat64().Histogram("keptn.app.duration", instrument.WithDescription("a histogram of duration for Keptn Apps"), instrument.WithUnit(unit.Unit("s"))) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + evaluationCount, err := meter.SyncInt64().Counter("keptn.evaluation.count", instrument.WithDescription("a simple counter for Keptn Evaluations")) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + evaluationDuration, err := meter.SyncFloat64().Histogram("keptn.evaluation.duration", instrument.WithDescription("a histogram of duration for Keptn Evaluations"), instrument.WithUnit(unit.Unit("s"))) + if err != nil { + setupLog.Error(err, unableToStartOTelError) + } + + meters := common.KeptnMeters{ + TaskCount: taskCount, + TaskDuration: taskDuration, + DeploymentCount: deploymentCount, + DeploymentDuration: deploymentDuration, + AppCount: appCount, + AppDuration: appDuration, + EvaluationCount: evaluationCount, + EvaluationDuration: evaluationDuration, + } + return meters +} + func setupEvaluationReconciler(mgr manager.Manager, meters common.KeptnMeters) { evaluationReconciler := &keptnevaluation.KeptnEvaluationReconciler{ Client: mgr.GetClient(), From 3a82a48f7921deb1f96187cc8a479339cad8f9be Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:56:10 +0100 Subject: [PATCH 09/23] more main refactoring, its not pretty Signed-off-by: Moritz Wiesinger --- operator/main.go | 165 ++++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 73 deletions(-) diff --git a/operator/main.go b/operator/main.go index b51e6d75c8..6123a64298 100644 --- a/operator/main.go +++ b/operator/main.go @@ -21,6 +21,8 @@ import ( "flag" "fmt" metric2 "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" + "go.opentelemetry.io/otel/metric/instrument/asyncint64" "log" "net/http" "os" @@ -230,81 +232,32 @@ func main() { setupEvaluationReconciler(mgr, meters) //+kubebuilder:scaffold:builder + asyncInstruments := []instrument.Asynchronous{ + deploymentActiveGauge, + taskActiveGauge, + appActiveGauge, + evaluationActiveGauge, + appDeploymentIntervalGauge, + appDeploymentDurationGauge, + workloadDeploymentIntervalGauge, + workloadDeploymentDurationGauge, + } + err = meter.RegisterCallback( - []instrument.Asynchronous{ - deploymentActiveGauge, - taskActiveGauge, - appActiveGauge, - evaluationActiveGauge, - appDeploymentIntervalGauge, - appDeploymentDurationGauge, - workloadDeploymentIntervalGauge, - workloadDeploymentDurationGauge, - }, + asyncInstruments, func(ctx context.Context) { - activeDeployments, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnWorkloadInstanceList{}) - if err != nil { - setupLog.Error(err, "unable to gather active deployments") - } - for _, val := range activeDeployments { - deploymentActiveGauge.Observe(ctx, val.Value, val.Attributes...) - } - - activeApps, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnAppVersionList{}) - if err != nil { - setupLog.Error(err, "unable to gather active apps") - } - for _, val := range activeApps { - appActiveGauge.Observe(ctx, val.Value, val.Attributes...) - } - - activeTasks, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnTaskList{}) - if err != nil { - setupLog.Error(err, "unable to gather active tasks") - } - for _, val := range activeTasks { - taskActiveGauge.Observe(ctx, val.Value, val.Attributes...) - } - - activeEvaluations, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnEvaluationList{}) - if err != nil { - setupLog.Error(err, "unable to gather active evaluations") - } - for _, val := range activeEvaluations { - evaluationActiveGauge.Observe(ctx, val.Value, val.Attributes...) - } - - appDeploymentInterval, err := controllercommon.GetDeploymentInterval(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnAppVersionList{}, &lifecyclev1alpha1.KeptnAppVersion{}) - if err != nil { - setupLog.Error(err, "unable to gather app deployment intervals") - } - for _, val := range appDeploymentInterval { - appDeploymentIntervalGauge.Observe(ctx, val.Value, val.Attributes...) - } - - appDeploymentDuration, err := controllercommon.GetDeploymentDuration(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnAppVersionList{}) - if err != nil { - setupLog.Error(err, "unable to gather app deployment durations") - } - for _, val := range appDeploymentDuration { - appDeploymentDurationGauge.Observe(ctx, val.Value, val.Attributes...) - } - - workloadDeploymentInterval, err := controllercommon.GetDeploymentInterval(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnWorkloadInstanceList{}, &lifecyclev1alpha1.KeptnWorkloadInstance{}) - if err != nil { - setupLog.Error(err, "unable to gather workload deployment intervals") - } - for _, val := range workloadDeploymentInterval { - workloadDeploymentIntervalGauge.Observe(ctx, val.Value, val.Attributes...) - } - - workloadDeploymentDuration, err := controllercommon.GetDeploymentDuration(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnWorkloadInstanceList{}) - if err != nil { - setupLog.Error(err, "unable to gather workload deployment durations") - } - for _, val := range workloadDeploymentDuration { - workloadDeploymentDurationGauge.Observe(ctx, val.Value, val.Attributes...) - } + setupMeterCallbacks( + ctx, + mgr, + deploymentActiveGauge, + taskActiveGauge, + appActiveGauge, + evaluationActiveGauge, + appDeploymentIntervalGauge, + appDeploymentDurationGauge, + workloadDeploymentIntervalGauge, + workloadDeploymentDurationGauge, + ) }) if err != nil { fmt.Println("Failed to register callback") @@ -321,6 +274,72 @@ func main() { } } +func setupMeterCallbacks(ctx context.Context, mgr manager.Manager, deploymentActiveGauge asyncint64.Gauge, taskActiveGauge asyncint64.Gauge, appActiveGauge asyncint64.Gauge, evaluationActiveGauge asyncint64.Gauge, appDeploymentIntervalGauge asyncfloat64.Gauge, appDeploymentDurationGauge asyncfloat64.Gauge, workloadDeploymentIntervalGauge asyncfloat64.Gauge, workloadDeploymentDurationGauge asyncfloat64.Gauge) { + activeDeployments, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnWorkloadInstanceList{}) + if err != nil { + setupLog.Error(err, "unable to gather active deployments") + } + for _, val := range activeDeployments { + deploymentActiveGauge.Observe(ctx, val.Value, val.Attributes...) + } + + activeApps, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnAppVersionList{}) + if err != nil { + setupLog.Error(err, "unable to gather active apps") + } + for _, val := range activeApps { + appActiveGauge.Observe(ctx, val.Value, val.Attributes...) + } + + activeTasks, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnTaskList{}) + if err != nil { + setupLog.Error(err, "unable to gather active tasks") + } + for _, val := range activeTasks { + taskActiveGauge.Observe(ctx, val.Value, val.Attributes...) + } + + activeEvaluations, err := controllercommon.GetActiveInstances(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnEvaluationList{}) + if err != nil { + setupLog.Error(err, "unable to gather active evaluations") + } + for _, val := range activeEvaluations { + evaluationActiveGauge.Observe(ctx, val.Value, val.Attributes...) + } + + appDeploymentInterval, err := controllercommon.GetDeploymentInterval(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnAppVersionList{}, &lifecyclev1alpha1.KeptnAppVersion{}) + if err != nil { + setupLog.Error(err, "unable to gather app deployment intervals") + } + for _, val := range appDeploymentInterval { + appDeploymentIntervalGauge.Observe(ctx, val.Value, val.Attributes...) + } + + appDeploymentDuration, err := controllercommon.GetDeploymentDuration(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnAppVersionList{}) + if err != nil { + setupLog.Error(err, "unable to gather app deployment durations") + } + for _, val := range appDeploymentDuration { + appDeploymentDurationGauge.Observe(ctx, val.Value, val.Attributes...) + } + + workloadDeploymentInterval, err := controllercommon.GetDeploymentInterval(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnWorkloadInstanceList{}, &lifecyclev1alpha1.KeptnWorkloadInstance{}) + if err != nil { + setupLog.Error(err, "unable to gather workload deployment intervals") + } + for _, val := range workloadDeploymentInterval { + workloadDeploymentIntervalGauge.Observe(ctx, val.Value, val.Attributes...) + } + + workloadDeploymentDuration, err := controllercommon.GetDeploymentDuration(ctx, mgr.GetClient(), &lifecyclev1alpha1.KeptnWorkloadInstanceList{}) + if err != nil { + setupLog.Error(err, "unable to gather workload deployment durations") + } + for _, val := range workloadDeploymentDuration { + workloadDeploymentDurationGauge.Observe(ctx, val.Value, val.Attributes...) + } +} + func setupKeptnMeters(meter metric2.Meter) common.KeptnMeters { taskCount, err := meter.SyncInt64().Counter("keptn.task.count", instrument.WithDescription("a simple counter for Keptn Tasks")) if err != nil { From f5a56a5284510aa03afb688777262c61b074b93f Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 15:56:19 +0100 Subject: [PATCH 10/23] go fmt Signed-off-by: Moritz Wiesinger --- .../common/fake/spanhandler_mock.go | 32 +-- .../interfaces/fake/phaseitem_mock.go | 248 ++++++++++-------- .../interfaces/fake/spanitem_mock.go | 50 ++-- .../interfaces/fake/tracer_mock.go | 23 +- 4 files changed, 193 insertions(+), 160 deletions(-) diff --git a/operator/controllers/common/fake/spanhandler_mock.go b/operator/controllers/common/fake/spanhandler_mock.go index 2efbec10b4..d951329b84 100644 --- a/operator/controllers/common/fake/spanhandler_mock.go +++ b/operator/controllers/common/fake/spanhandler_mock.go @@ -12,22 +12,22 @@ import ( // ISpanHandlerMock is a mock implementation of common.ISpanHandler. // -// func TestSomethingThatUsesISpanHandler(t *testing.T) { +// func TestSomethingThatUsesISpanHandler(t *testing.T) { // -// // make and configure a mocked common.ISpanHandler -// mockedISpanHandler := &ISpanHandlerMock{ -// GetSpanFunc: func(ctx context.Context, tracer trace.Tracer, reconcileObject client.Object, phase string) (context.Context, trace.Span, error) { -// panic("mock out the GetSpan method") -// }, -// UnbindSpanFunc: func(reconcileObject client.Object, phase string) error { -// panic("mock out the UnbindSpan method") -// }, -// } +// // make and configure a mocked common.ISpanHandler +// mockedISpanHandler := &ISpanHandlerMock{ +// GetSpanFunc: func(ctx context.Context, tracer trace.Tracer, reconcileObject client.Object, phase string) (context.Context, trace.Span, error) { +// panic("mock out the GetSpan method") +// }, +// UnbindSpanFunc: func(reconcileObject client.Object, phase string) error { +// panic("mock out the UnbindSpan method") +// }, +// } // -// // use mockedISpanHandler in code that requires common.ISpanHandler -// // and then make assertions. +// // use mockedISpanHandler in code that requires common.ISpanHandler +// // and then make assertions. // -// } +// } type ISpanHandlerMock struct { // GetSpanFunc mocks the GetSpan method. GetSpanFunc func(ctx context.Context, tracer trace.Tracer, reconcileObject client.Object, phase string) (context.Context, trace.Span, error) @@ -84,7 +84,8 @@ func (mock *ISpanHandlerMock) GetSpan(ctx context.Context, tracer trace.Tracer, // GetSpanCalls gets all the calls that were made to GetSpan. // Check the length with: -// len(mockedISpanHandler.GetSpanCalls()) +// +// len(mockedISpanHandler.GetSpanCalls()) func (mock *ISpanHandlerMock) GetSpanCalls() []struct { Ctx context.Context Tracer trace.Tracer @@ -123,7 +124,8 @@ func (mock *ISpanHandlerMock) UnbindSpan(reconcileObject client.Object, phase st // UnbindSpanCalls gets all the calls that were made to UnbindSpan. // Check the length with: -// len(mockedISpanHandler.UnbindSpanCalls()) +// +// len(mockedISpanHandler.UnbindSpanCalls()) func (mock *ISpanHandlerMock) UnbindSpanCalls() []struct { ReconcileObject client.Object Phase string diff --git a/operator/controllers/interfaces/fake/phaseitem_mock.go b/operator/controllers/interfaces/fake/phaseitem_mock.go index 164dfc2b28..7b275710c7 100644 --- a/operator/controllers/interfaces/fake/phaseitem_mock.go +++ b/operator/controllers/interfaces/fake/phaseitem_mock.go @@ -14,94 +14,94 @@ import ( // PhaseItemMock is a mock implementation of common.PhaseItem. // -// func TestSomethingThatUsesPhaseItem(t *testing.T) { +// func TestSomethingThatUsesPhaseItem(t *testing.T) { // -// // make and configure a mocked common.PhaseItem -// mockedPhaseItem := &PhaseItemMock{ -// CancelRemainingPhasesFunc: func(phase apicommon.KeptnPhaseType) { -// panic("mock out the CancelRemainingPhases method") -// }, -// CompleteFunc: func() { -// panic("mock out the Complete method") -// }, -// GenerateEvaluationFunc: func(evaluationDefinition string, checkType apicommon.CheckType) lfcv1alpha1.KeptnEvaluation { -// panic("mock out the GenerateEvaluation method") -// }, -// GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) lfcv1alpha1.KeptnTask { -// panic("mock out the GenerateTask method") -// }, -// GetAppNameFunc: func() string { -// panic("mock out the GetAppName method") -// }, -// GetCurrentPhaseFunc: func() string { -// panic("mock out the GetCurrentPhase method") -// }, -// GetEndTimeFunc: func() time.Time { -// panic("mock out the GetEndTime method") -// }, -// GetNamespaceFunc: func() string { -// panic("mock out the GetNamespace method") -// }, -// GetParentNameFunc: func() string { -// panic("mock out the GetParentName method") -// }, -// GetPostDeploymentEvaluationTaskStatusFunc: func() []lfcv1alpha1.EvaluationStatus { -// panic("mock out the GetPostDeploymentEvaluationTaskStatus method") -// }, -// GetPostDeploymentEvaluationsFunc: func() []string { -// panic("mock out the GetPostDeploymentEvaluations method") -// }, -// GetPostDeploymentTaskStatusFunc: func() []lfcv1alpha1.TaskStatus { -// panic("mock out the GetPostDeploymentTaskStatus method") -// }, -// GetPostDeploymentTasksFunc: func() []string { -// panic("mock out the GetPostDeploymentTasks method") -// }, -// GetPreDeploymentEvaluationTaskStatusFunc: func() []lfcv1alpha1.EvaluationStatus { -// panic("mock out the GetPreDeploymentEvaluationTaskStatus method") -// }, -// GetPreDeploymentEvaluationsFunc: func() []string { -// panic("mock out the GetPreDeploymentEvaluations method") -// }, -// GetPreDeploymentTaskStatusFunc: func() []lfcv1alpha1.TaskStatus { -// panic("mock out the GetPreDeploymentTaskStatus method") -// }, -// GetPreDeploymentTasksFunc: func() []string { -// panic("mock out the GetPreDeploymentTasks method") -// }, -// GetPreviousVersionFunc: func() string { -// panic("mock out the GetPreviousVersion method") -// }, -// GetSpanAttributesFunc: func() []attribute.KeyValue { -// panic("mock out the GetSpanAttributes method") -// }, -// GetStartTimeFunc: func() time.Time { -// panic("mock out the GetStartTime method") -// }, -// GetStateFunc: func() apicommon.KeptnState { -// panic("mock out the GetState method") -// }, -// GetVersionFunc: func() string { -// panic("mock out the GetVersion method") -// }, -// IsEndTimeSetFunc: func() bool { -// panic("mock out the IsEndTimeSet method") -// }, -// SetCurrentPhaseFunc: func(s string) { -// panic("mock out the SetCurrentPhase method") -// }, -// SetSpanAttributesFunc: func(span trace.Span) { -// panic("mock out the SetSpanAttributes method") -// }, -// SetStateFunc: func(keptnState apicommon.KeptnState) { -// panic("mock out the SetState method") -// }, -// } +// // make and configure a mocked common.PhaseItem +// mockedPhaseItem := &PhaseItemMock{ +// CancelRemainingPhasesFunc: func(phase apicommon.KeptnPhaseType) { +// panic("mock out the CancelRemainingPhases method") +// }, +// CompleteFunc: func() { +// panic("mock out the Complete method") +// }, +// GenerateEvaluationFunc: func(evaluationDefinition string, checkType apicommon.CheckType) lfcv1alpha1.KeptnEvaluation { +// panic("mock out the GenerateEvaluation method") +// }, +// GenerateTaskFunc: func(taskDefinition string, checkType apicommon.CheckType) lfcv1alpha1.KeptnTask { +// panic("mock out the GenerateTask method") +// }, +// GetAppNameFunc: func() string { +// panic("mock out the GetAppName method") +// }, +// GetCurrentPhaseFunc: func() string { +// panic("mock out the GetCurrentPhase method") +// }, +// GetEndTimeFunc: func() time.Time { +// panic("mock out the GetEndTime method") +// }, +// GetNamespaceFunc: func() string { +// panic("mock out the GetNamespace method") +// }, +// GetParentNameFunc: func() string { +// panic("mock out the GetParentName method") +// }, +// GetPostDeploymentEvaluationTaskStatusFunc: func() []lfcv1alpha1.EvaluationStatus { +// panic("mock out the GetPostDeploymentEvaluationTaskStatus method") +// }, +// GetPostDeploymentEvaluationsFunc: func() []string { +// panic("mock out the GetPostDeploymentEvaluations method") +// }, +// GetPostDeploymentTaskStatusFunc: func() []lfcv1alpha1.TaskStatus { +// panic("mock out the GetPostDeploymentTaskStatus method") +// }, +// GetPostDeploymentTasksFunc: func() []string { +// panic("mock out the GetPostDeploymentTasks method") +// }, +// GetPreDeploymentEvaluationTaskStatusFunc: func() []lfcv1alpha1.EvaluationStatus { +// panic("mock out the GetPreDeploymentEvaluationTaskStatus method") +// }, +// GetPreDeploymentEvaluationsFunc: func() []string { +// panic("mock out the GetPreDeploymentEvaluations method") +// }, +// GetPreDeploymentTaskStatusFunc: func() []lfcv1alpha1.TaskStatus { +// panic("mock out the GetPreDeploymentTaskStatus method") +// }, +// GetPreDeploymentTasksFunc: func() []string { +// panic("mock out the GetPreDeploymentTasks method") +// }, +// GetPreviousVersionFunc: func() string { +// panic("mock out the GetPreviousVersion method") +// }, +// GetSpanAttributesFunc: func() []attribute.KeyValue { +// panic("mock out the GetSpanAttributes method") +// }, +// GetStartTimeFunc: func() time.Time { +// panic("mock out the GetStartTime method") +// }, +// GetStateFunc: func() apicommon.KeptnState { +// panic("mock out the GetState method") +// }, +// GetVersionFunc: func() string { +// panic("mock out the GetVersion method") +// }, +// IsEndTimeSetFunc: func() bool { +// panic("mock out the IsEndTimeSet method") +// }, +// SetCurrentPhaseFunc: func(s string) { +// panic("mock out the SetCurrentPhase method") +// }, +// SetSpanAttributesFunc: func(span trace.Span) { +// panic("mock out the SetSpanAttributes method") +// }, +// SetStateFunc: func(keptnState apicommon.KeptnState) { +// panic("mock out the SetState method") +// }, +// } // -// // use mockedPhaseItem in code that requires common.PhaseItem -// // and then make assertions. +// // use mockedPhaseItem in code that requires common.PhaseItem +// // and then make assertions. // -// } +// } type PhaseItemMock struct { // CancelRemainingPhasesFunc mocks the CancelRemainingPhases method. CancelRemainingPhasesFunc func(phase apicommon.KeptnPhaseType) @@ -324,7 +324,8 @@ func (mock *PhaseItemMock) CancelRemainingPhases(phase apicommon.KeptnPhaseType) // CancelRemainingPhasesCalls gets all the calls that were made to CancelRemainingPhases. // Check the length with: -// len(mockedPhaseItem.CancelRemainingPhasesCalls()) +// +// len(mockedPhaseItem.CancelRemainingPhasesCalls()) func (mock *PhaseItemMock) CancelRemainingPhasesCalls() []struct { Phase apicommon.KeptnPhaseType } { @@ -352,7 +353,8 @@ func (mock *PhaseItemMock) Complete() { // CompleteCalls gets all the calls that were made to Complete. // Check the length with: -// len(mockedPhaseItem.CompleteCalls()) +// +// len(mockedPhaseItem.CompleteCalls()) func (mock *PhaseItemMock) CompleteCalls() []struct { } { var calls []struct { @@ -383,7 +385,8 @@ func (mock *PhaseItemMock) GenerateEvaluation(evaluationDefinition string, check // GenerateEvaluationCalls gets all the calls that were made to GenerateEvaluation. // Check the length with: -// len(mockedPhaseItem.GenerateEvaluationCalls()) +// +// len(mockedPhaseItem.GenerateEvaluationCalls()) func (mock *PhaseItemMock) GenerateEvaluationCalls() []struct { EvaluationDefinition string CheckType apicommon.CheckType @@ -418,7 +421,8 @@ func (mock *PhaseItemMock) GenerateTask(taskDefinition string, checkType apicomm // GenerateTaskCalls gets all the calls that were made to GenerateTask. // Check the length with: -// len(mockedPhaseItem.GenerateTaskCalls()) +// +// len(mockedPhaseItem.GenerateTaskCalls()) func (mock *PhaseItemMock) GenerateTaskCalls() []struct { TaskDefinition string CheckType apicommon.CheckType @@ -448,7 +452,8 @@ func (mock *PhaseItemMock) GetAppName() string { // GetAppNameCalls gets all the calls that were made to GetAppName. // Check the length with: -// len(mockedPhaseItem.GetAppNameCalls()) +// +// len(mockedPhaseItem.GetAppNameCalls()) func (mock *PhaseItemMock) GetAppNameCalls() []struct { } { var calls []struct { @@ -474,7 +479,8 @@ func (mock *PhaseItemMock) GetCurrentPhase() string { // GetCurrentPhaseCalls gets all the calls that were made to GetCurrentPhase. // Check the length with: -// len(mockedPhaseItem.GetCurrentPhaseCalls()) +// +// len(mockedPhaseItem.GetCurrentPhaseCalls()) func (mock *PhaseItemMock) GetCurrentPhaseCalls() []struct { } { var calls []struct { @@ -500,7 +506,8 @@ func (mock *PhaseItemMock) GetEndTime() time.Time { // GetEndTimeCalls gets all the calls that were made to GetEndTime. // Check the length with: -// len(mockedPhaseItem.GetEndTimeCalls()) +// +// len(mockedPhaseItem.GetEndTimeCalls()) func (mock *PhaseItemMock) GetEndTimeCalls() []struct { } { var calls []struct { @@ -526,7 +533,8 @@ func (mock *PhaseItemMock) GetNamespace() string { // GetNamespaceCalls gets all the calls that were made to GetNamespace. // Check the length with: -// len(mockedPhaseItem.GetNamespaceCalls()) +// +// len(mockedPhaseItem.GetNamespaceCalls()) func (mock *PhaseItemMock) GetNamespaceCalls() []struct { } { var calls []struct { @@ -552,7 +560,8 @@ func (mock *PhaseItemMock) GetParentName() string { // GetParentNameCalls gets all the calls that were made to GetParentName. // Check the length with: -// len(mockedPhaseItem.GetParentNameCalls()) +// +// len(mockedPhaseItem.GetParentNameCalls()) func (mock *PhaseItemMock) GetParentNameCalls() []struct { } { var calls []struct { @@ -578,7 +587,8 @@ func (mock *PhaseItemMock) GetPostDeploymentEvaluationTaskStatus() []lfcv1alpha1 // GetPostDeploymentEvaluationTaskStatusCalls gets all the calls that were made to GetPostDeploymentEvaluationTaskStatus. // Check the length with: -// len(mockedPhaseItem.GetPostDeploymentEvaluationTaskStatusCalls()) +// +// len(mockedPhaseItem.GetPostDeploymentEvaluationTaskStatusCalls()) func (mock *PhaseItemMock) GetPostDeploymentEvaluationTaskStatusCalls() []struct { } { var calls []struct { @@ -604,7 +614,8 @@ func (mock *PhaseItemMock) GetPostDeploymentEvaluations() []string { // GetPostDeploymentEvaluationsCalls gets all the calls that were made to GetPostDeploymentEvaluations. // Check the length with: -// len(mockedPhaseItem.GetPostDeploymentEvaluationsCalls()) +// +// len(mockedPhaseItem.GetPostDeploymentEvaluationsCalls()) func (mock *PhaseItemMock) GetPostDeploymentEvaluationsCalls() []struct { } { var calls []struct { @@ -630,7 +641,8 @@ func (mock *PhaseItemMock) GetPostDeploymentTaskStatus() []lfcv1alpha1.TaskStatu // GetPostDeploymentTaskStatusCalls gets all the calls that were made to GetPostDeploymentTaskStatus. // Check the length with: -// len(mockedPhaseItem.GetPostDeploymentTaskStatusCalls()) +// +// len(mockedPhaseItem.GetPostDeploymentTaskStatusCalls()) func (mock *PhaseItemMock) GetPostDeploymentTaskStatusCalls() []struct { } { var calls []struct { @@ -656,7 +668,8 @@ func (mock *PhaseItemMock) GetPostDeploymentTasks() []string { // GetPostDeploymentTasksCalls gets all the calls that were made to GetPostDeploymentTasks. // Check the length with: -// len(mockedPhaseItem.GetPostDeploymentTasksCalls()) +// +// len(mockedPhaseItem.GetPostDeploymentTasksCalls()) func (mock *PhaseItemMock) GetPostDeploymentTasksCalls() []struct { } { var calls []struct { @@ -682,7 +695,8 @@ func (mock *PhaseItemMock) GetPreDeploymentEvaluationTaskStatus() []lfcv1alpha1. // GetPreDeploymentEvaluationTaskStatusCalls gets all the calls that were made to GetPreDeploymentEvaluationTaskStatus. // Check the length with: -// len(mockedPhaseItem.GetPreDeploymentEvaluationTaskStatusCalls()) +// +// len(mockedPhaseItem.GetPreDeploymentEvaluationTaskStatusCalls()) func (mock *PhaseItemMock) GetPreDeploymentEvaluationTaskStatusCalls() []struct { } { var calls []struct { @@ -708,7 +722,8 @@ func (mock *PhaseItemMock) GetPreDeploymentEvaluations() []string { // GetPreDeploymentEvaluationsCalls gets all the calls that were made to GetPreDeploymentEvaluations. // Check the length with: -// len(mockedPhaseItem.GetPreDeploymentEvaluationsCalls()) +// +// len(mockedPhaseItem.GetPreDeploymentEvaluationsCalls()) func (mock *PhaseItemMock) GetPreDeploymentEvaluationsCalls() []struct { } { var calls []struct { @@ -734,7 +749,8 @@ func (mock *PhaseItemMock) GetPreDeploymentTaskStatus() []lfcv1alpha1.TaskStatus // GetPreDeploymentTaskStatusCalls gets all the calls that were made to GetPreDeploymentTaskStatus. // Check the length with: -// len(mockedPhaseItem.GetPreDeploymentTaskStatusCalls()) +// +// len(mockedPhaseItem.GetPreDeploymentTaskStatusCalls()) func (mock *PhaseItemMock) GetPreDeploymentTaskStatusCalls() []struct { } { var calls []struct { @@ -760,7 +776,8 @@ func (mock *PhaseItemMock) GetPreDeploymentTasks() []string { // GetPreDeploymentTasksCalls gets all the calls that were made to GetPreDeploymentTasks. // Check the length with: -// len(mockedPhaseItem.GetPreDeploymentTasksCalls()) +// +// len(mockedPhaseItem.GetPreDeploymentTasksCalls()) func (mock *PhaseItemMock) GetPreDeploymentTasksCalls() []struct { } { var calls []struct { @@ -786,7 +803,8 @@ func (mock *PhaseItemMock) GetPreviousVersion() string { // GetPreviousVersionCalls gets all the calls that were made to GetPreviousVersion. // Check the length with: -// len(mockedPhaseItem.GetPreviousVersionCalls()) +// +// len(mockedPhaseItem.GetPreviousVersionCalls()) func (mock *PhaseItemMock) GetPreviousVersionCalls() []struct { } { var calls []struct { @@ -812,7 +830,8 @@ func (mock *PhaseItemMock) GetSpanAttributes() []attribute.KeyValue { // GetSpanAttributesCalls gets all the calls that were made to GetSpanAttributes. // Check the length with: -// len(mockedPhaseItem.GetSpanAttributesCalls()) +// +// len(mockedPhaseItem.GetSpanAttributesCalls()) func (mock *PhaseItemMock) GetSpanAttributesCalls() []struct { } { var calls []struct { @@ -838,7 +857,8 @@ func (mock *PhaseItemMock) GetStartTime() time.Time { // GetStartTimeCalls gets all the calls that were made to GetStartTime. // Check the length with: -// len(mockedPhaseItem.GetStartTimeCalls()) +// +// len(mockedPhaseItem.GetStartTimeCalls()) func (mock *PhaseItemMock) GetStartTimeCalls() []struct { } { var calls []struct { @@ -864,7 +884,8 @@ func (mock *PhaseItemMock) GetState() apicommon.KeptnState { // GetStateCalls gets all the calls that were made to GetState. // Check the length with: -// len(mockedPhaseItem.GetStateCalls()) +// +// len(mockedPhaseItem.GetStateCalls()) func (mock *PhaseItemMock) GetStateCalls() []struct { } { var calls []struct { @@ -890,7 +911,8 @@ func (mock *PhaseItemMock) GetVersion() string { // GetVersionCalls gets all the calls that were made to GetVersion. // Check the length with: -// len(mockedPhaseItem.GetVersionCalls()) +// +// len(mockedPhaseItem.GetVersionCalls()) func (mock *PhaseItemMock) GetVersionCalls() []struct { } { var calls []struct { @@ -916,7 +938,8 @@ func (mock *PhaseItemMock) IsEndTimeSet() bool { // IsEndTimeSetCalls gets all the calls that were made to IsEndTimeSet. // Check the length with: -// len(mockedPhaseItem.IsEndTimeSetCalls()) +// +// len(mockedPhaseItem.IsEndTimeSetCalls()) func (mock *PhaseItemMock) IsEndTimeSetCalls() []struct { } { var calls []struct { @@ -945,7 +968,8 @@ func (mock *PhaseItemMock) SetCurrentPhase(s string) { // SetCurrentPhaseCalls gets all the calls that were made to SetCurrentPhase. // Check the length with: -// len(mockedPhaseItem.SetCurrentPhaseCalls()) +// +// len(mockedPhaseItem.SetCurrentPhaseCalls()) func (mock *PhaseItemMock) SetCurrentPhaseCalls() []struct { S string } { @@ -976,7 +1000,8 @@ func (mock *PhaseItemMock) SetSpanAttributes(span trace.Span) { // SetSpanAttributesCalls gets all the calls that were made to SetSpanAttributes. // Check the length with: -// len(mockedPhaseItem.SetSpanAttributesCalls()) +// +// len(mockedPhaseItem.SetSpanAttributesCalls()) func (mock *PhaseItemMock) SetSpanAttributesCalls() []struct { Span trace.Span } { @@ -1007,7 +1032,8 @@ func (mock *PhaseItemMock) SetState(keptnState apicommon.KeptnState) { // SetStateCalls gets all the calls that were made to SetState. // Check the length with: -// len(mockedPhaseItem.SetStateCalls()) +// +// len(mockedPhaseItem.SetStateCalls()) func (mock *PhaseItemMock) SetStateCalls() []struct { KeptnState apicommon.KeptnState } { diff --git a/operator/controllers/interfaces/fake/spanitem_mock.go b/operator/controllers/interfaces/fake/spanitem_mock.go index 850a692eb0..d5c686bef5 100644 --- a/operator/controllers/interfaces/fake/spanitem_mock.go +++ b/operator/controllers/interfaces/fake/spanitem_mock.go @@ -11,28 +11,28 @@ import ( // SpanItemMock is a mock implementation of common.SpanItem. // -// func TestSomethingThatUsesSpanItem(t *testing.T) { +// func TestSomethingThatUsesSpanItem(t *testing.T) { // -// // make and configure a mocked common.SpanItem -// mockedSpanItem := &SpanItemMock{ -// GetSpanKeyFunc: func(phase string) string { -// panic("mock out the GetSpanKey method") -// }, -// GetSpanNameFunc: func(phase string) string { -// panic("mock out the GetSpanName method") -// }, -// SetPhaseTraceIDFunc: func(phase string, carrier propagation.MapCarrier) { -// panic("mock out the SetPhaseTraceID method") -// }, -// SetSpanAttributesFunc: func(span trace.Span) { -// panic("mock out the SetSpanAttributes method") -// }, -// } +// // make and configure a mocked common.SpanItem +// mockedSpanItem := &SpanItemMock{ +// GetSpanKeyFunc: func(phase string) string { +// panic("mock out the GetSpanKey method") +// }, +// GetSpanNameFunc: func(phase string) string { +// panic("mock out the GetSpanName method") +// }, +// SetPhaseTraceIDFunc: func(phase string, carrier propagation.MapCarrier) { +// panic("mock out the SetPhaseTraceID method") +// }, +// SetSpanAttributesFunc: func(span trace.Span) { +// panic("mock out the SetSpanAttributes method") +// }, +// } // -// // use mockedSpanItem in code that requires common.SpanItem -// // and then make assertions. +// // use mockedSpanItem in code that requires common.SpanItem +// // and then make assertions. // -// } +// } type SpanItemMock struct { // GetSpanKeyFunc mocks the GetSpanKey method. GetSpanKeyFunc func(phase string) string @@ -95,7 +95,8 @@ func (mock *SpanItemMock) GetSpanKey(phase string) string { // GetSpanKeyCalls gets all the calls that were made to GetSpanKey. // Check the length with: -// len(mockedSpanItem.GetSpanKeyCalls()) +// +// len(mockedSpanItem.GetSpanKeyCalls()) func (mock *SpanItemMock) GetSpanKeyCalls() []struct { Phase string } { @@ -126,7 +127,8 @@ func (mock *SpanItemMock) GetSpanName(phase string) string { // GetSpanNameCalls gets all the calls that were made to GetSpanName. // Check the length with: -// len(mockedSpanItem.GetSpanNameCalls()) +// +// len(mockedSpanItem.GetSpanNameCalls()) func (mock *SpanItemMock) GetSpanNameCalls() []struct { Phase string } { @@ -159,7 +161,8 @@ func (mock *SpanItemMock) SetPhaseTraceID(phase string, carrier propagation.MapC // SetPhaseTraceIDCalls gets all the calls that were made to SetPhaseTraceID. // Check the length with: -// len(mockedSpanItem.SetPhaseTraceIDCalls()) +// +// len(mockedSpanItem.SetPhaseTraceIDCalls()) func (mock *SpanItemMock) SetPhaseTraceIDCalls() []struct { Phase string Carrier propagation.MapCarrier @@ -192,7 +195,8 @@ func (mock *SpanItemMock) SetSpanAttributes(span trace.Span) { // SetSpanAttributesCalls gets all the calls that were made to SetSpanAttributes. // Check the length with: -// len(mockedSpanItem.SetSpanAttributesCalls()) +// +// len(mockedSpanItem.SetSpanAttributesCalls()) func (mock *SpanItemMock) SetSpanAttributesCalls() []struct { Span trace.Span } { diff --git a/operator/controllers/interfaces/fake/tracer_mock.go b/operator/controllers/interfaces/fake/tracer_mock.go index 9bdc5314dc..5934e8ba0b 100644 --- a/operator/controllers/interfaces/fake/tracer_mock.go +++ b/operator/controllers/interfaces/fake/tracer_mock.go @@ -11,19 +11,19 @@ import ( // ITracerMock is a mock implementation of common.ITracer. // -// func TestSomethingThatUsesITracer(t *testing.T) { +// func TestSomethingThatUsesITracer(t *testing.T) { // -// // make and configure a mocked common.ITracer -// mockedITracer := &ITracerMock{ -// StartFunc: func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { -// panic("mock out the Start method") -// }, -// } +// // make and configure a mocked common.ITracer +// mockedITracer := &ITracerMock{ +// StartFunc: func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { +// panic("mock out the Start method") +// }, +// } // -// // use mockedITracer in code that requires common.ITracer -// // and then make assertions. +// // use mockedITracer in code that requires common.ITracer +// // and then make assertions. // -// } +// } type ITracerMock struct { // StartFunc mocks the Start method. StartFunc func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) @@ -65,7 +65,8 @@ func (mock *ITracerMock) Start(ctx context.Context, spanName string, opts ...tra // StartCalls gets all the calls that were made to Start. // Check the length with: -// len(mockedITracer.StartCalls()) +// +// len(mockedITracer.StartCalls()) func (mock *ITracerMock) StartCalls() []struct { Ctx context.Context SpanName string From 760b44af562eb64c2f732f3b4b21d5779ae7998c Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Wed, 30 Nov 2022 16:01:52 +0100 Subject: [PATCH 11/23] refactor app version reconcile loop Signed-off-by: Moritz Wiesinger --- .../controllers/keptnappversion/controller.go | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/operator/controllers/keptnappversion/controller.go b/operator/controllers/keptnappversion/controller.go index 0478705130..70e116a7d2 100644 --- a/operator/controllers/keptnappversion/controller.go +++ b/operator/controllers/keptnappversion/controller.go @@ -105,13 +105,54 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ controllercommon.RecordEvent(r.Recorder, phase, "Normal", appVersion, "Started", "have started", appVersion.GetVersion()) } + phase, result, err2, done := r.goThroughPhases(ctx, appVersion, phaseHandler, ctxAppTrace, phase, span) + if done { + return result, err2 + } + + controllercommon.RecordEvent(r.Recorder, phase, "Normal", appVersion, "Finished", "is finished", appVersion.GetVersion()) + err = r.Client.Status().Update(ctx, appVersion) + if err != nil { + span.SetStatus(codes.Error, err.Error()) + return ctrl.Result{Requeue: true}, err + } + + // AppVersion is completed at this place + + if !appVersion.IsEndTimeSet() { + appVersion.Status.CurrentPhase = apicommon.PhaseCompleted.ShortName + appVersion.SetEndTime() + } + + err = r.Client.Status().Update(ctx, appVersion) + if err != nil { + return ctrl.Result{Requeue: true}, err + } + + attrs := appVersion.GetMetricsAttributes() + + // metrics: add app duration + duration := appVersion.Status.EndTime.Time.Sub(appVersion.Status.StartTime.Time) + r.Meters.AppDuration.Record(ctx, duration.Seconds(), attrs...) + + spanAppTrace.AddEvent(appVersion.Name + " has finished") + spanAppTrace.SetStatus(codes.Ok, "Finished") + spanAppTrace.End() + if err := r.SpanHandler.UnbindSpan(appVersion, ""); err != nil { + r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, appVersion.Name) + } + + return ctrl.Result{}, nil +} + +func (r *KeptnAppVersionReconciler) goThroughPhases(ctx context.Context, appVersion *klcv1alpha1.KeptnAppVersion, phaseHandler controllercommon.PhaseHandler, ctxAppTrace context.Context, phase apicommon.KeptnPhaseType, span trace.Span) (apicommon.KeptnPhaseType, ctrl.Result, error, bool) { if !appVersion.IsPreDeploymentSucceeded() { reconcilePreDep := func(phaseCtx context.Context) (apicommon.KeptnState, error) { return r.reconcilePrePostDeployment(ctx, phaseCtx, appVersion, apicommon.PreDeploymentCheckType) } result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.Tracer, appVersion, phase, span, reconcilePreDep) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -122,7 +163,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ } result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.Tracer, appVersion, phase, span, reconcilePreEval) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -133,7 +174,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ } result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.Tracer, appVersion, phase, span, reconcileAppDep) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -144,7 +185,7 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ } result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.Tracer, appVersion, phase, span, reconcilePostDep) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -155,43 +196,10 @@ func (r *KeptnAppVersionReconciler) Reconcile(ctx context.Context, req ctrl.Requ } result, err := phaseHandler.HandlePhase(ctx, ctxAppTrace, r.Tracer, appVersion, phase, span, reconcilePostEval) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } - - controllercommon.RecordEvent(r.Recorder, phase, "Normal", appVersion, "Finished", "is finished", appVersion.GetVersion()) - err = r.Client.Status().Update(ctx, appVersion) - if err != nil { - span.SetStatus(codes.Error, err.Error()) - return ctrl.Result{Requeue: true}, err - } - - // AppVersion is completed at this place - - if !appVersion.IsEndTimeSet() { - appVersion.Status.CurrentPhase = apicommon.PhaseCompleted.ShortName - appVersion.SetEndTime() - } - - err = r.Client.Status().Update(ctx, appVersion) - if err != nil { - return ctrl.Result{Requeue: true}, err - } - - attrs := appVersion.GetMetricsAttributes() - - // metrics: add app duration - duration := appVersion.Status.EndTime.Time.Sub(appVersion.Status.StartTime.Time) - r.Meters.AppDuration.Record(ctx, duration.Seconds(), attrs...) - - spanAppTrace.AddEvent(appVersion.Name + " has finished") - spanAppTrace.SetStatus(codes.Ok, "Finished") - spanAppTrace.End() - if err := r.SpanHandler.UnbindSpan(appVersion, ""); err != nil { - r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, appVersion.Name) - } - - return ctrl.Result{}, nil + return phase, ctrl.Result{}, nil, false } func setupSpansContexts(ctx context.Context, appVersion *klcv1alpha1.KeptnAppVersion, r *KeptnAppVersionReconciler) (context.Context, context.Context, trace.Span, func()) { From 644cfa19ad14b71d42cf8309fbfaf8fc1abed596 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 07:30:09 +0100 Subject: [PATCH 12/23] add funlen to test file exclusions Signed-off-by: Moritz Wiesinger --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 59c42a224e..2a8d87512a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,6 +23,7 @@ issues: - containedctx - gocyclo - gocognit + - funlen path: _test\.go linters-settings: From b32c0399696208b7a3b138ee87e96ab438a330c3 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 07:59:50 +0100 Subject: [PATCH 13/23] exclude all test files for go lint Signed-off-by: Moritz Wiesinger --- .golangci.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2a8d87512a..2a022c197b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,7 @@ run: timeout: 5m go: '1.19' + tests: false linters: enable: # - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification @@ -19,12 +20,6 @@ issues: - linters: - gci text: '//+kubebuilder' - - linters: - - containedctx - - gocyclo - - gocognit - - funlen - path: _test\.go linters-settings: gocyclo: From c00dd6ed9ad6cce154644a88e5252c22db097c74 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 08:06:29 +0100 Subject: [PATCH 14/23] refactor workload instance reconcile loop Signed-off-by: Moritz Wiesinger --- .../keptnworkloadinstance/controller.go | 86 +++++++++++-------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/operator/controllers/keptnworkloadinstance/controller.go b/operator/controllers/keptnworkloadinstance/controller.go index 838c05ff5b..161b921bee 100644 --- a/operator/controllers/keptnworkloadinstance/controller.go +++ b/operator/controllers/keptnworkloadinstance/controller.go @@ -142,16 +142,9 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr } // set the App trace id if not already set - if len(workloadInstance.Spec.TraceId) < 1 { - appDeploymentTraceID := appVersion.Status.PhaseTraceIDs[apicommon.PhaseAppDeployment.ShortName] - if appDeploymentTraceID != nil { - workloadInstance.Spec.TraceId = appDeploymentTraceID - } else { - workloadInstance.Spec.TraceId = appVersion.Spec.TraceId - } - if err := r.Update(ctx, workloadInstance); err != nil { - return ctrl.Result{}, err - } + result, err := r.setTraceId(ctx, workloadInstance, appVersion) + if err != nil { + return result, err } appTraceContextCarrier := propagation.MapCarrier(workloadInstance.Spec.TraceId) @@ -163,6 +156,36 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr r.Log.Error(err, "could not get span") } + phase, c, err, done := r.goThroughPhases(ctx, workloadInstance, spanWorkloadTrace, phase, phaseHandler, ctxWorkloadTrace, span) + if done { + return c, err + } + + err = r.Client.Status().Update(ctx, workloadInstance) + if err != nil { + span.SetStatus(codes.Error, err.Error()) + return ctrl.Result{Requeue: true}, err + } + + attrs := workloadInstance.GetMetricsAttributes() + + // metrics: add deployment duration + duration := workloadInstance.Status.EndTime.Time.Sub(workloadInstance.Status.StartTime.Time) + r.Meters.DeploymentDuration.Record(ctx, duration.Seconds(), attrs...) + + spanWorkloadTrace.AddEvent(workloadInstance.Name + " has finished") + spanWorkloadTrace.SetStatus(codes.Ok, "Finished") + spanWorkloadTrace.End() + if err := r.SpanHandler.UnbindSpan(workloadInstance, ""); err != nil { + r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, workloadInstance.Name) + } + + controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "Finished", "is finished", workloadInstance.GetVersion()) + + return ctrl.Result{}, nil +} + +func (r *KeptnWorkloadInstanceReconciler) goThroughPhases(ctx context.Context, workloadInstance *klcv1alpha1.KeptnWorkloadInstance, spanWorkloadTrace trace.Span, phase apicommon.KeptnPhaseType, phaseHandler controllercommon.PhaseHandler, ctxWorkloadTrace context.Context, span trace.Span) (apicommon.KeptnPhaseType, ctrl.Result, error, bool) { if workloadInstance.Status.CurrentPhase == "" { spanWorkloadTrace.AddEvent("WorkloadInstance Pre-Deployment Tasks started", trace.WithTimestamp(time.Now())) controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "Started", "have started", workloadInstance.GetVersion()) @@ -174,7 +197,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr } result, err := phaseHandler.HandlePhase(ctx, ctxWorkloadTrace, r.Tracer, workloadInstance, phase, span, reconcilePre) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -186,7 +209,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr } result, err := phaseHandler.HandlePhase(ctx, ctxWorkloadTrace, r.Tracer, workloadInstance, phase, span, reconcilePreEval) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -198,7 +221,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr } result, err := phaseHandler.HandlePhase(ctx, ctxWorkloadTrace, r.Tracer, workloadInstance, phase, span, reconcileWorkloadInstance) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -210,7 +233,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr } result, err := phaseHandler.HandlePhase(ctx, ctxWorkloadTrace, r.Tracer, workloadInstance, phase, span, reconcilePostDeployment) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -222,7 +245,7 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr } result, err := phaseHandler.HandlePhase(ctx, ctxWorkloadTrace, r.Tracer, workloadInstance, phase, span, reconcilePostEval) if !result.Continue { - return result.Result, err + return apicommon.KeptnPhaseType{}, result.Result, err, true } } @@ -232,28 +255,21 @@ func (r *KeptnWorkloadInstanceReconciler) Reconcile(ctx context.Context, req ctr workloadInstance.Status.Status = apicommon.StateSucceeded workloadInstance.SetEndTime() } + return phase, ctrl.Result{}, nil, false +} - err = r.Client.Status().Update(ctx, workloadInstance) - if err != nil { - span.SetStatus(codes.Error, err.Error()) - return ctrl.Result{Requeue: true}, err - } - - attrs := workloadInstance.GetMetricsAttributes() - - // metrics: add deployment duration - duration := workloadInstance.Status.EndTime.Time.Sub(workloadInstance.Status.StartTime.Time) - r.Meters.DeploymentDuration.Record(ctx, duration.Seconds(), attrs...) - - spanWorkloadTrace.AddEvent(workloadInstance.Name + " has finished") - spanWorkloadTrace.SetStatus(codes.Ok, "Finished") - spanWorkloadTrace.End() - if err := r.SpanHandler.UnbindSpan(workloadInstance, ""); err != nil { - r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, workloadInstance.Name) +func (r *KeptnWorkloadInstanceReconciler) setTraceId(ctx context.Context, workloadInstance *klcv1alpha1.KeptnWorkloadInstance, appVersion klcv1alpha1.KeptnAppVersion) (ctrl.Result, error) { + if len(workloadInstance.Spec.TraceId) < 1 { + appDeploymentTraceID := appVersion.Status.PhaseTraceIDs[apicommon.PhaseAppDeployment.ShortName] + if appDeploymentTraceID != nil { + workloadInstance.Spec.TraceId = appDeploymentTraceID + } else { + workloadInstance.Spec.TraceId = appVersion.Spec.TraceId + } + if err := r.Update(ctx, workloadInstance); err != nil { + return ctrl.Result{}, err + } } - - controllercommon.RecordEvent(r.Recorder, phase, "Normal", workloadInstance, "Finished", "is finished", workloadInstance.GetVersion()) - return ctrl.Result{}, nil } From 019bfbb528c00d2fb51ead691f685ef3bb0fcb68 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 08:08:01 +0100 Subject: [PATCH 15/23] skip test folders Signed-off-by: Moritz Wiesinger --- .golangci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 2a022c197b..90717d99e4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,6 +2,8 @@ run: timeout: 5m go: '1.19' tests: false + skip-dirs: + - test linters: enable: # - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification From ee87367b0e0397662b14af475c98b40bbe1fae0e Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 08:38:19 +0100 Subject: [PATCH 16/23] try to refactor getLatestAppVersion func Signed-off-by: Moritz Wiesinger --- .../keptnworkloadinstance/controller.go | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/operator/controllers/keptnworkloadinstance/controller.go b/operator/controllers/keptnworkloadinstance/controller.go index 161b921bee..3524446ed3 100644 --- a/operator/controllers/keptnworkloadinstance/controller.go +++ b/operator/controllers/keptnworkloadinstance/controller.go @@ -305,21 +305,27 @@ func getLatestAppVersion(apps *klcv1alpha1.KeptnAppVersionList, wli *klcv1alpha1 latestVersion := klcv1alpha1.KeptnAppVersion{} // ignore the potential error since this can not return an error with 0.0.0 oldVersion, _ := version.NewVersion("0.0.0") - workloadFound := false - for _, app := range apps.Items { - if app.Spec.AppName == wli.Spec.AppName { - for _, appWorkload := range app.Spec.Workloads { - if appWorkload.Version == wli.Spec.Version && app.GetWorkloadNameOfApp(appWorkload.Name) == wli.Spec.WorkloadName { - workloadFound = true - newVersion, err := version.NewVersion(app.Spec.Version) - if err != nil { - return false, klcv1alpha1.KeptnAppVersion{}, err - } - if newVersion.GreaterThan(oldVersion) { - latestVersion = app - oldVersion = newVersion - } + + var appsWithMatchingName []klcv1alpha1.KeptnAppVersion + + for _, app2 := range apps.Items { + if app2.Spec.AppName == wli.Spec.AppName { + appsWithMatchingName = append(appsWithMatchingName, app2) + } + } + + for _, app := range appsWithMatchingName { + for _, appWorkload := range app.Spec.Workloads { + if appWorkload.Version == wli.Spec.Version && app.GetWorkloadNameOfApp(appWorkload.Name) == wli.Spec.WorkloadName { + workloadFound = true + newVersion, err := version.NewVersion(app.Spec.Version) + if err != nil { + return false, klcv1alpha1.KeptnAppVersion{}, err + } + if newVersion.GreaterThan(oldVersion) { + latestVersion = app + oldVersion = newVersion } } } From e5ea1a420e9deb07a38d29a22b5bb946d62c8fc1 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 09:21:52 +0100 Subject: [PATCH 17/23] refactor phase handler Signed-off-by: Moritz Wiesinger --- operator/controllers/common/phasehandler.go | 48 +++++++++++---------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/operator/controllers/common/phasehandler.go b/operator/controllers/common/phasehandler.go index c74a179199..3f858fca74 100644 --- a/operator/controllers/common/phasehandler.go +++ b/operator/controllers/common/phasehandler.go @@ -73,34 +73,38 @@ func (r PhaseHandler) HandlePhase(ctx context.Context, ctxTrace context.Context, }(ctx, oldStatus, oldPhase, reconcileObject) if state.IsCompleted() { - if state.IsFailed() { - piWrapper.Complete() - piWrapper.SetState(apicommon.StateFailed) - spanPhaseTrace.AddEvent(phase.LongName + " has failed") - spanPhaseTrace.SetStatus(codes.Error, "Failed") - spanPhaseTrace.End() - if err := r.SpanHandler.UnbindSpan(reconcileObject, phase.ShortName); err != nil { - r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, reconcileObject.GetName()) - } - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "Failed", "has failed", piWrapper.GetVersion()) - piWrapper.CancelRemainingPhases(phase) - return &PhaseResult{Continue: false, Result: ctrl.Result{}}, nil - } + return r.handleCompletedState(state, piWrapper, spanPhaseTrace, phase, reconcileObject, requeueResult) + } + + piWrapper.SetState(apicommon.StateProgressing) + RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) - piWrapper.SetState(apicommon.StateSucceeded) - spanPhaseTrace.AddEvent(phase.LongName + " has succeeded") - spanPhaseTrace.SetStatus(codes.Ok, "Succeeded") + return &PhaseResult{Continue: false, Result: requeueResult}, nil +} + +func (r PhaseHandler) handleCompletedState(state apicommon.KeptnState, piWrapper *interfaces.PhaseItemWrapper, spanPhaseTrace trace.Span, phase apicommon.KeptnPhaseType, reconcileObject client.Object, requeueResult ctrl.Result) (*PhaseResult, error) { + if state.IsFailed() { + piWrapper.Complete() + piWrapper.SetState(apicommon.StateFailed) + spanPhaseTrace.AddEvent(phase.LongName + " has failed") + spanPhaseTrace.SetStatus(codes.Error, "Failed") spanPhaseTrace.End() if err := r.SpanHandler.UnbindSpan(reconcileObject, phase.ShortName); err != nil { r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, reconcileObject.GetName()) } - RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "Succeeded", "has succeeded", piWrapper.GetVersion()) - - return &PhaseResult{Continue: true, Result: requeueResult}, nil + RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "Failed", "has failed", piWrapper.GetVersion()) + piWrapper.CancelRemainingPhases(phase) + return &PhaseResult{Continue: false, Result: ctrl.Result{}}, nil } - piWrapper.SetState(apicommon.StateProgressing) - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) + piWrapper.SetState(apicommon.StateSucceeded) + spanPhaseTrace.AddEvent(phase.LongName + " has succeeded") + spanPhaseTrace.SetStatus(codes.Ok, "Succeeded") + spanPhaseTrace.End() + if err := r.SpanHandler.UnbindSpan(reconcileObject, phase.ShortName); err != nil { + r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, reconcileObject.GetName()) + } + RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "Succeeded", "has succeeded", piWrapper.GetVersion()) - return &PhaseResult{Continue: false, Result: requeueResult}, nil + return &PhaseResult{Continue: true, Result: requeueResult}, nil } From caa472b32e4a8df9f83a3bb049539b0e1f937885 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 09:43:39 +0100 Subject: [PATCH 18/23] refactor evaluation handler Signed-off-by: Moritz Wiesinger --- .../controllers/common/evaluationhandler.go | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index 4dbe2eae7d..52a55041d3 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -41,17 +41,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co return nil, apicommon.StatusSummary{}, err } - var evaluations []string - var statuses []klcv1alpha1.EvaluationStatus - - switch evaluationCreateAttributes.CheckType { - case apicommon.PreDeploymentEvaluationCheckType: - evaluations = piWrapper.GetPreDeploymentEvaluations() - statuses = piWrapper.GetPreDeploymentEvaluationTaskStatus() - case apicommon.PostDeploymentEvaluationCheckType: - evaluations = piWrapper.GetPostDeploymentEvaluations() - statuses = piWrapper.GetPostDeploymentEvaluationTaskStatus() - } + evaluations, statuses := r.setupEvaluations(evaluationCreateAttributes, piWrapper) var summary apicommon.StatusSummary summary.Total = len(evaluations) @@ -140,6 +130,21 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co return newStatus, summary, nil } +func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes EvaluationCreateAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha1.EvaluationStatus) { + var evaluations []string + var statuses []klcv1alpha1.EvaluationStatus + + switch evaluationCreateAttributes.CheckType { + case apicommon.PreDeploymentEvaluationCheckType: + evaluations = piWrapper.GetPreDeploymentEvaluations() + statuses = piWrapper.GetPreDeploymentEvaluationTaskStatus() + case apicommon.PostDeploymentEvaluationCheckType: + evaluations = piWrapper.GetPostDeploymentEvaluations() + statuses = piWrapper.GetPostDeploymentEvaluationTaskStatus() + } + return evaluations, statuses +} + func (r EvaluationHandler) CreateKeptnEvaluation(ctx context.Context, namespace string, reconcileObject client.Object, evaluationCreateAttributes EvaluationCreateAttributes) (string, error) { piWrapper, err := interfaces.NewPhaseItemWrapperFromClientObject(reconcileObject) if err != nil { From c15c296819acbcec31811c0a3bed1e85d41422a9 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 09:51:25 +0100 Subject: [PATCH 19/23] refactor evaluation handler Signed-off-by: Moritz Wiesinger --- .../controllers/common/evaluationhandler.go | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index 52a55041d3..4977150ed4 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -46,21 +46,31 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co var summary apicommon.StatusSummary summary.Total = len(evaluations) // Check current state of the PrePostEvaluationTasks + newStatus, evaluationStatuses, statusSummary, err2 := r.handlePrePostEvaluations(ctx, phaseCtx, reconcileObject, evaluationCreateAttributes, evaluations, statuses, phase, piWrapper, summary) + if err2 != nil { + return evaluationStatuses, statusSummary, err2 + } + + for _, ns := range newStatus { + summary = apicommon.UpdateStatusSummary(ns.Status, summary) + } + if apicommon.GetOverallState(summary) != apicommon.StateSucceeded { + RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) + } + return newStatus, summary, nil +} + +func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes EvaluationCreateAttributes, evaluations []string, statuses []klcv1alpha1.EvaluationStatus, phase apicommon.KeptnPhaseType, piWrapper *interfaces.PhaseItemWrapper, summary apicommon.StatusSummary) ([]klcv1alpha1.EvaluationStatus, []klcv1alpha1.EvaluationStatus, apicommon.StatusSummary, error) { var newStatus []klcv1alpha1.EvaluationStatus for _, evaluationName := range evaluations { - var oldstatus apicommon.KeptnState - for _, ts := range statuses { - if ts.EvaluationDefinitionName == evaluationName { - oldstatus = ts.Status - } - } + oldStatus := r.findOldEvaluationStatus(statuses, evaluationName) evaluationStatus := GetEvaluationStatus(evaluationName, statuses) evaluation := &klcv1alpha1.KeptnEvaluation{} evaluationExists := false - if oldstatus != evaluationStatus.Status { - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Normal", reconcileObject, "EvaluationStatusChanged", fmt.Sprintf("evaluation status changed from %s to %s", oldstatus, evaluationStatus.Status), piWrapper.GetVersion()) + if oldStatus != evaluationStatus.Status { + RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "EvaluationStatusChanged", fmt.Sprintf("evaluation status changed from %s to %s", oldStatus, evaluationStatus.Status), piWrapper.GetVersion()) } // Check if evaluation has already succeeded or failed @@ -75,7 +85,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co if err != nil && errors.IsNotFound(err) { evaluationStatus.EvaluationName = "" } else if err != nil { - return nil, summary, err + return nil, nil, summary, err } evaluationExists = true } @@ -85,7 +95,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co evaluationCreateAttributes.EvaluationDefinition = evaluationName evaluationName, err := r.CreateKeptnEvaluation(ctx, piWrapper.GetNamespace(), reconcileObject, evaluationCreateAttributes) if err != nil { - return nil, summary, err + return nil, nil, summary, err } evaluationStatus.EvaluationName = evaluationName evaluationStatus.SetStartTime() @@ -120,14 +130,17 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co // Update state of the Check newStatus = append(newStatus, evaluationStatus) } + return newStatus, nil, apicommon.StatusSummary{}, nil +} - for _, ns := range newStatus { - summary = apicommon.UpdateStatusSummary(ns.Status, summary) - } - if apicommon.GetOverallState(summary) != apicommon.StateSucceeded { - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) +func (r EvaluationHandler) findOldEvaluationStatus(statuses []klcv1alpha1.EvaluationStatus, evaluationName string) apicommon.KeptnState { + var oldstatus apicommon.KeptnState + for _, ts := range statuses { + if ts.EvaluationDefinitionName == evaluationName { + oldstatus = ts.Status + } } - return newStatus, summary, nil + return oldstatus } func (r EvaluationHandler) setupEvaluations(evaluationCreateAttributes EvaluationCreateAttributes, piWrapper *interfaces.PhaseItemWrapper) ([]string, []klcv1alpha1.EvaluationStatus) { From 0cbd8ae42d9a1f4822e8feeed804f65a070e570e Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 09:53:56 +0100 Subject: [PATCH 20/23] refactor evaluation handler Signed-off-by: Moritz Wiesinger --- .../controllers/common/evaluationhandler.go | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index 4977150ed4..a8663afcda 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -67,7 +67,6 @@ func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCt evaluationStatus := GetEvaluationStatus(evaluationName, statuses) evaluation := &klcv1alpha1.KeptnEvaluation{} - evaluationExists := false if oldStatus != evaluationStatus.Status { RecordEvent(r.Recorder, phase, "Normal", reconcileObject, "EvaluationStatusChanged", fmt.Sprintf("evaluation status changed from %s to %s", oldStatus, evaluationStatus.Status), piWrapper.GetVersion()) @@ -80,14 +79,9 @@ func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCt } // Check if Evaluation is already created - if evaluationStatus.EvaluationName != "" { - err := r.Client.Get(ctx, types.NamespacedName{Name: evaluationStatus.EvaluationName, Namespace: piWrapper.GetNamespace()}, evaluation) - if err != nil && errors.IsNotFound(err) { - evaluationStatus.EvaluationName = "" - } else if err != nil { - return nil, nil, summary, err - } - evaluationExists = true + evaluationExists, evaluationStatuses, i, statusSummary, err := r.checkAlreadyCreated(ctx, evaluationStatus, piWrapper, evaluation, summary) + if err != nil { + return evaluationStatuses, i, statusSummary, err } // Create new Evaluation if it does not exist @@ -133,6 +127,20 @@ func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCt return newStatus, nil, apicommon.StatusSummary{}, nil } +func (r EvaluationHandler) checkAlreadyCreated(ctx context.Context, evaluationStatus klcv1alpha1.EvaluationStatus, piWrapper *interfaces.PhaseItemWrapper, evaluation *klcv1alpha1.KeptnEvaluation, summary apicommon.StatusSummary) (bool, []klcv1alpha1.EvaluationStatus, []klcv1alpha1.EvaluationStatus, apicommon.StatusSummary, error) { + evaluationExists := false + if evaluationStatus.EvaluationName != "" { + err := r.Client.Get(ctx, types.NamespacedName{Name: evaluationStatus.EvaluationName, Namespace: piWrapper.GetNamespace()}, evaluation) + if err != nil && errors.IsNotFound(err) { + evaluationStatus.EvaluationName = "" + } else if err != nil { + return false, nil, nil, summary, err + } + evaluationExists = true + } + return evaluationExists, nil, nil, apicommon.StatusSummary{}, nil +} + func (r EvaluationHandler) findOldEvaluationStatus(statuses []klcv1alpha1.EvaluationStatus, evaluationName string) apicommon.KeptnState { var oldstatus apicommon.KeptnState for _, ts := range statuses { From fcb4048c104d94c180e61421e777d9ab895f8389 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 10:16:22 +0100 Subject: [PATCH 21/23] refactor evaluation handler Signed-off-by: Moritz Wiesinger --- .../controllers/common/evaluationhandler.go | 70 +++++++++++-------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index a8663afcda..cd8b9052df 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -79,23 +79,16 @@ func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCt } // Check if Evaluation is already created - evaluationExists, evaluationStatuses, i, statusSummary, err := r.checkAlreadyCreated(ctx, evaluationStatus, piWrapper, evaluation, summary) + evaluationExists, evaluationStatuses, i, err := r.checkAlreadyCreated(ctx, evaluationStatus, piWrapper, evaluation, &summary) if err != nil { - return evaluationStatuses, i, statusSummary, err + return evaluationStatuses, i, summary, err } // Create new Evaluation if it does not exist if !evaluationExists { - evaluationCreateAttributes.EvaluationDefinition = evaluationName - evaluationName, err := r.CreateKeptnEvaluation(ctx, piWrapper.GetNamespace(), reconcileObject, evaluationCreateAttributes) + statusSummary, err := r.createEvaluation(ctx, phaseCtx, reconcileObject, evaluationCreateAttributes, evaluationName, piWrapper, summary, evaluationStatus, evaluation) if err != nil { - return nil, nil, summary, err - } - evaluationStatus.EvaluationName = evaluationName - evaluationStatus.SetStartTime() - _, _, err = r.SpanHandler.GetSpan(phaseCtx, r.Tracer, evaluation, "") - if err != nil { - r.Log.Error(err, "could not get span") + return nil, nil, statusSummary, err } } else { _, spanEvaluationTrace, err := r.SpanHandler.GetSpan(phaseCtx, r.Tracer, evaluation, "") @@ -104,22 +97,7 @@ func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCt } // Update state of Evaluation if it is already created evaluationStatus.Status = evaluation.Status.OverallStatus - if evaluationStatus.Status.IsCompleted() { - if evaluationStatus.Status.IsSucceeded() { - spanEvaluationTrace.AddEvent(evaluation.Name + " has finished") - spanEvaluationTrace.SetStatus(codes.Ok, "Finished") - RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Normal", evaluation, "Succeeded", "evaluation succeeded", piWrapper.GetVersion()) - } else { - spanEvaluationTrace.AddEvent(evaluation.Name + " has failed") - r.emitEvaluationFailureEvents(evaluation, spanEvaluationTrace, piWrapper) - spanEvaluationTrace.SetStatus(codes.Error, "Failed") - } - spanEvaluationTrace.End() - if err := r.SpanHandler.UnbindSpan(evaluation, ""); err != nil { - r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, evaluation.Name) - } - evaluationStatus.SetEndTime() - } + r.updateEvaluation(evaluationStatus, spanEvaluationTrace, evaluation) } // Update state of the Check newStatus = append(newStatus, evaluationStatus) @@ -127,18 +105,50 @@ func (r EvaluationHandler) handlePrePostEvaluations(ctx context.Context, phaseCt return newStatus, nil, apicommon.StatusSummary{}, nil } -func (r EvaluationHandler) checkAlreadyCreated(ctx context.Context, evaluationStatus klcv1alpha1.EvaluationStatus, piWrapper *interfaces.PhaseItemWrapper, evaluation *klcv1alpha1.KeptnEvaluation, summary apicommon.StatusSummary) (bool, []klcv1alpha1.EvaluationStatus, []klcv1alpha1.EvaluationStatus, apicommon.StatusSummary, error) { +func (r EvaluationHandler) updateEvaluation(evaluationStatus klcv1alpha1.EvaluationStatus, spanEvaluationTrace trace.Span, evaluation *klcv1alpha1.KeptnEvaluation) { + if evaluationStatus.Status.IsCompleted() { + if evaluationStatus.Status.IsSucceeded() { + spanEvaluationTrace.AddEvent(evaluation.Name + " has finished") + spanEvaluationTrace.SetStatus(codes.Ok, "Finished") + } else { + spanEvaluationTrace.AddEvent(evaluation.Name + " has failed") + spanEvaluationTrace.SetStatus(codes.Error, "Failed") + } + spanEvaluationTrace.End() + if err := r.SpanHandler.UnbindSpan(evaluation, ""); err != nil { + r.Log.Error(err, controllererrors.ErrCouldNotUnbindSpan, evaluation.Name) + } + evaluationStatus.SetEndTime() + } +} + +func (r EvaluationHandler) createEvaluation(ctx context.Context, phaseCtx context.Context, reconcileObject client.Object, evaluationCreateAttributes EvaluationCreateAttributes, evaluationName string, piWrapper *interfaces.PhaseItemWrapper, summary apicommon.StatusSummary, evaluationStatus klcv1alpha1.EvaluationStatus, evaluation *klcv1alpha1.KeptnEvaluation) (apicommon.StatusSummary, error) { + evaluationCreateAttributes.EvaluationDefinition = evaluationName + evaluationName, err := r.CreateKeptnEvaluation(ctx, piWrapper.GetNamespace(), reconcileObject, evaluationCreateAttributes) + if err != nil { + return summary, err + } + evaluationStatus.EvaluationName = evaluationName + evaluationStatus.SetStartTime() + _, _, err = r.SpanHandler.GetSpan(phaseCtx, r.Tracer, evaluation, "") + if err != nil { + r.Log.Error(err, "could not get span") + } + return apicommon.StatusSummary{}, nil +} + +func (r EvaluationHandler) checkAlreadyCreated(ctx context.Context, evaluationStatus klcv1alpha1.EvaluationStatus, piWrapper *interfaces.PhaseItemWrapper, evaluation *klcv1alpha1.KeptnEvaluation, summary *apicommon.StatusSummary) (bool, []klcv1alpha1.EvaluationStatus, []klcv1alpha1.EvaluationStatus, error) { evaluationExists := false if evaluationStatus.EvaluationName != "" { err := r.Client.Get(ctx, types.NamespacedName{Name: evaluationStatus.EvaluationName, Namespace: piWrapper.GetNamespace()}, evaluation) if err != nil && errors.IsNotFound(err) { evaluationStatus.EvaluationName = "" } else if err != nil { - return false, nil, nil, summary, err + return false, nil, nil, err } evaluationExists = true } - return evaluationExists, nil, nil, apicommon.StatusSummary{}, nil + return evaluationExists, nil, nil, nil } func (r EvaluationHandler) findOldEvaluationStatus(statuses []klcv1alpha1.EvaluationStatus, evaluationName string) apicommon.KeptnState { From 4fdf61ff6ee0e2dd0acae65c55da12e70d3385b8 Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Thu, 1 Dec 2022 10:22:26 +0100 Subject: [PATCH 22/23] reorder imports Signed-off-by: Moritz Wiesinger --- operator/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/operator/main.go b/operator/main.go index 6123a64298..cf140837ca 100644 --- a/operator/main.go +++ b/operator/main.go @@ -20,13 +20,9 @@ import ( "context" "flag" "fmt" - metric2 "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" "log" "net/http" "os" - "sigs.k8s.io/controller-runtime/pkg/manager" "time" "github.com/kelseyhightower/envconfig" @@ -47,7 +43,10 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" otelprom "go.opentelemetry.io/otel/exporters/prometheus" "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" + metric2 "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/instrument" + "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" + "go.opentelemetry.io/otel/metric/instrument/asyncint64" "go.opentelemetry.io/otel/metric/unit" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/metric" @@ -65,6 +64,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/webhook" //+kubebuilder:scaffold:imports ) From 138253422f75a6a647396e0e73ccb8ed37e4cd1d Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 1 Dec 2022 12:07:43 +0100 Subject: [PATCH 23/23] fix after rebase Signed-off-by: odubajDT --- operator/controllers/common/evaluationhandler.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/operator/controllers/common/evaluationhandler.go b/operator/controllers/common/evaluationhandler.go index cd8b9052df..6ac09cb2b2 100644 --- a/operator/controllers/common/evaluationhandler.go +++ b/operator/controllers/common/evaluationhandler.go @@ -46,7 +46,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co var summary apicommon.StatusSummary summary.Total = len(evaluations) // Check current state of the PrePostEvaluationTasks - newStatus, evaluationStatuses, statusSummary, err2 := r.handlePrePostEvaluations(ctx, phaseCtx, reconcileObject, evaluationCreateAttributes, evaluations, statuses, phase, piWrapper, summary) + newStatus, evaluationStatuses, statusSummary, err2 := r.handlePrePostEvaluations(ctx, phaseCtx, reconcileObject, evaluationCreateAttributes, evaluations, statuses, apicommon.PhaseReconcileEvaluation, piWrapper, summary) if err2 != nil { return evaluationStatuses, statusSummary, err2 } @@ -55,7 +55,7 @@ func (r EvaluationHandler) ReconcileEvaluations(ctx context.Context, phaseCtx co summary = apicommon.UpdateStatusSummary(ns.Status, summary) } if apicommon.GetOverallState(summary) != apicommon.StateSucceeded { - RecordEvent(r.Recorder, phase, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) + RecordEvent(r.Recorder, apicommon.PhaseReconcileEvaluation, "Warning", reconcileObject, "NotFinished", "has not finished", piWrapper.GetVersion()) } return newStatus, summary, nil }