diff --git a/appstudio-controller/controllers/appstudio.redhat.com/application_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/application_controller.go index 7e039ae0a4..6d44fcf5ad 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/application_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/application_controller.go @@ -52,7 +52,7 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops) - log.Info("Detected AppStudio Application event:", "request", req) + log.V(logutil.LogLevel_Debug).Info("AppStudio Application event", "request", req) return ctrl.Result{}, nil } diff --git a/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetbinder_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetbinder_controller.go index 585083c323..793097a404 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetbinder_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetbinder_controller.go @@ -59,7 +59,10 @@ type DeploymentTargetClaimReconciler struct { // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *DeploymentTargetClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name, + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller) dtc := applicationv1alpha1.DeploymentTargetClaim{ ObjectMeta: metav1.ObjectMeta{ diff --git a/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetreclaimer_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetreclaimer_controller.go index 4f8f43a8c4..a963d639e9 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetreclaimer_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/deploymenttargetreclaimer_controller.go @@ -68,7 +68,11 @@ const ( // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *DeploymentTargetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := log.FromContext(ctx).WithValues("name", req.Name, "namespace", req.Namespace, "component", "deploymentTargetReclaimer") + log := log.FromContext(ctx). + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name, + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller) dt := applicationv1alpha1.DeploymentTarget{ ObjectMeta: metav1.ObjectMeta{ diff --git a/appstudio-controller/controllers/appstudio.redhat.com/devsandbox_deployment_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/devsandbox_deployment_controller.go index 37879c7b4c..abf4f4105b 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/devsandbox_deployment_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/devsandbox_deployment_controller.go @@ -64,7 +64,10 @@ func (r *DevsandboxDeploymentReconciler) Reconcile(ctx context.Context, req ctrl var err error log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller, + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name) spacerequest := codereadytoolchainv1alpha1.SpaceRequest{ ObjectMeta: metav1.ObjectMeta{ diff --git a/appstudio-controller/controllers/appstudio.redhat.com/environment_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/environment_controller.go index 26cfe8d44f..faa4fd7202 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/environment_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/environment_controller.go @@ -75,7 +75,10 @@ func (r *EnvironmentReconciler) Reconcile(ctx context.Context, req ctrl.Request) log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("request", req) + WithValues( + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller, + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name) rClient := sharedutil.IfEnabledSimulateUnreliableClient(r.Client) diff --git a/appstudio-controller/controllers/appstudio.redhat.com/promotionrun_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/promotionrun_controller.go index 270912f8b5..a563fffb6a 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/promotionrun_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/promotionrun_controller.go @@ -74,7 +74,11 @@ const ( func (r *PromotionRunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller, + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name) + defer log.V(logutil.LogLevel_Debug).Info("Promotion Run Reconcile() complete.") promotionRun := &appstudioshared.PromotionRun{} diff --git a/appstudio-controller/controllers/appstudio.redhat.com/sandboxprovisioner_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/sandboxprovisioner_controller.go index e929af8680..cdb08e6a1e 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/sandboxprovisioner_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/sandboxprovisioner_controller.go @@ -63,7 +63,10 @@ type SandboxProvisionerReconciler struct { // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *SandboxProvisionerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller, + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name) dtc := applicationv1alpha1.DeploymentTargetClaim{ ObjectMeta: metav1.ObjectMeta{ diff --git a/appstudio-controller/controllers/appstudio.redhat.com/snapshot_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/snapshot_controller.go index 355eaac96b..88f4440a21 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/snapshot_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/snapshot_controller.go @@ -48,9 +48,12 @@ type SnapshotReconciler struct { // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.0/pkg/reconcile func (r *SnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller, + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name) - log.Info("Snapshot event: ", "request", req) + log.V(logutil.LogLevel_Debug).Info("Snapshot event", "request", req) return ctrl.Result{}, nil } diff --git a/appstudio-controller/controllers/appstudio.redhat.com/snapshotenvironmentbinding_controller.go b/appstudio-controller/controllers/appstudio.redhat.com/snapshotenvironmentbinding_controller.go index bae879d2fa..81e5c61ce8 100644 --- a/appstudio-controller/controllers/appstudio.redhat.com/snapshotenvironmentbinding_controller.go +++ b/appstudio-controller/controllers/appstudio.redhat.com/snapshotenvironmentbinding_controller.go @@ -94,8 +94,10 @@ type SnapshotEnvironmentBindingReconciler struct { func (r *SnapshotEnvironmentBindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops). - WithValues("name", req.Name, "namespace", req.Namespace, "component", "bindingReconcile") + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_Component, logutil.Log_Component_Appstudio_Controller, + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name) defer log.V(logutil.LogLevel_Debug).Info("Snapshot Environment Binding Reconcile() complete.") @@ -417,9 +419,9 @@ const ( // processExpectedGitOpsDeployment processed the GitOpsDeployment that is expected for a particular Component func processExpectedGitOpsDeployment(ctx context.Context, expectedGitopsDeployment apibackend.GitOpsDeployment, - binding appstudioshared.SnapshotEnvironmentBinding, k8sClient client.Client, l logr.Logger) error { + binding appstudioshared.SnapshotEnvironmentBinding, k8sClient client.Client, logParam logr.Logger) error { - log := l.WithValues("binding", binding.Name, "gitOpsDeployment", expectedGitopsDeployment.Name, "bindingNamespace", binding.Namespace) + log := logParam.WithValues("expectedGitOpsDeploymentName", expectedGitopsDeployment.Name) actualGitOpsDeployment := apibackend.GitOpsDeployment{} diff --git a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeployment_webhook.go b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeployment_webhook.go index 5434b5d53b..2e2a96386d 100644 --- a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeployment_webhook.go +++ b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeployment_webhook.go @@ -47,8 +47,9 @@ var _ webhook.Defaulter = &GitOpsDeployment{} // Default implements webhook.Defaulter so a webhook will be registered for the type func (r *GitOpsDeployment) Default() { - gitopsdeploymentlog.Info("default", "name", r.Name) + log := gitopsdeploymentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeployment") + log.V(logutil.LogLevel_Debug).Info("default") } //+kubebuilder:webhook:path=/validate-managed-gitops-redhat-com-v1alpha1-gitopsdeployment,mutating=false,failurePolicy=fail,sideEffects=None,groups=managed-gitops.redhat.com,resources=gitopsdeployments,verbs=create;update,versions=v1alpha1,name=vgitopsdeployment.kb.io,admissionReviewVersions=v1 @@ -57,9 +58,13 @@ var _ webhook.Validator = &GitOpsDeployment{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeployment) ValidateCreate() error { - gitopsdeploymentlog.Info("validate create", "name", r.Name) - if err := r.ValidateGitOpsDeployment(); err != nil { + log := gitopsdeploymentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeployment") + + log.V(logutil.LogLevel_Debug).Info("validate create") + + if err := r.validateGitOpsDeployment(); err != nil { + log.Info("webhook rejected invalid create", "error", fmt.Sprintf("%v", err)) return err } @@ -68,9 +73,13 @@ func (r *GitOpsDeployment) ValidateCreate() error { // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeployment) ValidateUpdate(old runtime.Object) error { - gitopsdeploymentlog.Info("validate update", "name", r.Name) - if err := r.ValidateGitOpsDeployment(); err != nil { + log := gitopsdeploymentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeployment") + + log.V(logutil.LogLevel_Debug).Info("validate update") + + if err := r.validateGitOpsDeployment(); err != nil { + log.Info("webhook rejected invalid update", "error", fmt.Sprintf("%v", err)) return err } @@ -79,12 +88,15 @@ func (r *GitOpsDeployment) ValidateUpdate(old runtime.Object) error { // ValidateDelete implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeployment) ValidateDelete() error { - gitopsdeploymentlog.Info("validate delete", "name", r.Name) + + log := gitopsdeploymentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeployment") + + log.V(logutil.LogLevel_Debug).Info("validate delete") return nil } -func (r *GitOpsDeployment) ValidateGitOpsDeployment() error { +func (r *GitOpsDeployment) validateGitOpsDeployment() error { // Check whether Type is manual or automated if !(r.Spec.Type == GitOpsDeploymentSpecType_Automated || r.Spec.Type == GitOpsDeploymentSpecType_Manual) { diff --git a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentmanagedenvironment_webhook.go b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentmanagedenvironment_webhook.go index 263523bc5f..e307850c5c 100644 --- a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentmanagedenvironment_webhook.go +++ b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentmanagedenvironment_webhook.go @@ -44,7 +44,10 @@ var _ webhook.Defaulter = &GitOpsDeploymentManagedEnvironment{} // Default implements webhook.Defaulter so a webhook will be registered for the type func (r *GitOpsDeploymentManagedEnvironment) Default() { - gitopsdeploymentmanagedenvironmentlog.Info("default", "name", r.Name) + + log := gitopsdeploymentmanagedenvironmentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentManagedEnvironment") + + log.V(logutil.LogLevel_Debug).Info("default", "name", r.Name) } @@ -54,9 +57,13 @@ var _ webhook.Validator = &GitOpsDeploymentManagedEnvironment{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentManagedEnvironment) ValidateCreate() error { - gitopsdeploymentmanagedenvironmentlog.Info("validate create", "name", r.Name) + + log := gitopsdeploymentmanagedenvironmentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentManagedEnvironment") + + log.V(logutil.LogLevel_Debug).Info("validate create") if err := r.ValidateGitOpsDeploymentManagedEnv(); err != nil { + log.Info("webhook rejected invalid create", "error", fmt.Sprintf("%v", err)) return err } @@ -65,9 +72,13 @@ func (r *GitOpsDeploymentManagedEnvironment) ValidateCreate() error { // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentManagedEnvironment) ValidateUpdate(old runtime.Object) error { - gitopsdeploymentmanagedenvironmentlog.Info("validate update", "name", r.Name) + + log := gitopsdeploymentmanagedenvironmentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentManagedEnvironment") + + log.V(logutil.LogLevel_Debug).Info("validate update") if err := r.ValidateGitOpsDeploymentManagedEnv(); err != nil { + log.Info("webhook rejected invalid update", "error", fmt.Sprintf("%v", err)) return err } @@ -76,7 +87,10 @@ func (r *GitOpsDeploymentManagedEnvironment) ValidateUpdate(old runtime.Object) // ValidateDelete implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentManagedEnvironment) ValidateDelete() error { - gitopsdeploymentmanagedenvironmentlog.Info("validate delete", "name", r.Name) + + log := gitopsdeploymentmanagedenvironmentlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentManagedEnvironment") + + log.V(logutil.LogLevel_Debug).Info("validate delete", "name", r.Name) return nil } diff --git a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentrepositorycredential_webhook.go b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentrepositorycredential_webhook.go index ab928e0d31..e1cd8f1bdf 100644 --- a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentrepositorycredential_webhook.go +++ b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentrepositorycredential_webhook.go @@ -44,7 +44,9 @@ var _ webhook.Defaulter = &GitOpsDeploymentRepositoryCredential{} // Default implements webhook.Defaulter so a webhook will be registered for the type func (r *GitOpsDeploymentRepositoryCredential) Default() { - gitopsdeploymentrepositorycredentiallog.Info("default", "name", r.Name) + log := gitopsdeploymentrepositorycredentiallog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentRepositoryCredential") + + log.V(logutil.LogLevel_Debug).Info("default", "name", r.Name) } @@ -54,9 +56,13 @@ var _ webhook.Validator = &GitOpsDeploymentRepositoryCredential{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentRepositoryCredential) ValidateCreate() error { - gitopsdeploymentrepositorycredentiallog.Info("validate create", "name", r.Name) + + log := gitopsdeploymentrepositorycredentiallog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentRepositoryCredential") + + log.V(logutil.LogLevel_Debug).Info("validate create") if err := r.ValidateGitOpsDeploymentRepoCred(); err != nil { + log.Info("webhook rejected invalid create", "error", fmt.Sprintf("%v", err)) return err } @@ -65,9 +71,13 @@ func (r *GitOpsDeploymentRepositoryCredential) ValidateCreate() error { // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentRepositoryCredential) ValidateUpdate(old runtime.Object) error { - gitopsdeploymentrepositorycredentiallog.Info("validate update", "name", r.Name) + + log := gitopsdeploymentrepositorycredentiallog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentRepositoryCredential") + + log.V(logutil.LogLevel_Debug).Info("validate update") if err := r.ValidateGitOpsDeploymentRepoCred(); err != nil { + log.Info("webhook rejected invalid update", "error", fmt.Sprintf("%v", err)) return err } @@ -76,7 +86,10 @@ func (r *GitOpsDeploymentRepositoryCredential) ValidateUpdate(old runtime.Object // ValidateDelete implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentRepositoryCredential) ValidateDelete() error { - gitopsdeploymentrepositorycredentiallog.Info("validate delete", "name", r.Name) + + log := gitopsdeploymentrepositorycredentiallog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentRepositoryCredential") + + log.V(logutil.LogLevel_Debug).Info("validate delete") return nil } diff --git a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentsyncrun_webhook.go b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentsyncrun_webhook.go index 675e87dc9a..0c65f0e52b 100644 --- a/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentsyncrun_webhook.go +++ b/backend-shared/apis/managed-gitops/v1alpha1/gitopsdeploymentsyncrun_webhook.go @@ -45,8 +45,12 @@ func (r *GitOpsDeploymentSyncRun) SetupWebhookWithManager(mgr ctrl.Manager) erro var _ webhook.Defaulter = &GitOpsDeploymentSyncRun{} // Default implements webhook.Defaulter so a webhook will be registered for the type + func (r *GitOpsDeploymentSyncRun) Default() { - gitopsdeploymentsyncrunlog.Info("default", "name", r.Name) + + log := gitopsdeploymentsyncrunlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentSyncRun") + + log.V(logutil.LogLevel_Debug).Info("default") } @@ -56,10 +60,15 @@ var _ webhook.Validator = &GitOpsDeploymentSyncRun{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentSyncRun) ValidateCreate() error { - gitopsdeploymentsyncrunlog.Info("validate create", "name", r.Name) + + log := gitopsdeploymentsyncrunlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentSyncRun") + + log.V(logutil.LogLevel_Debug).Info("validate create") if r.Name == invalid_name { - return fmt.Errorf(error_invalid_name) + err := fmt.Errorf(error_invalid_name) + log.Info("webhook rejected invalid create", "error", fmt.Sprintf("%v", err)) + return err } return nil @@ -67,14 +76,20 @@ func (r *GitOpsDeploymentSyncRun) ValidateCreate() error { // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentSyncRun) ValidateUpdate(old runtime.Object) error { - gitopsdeploymentsyncrunlog.Info("validate update", "name", r.Name) + + log := gitopsdeploymentsyncrunlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentSyncRun") + + log.V(logutil.LogLevel_Debug).Info("validate update") return nil } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type func (r *GitOpsDeploymentSyncRun) ValidateDelete() error { - gitopsdeploymentsyncrunlog.Info("validate delete", "name", r.Name) + + log := gitopsdeploymentsyncrunlog.WithValues(logutil.Log_K8s_Request_Name, r.Name, logutil.Log_K8s_Request_Namespace, r.Namespace, "kind", "GitOpsDeploymentSyncRun") + + log.V(logutil.LogLevel_Debug).Info("validate delete") return nil } diff --git a/backend-shared/util/log/log.go b/backend-shared/util/log/log.go index 4220eddc45..6f53923fa4 100644 --- a/backend-shared/util/log/log.go +++ b/backend-shared/util/log/log.go @@ -21,6 +21,24 @@ const ( LogLevel_Warn int = -1 ) +const ( + Log_Component = "component" + Log_Component_Appstudio_Controller = "appstudio-controller" + Log_Component_ClusterAgent = "cluster-agent" + Log_Component_Backend_ClusterReconciler = "cluster-reconciler" + Log_Component_Backend_DatabaseMetricsReconciler = "database-metrics-reconciler" + Log_Component_Backend_DatabaseReconciler = "database-reconciler" + Log_Component_Backend_RepocredReconciler = "repocred-reconciler" + Log_Component_Backend_WorkspaceResourceEventLoop = "workspace_resource_event_loop" + + Log_K8s_Request_Name = "requestName" + Log_K8s_Request_Namespace = "requestNamespace" + Log_K8s_Request_UID = "requestUID" + Log_K8s_Request_NamespaceID = "requestNamespaceID" + + Log_ApplicationID = "applicationID" +) + type ResourceChangeType string const ( diff --git a/backend-shared/util/operations/types.go b/backend-shared/util/operations/types.go index 3145aef31a..4e14b3224a 100644 --- a/backend-shared/util/operations/types.go +++ b/backend-shared/util/operations/types.go @@ -74,10 +74,10 @@ func createOperationInternal(ctx context.Context, waitForOperation bool, dbOpera l logr.Logger) (*managedgitopsv1alpha1.Operation, *db.Operation, error) { var err error - l = l.WithValues("Operation GitOpsEngineInstanceID", dbOperationParam.Instance_id, - "Operation ResourceID", dbOperationParam.Resource_id, - "Operation ResourceType", dbOperationParam.Resource_type, - "Operation OwnerUserID", clusterUserID, + l = l.WithValues("operationGitOpsEngineInstanceID", dbOperationParam.Instance_id, + "operationResourceID", dbOperationParam.Resource_id, + "operationResourceType", dbOperationParam.Resource_type, + "operationOwnerUserID", clusterUserID, ) // GitopsEngineInstance Namespace and OperationNamespace should match, if it doesn't match we won't process the operation further. @@ -90,20 +90,20 @@ func createOperationInternal(ctx context.Context, waitForOperation bool, dbOpera } if gitopsEngineInstance.Namespace_name == "" { - l.Error(err, "Invalid: GitopsEngineInstance namespace is empty, GitopsEngineInstanceID: %s", gitopsEngineInstance.Gitopsengineinstance_id) - return nil, nil, fmt.Errorf("Invalid GitopsEngine namespace") + l.Error(err, "Invalid: GitopsEngineInstance namespace is empty") + return nil, nil, fmt.Errorf("invalid GitopsEngine namespace") } if operationNamespace == "" { - l.Error(err, "Invalid: Operation namespace is empty, OperationID: %s", dbOperationParam.Operation_id) - return nil, nil, fmt.Errorf("Invalid Operation namespace") + l.Error(err, "Invalid: Operation namespace is empty") + return nil, nil, fmt.Errorf("invalid Operation namespace") } if operationNamespace != gitopsEngineInstance.Namespace_name { mismatchedNamespace := "OperationNS: " + operationNamespace + " " + "GitopsEngineInstanceNS: " + gitopsEngineInstance.Namespace_name - return nil, nil, fmt.Errorf("Namespace mismatched in given OperationCR and existing GitopsEngineInstance " + mismatchedNamespace) + return nil, nil, fmt.Errorf("namespace mismatched in given OperationCR and existing GitopsEngineInstance " + mismatchedNamespace) } var dbOperationList []db.Operation @@ -131,7 +131,7 @@ func createOperationInternal(ctx context.Context, waitForOperation bool, dbOpera } if err = gitopsEngineClient.Get(ctx, client.ObjectKeyFromObject(&k8sOperation), &k8sOperation); err != nil { - l.Error(err, "unable to fetch existing Operation from cluster, skipping.", "Operation k8s Name", k8sOperation.Name) + l.Error(err, "unable to fetch existing Operation from cluster, skipping.", "operationK8sName", k8sOperation.Name) // We intentionally don't return here: we keep going through the list, even if an error occurs. // Only one needs to match. } else { @@ -157,7 +157,7 @@ func createOperationInternal(ctx context.Context, waitForOperation bool, dbOpera l.Error(err, "Unable to create Operation database row") return nil, nil, err } - l.Info("Created Operation database row", "Operation DB ID", dbOperation.Operation_id) + l.Info("Created Operation database row", "operationDBID", dbOperation.Operation_id) // Create K8s operation operation := managedgitopsv1alpha1.Operation{ @@ -179,20 +179,21 @@ func createOperationInternal(ctx context.Context, waitForOperation bool, dbOpera l.Error(err, "Unable to create K8s Operation") return nil, nil, err } - l.Info("Created K8s Operation CR", "Operation CR Name", operation.Name, - "Operation CR Namespace", operation.Namespace, "Operation CR ID", operation.Spec.OperationID, - "Operation CR State", operation.Status) + + l = l.WithValues("operationDBID", dbOperation.Operation_id) + + l.Info("Created K8s Operation CR", "operationCRName", operation.Name, "operationCRNamespace", operation.Namespace) // Wait for operation to complete. if waitForOperation { l.V(logutil.LogLevel_Debug).Info("Waiting for Operation to complete") if err = waitForOperationToComplete(ctx, &dbOperation, dbQueries, l); err != nil { - l.Error(err, "operation did not complete", "operation", dbOperation.Operation_id, "namespace", operation.Namespace) + l.Error(err, "operation did not complete", "operation", dbOperation.Operation_id, "operationNamespace", operation.Namespace) return nil, nil, err } - l.Info("Operation completed", "operation", fmt.Sprintf("%v", operation.Spec.OperationID)) + l.Info("Operation completed") } return &operation, &dbOperation, nil @@ -203,7 +204,7 @@ func createOperationInternal(ctx context.Context, waitForOperation bool, dbOpera func CleanupOperation(ctx context.Context, dbOperation db.Operation, k8sOperation managedgitopsv1alpha1.Operation, dbQueries db.ApplicationScopedQueries, gitopsEngineClient client.Client, deleteDBOperation bool, log logr.Logger) error { - log = log.WithValues("operation", dbOperation.Operation_id, "namespace", k8sOperation.Namespace) + log = log.WithValues("dbOperationID", dbOperation.Operation_id, "operationNamespace", k8sOperation.Namespace) if deleteDBOperation { // Delete the database entry @@ -212,8 +213,10 @@ func CleanupOperation(ctx context.Context, dbOperation db.Operation, k8sOperatio return err } if rowsDeleted != 1 { - log.V(logutil.LogLevel_Warn).Info("unexpected number of operation rows deleted", "operation-id", dbOperation.Operation_id, "rows", rowsDeleted) + log.V(logutil.LogLevel_Warn).Info("Unexpected number of operation rows deleted", "rows", rowsDeleted) } + + log.V(logutil.LogLevel_Debug).Info("Deleted operation Row") } // Delete the Operation CR @@ -223,7 +226,7 @@ func CleanupOperation(ctx context.Context, dbOperation db.Operation, k8sOperatio return err } } - log.V(logutil.LogLevel_Debug).Info("Deleted operation CR: " + k8sOperation.Name) + log.V(logutil.LogLevel_Debug).Info("Deleted Operation CR", "operationName", k8sOperation.Name) return nil diff --git a/backend-shared/util/service_account.go b/backend-shared/util/service_account.go index dc79db2391..e0a62684ce 100644 --- a/backend-shared/util/service_account.go +++ b/backend-shared/util/service_account.go @@ -53,7 +53,7 @@ func getOrCreateServiceAccount(ctx context.Context, k8sClient client.Client, ser return serviceAccount, nil } - log = log.WithValues("serviceAccount", serviceAccountName, "namespace", serviceAccountNS) + log = log.WithValues("serviceAccount", serviceAccountName, "serviceAccountNS", serviceAccountNS) if err := k8sClient.Create(ctx, serviceAccount); err != nil { log.Error(err, "Unable to create ServiceAccount") @@ -167,7 +167,7 @@ func createServiceAccountTokenSecret(ctx context.Context, k8sClient client.Clien Type: corev1.SecretTypeServiceAccountToken, } - log = log.WithValues("name", tokenSecret.Name, "namespace", tokenSecret.Namespace) + log = log.WithValues("tokenSecretName", tokenSecret.Name, "tokenSecretNamespace", tokenSecret.Namespace) if err := k8sClient.Create(ctx, tokenSecret); err != nil { log.Error(err, "Unable to create ServiceAccountToken Secret") @@ -192,7 +192,7 @@ func createOrUpdateClusterRoleAndRoleBinding(ctx context.Context, uuid string, k return fmt.Errorf("unable to get cluster role: %w", err) } - log := log.WithValues("name", clusterRole.Name) + log := log.WithValues("clusterRoleName", clusterRole.Name) clusterRole.Rules = ArgoCDManagerNamespacePolicyRules if err := k8sClient.Create(ctx, clusterRole); err != nil { @@ -202,7 +202,7 @@ func createOrUpdateClusterRoleAndRoleBinding(ctx context.Context, uuid string, k logutil.LogAPIResourceChangeEvent(clusterRole.Namespace, clusterRole.Name, clusterRole, logutil.ResourceCreated, log) } else { - log := log.WithValues("name", clusterRole.Name) + log := log.WithValues("clusterRoleName", clusterRole.Name) clusterRole.Rules = ArgoCDManagerNamespacePolicyRules if err := k8sClient.Update(ctx, clusterRole); err != nil { @@ -237,7 +237,7 @@ func createOrUpdateClusterRoleAndRoleBinding(ctx context.Context, uuid string, k Namespace: serviceAccountNamespace, }} - log = log.WithValues("name", clusterRoleBinding.Name) + log = log.WithValues("clusterRoleNameBindingName", clusterRoleBinding.Name) if update { if err := k8sClient.Update(ctx, clusterRoleBinding); err != nil { diff --git a/backend-shared/util/util.go b/backend-shared/util/util.go index e9acb58567..a64d70f63c 100644 --- a/backend-shared/util/util.go +++ b/backend-shared/util/util.go @@ -31,9 +31,9 @@ const ( ManagedEnvironmentSecretType = "managed-gitops.redhat.com/managed-environment" - JobKey = "job" // Clean up job key - JobKeyValue = "managed-gitops-cleanup" // Clean up job value - JobTypeKey = "jobType" // Key to identify clean up job type + Log_JobKey = "job" // Clean up job key + Log_JobKeyValue = "managed-gitops-cleanup" // Clean up job value + Log_JobTypeKey = "jobType" // Key to identify clean up job type ) // ExponentialBackoff: the more times in a row something fails, the longer we wait. diff --git a/backend/eventloop/application_event_loop/application_event_loop.go b/backend/eventloop/application_event_loop/application_event_loop.go index ba7f46aae7..54542f6e94 100644 --- a/backend/eventloop/application_event_loop/application_event_loop.go +++ b/backend/eventloop/application_event_loop/application_event_loop.go @@ -150,14 +150,13 @@ func applicationEventQueueLoop(ctx context.Context, k8sClient client.Client, aerFactory applicationEventRunnerFactory) { log := log.FromContext(ctx). - WithValues("workspaceID", workspaceID, - "gitOpsDeplName", gitopsDeploymentName, - "gitopsDeplNamespace", gitopsDeploymentNamespace, - "namespace", gitopsDeploymentNamespace). + WithValues(logutil.Log_K8s_Request_NamespaceID, workspaceID, + logutil.Log_K8s_Request_Name, gitopsDeploymentName, + logutil.Log_K8s_Request_Namespace, gitopsDeploymentNamespace). WithName(logutil.LogLogger_managed_gitops) - log.Info("applicationEventQueueLoop started.") - defer log.Info("applicationEventQueueLoop ended.") + log.V(logutil.LogLevel_Debug).Info("applicationEventQueueLoop started.") + defer log.V(logutil.LogLevel_Debug).Info("applicationEventQueueLoop ended.") state := applicationEventQueueLoopState{ activeDeploymentEvent: nil, diff --git a/backend/eventloop/application_event_loop/application_event_runner.go b/backend/eventloop/application_event_loop/application_event_runner.go index e24b4e9e88..da40443a44 100644 --- a/backend/eventloop/application_event_loop/application_event_runner.go +++ b/backend/eventloop/application_event_loop/application_event_runner.go @@ -84,9 +84,11 @@ func applicationEventLoopRunner(inputChannel chan *eventlooptypes.EventLoopEvent outerContext := context.Background() log := log.FromContext(outerContext). WithName(logutil.LogLogger_managed_gitops). - WithValues("namespaceID", namespaceID, - "debugContext", debugContext, - "namespace", gitopsDeploymentNamespace) + WithValues( + logutil.Log_K8s_Request_NamespaceID, namespaceID, + logutil.Log_K8s_Request_Namespace, gitopsDeploymentNamespace, + logutil.Log_K8s_Request_Name, gitopsDeploymentName, + "debugContext", debugContext) log.V(logutil.LogLevel_Debug).Info("applicationEventLoopRunner started") diff --git a/backend/eventloop/application_event_loop/application_event_runner_deployments.go b/backend/eventloop/application_event_loop/application_event_runner_deployments.go index f145e112cf..1a1e315c4c 100644 --- a/backend/eventloop/application_event_loop/application_event_runner_deployments.go +++ b/backend/eventloop/application_event_loop/application_event_runner_deployments.go @@ -329,7 +329,7 @@ func (a applicationEventLoopRunner_Action) handleNewGitOpsDeplEvent(ctx context. return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewDevOnlyError(err) } - a.log.Info("Created new Application in DB: "+application.Application_id, application.GetAsLogKeyValues()...) + a.log.Info("Created new Application in DB", application.GetAsLogKeyValues()...) // Create ApplicationOwner row in DB applicationOwner := &db.ApplicationOwner{ @@ -345,7 +345,7 @@ func (a applicationEventLoopRunner_Action) handleNewGitOpsDeplEvent(ctx context. return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewDevOnlyError(err) } - a.log.Info("Created new Application Owner in DB: "+applicationOwner.ApplicationOwnerApplicationID, applicationOwner.GetAsLogKeyValues()...) + a.log.Info("Created new ApplicationOwner in DB", applicationOwner.GetAsLogKeyValues()...) } else { a.log.Error(err, "unable to retrieve applicationOwner", "applicationOwner", applicationOwner) @@ -391,7 +391,7 @@ func (a applicationEventLoopRunner_Action) handleNewGitOpsDeplEvent(ctx context. k8sOperation, dbOperation, err := operations.CreateOperation(ctx, waitForOperation, dbOperationInput, clusterUser.Clusteruser_id, engineInstance.Namespace_name, dbQueries, gitopsEngineClient, a.log) if err != nil { - a.log.Error(err, "could not create operation", "namespace", engineInstance.Namespace_name) + a.log.Error(err, "could not create operation", "operationNamespace", engineInstance.Namespace_name) return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewDevOnlyError(err) } @@ -418,7 +418,7 @@ func (a applicationEventLoopRunner_Action) handleDeleteGitOpsDeplEvent(ctx conte return false, gitopserrors.NewDevOnlyError(fmt.Errorf("required parameter should not be nil in handleDelete: %v %v", deplToAppMappingList, clusterUser)) } - a.log.Info("Received GitOpsDeployment event for a GitOpsDeployment resource that no longer exists (or does not exist)") + a.log.Info("Reconciling GitOpsDeployment that no longer exists (or never existed)") apiNamespace := corev1.Namespace{} if err := a.workspaceClient.Get(ctx, types.NamespacedName{Name: a.eventResourceNamespace}, &apiNamespace); err != nil { @@ -458,7 +458,7 @@ func (a applicationEventLoopRunner_Action) handleDeleteGitOpsDeplEvent(ctx conte if gitopsDepl != nil && isGitOpsDeploymentDeleted(gitopsDepl) { // remove the finalizer if all the dependencies are cleaned up if err := removeFinalizerIfExists(ctx, a.workspaceClient, gitopsDepl, managedgitopsv1alpha1.DeletionFinalizer); err != nil { - a.log.Error(err, "failed to remove the deletion finalizer from GitOpsDeployment", "name", gitopsDepl.Name, "namespace", gitopsDepl.Namespace) + a.log.Error(err, "failed to remove the deletion finalizer from GitOpsDeployment") return false, gitopserrors.NewDevOnlyError(err) } } @@ -564,9 +564,9 @@ func (a applicationEventLoopRunner_Action) handleUpdatedGitOpsDeplEvent(ctx cont deplToAppMapping, gitopsDeployment, clusterUser)) } - log := a.log.WithValues("applicationId", deplToAppMapping.Application_id, "gitopsDeplUID", gitopsDeployment.UID) + log := a.log.WithValues(logutil.Log_ApplicationID, deplToAppMapping.Application_id, logutil.Log_K8s_Request_UID, gitopsDeployment.UID) - a.log.Info("Received GitOpsDeployment event for an existing GitOpsDeployment resource") + log.Info("Received GitOpsDeployment event for an existing GitOpsDeployment resource") application := &db.Application{Application_id: deplToAppMapping.Application_id} if err := dbQueries.GetApplicationById(ctx, application); err != nil { @@ -638,7 +638,7 @@ func (a applicationEventLoopRunner_Action) handleUpdatedGitOpsDeplEvent(ctx cont return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewUserDevError(userError, devError) } - appProjectDBRowsUpdated, err := a.sharedResourceEventLoop.ReconcileAppProjectRepositories(ctx, a.workspaceClient, apiNamespace, a.log) + appProjectDBRowsUpdated, err := a.sharedResourceEventLoop.ReconcileAppProjectRepositories(ctx, a.workspaceClient, apiNamespace, log) if err != nil { return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewDevOnlyError(fmt.Errorf("unexpected error on reconciling appproject repos: %w", err)) @@ -728,14 +728,14 @@ func (a applicationEventLoopRunner_Action) handleUpdatedGitOpsDeplEvent(ctx cont if err := dbQueries.GetApplicationOwnerByApplicationID(ctx, applicationOwner); err != nil { if db.IsResultNotFoundError(err) { if err := dbQueries.CreateApplicationOwner(ctx, applicationOwner); err != nil { - a.log.Error(err, "Unable to create application owner row in database", applicationOwner.GetAsLogKeyValues()...) + log.Error(err, "Unable to create application owner row in database", applicationOwner.GetAsLogKeyValues()...) return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewDevOnlyError(err) } - a.log.Info("Created new Application Owner in DB: "+applicationOwner.ApplicationOwnerApplicationID, applicationOwner.GetAsLogKeyValues()...) + log.Info("Created new Application Owner in DB", applicationOwner.GetAsLogKeyValues()...) } else { - a.log.Error(err, "unable to retrieve applicationOwner", "applicationOwner", applicationOwner) + log.Error(err, "unable to retrieve applicationOwner", "applicationOwner", applicationOwner) return nil, nil, deploymentModifiedResult_Failed, gitopserrors.NewDevOnlyError(err) } } @@ -792,7 +792,7 @@ func (a applicationEventLoopRunner_Action) cleanOldGitOpsDeploymentEntry(ctx con } if err := dbQueries.GetApplicationById(ctx, &dbApplication); err != nil { - a.log.Error(err, "unable to get application by id", "id", deplToAppMapping.Application_id) + a.log.Error(err, "unable to get application by id", logutil.Log_ApplicationID, deplToAppMapping.Application_id) if db.IsResultNotFoundError(err) { dbApplicationFound = false @@ -802,7 +802,7 @@ func (a applicationEventLoopRunner_Action) cleanOldGitOpsDeploymentEntry(ctx con } } - log := a.log.WithValues("applicationID", deplToAppMapping.Application_id) + log := a.log.WithValues(logutil.Log_ApplicationID, deplToAppMapping.Application_id) // 1) Remove the ApplicationState from the database rowsDeleted, err := dbQueries.DeleteApplicationStateById(ctx, deplToAppMapping.Application_id) @@ -822,11 +822,11 @@ func (a applicationEventLoopRunner_Action) cleanOldGitOpsDeploymentEntry(ctx con // - this ensures that the foreign key constraint of SyncOperation doesn't prevent us from deletion the Application rowsUpdated, err := dbQueries.UpdateSyncOperationRemoveApplicationField(ctx, deplToAppMapping.Application_id) if err != nil { - log.Error(err, "unable to update old sync operations", "applicationId", deplToAppMapping.Application_id) + log.Error(err, "unable to update old sync operations", logutil.Log_ApplicationID, deplToAppMapping.Application_id) return signalledShutdown_false, err } else if rowsUpdated == 0 { - log.Info("no SyncOperation rows updated, for updating old syncoperations on GitOpsDeployment deletion") + log.Info("No SyncOperation rows updated, for updating old syncoperations on GitOpsDeployment deletion") } else { log.Info("Removed references to Application from all SyncOperations that reference it") } @@ -882,7 +882,7 @@ func (a applicationEventLoopRunner_Action) cleanOldGitOpsDeploymentEntry(ctx con } // Ensure the the AppProjectRepository database entries accurately reflect the current state of the GitOpsDeployments/RepositoryCredentials in the Namespace - if _, err := a.sharedResourceEventLoop.ReconcileAppProjectRepositories(ctx, a.workspaceClient, apiNamespace, a.log); err != nil { + if _, err := a.sharedResourceEventLoop.ReconcileAppProjectRepositories(ctx, a.workspaceClient, apiNamespace, log); err != nil { log.Error(err, "unable to reconcile app project repositories") return signalledShutdown_false, err } @@ -950,8 +950,6 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD crUpdated_false = false // false: not updated ) - log := a.log.WithValues("gitOpsDeploymentName", resourceName, "gitopsDeploymentNamespace", namespaceName) - // TODO: GITOPSRVCE-68 - PERF - In general, polling for all GitOpsDeployments in a workspace will scale poorly with large number of applications in the workspace. We should switch away from polling in the future. // 1) Retrieve the GitOpsDeployment from the namespace, using the namespace and resource name @@ -965,7 +963,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD // Our work is done return crUpdated_false, nil } else { - log.Error(err, "unable to locate gitops object in handleUpdateDeploymentStatusTick") + a.log.Error(err, "unable to locate gitops object in handleUpdateDeploymentStatusTick") return crUpdated_false, err } } @@ -984,7 +982,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD // No Application associated with this GitOpsDeployment, so no work to do return crUpdated_false, nil } else { - log.Error(err, "unable to retrieve DeploymentToApplicationMapping in tick status update") + a.log.Error(err, "unable to retrieve DeploymentToApplicationMapping in tick status update") return crUpdated_false, err } } @@ -994,7 +992,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD if err := dbQueries.GetApplicationStateById(ctx, &applicationState); err != nil { if db.IsResultNotFoundError(err) { - log.Info("ApplicationState not found for application, on deploymentStatusTick: " + applicationState.Applicationstate_application_id) + a.log.Info("ApplicationState not found for application, on deploymentStatusTick: " + applicationState.Applicationstate_application_id) return crUpdated_false, nil } else { return crUpdated_false, err @@ -1013,7 +1011,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD newGitopsDeplConditions := []managedgitopsv1alpha1.GitOpsDeploymentCondition{} if len(applicationState.Conditions) != 0 { if err := yaml.Unmarshal(applicationState.Conditions, &newGitopsDeplConditions); err != nil { - log.Error(err, "failed to unmarshal ApplicationState conditions") + a.log.Error(err, "failed to unmarshal ApplicationState conditions") return crUpdated_false, err } } @@ -1036,20 +1034,20 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD var err error gitopsDeployment.Status.Resources, err = decompressResourceData(applicationState.Resources) if err != nil { - log.Error(err, "unable to decompress resources byte array received from table.") + a.log.Error(err, "unable to decompress resources byte array received from table.") return crUpdated_false, err } gitopsDeployment.Status.OperationState, err = decompressOperationState(applicationState.OperationState) if err != nil { - log.Error(err, "unable to decompress operationState byte array received from table.") + a.log.Error(err, "unable to decompress operationState byte array received from table.") return crUpdated_false, err } var comparedTo fauxargocd.FauxComparedTo comparedTo, err = retrieveComparedToFieldInApplicationState(applicationState.ReconciledState) if err != nil { - log.Error(err, "SEVERE: unable to retrieve comparedTo field in ApplicationState") + a.log.Error(err, "SEVERE: unable to retrieve comparedTo field in ApplicationState") return crUpdated_false, err } @@ -1088,7 +1086,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleUpdateD } // We don't need to log status updates, e.g. via 'sharedutil.LogAPIResourceChangeEvent' - log.V(logutil.LogLevel_Debug).Info("Updated status in deploymentStatusTick") + a.log.V(logutil.LogLevel_Debug).Info("Updated status in deploymentStatusTick") // NOTE: make sure to preserve the existing conditions fields that are in the status field of the CR, when updating the status! diff --git a/backend/eventloop/application_event_loop/application_event_runner_syncruns.go b/backend/eventloop/application_event_loop/application_event_runner_syncruns.go index 642049b372..9e1e329d52 100644 --- a/backend/eventloop/application_event_loop/application_event_runner_syncruns.go +++ b/backend/eventloop/application_event_loop/application_event_runner_syncruns.go @@ -156,7 +156,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleSyncRun syncRunCRExists = false } else { userError := "unable to retrieve the GitOpsDeploymentSyncRun object from the namespace, due to unknown error." - log.Error(err, "unable to locate object in handleSyncRunModified", "request", syncRunKey) + log.Error(err, "unable to locate object in handleSyncRunModified") return gitopserrors.NewUserDevError(userError, err) } } @@ -207,8 +207,7 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleSyncRun a.eventResourceName, a.eventResourceNamespace, eventlooptypes.GetWorkspaceIDFromNamespaceID(namespace), db.APICRToDatabaseMapping_DBRelationType_SyncOperation, &apiCRToDBList); err != nil { userError := "unable to retrieve data related to previous GitOpsDeploymentSyncRun in the namespace, due to an unknown error" - log.Error(err, "unable to find API CR to DB Mapping, by API name/namespace/uid", - "name", a.eventResourceName, "namespace", a.eventResourceNamespace, "UID", string(namespace.UID)) + log.Error(err, "unable to find API CR to DB Mapping, by API name/namespace/uid", logutil.Log_K8s_Request_NamespaceID, string(namespace.UID)) return gitopserrors.NewUserDevError(userError, err) } @@ -300,20 +299,20 @@ func (a *applicationEventLoopRunner_Action) applicationEventRunner_handleSyncRun deplToAppMapping := &db.DeploymentToApplicationMapping{Deploymenttoapplicationmapping_uid_id: string(gitopsDepl.UID)} if err = dbQueries.GetDeploymentToApplicationMappingByDeplId(ctx, deplToAppMapping); err != nil { - log.Error(err, "unable to retrieve deployment to application mapping, on sync run modified", "uid", string(gitopsDepl.UID)) + log.Error(err, "unable to retrieve deployment to application mapping, on sync run modified", logutil.Log_K8s_Request_UID, string(gitopsDepl.UID)) return gitopserrors.NewDevOnlyError(err) } application = &db.Application{Application_id: deplToAppMapping.Application_id} if err := dbQueries.GetApplicationById(ctx, application); err != nil { - log.Error(err, "unable to retrieve application, on sync run modified", "applicationId", string(deplToAppMapping.Application_id)) + log.Error(err, "unable to retrieve application, on sync run modified", logutil.Log_ApplicationID, deplToAppMapping.Application_id) return gitopserrors.NewDevOnlyError(err) } if gitopsEngineInstance, err = a.sharedResourceEventLoop.GetGitopsEngineInstanceById(ctx, application.Engine_instance_inst_id, - a.workspaceClient, namespace, a.log); err != nil { + a.workspaceClient, namespace, log); err != nil { - log.Error(err, "unable to retrieve gitopsengineinstance, on sync run modified", "instanceId", string(application.Engine_instance_inst_id)) + log.Error(err, "unable to retrieve gitopsengineinstance, on sync run modified", "gitopsEngineInstanceID", application.Engine_instance_inst_id) return gitopserrors.NewDevOnlyError(err) } @@ -359,7 +358,7 @@ func (a *applicationEventLoopRunner_Action) handleDeletedGitOpsDeplSyncRunEvent( log.Info("Received GitOpsDeploymentSyncRun event for a GitOpsDeploymentSyncRun resource that no longer exists") if syncOperation.Application_id == "" { - log.Info("Application row not found for SyncOperation. This is normally because the Application has already been deleted.", "syncOperationID", syncOperation.SyncOperation_id, "applicationID", syncOperation.Application_id) + log.Info("'application_id' field is empty for SyncOperation. This is normally because the Application has already been deleted.", "syncOperationID", syncOperation.SyncOperation_id) // Since there is no Application, remove the DB resources without creating an Operation. @@ -403,15 +402,14 @@ func (a *applicationEventLoopRunner_Action) handleDeletedGitOpsDeplSyncRunEvent( application := &db.Application{Application_id: syncOperation.Application_id} if err := dbQueries.GetApplicationById(ctx, application); err != nil { - log.Error(err, "unable to retrieve application, on sync run modified", "applicationId", string(syncOperation.Application_id)) + log.Error(err, "unable to retrieve application, on sync run modified", logutil.Log_ApplicationID, syncOperation.Application_id) return gitopserrors.NewDevOnlyError(err) } gitopsEngineInstance, err := a.sharedResourceEventLoop.GetGitopsEngineInstanceById(ctx, application.Engine_instance_inst_id, - a.workspaceClient, namespace, a.log) + a.workspaceClient, namespace, log) if err != nil { - - log.Error(err, "unable to retrieve gitopsengineinstance, on sync run modified", "instanceId", string(application.Engine_instance_inst_id)) + log.Error(err, "unable to retrieve gitopsengineinstance, on sync run modified", "instanceId", application.Engine_instance_inst_id) return gitopserrors.NewDevOnlyError(err) } if gitopsEngineInstance == nil { diff --git a/backend/eventloop/cluster_reconciler.go b/backend/eventloop/cluster_reconciler.go index b025e6d290..0c59e0ad96 100644 --- a/backend/eventloop/cluster_reconciler.go +++ b/backend/eventloop/cluster_reconciler.go @@ -12,6 +12,7 @@ import ( argocdutil "github.com/redhat-appstudio/managed-gitops/backend-shared/util/argocd" logutil "github.com/redhat-appstudio/managed-gitops/backend-shared/util/log" + apierr "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/discovery" @@ -53,8 +54,8 @@ func (c *ClusterReconciler) Start() { ctx := context.Background() log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("component", "cluster-reconciler"). - WithValues(sharedutil.JobKey, sharedutil.JobKeyValue) + WithValues(logutil.Log_Component, logutil.Log_Component_Backend_ClusterReconciler). + WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue) _, _ = sharedutil.CatchPanic(func() error { c.cleanOrphanedResources(ctx, log) @@ -69,11 +70,11 @@ func (c *ClusterReconciler) Start() { // A k8s resource is considered to be orphaned when: // 1. It was previously managed by Argo CD i.e has label "app.kubernetes.io/instance". // 2. It doesn't have a corresponding GitOpsDeployment resource. -func (c *ClusterReconciler) cleanOrphanedResources(ctx context.Context, log logr.Logger) { +func (c *ClusterReconciler) cleanOrphanedResources(ctx context.Context, logParam logr.Logger) { // Use a label selector to filter resources managed by Argo CD labelSelector, err := labels.Parse(argocdutil.ArgocdResourceLabel) if err != nil { - log.Error(err, "failed to create a label selector for listing resources managed by Argo CD") + logParam.Error(err, "failed to create a label selector for listing resources managed by Argo CD") return } @@ -81,14 +82,19 @@ func (c *ClusterReconciler) cleanOrphanedResources(ctx context.Context, log logr LabelSelector: labelSelector, } - apiObjects, err := c.getAllNamespacedAPIResources(ctx, log, listOpts) + apiObjects, err := c.getAllNamespacedAPIResources(ctx, logParam, listOpts) if err != nil { - log.Error(err, "failed to get namespaced API resources from the cluster") + logParam.Error(err, "failed to get namespaced API resources from the cluster") return } for i, obj := range apiObjects { + log := logParam.WithValues( + logutil.Log_K8s_Request_Namespace, obj.GetNamespace(), + logutil.Log_K8s_Request_Name, obj.GetName(), + "gvk", obj.GroupVersionKind()) + // Skip all openshift namespces if strings.HasPrefix(obj.GetNamespace(), "openshift-") { continue @@ -117,7 +123,7 @@ func (c *ClusterReconciler) cleanOrphanedResources(ctx context.Context, log logr if err := c.client.List(ctx, gitopsDeplList, &client.ListOptions{ Namespace: obj.GetNamespace(), }); err != nil { - log.Error(err, "failed to list GitOpsDeployments", "namespace", obj.GetNamespace()) + log.Error(err, "failed to list GitOpsDeployments") continue } @@ -138,12 +144,15 @@ func (c *ClusterReconciler) cleanOrphanedResources(ctx context.Context, log logr // The resource was managed by Argo CD i.e. it has label "app.kubernetes.io/instance" // But it doesn't have a corresponding GitOpsDeployment so it can be deleted. if !found { + if err := c.client.Delete(ctx, &apiObjects[i]); err != nil { - log.Error(err, "failed to delete object in the orphaned reconciler", "name", obj.GetName(), "namespace", obj.GetNamespace(), "gvk", obj.GroupVersionKind()) + if !apierr.IsNotFound(err) { + log.Error(err, "failed to delete object in the orphaned reconciler") + } continue } - log.Info("Deleted an orphaned resource that is not managed by Argo CD anymore", "Name", obj.GetName(), "Namespace", obj.GetNamespace(), "gvk", obj.GroupVersionKind()) + log.Info("Deleted an orphaned resource that is not managed by Argo CD anymore") } } } @@ -176,7 +185,7 @@ func (c *ClusterReconciler) getAllNamespacedAPIResources(ctx context.Context, lo objList.SetKind(apiResource.Kind) if err := c.client.List(ctx, objList, opts...); err != nil { - log.V(logutil.LogLevel_Debug).Info("failed to list resources", "resource", apiResource.Kind, "error", fmt.Sprintf("%v", err)) + log.V(logutil.LogLevel_Debug).Info("failed to list resources", "resourceKind", apiResource.Kind, "error", fmt.Sprintf("%v", err)) continue } diff --git a/backend/eventloop/controller_event_loop.go b/backend/eventloop/controller_event_loop.go index cf25799707..5b5d8d479f 100644 --- a/backend/eventloop/controller_event_loop.go +++ b/backend/eventloop/controller_event_loop.go @@ -66,11 +66,11 @@ type controllerEventLoop_workspaceEntry struct { // This channel is non-blocking. func controllerEventLoopRouter(input chan eventlooptypes.EventLoopEvent, workspaceEventFactory workspaceEventLoopRouterFactory) { - eventLoopRouterLog := log.FromContext(context.Background()). + outerEventLoopRouterLog := log.FromContext(context.Background()). WithName(logutil.LogLogger_managed_gitops) - eventLoopRouterLog.Info("controllerEventLoopRouter started.") - defer eventLoopRouterLog.Error(nil, "SEVERE: controllerEventLoopRouter ended.") + outerEventLoopRouterLog.Info("controllerEventLoopRouter started.") + defer outerEventLoopRouterLog.Error(nil, "SEVERE: controllerEventLoopRouter ended.") workspaceEntries := map[string] /* workspace id -> */ controllerEventLoop_workspaceEntry{} @@ -78,13 +78,17 @@ func controllerEventLoopRouter(input chan eventlooptypes.EventLoopEvent, workspa event := <-input - eventLoopRouterLog.V(logutil.LogLevel_Debug).WithValues("namespace", event.Request.Namespace).Info("eventLoop received event", - "event", eventlooptypes.StringEventLoopEvent(&event), "workspace", event.WorkspaceID) + log := outerEventLoopRouterLog.WithValues(logutil.Log_K8s_Request_Namespace, event.Request.Namespace, + logutil.Log_K8s_Request_Name, event.Request.Name, + logutil.Log_K8s_Request_NamespaceID, event.WorkspaceID) + + log.V(logutil.LogLevel_Debug).Info("eventLoop received event", + "event", eventlooptypes.StringEventLoopEvent(&event)) workspaceEntryVal, ok := workspaceEntries[event.WorkspaceID] if !ok { - workspaceEventLoop := workspaceEventFactory.startWorkspaceEventLoopRouter(event.WorkspaceID) + workspaceEventLoop := workspaceEventFactory.startWorkspaceEventLoopRouter(event.Request.Namespace, event.WorkspaceID) // Start the workspace's event loop go-routine, if it's not already started. workspaceEntryVal = controllerEventLoop_workspaceEntry{ @@ -109,7 +113,7 @@ func controllerEventLoopRouter(input chan eventlooptypes.EventLoopEvent, workspa // defaultWorkspaceEventLoopRouterFactory should always be used, unless a mocked replacement is needed // for a unit test. type workspaceEventLoopRouterFactory interface { - startWorkspaceEventLoopRouter(workspaceID string) WorkspaceEventLoopRouterStruct + startWorkspaceEventLoopRouter(namespaceName string, namespaceID string) WorkspaceEventLoopRouterStruct } type defaultWorkspaceEventLoopRouterFactory struct { @@ -117,8 +121,8 @@ type defaultWorkspaceEventLoopRouterFactory struct { var _ workspaceEventLoopRouterFactory = defaultWorkspaceEventLoopRouterFactory{} -func (d defaultWorkspaceEventLoopRouterFactory) startWorkspaceEventLoopRouter(workspaceID string) WorkspaceEventLoopRouterStruct { +func (d defaultWorkspaceEventLoopRouterFactory) startWorkspaceEventLoopRouter(namespaceName string, namespaceID string) WorkspaceEventLoopRouterStruct { - return newWorkspaceEventLoopRouter(workspaceID) + return newWorkspaceEventLoopRouter(namespaceName, namespaceID) } diff --git a/backend/eventloop/controller_event_loop_test.go b/backend/eventloop/controller_event_loop_test.go index 569605a01b..01aea393be 100644 --- a/backend/eventloop/controller_event_loop_test.go +++ b/backend/eventloop/controller_event_loop_test.go @@ -47,7 +47,7 @@ type mockWorkspaceEventLoopFactory struct { var _ workspaceEventLoopRouterFactory = &mockWorkspaceEventLoopFactory{} -func (cetf *mockWorkspaceEventLoopFactory) startWorkspaceEventLoopRouter(workspaceID string) WorkspaceEventLoopRouterStruct { +func (cetf *mockWorkspaceEventLoopFactory) startWorkspaceEventLoopRouter(namespaceName string, namespaceID string) WorkspaceEventLoopRouterStruct { // Rather than starting a new workspace event loop, instead just return a pre-provided channel return WorkspaceEventLoopRouterStruct{ channel: cetf.mockChannel, diff --git a/backend/eventloop/db_metrics.go b/backend/eventloop/db_metrics.go index 3cddcf8d61..9752e435f4 100644 --- a/backend/eventloop/db_metrics.go +++ b/backend/eventloop/db_metrics.go @@ -33,7 +33,7 @@ func (r *MetricsReconciler) StartDBMetricsReconcilerForMetrics() { ctx := context.Background() log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("component", "database-metrics-reconciler") + WithValues(logutil.Log_Component, logutil.Log_Component_Backend_DatabaseMetricsReconciler) _, _ = sharedutil.CatchPanic(func() error { operationDbReconcile(ctx, r.DB, r.Client, log) diff --git a/backend/eventloop/db_reconciler.go b/backend/eventloop/db_reconciler.go index 352d252567..267cbe7862 100644 --- a/backend/eventloop/db_reconciler.go +++ b/backend/eventloop/db_reconciler.go @@ -52,9 +52,11 @@ type DatabaseReconciler struct { // This function iterates through each entry of DTAM and ACTDM tables in DB and ensures that the required CRs is present in cluster. func (r *DatabaseReconciler) StartDatabaseReconciler() { ctx := context.Background() + log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("component", "database-reconciler") + WithValues(logutil.Log_Component, logutil.Log_Component_Backend_DatabaseReconciler) + databaseReconcilerInterval := sharedutil.SelfHealInterval(defaultDatabaseReconcilerInterval, log) if databaseReconcilerInterval > 0 { r.startTimerForNextCycle(ctx, databaseReconcilerInterval, log) @@ -110,11 +112,10 @@ func (r *DatabaseReconciler) startTimerForNextCycle(ctx context.Context, databas // cleanOrphanedEntriesfromTable_DTAM loops through the DTAMs in a database and verifies they are still valid. If not, the resources are deleted. // - The skipDelay can be used to skip the time.Sleep(), but this should true when called from a unit test. -func cleanOrphanedEntriesfromTable_DTAM(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, skipDelay bool, l logr.Logger) { +func cleanOrphanedEntriesfromTable_DTAM(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, skipDelay bool, logParam logr.Logger) { offSet := 0 - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_DTAM") + log := logParam.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue, sharedutil.Log_JobTypeKey, "DB_DTAM") // Continuously iterate and fetch batches until all entries of DeploymentToApplicationMapping table are processed. for { @@ -140,40 +141,41 @@ func cleanOrphanedEntriesfromTable_DTAM(ctx context.Context, dbQueries db.Databa // Iterate over batch received above. for i := range listOfdeplToAppMapping { deplToAppMappingFromDB := listOfdeplToAppMapping[i] // To avoid "Implicit memory aliasing in for loop." error. - gitOpsDeployment := managedgitopsv1alpha1.GitOpsDeployment{ + gitopsDeployment := managedgitopsv1alpha1.GitOpsDeployment{ ObjectMeta: metav1.ObjectMeta{ Name: deplToAppMappingFromDB.DeploymentName, Namespace: deplToAppMappingFromDB.DeploymentNamespace, }, } - if err := client.Get(ctx, types.NamespacedName{Name: deplToAppMappingFromDB.DeploymentName, Namespace: deplToAppMappingFromDB.DeploymentNamespace}, &gitOpsDeployment); err != nil { + log := log.WithValues("gitopsDeploymentName", gitopsDeployment.Name) + + if err := client.Get(ctx, types.NamespacedName{Name: deplToAppMappingFromDB.DeploymentName, Namespace: deplToAppMappingFromDB.DeploymentNamespace}, &gitopsDeployment); err != nil { if apierr.IsNotFound(err) { // A) If GitOpsDeployment CR is not found in cluster, delete related entries from table - log.Info("GitOpsDeployment " + gitOpsDeployment.Name + " not found in Cluster, probably user deleted it, " + - "but It still exists in DB, hence deleting related database entries.") + log.Info("GitOpsDeployment not found in Cluster, probably user deleted it, but it still exists in DB, hence deleting related database entries.") if err := cleanOrphanedEntriesfromTable_DTAM_DeleteEntry(ctx, &deplToAppMappingFromDB, dbQueries, log); err != nil { - log.Error(err, "Error occurred in DTAM Reconciler while cleaning gitOpsDeployment entries from DB: "+gitOpsDeployment.Name) + log.Error(err, "Error occurred in DTAM Reconciler while cleaning gitOpsDeployment entries from DB") } } else { // B) Some other unexpected error occurred, so we just skip it until next time - log.Error(err, "Error occurred in DTAM Reconciler while fetching GitOpsDeployment from cluster: "+gitOpsDeployment.Name) + log.Error(err, "Error occurred in DTAM Reconciler while fetching GitOpsDeployment from cluster") } // C) If the GitOpsDeployment does exist, but the UID doesn't match, then we can delete DTAM and Application - } else if string(gitOpsDeployment.UID) != deplToAppMappingFromDB.Deploymenttoapplicationmapping_uid_id { + } else if string(gitopsDeployment.UID) != deplToAppMappingFromDB.Deploymenttoapplicationmapping_uid_id { // This means that another GitOpsDeployment exists in the namespace with this name. if err := cleanOrphanedEntriesfromTable_DTAM_DeleteEntry(ctx, &deplToAppMappingFromDB, dbQueries, log); err != nil { - log.Error(err, "Error occurred in DTAM Reconciler while cleaning gitOpsDeployment entries from DB: "+gitOpsDeployment.Name) + log.Error(err, "Error occurred in DTAM Reconciler while cleaning gitOpsDeployment entries from DB") } } - log.Info("DTAM Reconcile processed deploymentToApplicationMapping entry: " + deplToAppMappingFromDB.Deploymenttoapplicationmapping_uid_id) + log.V(logutil.LogLevel_Debug).Info("DTAM Reconcile processed deploymentToApplicationMapping entry: " + deplToAppMappingFromDB.Deploymenttoapplicationmapping_uid_id) } // Skip processed entries in next iteration @@ -205,7 +207,7 @@ func cleanOrphanedEntriesfromTable_DTAM_DeleteEntry(ctx context.Context, deplToA log := logger.WithValues("applicationID", deplToAppMapping.Application_id) // 1) Remove the ApplicationState from the database - if err := deleteDbEntry(ctx, dbQueries, deplToAppMapping.Application_id, dbType_ApplicationState, log, deplToAppMapping); err != nil { + if err := deleteDbEntry(ctx, deplToAppMapping.Application_id, dbType_ApplicationState, deplToAppMapping, dbQueries, log); err != nil { return err } @@ -222,7 +224,7 @@ func cleanOrphanedEntriesfromTable_DTAM_DeleteEntry(ctx context.Context, deplToA } // 3) Delete DeplToAppMapping row that points to this Application - if err := deleteDbEntry(ctx, dbQueries, deplToAppMapping.Deploymenttoapplicationmapping_uid_id, dbType_DeploymentToApplicationMapping, log, deplToAppMapping); err != nil { + if err := deleteDbEntry(ctx, deplToAppMapping.Deploymenttoapplicationmapping_uid_id, dbType_DeploymentToApplicationMapping, deplToAppMapping, dbQueries, log); err != nil { return err } @@ -236,7 +238,7 @@ func cleanOrphanedEntriesfromTable_DTAM_DeleteEntry(ctx context.Context, deplToA // 4) Remove the Application from the database log.Info("GitOpsDeployment was deleted, so deleting Application row from database") - if err := deleteDbEntry(ctx, dbQueries, deplToAppMapping.Application_id, dbType_Application, log, deplToAppMapping); err != nil { + if err := deleteDbEntry(ctx, deplToAppMapping.Application_id, dbType_Application, deplToAppMapping, dbQueries, log); err != nil { return err } return nil @@ -251,8 +253,8 @@ func cleanOrphanedEntriesfromTable_DTAM_DeleteEntry(ctx context.Context, deplToA func cleanOrphanedEntriesfromTable_ACTDM(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, k8sClientFactory sharedresourceloop.SRLK8sClientFactory, skipDelay bool, l logr.Logger) { offSet := 0 - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_ACTDM") + log := l.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "DB_ACTDM") // Continuously iterate and fetch batches until all entries of ACTDM table are processed. for { @@ -301,7 +303,7 @@ func cleanOrphanedEntriesfromTable_ACTDM(ctx context.Context, dbQueries db.Datab log.Error(nil, "SEVERE: unrecognized APIResourceType", "resourceType", apiCrToDbMappingFromDB.APIResourceType) } - log.Info("ACTDM Reconcile processed APICRToDatabaseMapping entry: " + apiCrToDbMappingFromDB.APIResourceUID) + log.V(logutil.LogLevel_Debug).Info("ACTDM Reconcile processed APICRToDatabaseMapping entry: " + apiCrToDbMappingFromDB.APIResourceUID) } // Skip processed entries in next iteration @@ -318,9 +320,11 @@ func cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment(ctx context.Context, return } + log = log.WithValues("dbRelationKey", apiCrToDbMappingFromDB.DBRelationKey) + // If CR is not present in cluster clean ACTDM entry - if err := deleteDbEntry(ctx, dbQueries, apiCrToDbMappingFromDB.DBRelationKey, dbType_APICRToDatabaseMapping, log, apiCrToDbMappingFromDB); err != nil { - log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while deleting APICRToDatabaseMapping entry : "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") + if err := deleteDbEntry(ctx, apiCrToDbMappingFromDB.DBRelationKey, dbType_APICRToDatabaseMapping, apiCrToDbMappingFromDB, dbQueries, log); err != nil { + log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while deleting APICRToDatabaseMapping entry from DB") return } @@ -328,21 +332,21 @@ func cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment(ctx context.Context, Managedenvironment_id: apiCrToDbMappingFromDB.DBRelationKey, } if err := dbQueries.GetManagedEnvironmentById(ctx, &managedEnvDb); err != nil { - log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while fetching ManagedEnvironment by Id : "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") + log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while fetching ManagedEnvironment from DB") return } var specialClusterUser db.ClusterUser if err := dbQueries.GetOrCreateSpecialClusterUser(context.Background(), &specialClusterUser); err != nil { - log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while fetching SpecialClusterUser from DB.") + log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while fetching SpecialClusterUser from DB") return } if err := sharedresourceloop.DeleteManagedEnvironmentResources(ctx, apiCrToDbMappingFromDB.DBRelationKey, &managedEnvDb, specialClusterUser, k8sClientFactory, dbQueries, log); err != nil { - log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while cleaning ManagedEnvironment entry "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") + log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_ManagedEnvironment while cleaning ManagedEnvironment entry from DB") return } - log.Info("Managed-gitops clean up job for DB deleted ManagedEnvironment: " + apiCrToDbMappingFromDB.DBRelationKey + " entry from DB.") + log.Info("Managed-gitops clean up job for DB deleted ManagedEnvironment entry from DB.") } func cleanOrphanedEntriesfromTable_ACTDM_RepositoryCredential(ctx context.Context, client client.Client, dbQueries db.DatabaseQueries, apiCrToDbMappingFromDB db.APICRToDatabaseMapping, objectMeta metav1.ObjectMeta, log logr.Logger) { @@ -355,7 +359,7 @@ func cleanOrphanedEntriesfromTable_ACTDM_RepositoryCredential(ctx context.Contex } // If CR is not present in cluster clean ACTDM entry - if err := deleteDbEntry(ctx, dbQueries, apiCrToDbMappingFromDB.DBRelationKey, dbType_APICRToDatabaseMapping, log, apiCrToDbMappingFromDB); err != nil { + if err := deleteDbEntry(ctx, apiCrToDbMappingFromDB.DBRelationKey, dbType_APICRToDatabaseMapping, apiCrToDbMappingFromDB, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_RepositoryCredential while deleting APICRToDatabaseMapping entry : "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") return } @@ -369,7 +373,7 @@ func cleanOrphanedEntriesfromTable_ACTDM_RepositoryCredential(ctx context.Contex } // Clean RepositoryCredential table entry - if err := deleteDbEntry(ctx, dbQueries, apiCrToDbMappingFromDB.DBRelationKey, dbType_RespositoryCredential, log, repoCredentialK8s); err != nil { + if err := deleteDbEntry(ctx, apiCrToDbMappingFromDB.DBRelationKey, dbType_RespositoryCredential, repoCredentialK8s, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_RepositoryCredential while deleting RepositoryCredential entry : "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") return } @@ -388,7 +392,7 @@ func cleanOrphanedEntriesfromTable_ACTDM_GitOpsDeploymentSyncRun(ctx context.Con } // If CR is not present in cluster clean ACTDM entry - if err := deleteDbEntry(ctx, dbQueries, apiCrToDbMappingFromDB.DBRelationKey, dbType_APICRToDatabaseMapping, log, apiCrToDbMappingFromDB); err != nil { + if err := deleteDbEntry(ctx, apiCrToDbMappingFromDB.DBRelationKey, dbType_APICRToDatabaseMapping, apiCrToDbMappingFromDB, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_GitOpsDeploymentSyncRun while deleting APICRToDatabaseMapping entry : "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") return } @@ -402,7 +406,7 @@ func cleanOrphanedEntriesfromTable_ACTDM_GitOpsDeploymentSyncRun(ctx context.Con return } - if err := deleteDbEntry(ctx, dbQueries, apiCrToDbMappingFromDB.DBRelationKey, dbType_SyncOperation, log, syncRunK8s); err != nil { + if err := deleteDbEntry(ctx, apiCrToDbMappingFromDB.DBRelationKey, dbType_SyncOperation, syncRunK8s, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ACTDM_GitOpsDeploymentSyncRun while deleting GitOpsDeploymentSyncRun entry : "+apiCrToDbMappingFromDB.DBRelationKey+" from DB.") return } @@ -410,7 +414,7 @@ func cleanOrphanedEntriesfromTable_ACTDM_GitOpsDeploymentSyncRun(ctx context.Con // There is no need to create an Operation if the Application ID is empty possibly due to its deletion. // We can delete the SyncRun CR and return. if syncOperationDb.Application_id == "" { - log.Info("Application row not found for SyncOperation. This is normally because the Application has already been deleted.", "syncOperationID", syncOperationDb.SyncOperation_id, "applicationID", syncOperationDb.Application_id) + log.Info("Application row field was empty for SyncOperation. This is normally because the Application has already been deleted.", "syncOperationID", syncOperationDb.SyncOperation_id) if err := client.Delete(ctx, &syncRunK8s); err != nil { log.Error(err, "failed to remove GitOpsDeploymentSyncRun when the Application is already deleted") @@ -474,7 +478,7 @@ const ( ) // deleteDbEntry deletes database entry of a given CR -func deleteDbEntry(ctx context.Context, dbQueries db.DatabaseQueries, id string, dbRow dbTableName, logger logr.Logger, t interface{}) error { +func deleteDbEntry(ctx context.Context, id string, dbRow dbTableName, t interface{}, dbQueries db.DatabaseQueries, log logr.Logger) error { var rowsDeleted int var err error @@ -510,14 +514,16 @@ func deleteDbEntry(ctx context.Context, dbQueries db.DatabaseQueries, id string, return fmt.Errorf("SEVERE: unrecognized db row type") } + log = log.WithValues("dbRow", string(dbRow), "dbRowID", id) + if err != nil { - logger.Error(err, "Error occurred in deleteDbEntry while cleaning "+string(dbRow)+" entry "+id+" from DB.") + log.Error(err, "Error occurred in deleteDbEntry while cleaning from DB.") return err } else if rowsDeleted == 0 { // Log the warning, but continue - logger.Info("No rows were found in table, while cleaning up after deleted "+string(dbRow), "rowsDeleted", rowsDeleted) + log.V(logutil.LogLevel_Warn).Info("Row was not found in table, wheen cleaning up after deleted row", "rowsDeleted", rowsDeleted) } else { - logger.Info("Managed-gitops clean up job for DB deleted" + string(dbRow) + " : " + id + " entry from DB.") + log.Info("Clean up job deleted entry from DB.") } return nil } @@ -547,8 +553,8 @@ func cleanOrphanedEntriesfromTable(ctx context.Context, dbQueries db.DatabaseQue // cleanOrphanedEntriesfromTable_RepositoryCredential loops through RepositoryCredentials in database and verifies they are still valid (Having entry in ACTDM). If not, the resources are deleted. func cleanOrphanedEntriesfromTable_RepositoryCredential(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, listOfAppsIdsInDTAM map[string]bool, skipDelay bool, l logr.Logger) { - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_RepositoryCredential") + log := l.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "DB_RepositoryCredential") offSet := 0 @@ -592,7 +598,7 @@ func cleanOrphanedEntriesfromTable_RepositoryCredential(ctx context.Context, dbQ isEngineInstancePresent = false } - if err := deleteDbEntry(ctx, dbQueries, repCred.RepositoryCredentialsID, dbType_RespositoryCredential, log, repCred); err != nil { + if err := deleteDbEntry(ctx, repCred.RepositoryCredentialsID, dbType_RespositoryCredential, repCred, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_RepositoryCredential while deleting RepositoryCredential entry : "+repCred.RepositoryCredentialsID+" from DB.") } @@ -612,8 +618,8 @@ func cleanOrphanedEntriesfromTable_RepositoryCredential(ctx context.Context, dbQ // cleanOrphanedEntriesfromTable_SyncOperation loops through SyncOperations in database and verifies they are still valid (Having entry in ACTDM). If not, the resources are deleted. func cleanOrphanedEntriesfromTable_SyncOperation(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, listOfAppsIdsInDTAM map[string]bool, skipDelay bool, l logr.Logger) { - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_SyncOperation") + log := l.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "DB_SyncOperation") offSet := 0 // Continuously iterate and fetch batches until all entries of RepositoryCredentials table are processed. @@ -659,7 +665,7 @@ func cleanOrphanedEntriesfromTable_SyncOperation(ctx context.Context, dbQueries isApplicationPresent = false } - if err := deleteDbEntry(ctx, dbQueries, syncOperation.SyncOperation_id, dbType_SyncOperation, log, syncOperation); err != nil { + if err := deleteDbEntry(ctx, syncOperation.SyncOperation_id, dbType_SyncOperation, syncOperation, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_SyncOperation while deleting SyncOperation entry : "+syncOperation.SyncOperation_id+" from DB.") } @@ -679,8 +685,8 @@ func cleanOrphanedEntriesfromTable_SyncOperation(ctx context.Context, dbQueries // cleanOrphanedEntriesfromTable_ManagedEnvironment loops through ManagedEnvironments in database and verifies they are still valid (Having entry in ACTDM). If not, the resources are deleted. func cleanOrphanedEntriesfromTable_ManagedEnvironment(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, listOfRowIdsInAPICRToDBMapping map[string]bool, k8sClientFactory sharedresourceloop.SRLK8sClientFactory, skipDelay bool, l logr.Logger) { - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_ManagedEnvironment") + log := l.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "DB_ManagedEnvironment") // Retrieve the list of K8sToDBResourceMappings, so that we don't deleted any ManagedEnvironmentes referenced in them allK8sToDBResourceMappings := getListOfK8sToDBResourceMapping(ctx, dbQueries, skipDelay, l) @@ -757,8 +763,8 @@ func cleanOrphanedEntriesfromTable_ManagedEnvironment(ctx context.Context, dbQue // cleanOrphanedEntriesfromTable_Application loops through Applications in database and verifies they are still valid (Having entry in DTAM). If not, the resources are deleted. func cleanOrphanedEntriesfromTable_Application(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, skipDelay bool, l logr.Logger) { - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_Application") + log := l.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "DB_Application") // Get list of Applications having entry in DTAM table listOfAppsIdsInDTAM := getListOfCRIdsFromTable(ctx, dbQueries, dbType_Application, skipDelay, log) @@ -807,7 +813,7 @@ func cleanOrphanedEntriesfromTable_Application(ctx context.Context, dbQueries db if err := dbQueries.GetApplicationOwnerByApplicationID(ctx, &applicationOwner); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_Application while retrieving applicationOwner entry: "+appDB.Application_id) - } else if err := deleteDbEntry(ctx, dbQueries, applicationOwner.ApplicationOwnerApplicationID, dbType_ApplicationOwner, log, appDB); err != nil { + } else if err := deleteDbEntry(ctx, applicationOwner.ApplicationOwnerApplicationID, dbType_ApplicationOwner, appDB, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_Application while deleting ApplicationOwner entry : "+appDB.Application_id+" from DB.") } @@ -817,11 +823,11 @@ func cleanOrphanedEntriesfromTable_Application(ctx context.Context, dbQueries db if err := dbQueries.GetApplicationStateById(ctx, &applicationState); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_Application while retrieving applicationState from DB: "+applicationState.Applicationstate_application_id) - } else if err := deleteDbEntry(ctx, dbQueries, applicationState.Applicationstate_application_id, dbType_ApplicationState, log, applicationState); err != nil { + } else if err := deleteDbEntry(ctx, applicationState.Applicationstate_application_id, dbType_ApplicationState, applicationState, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_Application while deleting ApplicationState entry : "+applicationState.Applicationstate_application_id+" from DB.") } - if err := deleteDbEntry(ctx, dbQueries, appDB.Application_id, dbType_Application, log, appDB); err != nil { + if err := deleteDbEntry(ctx, appDB.Application_id, dbType_Application, appDB, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_Application while deleting Application entry : "+appDB.Application_id+" from DB.") } @@ -841,8 +847,8 @@ func cleanOrphanedEntriesfromTable_Application(ctx context.Context, dbQueries db // cleanOrphanedEntriesfromTable_Operation loops through Operations in database and verifies they are still valid (Having CR in Cluster). If not, the resources are deleted/Creates. func cleanOrphanedEntriesfromTable_Operation(ctx context.Context, dbQueries db.DatabaseQueries, k8sClient client.Client, skipDelay bool, l logr.Logger) { - log := l.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "DB_Operation") + log := l.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "DB_Operation") offSet := 0 // Continuously iterate and fetch batches until all entries of Operation table are processed. @@ -873,7 +879,7 @@ func cleanOrphanedEntriesfromTable_Operation(ctx context.Context, dbQueries db.D if opDB.State == db.OperationState_Completed && time.Since(opDB.Created_on) > (24*time.Hour) { - if err := deleteDbEntry(ctx, dbQueries, opDB.Operation_id, dbType_Operation, log, opDB); err != nil { + if err := deleteDbEntry(ctx, opDB.Operation_id, dbType_Operation, opDB, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_Operation while deleting Operation entry : "+opDB.Operation_id+" from DB.") } @@ -934,7 +940,7 @@ func cleanOrphanedEntriesfromTable_Operation(ctx context.Context, dbQueries db.D // cleanOrphanedEntriesfromTable_ClusterUser loops through ClusterUsers in database and verifies they are still valid in use. If not, these users are deleted. func cleanOrphanedEntriesfromTable_ClusterUser(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, skipDelay bool, l logr.Logger) { - log := l.WithValues("job", "cleanOrphanedEntriesfromTable_ClusterUser") + log := l.WithValues(sharedutil.Log_JobKey, "cleanOrphanedEntriesfromTable_ClusterUser") // Retrieve a list of user ids from the other tables that reference ClusterUser @@ -979,7 +985,7 @@ func cleanOrphanedEntriesfromTable_ClusterUser(ctx context.Context, dbQueries db time.Since(userDB.Created_on) > waitTimeforRowDelete { // 1) Remove the user from database - if err := deleteDbEntry(ctx, dbQueries, userDB.Clusteruser_id, dbType_ClusterUser, log, nil); err != nil { + if err := deleteDbEntry(ctx, userDB.Clusteruser_id, dbType_ClusterUser, nil, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ClusterUser while deleting ClusterUser entry : "+userDB.Clusteruser_id+" from DB.") } } @@ -992,7 +998,7 @@ func cleanOrphanedEntriesfromTable_ClusterUser(ctx context.Context, dbQueries db // cleanOrphanedEntriesfromTable_ClusterCredential loops through ClusterCredentials in database and verifies they are still in use. If not, the credentials are deleted. func cleanOrphanedEntriesfromTable_ClusterCredential(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, skipDelay bool, l logr.Logger) { - log := l.WithValues("job", "cleanOrphanedEntriesfromTable_ClusterCredential") + log := l.WithValues(sharedutil.Log_JobKey, "cleanOrphanedEntriesfromTable_ClusterCredential") // Retrieve a list of cluster credentials from the other tables that reference ClusterCredential table @@ -1032,7 +1038,7 @@ func cleanOrphanedEntriesfromTable_ClusterCredential(ctx context.Context, dbQuer time.Since(clusterCred.Created_on) > waitTimeforRowDelete { // 1) Remove the ClusterCredentials from the database - if err := deleteDbEntry(ctx, dbQueries, clusterCred.Clustercredentials_cred_id, dbType_ClusterCredentials, log, nil); err != nil { + if err := deleteDbEntry(ctx, clusterCred.Clustercredentials_cred_id, dbType_ClusterCredentials, nil, dbQueries, log); err != nil { log.Error(err, "Error occurred in cleanOrphanedEntriesfromTable_ClusterCredential while deleting ClusterCredentials entry : "+clusterCred.Clustercredentials_cred_id+" from DB.") } } diff --git a/backend/eventloop/repocred_reconciler.go b/backend/eventloop/repocred_reconciler.go index fbbfe2fc21..230f8c324c 100644 --- a/backend/eventloop/repocred_reconciler.go +++ b/backend/eventloop/repocred_reconciler.go @@ -7,7 +7,6 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" @@ -45,15 +44,17 @@ func (r *RepoCredReconciler) startTimerForNextCycle() { ctx := context.Background() log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("component", "repocred-reconciler") + WithValues(logutil.Log_Component, logutil.Log_Component_Backend_RepocredReconciler) - _, _ = sharedutil.CatchPanic(func() error { + if _, err := sharedutil.CatchPanic(func() error { // Reconcile RepositoryCredentials here reconcileRepositoryCredentials(ctx, r.DB, r.Client, log) return nil - }) + }); err != nil { + log.Error(err, "error on repository credentials reconcile") + } // Kick off the timer again, once the old task runs. // This ensures that at least 'repocredReconcilerInterval' time elapses from the end of one run to the beginning of another. @@ -66,10 +67,10 @@ func (r *RepoCredReconciler) startTimerForNextCycle() { // Reconcile logic for API CR To Database Mapping table and utility functions. // This will reconcile repository credential entries from ACTDM table and RepoistoryCredential table // ///////////// -func reconcileRepositoryCredentials(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, l logr.Logger) { +func reconcileRepositoryCredentials(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, logParam logr.Logger) { offSet := 0 - log := l.WithValues("job", "reconcileRepositoryCredentials") + log := logParam.WithValues(sharedutil.Log_JobKey, "reconcileRepositoryCredentials") // Continuously iterate and fetch batches until all entries of ACTDM table are processed. for { @@ -88,7 +89,7 @@ func reconcileRepositoryCredentials(ctx context.Context, dbQueries db.DatabaseQu // Break the loop if no entries are left in table to be processed. if len(listOfApiCrToDbMapping) == 0 { - log.Info("All ACTDM entries are processed by ACTDM Reconciler.") + log.Info("All ACTDM entries are processed by repository credential Reconciler.") break } @@ -100,13 +101,16 @@ func reconcileRepositoryCredentials(ctx context.Context, dbQueries db.DatabaseQu Name: apiCrToDbMappingFromDB.APIResourceName, Namespace: apiCrToDbMappingFromDB.APIResourceNamespace, } + if db.APICRToDatabaseMapping_ResourceType_GitOpsDeploymentRepositoryCredential == apiCrToDbMappingFromDB.APIResourceType { // Process if CR is of GitOpsDeploymentRepositoryCredential type. reconcileRepositoryCredentialStatus(ctx, client, dbQueries, apiCrToDbMappingFromDB, objectMeta, log) + + log.V(logutil.LogLevel_Debug).Info("RepositoryCredential ACTDM Reconcile processed APICRToDatabaseMapping entry: " + apiCrToDbMappingFromDB.APIResourceUID) + } - log.Info("RepositoryCredential ACTDM Reconcile processed APICRToDatabaseMapping entry: " + apiCrToDbMappingFromDB.APIResourceUID) } // Skip processed entries in next iteration @@ -117,26 +121,24 @@ func reconcileRepositoryCredentials(ctx context.Context, dbQueries db.DatabaseQu func reconcileRepositoryCredentialStatus(ctx context.Context, apiNamespaceClient client.Client, dbQueries db.DatabaseQueries, apiCrToDbMappingFromDB db.APICRToDatabaseMapping, objectMeta metav1.ObjectMeta, l logr.Logger) { gitopsDeploymentRepositoryCredentialCR := managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential{ObjectMeta: objectMeta} - log := l.WithValues("job", "reconcileRepositoryCredentialStatus") + + log := l.WithValues(sharedutil.Log_JobKey, "reconcileRepositoryCredentialStatus", "repositoryCRName", gitopsDeploymentRepositoryCredentialCR.GetName()) // Check if required CR is present in cluster. If no, skip - if err := apiNamespaceClient.Get(ctx, types.NamespacedName{Name: gitopsDeploymentRepositoryCredentialCR.GetName(), Namespace: gitopsDeploymentRepositoryCredentialCR.GetNamespace()}, &gitopsDeploymentRepositoryCredentialCR); err != nil { - log.Info(fmt.Sprintf("could not find GitopsDeploymentRepositoryCredential %s in the cluster. Skipping reconciliation.", gitopsDeploymentRepositoryCredentialCR.GetName())) + if err := apiNamespaceClient.Get(ctx, client.ObjectKeyFromObject(&gitopsDeploymentRepositoryCredentialCR), &gitopsDeploymentRepositoryCredentialCR); err != nil { + log.Info("could not find GitopsDeploymentRepositoryCredential in the cluster. Skipping reconciliation.") return } // Sanity test for gitopsDeploymentRepositoryCredentialCR.Spec.Secret to be non-empty value if gitopsDeploymentRepositoryCredentialCR.Spec.Secret == "" { if err := sharedresourceloop.UpdateGitopsDeploymentRepositoryCredentialStatus(ctx, &gitopsDeploymentRepositoryCredentialCR, apiNamespaceClient, nil, log); err != nil { - log.Error(err, fmt.Sprintf("error updating status of GitopsDeploymentRepositoryCredential %v", gitopsDeploymentRepositoryCredentialCR)) + log.Error(err, "error updating status of GitopsDeploymentRepositoryCredential") } return } secret := &corev1.Secret{ - TypeMeta: metav1.TypeMeta{ - Kind: "Secret", - }, ObjectMeta: metav1.ObjectMeta{ Name: gitopsDeploymentRepositoryCredentialCR.Spec.Secret, Namespace: apiCrToDbMappingFromDB.APIResourceNamespace, // we assume the secret is in the same namespace as the CR @@ -145,16 +147,16 @@ func reconcileRepositoryCredentialStatus(ctx context.Context, apiNamespaceClient // Fetch the secret from the cluster if err := apiNamespaceClient.Get(ctx, client.ObjectKey{Name: secret.Name, Namespace: secret.Namespace}, secret); err != nil { - log.Error(err, "Secret not found") + log.Error(err, "Secret not found when reconcling repository credential status", "secretName", secret.Name) if err := sharedresourceloop.UpdateGitopsDeploymentRepositoryCredentialStatus(ctx, &gitopsDeploymentRepositoryCredentialCR, apiNamespaceClient, nil, log); err != nil { - log.Error(err, fmt.Sprintf("error updating status of GitopsDeploymentRepositoryCredential %v", gitopsDeploymentRepositoryCredentialCR)) + log.Error(err, "error updating status of GitopsDeploymentRepositoryCredential") } return } // Update the status of GitopsDeploymentRepositoryCredential if err := sharedresourceloop.UpdateGitopsDeploymentRepositoryCredentialStatus(ctx, &gitopsDeploymentRepositoryCredentialCR, apiNamespaceClient, secret, log); err != nil { - log.Error(err, fmt.Sprintf("error updating status of GitopsDeploymentRepositoryCredential %v", gitopsDeploymentRepositoryCredentialCR)) + log.Error(err, "error updating status of GitopsDeploymentRepositoryCredential") } } diff --git a/backend/eventloop/shared_resource_loop/sharedresourceloop.go b/backend/eventloop/shared_resource_loop/sharedresourceloop.go index f7128c9156..3a5d332f6d 100644 --- a/backend/eventloop/shared_resource_loop/sharedresourceloop.go +++ b/backend/eventloop/shared_resource_loop/sharedresourceloop.go @@ -216,7 +216,7 @@ func (srEventLoop *SharedResourceEventLoop) ReconcileSharedManagedEnv(ctx contex func (srEventLoop *SharedResourceEventLoop) ReconcileRepositoryCredential(ctx context.Context, workspaceClient client.Client, workspaceNamespace corev1.Namespace, - repositoryCredentialCRName string, k8sClientFactory SRLK8sClientFactory) (*db.RepositoryCredentials, error) { + repositoryCredentialCRName string, k8sClientFactory SRLK8sClientFactory, l logr.Logger) (*db.RepositoryCredentials, error) { request := sharedResourceLoopMessage_reconcileRepositoryCredentialRequest{ repositoryCredentialCRName: repositoryCredentialCRName, @@ -226,11 +226,6 @@ func (srEventLoop *SharedResourceEventLoop) ReconcileRepositoryCredential(ctx co responseChannel := make(chan any) - // Create a logger with context - l := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops). - WithValues("namespace", workspaceNamespace.Name) - msg := sharedResourceLoopMessage{ log: l, ctx: ctx, @@ -396,12 +391,9 @@ func internalSharedResourceEventLoop(inputChan chan sharedResourceLoopMessage) { } } -func processSharedResourceMessage(ctx context.Context, msg sharedResourceLoopMessage, dbQueries db.DatabaseQueries, l logr.Logger) { - - log := l.WithValues("workspace", msg.workspaceNamespace.Name) +func processSharedResourceMessage(ctx context.Context, msg sharedResourceLoopMessage, dbQueries db.DatabaseQueries, log logr.Logger) { - log.V(logutil.LogLevel_Debug).Info("sharedResourceEventLoop received message: "+string(msg.messageType), - "workspace", msg.workspaceNamespace.UID) + log.V(logutil.LogLevel_Debug).Info("sharedResourceEventLoop received message: " + string(msg.messageType)) if msg.messageType == sharedResourceLoopMessage_getOrCreateSharedManagedEnv { @@ -640,7 +632,7 @@ func reconcileAppProjectRepositories(ctx context.Context, namespace corev1.Names func deleteRepoCredFromDB(ctx context.Context, repoCredRow db.RepositoryCredentials, repositoryCredentialCRNamespace corev1.Namespace, apiNamespaceClient client.Client, dbQueries db.DatabaseQueries, l logr.Logger) (bool, error) { const retry, noRetry = true, false - l = l.WithValues("RepositoryCredential ID", repoCredRow.RepositoryCredentialsID) + l = l.WithValues("repositoryCredentialID", repoCredRow.RepositoryCredentialsID) if _, err := reconcileAppProjectRepositories(ctx, repositoryCredentialCRNamespace, apiNamespaceClient, dbQueries, l); err != nil { // Log the error and retry diff --git a/backend/eventloop/shared_resource_loop/sharedresourceloop_managedenv.go b/backend/eventloop/shared_resource_loop/sharedresourceloop_managedenv.go index b103ebd244..073651502b 100644 --- a/backend/eventloop/shared_resource_loop/sharedresourceloop_managedenv.go +++ b/backend/eventloop/shared_resource_loop/sharedresourceloop_managedenv.go @@ -42,8 +42,6 @@ func internalProcessMessage_ReconcileSharedManagedEnv(ctx context.Context, works dbQueries db.DatabaseQueries, log logr.Logger) (SharedResourceManagedEnvContainer, error) { - log = log.WithValues("namespace", managedEnvironmentCRNamespace) - container, condition, err := internalProcessMessage_internalReconcileSharedManagedEnv(ctx, workspaceClient, managedEnvironmentCRName, managedEnvironmentCRNamespace, isWorkspaceTarget, workspaceNamespace, k8sClientFactory, dbQueries, log) @@ -67,6 +65,8 @@ func internalProcessMessage_internalReconcileSharedManagedEnv(ctx context.Contex dbQueries db.DatabaseQueries, log logr.Logger) (SharedResourceManagedEnvContainer, connectionInitializedCondition, error) { + log = log.WithValues("managedEnvCRName", managedEnvironmentCRName) + gitopsEngineClient, err := k8sClientFactory.GetK8sClientForGitOpsEngineInstance(ctx, nil) if err != nil { return newSharedResourceManagedEnvContainer(), createUnknownErrorEnvInitCondition(), err @@ -274,7 +274,7 @@ func getManagedEnvironmentCRs(ctx context.Context, // If the managed environment CR doesn't exist, then ensure it is deleted from the database, then exit. if apierr.IsNotFound(err) { - log.Info("Managed environment could not be found, so function was called to clean database entry.") + log.Info("ManagedEnvironment CR could not be found, so starting to delete ManagedEnvironment row") err = deleteManagedEnvironmentDBByAPINameAndNamespace(ctx, workspaceClient, managedEnvironmentCRName, managedEnvironmentCRNamespace, "", workspaceNamespace, k8sClientFactory, dbQueries, clusterUser, log) @@ -639,7 +639,7 @@ func DeleteManagedEnvironmentResources(ctx context.Context, managedEnvID string, for idx := range applications { app := applications[idx] - log := log.WithValues("applicationID", app.Application_id) + log := log.WithValues(logutil.Log_ApplicationID, app.Application_id) gitopsEngineInstance := &db.GitopsEngineInstance{ Gitopsengineinstance_id: app.Engine_instance_inst_id, @@ -1106,7 +1106,7 @@ func convertConditionErrorToConnInitCondition(conditionError gitopserrors.Condit } else { // This shouldn't happen under any circumstances, so sanity test and log as severe controllerLog.FromContext(context.Background()). - WithValues("namespace", managedEnvironmentCR.Namespace). + WithValues(logutil.Log_K8s_Request_Namespace, managedEnvironmentCR.Namespace). Error(nil, "SEVERE: nil error passed to convertConditionErrorToConnInitCondition") } diff --git a/backend/eventloop/shared_resource_loop/sharedresourceloop_repositorycredential.go b/backend/eventloop/shared_resource_loop/sharedresourceloop_repositorycredential.go index 21e5b55d0a..a37b93d257 100644 --- a/backend/eventloop/shared_resource_loop/sharedresourceloop_repositorycredential.go +++ b/backend/eventloop/shared_resource_loop/sharedresourceloop_repositorycredential.go @@ -38,9 +38,7 @@ func internalProcessMessage_ReconcileRepositoryCredential(ctx context.Context, repositoryCredentialCRName string, repositoryCredentialCRNamespace corev1.Namespace, apiNamespaceClient client.Client, - dbQueries db.DatabaseQueries, shouldWait bool, log logr.Logger) (*db.RepositoryCredentials, error) { - - l := log.WithValues("namespace", repositoryCredentialCRNamespace) + dbQueries db.DatabaseQueries, shouldWait bool, l logr.Logger) (*db.RepositoryCredentials, error) { resourceNS := repositoryCredentialCRNamespace.Name @@ -439,10 +437,10 @@ func createRepoCredOperation(ctx context.Context, dbRepoCred db.RepositoryCreden // Updates the given repository credential CR's status condition to match the given condition and additional checks. // If there is an existing status condition with the exact same status, reason and message, no update is made in order // to preserve the LastTransitionTime (see https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Condition.LastTransitionTime ) -func UpdateGitopsDeploymentRepositoryCredentialStatus(ctx context.Context, repositoryCredential *managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential, client client.Client, secret *corev1.Secret, log logr.Logger) error { +func UpdateGitopsDeploymentRepositoryCredentialStatus(ctx context.Context, repositoryCredential *managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential, k8sClient client.Client, secret *corev1.Secret, log logr.Logger) error { // if the condition was sent along with the function call, we don't need to perform additional checks - newConditions := generateValidRepositoryCredentialsConditions(repositoryCredential, ctx, secret) + newConditions := generateValidRepositoryCredentialsConditions(*repositoryCredential, ctx, secret) needToUpdateConditions := false for _, condition := range newConditions { @@ -457,20 +455,20 @@ func UpdateGitopsDeploymentRepositoryCredentialStatus(ctx context.Context, repos if needToUpdateConditions || len(repositoryCredential.Status.Conditions) != 3 { // 1) Attempt to get the latest gitopsDeploymentRepositoryCredentialCR from the namespace - if err := client.Get(ctx, types.NamespacedName{Namespace: repositoryCredential.Namespace, Name: repositoryCredential.Name}, - repositoryCredential); err != nil { + if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(repositoryCredential), repositoryCredential); err != nil { if apierr.IsNotFound(err) { return nil } // Something went wrong, retry vErr := fmt.Errorf("unexpected error in retrieving repository credentials: %v", err) - log.Error(err, vErr.Error(), "DebugErr", errGenericCR, "CR Name", repositoryCredential, "Namespace", repositoryCredential.Namespace) + log.Error(vErr, errGenericCR, "repositoryCredCRName", repositoryCredential) return vErr } + repositoryCredential.Status.SetConditions(newConditions) // Update the GitOpsDeploymentRepositoryCredential CR - if err := client.Status().Update(ctx, repositoryCredential); err != nil { + if err := k8sClient.Status().Update(ctx, repositoryCredential); err != nil { log.Error(err, "updating repository credential CR's status condition") } } @@ -478,7 +476,7 @@ func UpdateGitopsDeploymentRepositoryCredentialStatus(ctx context.Context, repos return nil } -func generateValidRepositoryCredentialsConditions(repositoryCredential *managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential, ctx context.Context, secret *corev1.Secret) []metav1.Condition { +func generateValidRepositoryCredentialsConditions(repositoryCredential managedgitopsv1alpha1.GitOpsDeploymentRepositoryCredential, ctx context.Context, secret *corev1.Secret) []metav1.Condition { var validRepoUrlCondition, validRepoCredCondition metav1.Condition diff --git a/backend/eventloop/shared_resource_loop/sharedresourceloop_test.go b/backend/eventloop/shared_resource_loop/sharedresourceloop_test.go index 4b78eacbb8..2698215aaa 100644 --- a/backend/eventloop/shared_resource_loop/sharedresourceloop_test.go +++ b/backend/eventloop/shared_resource_loop/sharedresourceloop_test.go @@ -1010,7 +1010,8 @@ var _ = Describe("SharedResourceEventLoop Test", func() { Expect(err).ToNot(HaveOccurred()) Expect(dbRepoCred).ToNot(BeNil()) - sharedResourceEventLoopRepoCred, err := sharedResourceEventLoop.ReconcileRepositoryCredential(ctx, k8sClient, repositoryCredentialCRNamespace, cr.Name, MockSRLK8sClientFactory{fakeClient: k8sClient}) + sharedResourceEventLoopRepoCred, err := sharedResourceEventLoop.ReconcileRepositoryCredential(ctx, k8sClient, repositoryCredentialCRNamespace, cr.Name, MockSRLK8sClientFactory{fakeClient: k8sClient}, log.FromContext(context.Background())) + Expect(sharedResourceEventLoopRepoCred.RepositoryCredentialsID).To(Equal(dbRepoCred.RepositoryCredentialsID)) Expect(err).ToNot(HaveOccurred()) // To be used by AfterEach to clean up the resources created by test @@ -1029,7 +1030,7 @@ var _ = Describe("SharedResourceEventLoop Test", func() { go internalSharedResourceEventLoop(sharedResourceEventLoop.inputChannel) - sharedResourceLoop, err := sharedResourceEventLoop.ReconcileRepositoryCredential(ctx, k8sClient, *namespace, "test-name", MockSRLK8sClientFactory{fakeClient: k8sClient}) + sharedResourceLoop, err := sharedResourceEventLoop.ReconcileRepositoryCredential(ctx, k8sClient, *namespace, "test-name", MockSRLK8sClientFactory{fakeClient: k8sClient}, log.FromContext(context.Background())) Expect(err).ToNot(HaveOccurred()) Expect(sharedResourceLoop).To(BeNil()) diff --git a/backend/eventloop/workspace_event_loop.go b/backend/eventloop/workspace_event_loop.go index f392b6a631..a6ad5b4459 100644 --- a/backend/eventloop/workspace_event_loop.go +++ b/backend/eventloop/workspace_event_loop.go @@ -38,24 +38,24 @@ import ( // Start a workspace event loop router go routine, which is responsible for handling API namespace events and // then passing them to the controller loop. -func newWorkspaceEventLoopRouter(workspaceID string) WorkspaceEventLoopRouterStruct { +func newWorkspaceEventLoopRouter(namespaceName string, namespaceID string) WorkspaceEventLoopRouterStruct { res := WorkspaceEventLoopRouterStruct{ channel: make(chan workspaceEventLoopMessage), } - internalStartWorkspaceEventLoopRouter(res.channel, workspaceID, defaultApplicationEventLoopFactory{}) + internalStartWorkspaceEventLoopRouter(res.channel, namespaceName, namespaceID, defaultApplicationEventLoopFactory{}) return res } -func newWorkspaceEventLoopRouterWithFactory(workspaceID string, applEventLoopFactory applicationEventQueueLoopFactory) WorkspaceEventLoopRouterStruct { +func newWorkspaceEventLoopRouterWithFactory(namespaceName string, namespaceID string, applEventLoopFactory applicationEventQueueLoopFactory) WorkspaceEventLoopRouterStruct { res := WorkspaceEventLoopRouterStruct{ channel: make(chan workspaceEventLoopMessage), } - internalStartWorkspaceEventLoopRouter(res.channel, workspaceID, applEventLoopFactory) + internalStartWorkspaceEventLoopRouter(res.channel, namespaceName, namespaceID, applEventLoopFactory) return res } @@ -88,7 +88,7 @@ type workspaceEventLoopMessage struct { // internalStartWorkspaceEventLoopRouter has the primary goal of catching panics from the workspaceEventLoopRouter, and // recovering from them. -func internalStartWorkspaceEventLoopRouter(input chan workspaceEventLoopMessage, workspaceID string, +func internalStartWorkspaceEventLoopRouter(input chan workspaceEventLoopMessage, namespaceName string, namespaceID string, applEventLoopFactory applicationEventQueueLoopFactory) { go func() { @@ -102,7 +102,7 @@ func internalStartWorkspaceEventLoopRouter(input chan workspaceEventLoopMessage, for { isPanic, _ := sharedutil.CatchPanic(func() error { - workspaceEventLoopRouter(input, workspaceID, applEventLoopFactory) + workspaceEventLoopRouter(input, namespaceName, namespaceID, applEventLoopFactory) return nil }) @@ -181,6 +181,9 @@ type workspaceEventLoopInternalState struct { // namespaceID is the UID of the namespace that the workspace event loop is handling namespaceID string + // namespaceName is the name of the namespace that the workspace event loop is handling + namespaceName string + // log is the logger to use when logging inside the workspace event loop log logr.Logger @@ -190,14 +193,14 @@ type workspaceEventLoopInternalState struct { // workspaceEventLoopRouter receives all events for the namespace, and passes them to specific goroutine responsible // for handling events for individual applications. -func workspaceEventLoopRouter(input chan workspaceEventLoopMessage, namespaceID string, +func workspaceEventLoopRouter(input chan workspaceEventLoopMessage, namespaceName string, namespaceID string, applEventLoopFactory applicationEventQueueLoopFactory) { ctx := context.Background() log := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("namespaceID", namespaceID) + WithValues(logutil.Log_K8s_Request_NamespaceID, namespaceID) log.Info("workspaceEventLoopRouter started") defer log.Info("workspaceEventLoopRouter ended.") @@ -209,11 +212,12 @@ func workspaceEventLoopRouter(input chan workspaceEventLoopMessage, namespaceID orphanedResources: map[string]map[string]eventlooptypes.EventLoopEvent{}, applicationMap: map[string]workspaceEventLoop_applicationEventLoopEntry{}, applEventLoopFactory: applEventLoopFactory, - workspaceResourceLoop: newWorkspaceResourceLoop(sharedResourceEventLoop, input), + workspaceResourceLoop: newWorkspaceResourceLoop(sharedResourceEventLoop, input, namespaceName, namespaceID), - log: log, - input: input, - namespaceID: namespaceID, + log: log, + input: input, + namespaceName: namespaceName, + namespaceID: namespaceID, } statusTicker := startStatusCheckTicker(statusCheckInterval, input) @@ -258,7 +262,8 @@ func handleWorkspaceEventLoopMessage(ctx context.Context, event eventlooptypes.E return } - log := state.log.WithValues("namespace", event.Event.Request.Namespace) + log := state.log.WithValues(logutil.Log_K8s_Request_Namespace, event.Event.Request.Namespace, + logutil.Log_K8s_Request_Name, event.Event.Request.Name) // First, sanity check the event if event.MessageType == eventlooptypes.ApplicationEventLoopMessageType_WorkComplete { @@ -369,7 +374,7 @@ func handleWorkspaceEventLoopMessage(ctx context.Context, event eventlooptypes.E // the applictions event loops about the event. func handleManagedEnvProcessedMessage(event eventlooptypes.EventLoopMessage, state workspaceEventLoopInternalState) { - log := state.log.WithValues("namespace", event.Event.Request.Namespace) + log := state.log.WithValues(logutil.Log_K8s_Request_Namespace, event.Event.Request.Namespace) log.V(logutil.LogLevel_Debug).Info(fmt.Sprintf("received ManagedEnvironment event, passed event to %d applications", len(state.applicationMap))) @@ -507,7 +512,7 @@ func checkIfOrphanedGitOpsDeploymentSyncRun(ctx context.Context, event eventloop } if err := event.Event.Client.Get(ctx, client.ObjectKeyFromObject(syncRunCR), syncRunCR); err != nil { if apierr.IsNotFound(err) { - log.V(logutil.LogLevel_Debug).Info("skipping potentially orphaned resource that could no longer be found:", "resource", syncRunCR.ObjectMeta) + log.V(logutil.LogLevel_Debug).Info("skipping potentially orphaned resource that could no longer be found:") // The SyncRun CR doesn't exist, so try fetching the SyncOperation from the database and extract the name of GitOpsDeployment dbQueries, err := db.NewSharedProductionPostgresDBQueries(false) @@ -524,7 +529,7 @@ func checkIfOrphanedGitOpsDeploymentSyncRun(ctx context.Context, event eventloop return syncOperation.DeploymentNameField } else { - log.Error(err, "unexpected client error on retrieving syncrun object", "resource", syncRunCR.ObjectMeta) + log.Error(err, "unexpected client error on retrieving syncrun object") return "" } } @@ -539,7 +544,7 @@ func checkIfOrphanedGitOpsDeploymentSyncRun(ctx context.Context, event eventloop if err := event.Event.Client.Get(ctx, client.ObjectKeyFromObject(gitopsDeplCR), gitopsDeplCR); err != nil { if !apierr.IsNotFound(err) { - log.Error(err, "unexpected client error on retrieving GitOpsDeployment references by GitOpsDeploymentSyncRun", "resource", syncRunCR.ObjectMeta) + log.Error(err, "unexpected client error on retrieving GitOpsDeployment references by GitOpsDeploymentSyncRun", "gitopsDeplResource", gitopsDeplCR.ObjectMeta) return "" } diff --git a/backend/eventloop/workspace_event_loop_test.go b/backend/eventloop/workspace_event_loop_test.go index 2af1ad8373..ad8f69ec77 100644 --- a/backend/eventloop/workspace_event_loop_test.go +++ b/backend/eventloop/workspace_event_loop_test.go @@ -57,7 +57,7 @@ var _ = Describe("Workspace Event Loop Test", Ordered, func() { tAELF = &testApplicationEventLoopFactory{} // Start the workspace event loop with our custom test factory, so that we can capture output - workspaceEventLoopRouter = newWorkspaceEventLoopRouterWithFactory(string(apiNamespace.UID), tAELF) + workspaceEventLoopRouter = newWorkspaceEventLoopRouterWithFactory(apiNamespace.Name, string(apiNamespace.UID), tAELF) k8sClient = fake.NewClientBuilder(). WithScheme(scheme). @@ -445,7 +445,7 @@ var _ = Describe("Workspace Event Loop Test", Ordered, func() { tAELF := &managedEnvironmentTestApplicationEventLoopFactory{ outputChannelMap: map[string]chan application_event_loop.RequestMessage{}, } - workspaceEventLoopRouter := newWorkspaceEventLoopRouterWithFactory(string(apiNamespace.UID), tAELF) + workspaceEventLoopRouter := newWorkspaceEventLoopRouterWithFactory(apiNamespace.Name, string(apiNamespace.UID), tAELF) k8sClient := fake.NewClientBuilder(). WithScheme(scheme). @@ -479,7 +479,7 @@ var _ = Describe("Workspace Event Loop Test", Ordered, func() { tAELF := &managedEnvironmentTestApplicationEventLoopFactory{ outputChannelMap: map[string]chan application_event_loop.RequestMessage{}, } - workspaceEventLoopRouter := newWorkspaceEventLoopRouterWithFactory(string(apiNamespace.UID), tAELF) + workspaceEventLoopRouter := newWorkspaceEventLoopRouterWithFactory(apiNamespace.Name, string(apiNamespace.UID), tAELF) k8sClient := fake.NewClientBuilder(). WithScheme(scheme). @@ -748,8 +748,9 @@ var _ = Describe("Workspace Event Loop Test", Ordered, func() { By("simulating a newly initialized workspace event loop") state := workspaceEventLoopInternalState{ - namespaceID: "namespace-id", - log: log.FromContext(ctx), + namespaceID: "namespace-id", + namespaceName: "namespace-name", + log: log.FromContext(ctx), workspaceResourceLoop: &workspaceResourceEventLoop{ inputChannel: responseChannel, }, @@ -904,6 +905,7 @@ var _ = Describe("Workspace Event Loop Test", Ordered, func() { applEventLoopFactory: tAELF, applicationMap: map[string]workspaceEventLoop_applicationEventLoopEntry{}, namespaceID: "namespace-id", + namespaceName: apiNamespace.Name, } By("calling the function beign tested") diff --git a/backend/eventloop/workspace_resource_event_loop.go b/backend/eventloop/workspace_resource_event_loop.go index 2822dca287..4f9a4f4052 100644 --- a/backend/eventloop/workspace_resource_event_loop.go +++ b/backend/eventloop/workspace_resource_event_loop.go @@ -75,25 +75,28 @@ func (werl *workspaceResourceEventLoop) processManagedEnvironment(ctx context.Co } func newWorkspaceResourceLoop(sharedResourceLoop *shared_resource_loop.SharedResourceEventLoop, - workspaceEventLoopInputChannel chan workspaceEventLoopMessage) *workspaceResourceEventLoop { + workspaceEventLoopInputChannel chan workspaceEventLoopMessage, namespaceName string, + namespaceUID string) *workspaceResourceEventLoop { workspaceResourceEventLoop := &workspaceResourceEventLoop{ inputChannel: make(chan workspaceResourceLoopMessage), } - go internalWorkspaceResourceEventLoop(workspaceResourceEventLoop.inputChannel, sharedResourceLoop, workspaceEventLoopInputChannel) + go internalWorkspaceResourceEventLoop(workspaceResourceEventLoop.inputChannel, sharedResourceLoop, workspaceEventLoopInputChannel, namespaceName, namespaceUID) return workspaceResourceEventLoop } func internalWorkspaceResourceEventLoop(inputChan chan workspaceResourceLoopMessage, sharedResourceLoop *shared_resource_loop.SharedResourceEventLoop, - workspaceEventLoopInputChannel chan workspaceEventLoopMessage) { + workspaceEventLoopInputChannel chan workspaceEventLoopMessage, + namespaceName string, + namespaceUID string) { ctx := context.Background() l := log.FromContext(ctx). WithName(logutil.LogLogger_managed_gitops). - WithValues("component", "workspace_resource_event_loop") + WithValues(logutil.Log_Component, logutil.Log_Component_Backend_WorkspaceResourceEventLoop) dbQueries, err := db.NewSharedProductionPostgresDBQueries(false) if err != nil { @@ -101,7 +104,7 @@ func internalWorkspaceResourceEventLoop(inputChan chan workspaceResourceLoopMess return } - taskRetryLoop := sharedutil.NewTaskRetryLoop("workspace-resource-event-retry-loop") + taskRetryLoop := sharedutil.NewTaskRetryLoop("workspace-resource-event-retry-loop" + namespaceName + "-" + namespaceUID) for { msg := <-inputChan @@ -199,7 +202,7 @@ func internalProcessWorkspaceResourceMessage(ctx context.Context, msg workspaceR return retry, fmt.Errorf("unexpected error in retrieving repo credentials: %v", err) } - log.V(logutil.LogLevel_Warn).Info("Received a message for a repository credential in a namepace that doesn't exist", "namespace", namespace) + log.V(logutil.LogLevel_Warn).Info("Received a message for a repository credential in a namepace that doesn't exist") return noRetry, nil } @@ -208,7 +211,7 @@ func internalProcessWorkspaceResourceMessage(ctx context.Context, msg workspaceR // - If the GitOpsDeploymentRepositoryCredential does exist, but not in the DB, then create a RepositoryCredential DB entry // - If the GitOpsDeploymentRepositoryCredential does exist, and also in the DB, then compare and change a RepositoryCredential DB entry // Then, in all 3 cases, create an Operation to update the cluster-agent - _, err := sharedResourceLoop.ReconcileRepositoryCredential(ctx, msg.apiNamespaceClient, *namespace, req.Name, shared_resource_loop.DefaultK8sClientFactory{}) + _, err := sharedResourceLoop.ReconcileRepositoryCredential(ctx, msg.apiNamespaceClient, *namespace, req.Name, shared_resource_loop.DefaultK8sClientFactory{}, log) if err != nil { return retry, fmt.Errorf("unable to reconcile repository credential. Error: %v", err) @@ -240,7 +243,7 @@ func internalProcessWorkspaceResourceMessage(ctx context.Context, msg workspaceR return retry, fmt.Errorf("unexpected error in retrieving namespace of managed env CR: %v", err) } - log.V(logutil.LogLevel_Warn).Info("Received a message for a managed end CR in a namespace that doesn't exist", "namespace", namespace) + log.V(logutil.LogLevel_Warn).Info("Received a message for a managed env CR in a namespace that doesn't exist") return noRetry, nil } diff --git a/cluster-agent/controllers/argoproj.io/application_controller.go b/cluster-agent/controllers/argoproj.io/application_controller.go index 9c0b6dd110..0fc208ed7a 100644 --- a/cluster-agent/controllers/argoproj.io/application_controller.go +++ b/cluster-agent/controllers/argoproj.io/application_controller.go @@ -64,7 +64,10 @@ type ApplicationReconciler struct { func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues( + logutil.Log_K8s_Request_Namespace, req.Namespace, + logutil.Log_K8s_Request_Name, req.Name, + logutil.Log_Component, logutil.Log_Component_ClusterAgent) defer log.V(logutil.LogLevel_Debug).Info("Application Reconcile() complete.") @@ -76,10 +79,10 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) app := appv1.Application{} if err := r.Get(ctx, req.NamespacedName, &app); err != nil { if apierr.IsNotFound(err) { - log.Info("Application deleted '" + req.NamespacedName.String() + "'") + log.Info("Application deleted") return ctrl.Result{}, nil } else { - log.Error(err, "Unexpected error on retrieving Application '"+req.NamespacedName.String()+"'") + log.Error(err, "Unexpected error on retrieving Application") return ctrl.Result{}, err } } @@ -94,11 +97,13 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) } else { applicationDB.Application_id = databaseID } + + log = log.WithValues(logutil.Log_ApplicationID, applicationDB.Application_id) + if _, _, err := r.Cache.GetApplicationById(ctx, applicationDB.Application_id); err != nil { if db.IsResultNotFoundError(err) { - log.V(logutil.LogLevel_Warn).Info("Application CR '" + req.NamespacedName.String() + - "' missing corresponding database entry: " + applicationDB.Application_id) + log.V(logutil.LogLevel_Warn).Info("Application CR missing corresponding database entry") adt := applicationDeleteTask{ applicationCR: app, @@ -112,12 +117,12 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, nil } else { - log.Error(err, "Unable to retrieve Application from database: "+applicationDB.Application_id) + log.Error(err, "Unable to retrieve Application from database") return ctrl.Result{}, err } } - log = log.WithValues("applicationID", applicationDB.Application_id) + log = log.WithValues(logutil.Log_ApplicationID, applicationDB.Application_id) // 3) Does there exist an ApplicationState for this Application, already? applicationState := &db.ApplicationState{ @@ -174,7 +179,7 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) // Successfully created ApplicationState return ctrl.Result{}, nil } else { - log.Error(errGet, "Unable to retrieve ApplicationState from database: "+applicationDB.Application_id) + log.Error(errGet, "Unable to retrieve ApplicationState from database") return ctrl.Result{}, errGet } } diff --git a/cluster-agent/controllers/argoproj.io/application_info_cache/application_info_cache.go b/cluster-agent/controllers/argoproj.io/application_info_cache/application_info_cache.go index e611c6de13..1fb7f68e18 100644 --- a/cluster-agent/controllers/argoproj.io/application_info_cache/application_info_cache.go +++ b/cluster-agent/controllers/argoproj.io/application_info_cache/application_info_cache.go @@ -201,7 +201,7 @@ func applicationInfoCacheLoop(inputChan chan applicationInfoCacheRequest) { }) log := log.FromContext(context.Background()). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops).WithValues(logutil.Log_Component, logutil.Log_Component_Appstudio_Controller) cacheAppState := map[string]applicationStateCacheEntry{} cacheApp := map[string]applicationCacheEntry{} diff --git a/cluster-agent/controllers/argoproj.io/namespace_reconciler.go b/cluster-agent/controllers/argoproj.io/namespace_reconciler.go index e7d8d5bd60..c74b93b803 100644 --- a/cluster-agent/controllers/argoproj.io/namespace_reconciler.go +++ b/cluster-agent/controllers/argoproj.io/namespace_reconciler.go @@ -39,8 +39,11 @@ const ( // This function iterates through each Workspace/Namespace present in DB and ensures that the state of resources in Cluster is in Sync with DB. func (r *ApplicationReconciler) StartNamespaceReconciler() { ctx := context.Background() + log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops). + WithValues(logutil.Log_Component, logutil.Log_Component_ClusterAgent) + namespaceReconcilerInterval := sharedutil.SelfHealInterval(defaultNamespaceReconcilerInterval, log) if namespaceReconcilerInterval > 0 { r.startTimerForNextCycle(ctx, namespaceReconcilerInterval, log) @@ -81,8 +84,8 @@ func (r *ApplicationReconciler) startTimerForNextCycle(ctx context.Context, name func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQueries, client client.Client, logger logr.Logger) { - log := logger.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "CR_Applications") + log := logger.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "CR_Applications") // Fetch list of ArgoCD applications to be used later // map: applications IDs seen (string) -> (map value not used) @@ -90,7 +93,8 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie argoApplicationList := appv1.ApplicationList{} if err := client.List(ctx, &argoApplicationList); err != nil { - log.Error(err, "Error occurred in Namespace Reconciler while fetching list of ArgoCD applications.") + log.Error(err, "Error occurred in Namespace Reconciler while fetching list of ArgoCD Aapplications") + return } argoApplications := argoApplicationList.Items @@ -103,11 +107,11 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie // Hence created a dummy Cluster User for internal purpose. var specialClusterUser db.ClusterUser if err := dbQueries.GetOrCreateSpecialClusterUser(ctx, &specialClusterUser); err != nil { - log.Error(err, "Error occurred in Namespace Reconciler while fetching clusterUser.") + log.Error(err, "Error occurred in Namespace Reconciler while fetching ClusterUser") return } - log.Info("Triggered Namespace Reconciler to keep Argo application in sync with DB.") + log.Info("Triggered Namespace Reconciler to keep Argo Application in sync with DB") // Continuously iterate and fetch batches until all entries of Application table are processed. for { @@ -135,11 +139,13 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie for _, applicationRowFromDB := range listOfApplicationsFromDB { var applicationFromDB fauxargocd.FauxApplication + log := log.WithValues(logutil.Log_ApplicationID, applicationRowFromDB.Application_id) + processedApplicationIds[applicationRowFromDB.Application_id] = false // Fetch the Application object from DB if err := yaml.Unmarshal([]byte(applicationRowFromDB.Spec_field), &applicationFromDB); err != nil { - log.Error(err, "Error occurred in Namespace Reconciler while unmarshalling application: "+applicationRowFromDB.Application_id) + log.Error(err, "Error occurred in Namespace Reconciler while unmarshalling application") continue // Skip to next iteration instead of stopping the entire loop. } @@ -160,8 +166,7 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie continue } - log.Info("Application " + applicationRowFromDB.Application_id + " not found in ArgoCD, probably user deleted it, " + - "but it still exists in DB, hence recreating application in ArgoCD.") + log.Info("Application not found in ArgoCD, probably user deleted it, but it still exists in DB, hence recreating application in ArgoCD.") // We need to recreate ArgoCD Application, to do that create Operation to inform ArgoCD about it. dbOperationInput := db.Operation{ @@ -181,9 +186,9 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie if err != nil { log.Error(err, "Namespace Reconciler is unable to create operation: "+dbOperationInput.ShortString()) } - log.Info("Operation is created to recreateArgoCD Application " + applicationRowFromDB.Application_id) + log.Info("Operation is created to recreateArgoCD") } else { - log.Error(err, "Error occurred in Namespace Reconciler while fetching application from cluster: "+applicationRowFromDB.Application_id) + log.Error(err, "Error occurred in Namespace Reconciler while fetching application from cluster") } continue } @@ -197,9 +202,9 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie log.Error(err, "unable to compare application contents") continue } else if compare != "" { - log.Info("Argo application is not in Sync with DB, updating Argo CD App. Application:" + applicationRowFromDB.Application_id) + log.Info("Argo application is not in Sync with DB, updating Argo CD App") } else { - log.V(logutil.LogLevel_Debug).Info("Argo application is in Sync with DB, Application:" + applicationRowFromDB.Application_id) + log.V(logutil.LogLevel_Debug).Info("Argo application is in Sync with DB") continue } } @@ -224,13 +229,16 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie log.Error(err, "Unable to fetch GitopsEngineInstance") continue } - if _, _, err := operations.CreateOperation(ctx, false, dbOperationInput, + + if _, dbOp, err := operations.CreateOperation(ctx, false, dbOperationInput, specialClusterUser.Clusteruser_id, engineInstanceDB.Namespace_name, dbQueries, client, log); err != nil { log.Error(err, "Namespace Reconciler is unable to create operation: "+dbOperationInput.ShortString()) continue + } else if dbOp != nil { + log.Info("Operation is created to sync Application row with Argo CD Application", "operationDBID", dbOp.Operation_id) + } - log.Info("Operation " + dbOperationInput.Operation_id + " is created to sync application: " + applicationRowFromDB.Application_id) } // Skip processed entries in next iteration @@ -244,8 +252,7 @@ func syncCRsWithDB_Applications(ctx context.Context, dbQueries db.DatabaseQuerie func syncCRsWithDB_Applications_Delete_Operations(ctx context.Context, dbq db.DatabaseQueries, client client.Client, log logr.Logger) { // Get list of Operations from cluster. listOfK8sOperation := v1alpha1.OperationList{} - err := client.List(ctx, &listOfK8sOperation) - if err != nil { + if err := client.List(ctx, &listOfK8sOperation); err != nil { log.Error(err, "Unable to fetch list of k8s Operation from cluster.") return } @@ -265,22 +272,22 @@ func syncCRsWithDB_Applications_Delete_Operations(ctx context.Context, dbq db.Da continue } + log := log.WithValues("dbOperation", k8sOperation.Spec.OperationID, "k8sOperationUID", string(k8sOperation.UID)) + if dbOperation.State != db.OperationState_Completed && dbOperation.State != db.OperationState_Failed { - log.V(logutil.LogLevel_Debug).Info("K8s Operation is not ready for cleanup : " + string(k8sOperation.UID) + " DbOperation: " + string(k8sOperation.Spec.OperationID)) + log.V(logutil.LogLevel_Debug).Info("K8s Operation is not ready for cleanup") continue } - log.Info("Managed-gitops clean up job for CR deleting Operation created by Namespace Reconciler." + string(k8sOperation.UID)) - // Delete the k8s operation now. if err := operations.CleanupOperation(ctx, dbOperation, k8sOperation, dbq, client, false, log); err != nil { - - log.Error(err, "Unable to Delete k8s Operation"+string(k8sOperation.UID)+" for DbOperation: "+string(k8sOperation.Spec.OperationID)) + log.Error(err, "Unable to delete k8s Operation") } else { - log.Info("Managed-gitops clean up job for CR deleting Operation created by Namespace Reconciler. k8s Operation: " + string(k8sOperation.UID) + " for DbOperation: " + string(k8sOperation.Spec.OperationID)) + log.V(logutil.LogLevel_Debug).Info("Clean up job for CR deleting Operation created by Namespace Reconciler.") } } - log.V(logutil.LogLevel_Debug).Info("Cleaned all Operations created by Namespace Reconciler.") + + log.Info("Cleaned all Operations created by Namespace Reconciler.") } func cleanOrphanedCRsfromCluster_Applications(argoApplications []appv1.Application, processedApplicationIds map[string]any, @@ -302,6 +309,8 @@ func cleanOrphanedCRsfromCluster_Applications(argoApplications []appv1.Applicati var deletedOrphanedApplications []appv1.Application for _, application := range shuffledList { + log := log.WithValues("argoCDApplicationName", application.Name) + // Skip Applications not created by the GitOps Service if value, exists := application.Labels[controllers.ArgoCDApplicationDatabaseIDLabel]; !exists || value == "" { continue @@ -309,10 +318,10 @@ func cleanOrphanedCRsfromCluster_Applications(argoApplications []appv1.Applicati if _, ok := processedApplicationIds[application.Labels["databaseID"]]; !ok { if err := controllers.DeleteArgoCDApplication(ctx, application, client, log); err != nil { - log.Error(err, "unable to delete an orphaned Argo CD Application "+application.Name) + log.Error(err, "unable to delete an orphaned Argo CD Application") } else { deletedOrphanedApplications = append(deletedOrphanedApplications, application) - log.Info("Deleting orphaned Argo CD Application " + application.Name) + log.Info("Deleting orphaned Argo CD Application") } } } @@ -322,8 +331,8 @@ func cleanOrphanedCRsfromCluster_Applications(argoApplications []appv1.Applicati // cleanOrphanedCRsfromCluster_Secret goes through the Argo CD Cluster/Repository Secrets, and deletes secrets that no longer point to valid database entries. func cleanOrphanedCRsfromCluster_Secret(ctx context.Context, dbQueries db.DatabaseQueries, k8sClient client.Client, logger logr.Logger) { - log := logger.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "CR_Secret") + log := logger.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "CR_Secret") kubesystemNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "kube-system"}} @@ -420,26 +429,27 @@ func cleanOrphanedCRsfromCluster_Secret_Delete(ctx context.Context, secret corev return } + log = log.WithValues("secretName", secret.Name, "secretNamespace", secret.Namespace, "secretType", secretType) + if db.IsResultNotFoundError(err) { - // If entry is not present in DB then it is an orphan CR, hence delete the Secret from Cluster. - k8sErr := k8sClient.Delete(ctx, &secret) // k8sErr is declared above To avoid "Implicit memory aliasing in for loop." error. - if k8sErr != nil { - log.Error(k8sErr, "error occurred in Secret Clean-up while deleting an orphan Secret: "+secret.Name, "namespace", secret.Namespace) + // If entry is not present in DB then it is an orphaned CR, hence we delete the Secret from cluster. + if err := k8sClient.Delete(ctx, &secret); err != nil { + log.Error(err, "error occurred in Secret Clean-up while deleting an orphan Secret") } else { - log.Info("Managed-gitops clean up job for CR deleted orphan Secret: "+secret.Name, "namespace", secret.Namespace) + log.Info("Clean up job deleted orphaned Secret CR") } } else { // Some other unexpected error occurred, so we just skip it until next time - log.Error(err, "unexpected error occurred in Secret Clean-up while fetching DB entry pointed by Secret : "+secret.Name, "namespace", secret.Namespace) + log.Error(err, "unexpected error occurred in Secret Clean-up while fetching DB entry pointed by Secret") } } // cleanOrphanedCRsfromCluster_Operation goes through the Operation CRs of cluster, and deletes CRs that are no longer point to valid database entries or already completed. func cleanOrphanedCRsfromCluster_Operation(ctx context.Context, dbQueries db.DatabaseQueries, k8sClient client.Client, logger logr.Logger) { - log := logger.WithValues(sharedutil.JobKey, sharedutil.JobKeyValue). - WithValues(sharedutil.JobTypeKey, "CR_Applications") + log := logger.WithValues(sharedutil.Log_JobKey, sharedutil.Log_JobKeyValue). + WithValues(sharedutil.Log_JobTypeKey, "CR_Applications") // Get list of Operations from cluster. listOfK8sOperation := v1alpha1.OperationList{} diff --git a/cluster-agent/controllers/managed-gitops/eventloop/operation_cr_metrics.go b/cluster-agent/controllers/managed-gitops/eventloop/operation_cr_metrics.go index cdd3600992..01cdbd4cb3 100644 --- a/cluster-agent/controllers/managed-gitops/eventloop/operation_cr_metrics.go +++ b/cluster-agent/controllers/managed-gitops/eventloop/operation_cr_metrics.go @@ -32,7 +32,8 @@ func (r *OperationCRMetricUpdater) StartOperationCRMetricUpdater() { ctx := context.Background() log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops). + WithValues(logutil.Log_Component, logutil.Log_Component_Appstudio_Controller) _, _ = sharedutil.CatchPanic(func() error { updateOperationCRMetrics(ctx, r.Client, log) diff --git a/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop.go b/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop.go index a355b004bd..5ba64c4e80 100644 --- a/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop.go +++ b/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop.go @@ -85,7 +85,8 @@ func operationEventLoopRouter(input chan operationEventLoopEvent) { ctx := context.Background() log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops). + WithValues(logutil.Log_Component, logutil.Log_Component_Appstudio_Controller) taskRetryLoop := sharedutil.NewTaskRetryLoop("cluster-agent") @@ -130,7 +131,7 @@ func operationEventLoopRouter(input chan operationEventLoopEvent) { } if mapKey == "" { - log.Info("mapkey was nil for request, continuing.") + log.V(logutil.LogLevel_Debug).Info("mapkey was nil for request, continuing.") continue } @@ -280,7 +281,7 @@ func (task *processOperationEventTask) PerformTask(taskContext context.Context) return shouldRetryTrue, err } - task.log.Info("Updated Operation state", "operationID", dbOperation.Operation_id, "new operationState", string(dbOperation.State)) + task.log.Info("Updated Operation state", "operationID", dbOperation.Operation_id, "operationState", string(dbOperation.State)) } return shouldRetry, err @@ -291,7 +292,8 @@ func (task *processOperationEventTask) internalPerformTask(taskContext context.C eventClient := task.event.client - log := task.log.WithValues("namespace", task.event.request.Namespace, "name", task.event.request.Name) + log := task.log.WithValues("operationNamespace", task.event.request.Namespace, "operationName", task.event.request.Name) + // 1) Retrieve an up-to-date copy of the Operation CR that we want to process. operationCR := &operation.Operation{ ObjectMeta: metav1.ObjectMeta{ @@ -343,7 +345,7 @@ func (task *processOperationEventTask) internalPerformTask(taskContext context.C return nil, shouldRetryFalse, nil } else { // some other generic error - log.Error(err, "Unable to retrieve gitopsEngineInstance due to generic error: "+dbGitopsEngineInstance.Gitopsengineinstance_id) + log.Error(err, "Unable to retrieve gitopsEngineInstance due to generic error", "gitopsEngineInstanceID", dbGitopsEngineInstance.Gitopsengineinstance_id) return nil, shouldRetryTrue, err } } @@ -351,7 +353,7 @@ func (task *processOperationEventTask) internalPerformTask(taskContext context.C if operationCR.Namespace != dbGitopsEngineInstance.Namespace_name { mismatchedNamespace := "OperationNS: " + operationCR.Namespace + " " + "GitopsEngineInstanceNS: " + dbGitopsEngineInstance.Namespace_name err := fmt.Errorf("OperationCR namespace did not match with existing namespace of GitopsEngineInstance " + mismatchedNamespace) - log.Error(err, "Invalid Operation Detected, Name: "+operationCR.Name+" Namespace: "+operationCR.Namespace) + log.Error(err, "Invalid Operation Detected") return nil, shouldRetryFalse, err } @@ -373,7 +375,7 @@ func (task *processOperationEventTask) internalPerformTask(taskContext context.C } - log.Info("Operation state", "state", dbOperation.State) + log.Info("Processing Operation", "state", dbOperation.State) // Sanity test: find the gitops engine cluster, by kube-system, and ensure that the // gitopsengineinstance matches the gitops engine cluster we are running on. @@ -405,7 +407,7 @@ func (task *processOperationEventTask) internalPerformTask(taskContext context.C // Return retry as true, as it's possible it may exist in the future. return &dbOperation, shouldRetryTrue, fmt.Errorf("argo CD namespace doesn't exist: %s", argoCDNamespace.Name) } else { - log.Error(err, "unexpected error on retrieve Argo CD namespace") + log.Error(err, "Unexpected error on retrieve Argo CD namespace") // some other generic error return &dbOperation, shouldRetryTrue, err } @@ -522,15 +524,18 @@ func processOperation_SyncOperation(ctx context.Context, dbOperation db.Operatio dbSyncOperation := &db.SyncOperation{ SyncOperation_id: dbOperation.Resource_id, } + + log = log.WithValues("syncOperationID", dbSyncOperation.SyncOperation_id) + if err := dbQueries.GetSyncOperationById(ctx, dbSyncOperation); err != nil { if !db.IsResultNotFoundError(err) { // On generic error, we should retry. - log.Error(err, "DB error occurred on retriving SyncOperation: "+dbSyncOperation.SyncOperation_id) + log.Error(err, "DB error occurred on retrieving SyncOperation") return shouldRetryTrue, err } else { // On db row not found, just ignore it and move on. - log.V(logutil.LogLevel_Debug).Info("SyncOperation '" + dbSyncOperation.SyncOperation_id + "' DB entry was no longer available.") + log.V(logutil.LogLevel_Debug).Info("SyncOperation DB entry was no longer available.") return shouldRetryFalse, err } } @@ -541,8 +546,10 @@ func processOperation_SyncOperation(ctx context.Context, dbOperation db.Operatio } if err := dbQueries.GetApplicationById(ctx, &dbApplication); err != nil { + log := log.WithValues(logutil.Log_ApplicationID, dbApplication.Application_id) + if db.IsResultNotFoundError(err) { - log.Error(err, "Unable to retrieve application ID in SyncOperation table: "+dbApplication.Application_id) + log.Error(err, "Unable to retrieve application ID in SyncOperation table") return shouldRetryFalse, err } else { // On generic error, return true so the operation is retried. @@ -628,9 +635,11 @@ func waitForApplicationToRefresh(ctx context.Context, k8sClient client.Client, a // returns shouldRetry, error func terminateExistingOperation(ctx context.Context, dbApplication *db.Application, opConfig operationConfig) (bool, error) { + log := opConfig.log.WithValues("dbApplicationName", dbApplication.Name) + isRunning, err := isOperationRunning(ctx, opConfig.eventClient, dbApplication.Name, opConfig.argoCDNamespace.Name) if err != nil { - opConfig.log.Error(err, "unable to determine if an Operation is running for Application: "+dbApplication.Name) + log.Error(err, "unable to determine if an Operation is running for Application: "+dbApplication.Name) return shouldRetryTrue, err } @@ -642,11 +651,11 @@ func terminateExistingOperation(ctx context.Context, dbApplication *db.Applicati if err := opConfig.syncFuncs.terminateOperation(ctx, dbApplication.Name, opConfig.argoCDNamespace, opConfig.credentialService, opConfig.eventClient, time.Duration(5*time.Minute), opConfig.log); err != nil { - opConfig.log.Error(err, "unable to terminate operation: "+dbApplication.Name) + log.Error(err, "unable to terminate operation") return shouldRetryTrue, err } - opConfig.log.Info("Successfully terminated operation for application '" + dbApplication.Name + "'") + log.Info("Successfully terminated operation for application") return shouldRetryFalse, nil @@ -727,23 +736,26 @@ outer: dbSyncOperation := &db.SyncOperation{ SyncOperation_id: dbOperation.Resource_id, } + + log := log.WithValues("syncOperationID", dbSyncOperation.SyncOperation_id) + if innerErr := opConfig.dbQueries.GetSyncOperationById(ctx, dbSyncOperation); innerErr != nil { // If the DB entry no longer exists, then no more work is to be done if db.IsResultNotFoundError(innerErr) { - log.V(logutil.LogLevel_Debug).Info("SyncOperation '" + dbSyncOperation.SyncOperation_id + "' DB entry was no longer available, during AppSync.") + log.V(logutil.LogLevel_Debug).Info("SyncOperation DB entry was no longer available, during AppSync.") shouldRetry = shouldRetryFalse err = nil break outer } else { - log.Error(innerErr, "Unable to retrieve SyncOperation '"+dbSyncOperation.SyncOperation_id+"', during AppSync.") + log.Error(innerErr, "Unable to retrieve SyncOperation, during AppSync.") } } // 2) If the user cancelled the SyncOperation (by altering the desired state field), then we should stop waiting // for the appsync to complete, here. if dbSyncOperation.DesiredState != db.SyncOperation_DesiredState_Running { - log.V(logutil.LogLevel_Debug).Info("SyncOperation '" + dbSyncOperation.SyncOperation_id + "' no longer had desired running state.") + log.V(logutil.LogLevel_Debug).Info("SyncOperation no longer had desired running state.") shouldRetry = shouldRetryFalse err = nil break outer @@ -882,7 +894,7 @@ func processOperation_Application(ctx context.Context, dbOperation db.Operation, // Before we create the application, make sure that the managed environment exists that the application points to if app.Spec.Destination.Name != argosharedutil.ArgoCDDefaultDestinationInCluster { - if err := ensureManagedEnvironmentExists(ctx, *dbApplication, opConfig); err != nil { + if err := ensureManagedEnvironmentExists(ctx, *dbApplication, opConfig, log); err != nil { log.Error(err, "unable to ensure that managed environment exists") return shouldRetryTrue, err } @@ -943,12 +955,12 @@ func processOperation_Application(ctx context.Context, dbOperation db.Operation, log.Info("Updated Argo CD Application CR", "specDiff", specDiff) } else { - log.Info("no changes detected in application, so no update needed") + log.Info("No changes detected when comparing Application row with Argo CD Application CR via Operation, so no update needed") } // Finally, ensure that the managed-environment secret is still up to date if app.Spec.Destination.Name != argosharedutil.ArgoCDDefaultDestinationInCluster { - if err := ensureManagedEnvironmentExists(ctx, *dbApplication, opConfig); err != nil { + if err := ensureManagedEnvironmentExists(ctx, *dbApplication, opConfig, log); err != nil { log.Error(err, "unable to ensure that managed environment exists") return shouldRetryTrue, err } @@ -1149,7 +1161,7 @@ const ( // ensureManagedEnvironmentExists ensures that the managed environment described by 'application' is defined as an Argo CD // cluster secret, in the Argo CD namespace. -func ensureManagedEnvironmentExists(ctx context.Context, application db.Application, opConfig operationConfig) error { +func ensureManagedEnvironmentExists(ctx context.Context, application db.Application, opConfig operationConfig, log logr.Logger) error { if application.Managed_environment_id == "" { // No work to do @@ -1161,7 +1173,7 @@ func ensureManagedEnvironmentExists(ctx context.Context, application db.Applicat return fmt.Errorf("unable to generate expected cluster secret: %v", err) } - log := opConfig.log.WithValues("expectedSecretName", expectedSecret.Name, "expectedSecretNamespace", expectedSecret.Namespace) + log = log.WithValues("expectedSecretName", expectedSecret.Name, "expectedSecretNamespace", expectedSecret.Namespace) // If we detected that the managed environment row was deleted, ensure the secret is deleted. if shouldDeleteSecret { diff --git a/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop_test.go b/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop_test.go index 5173921b57..329a3a1f39 100644 --- a/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop_test.go +++ b/cluster-agent/controllers/managed-gitops/eventloop/operation_event_loop_test.go @@ -231,7 +231,7 @@ var _ = Describe("Operation Controller", func() { Expect(err).ToNot(HaveOccurred()) By("calling function to test") - err = ensureManagedEnvironmentExists(ctx, *applicationDB, opConfigVal) + err = ensureManagedEnvironmentExists(ctx, *applicationDB, opConfigVal, logger) Expect(err).ToNot(HaveOccurred()) secretName := argosharedutil.GenerateArgoCDClusterSecretName(db.ManagedEnvironment{Managedenvironment_id: applicationDB.Managed_environment_id}) @@ -302,7 +302,7 @@ var _ = Describe("Operation Controller", func() { Expect(err).ToNot(HaveOccurred()) By("calling function to test") - err = ensureManagedEnvironmentExists(ctx, *applicationDB, opConfigVal) + err = ensureManagedEnvironmentExists(ctx, *applicationDB, opConfigVal, logger) Expect(err).ToNot(HaveOccurred()) managedEnvironmentSecret := corev1.Secret{ @@ -349,7 +349,7 @@ var _ = Describe("Operation Controller", func() { err := k8sClient.Create(ctx, &managedEnvironmentSecret) Expect(err).ToNot(HaveOccurred()) - err = ensureManagedEnvironmentExists(ctx, *applicationDB, opConfigVal) + err = ensureManagedEnvironmentExists(ctx, *applicationDB, opConfigVal, logger) Expect(err).ToNot(HaveOccurred()) err = k8sClient.Get(ctx, client.ObjectKeyFromObject(&managedEnvironmentSecret), &managedEnvironmentSecret) diff --git a/cluster-agent/controllers/managed-gitops/eventloop/repocred.go b/cluster-agent/controllers/managed-gitops/eventloop/repocred.go index 70e43cf939..6607ed03bc 100644 --- a/cluster-agent/controllers/managed-gitops/eventloop/repocred.go +++ b/cluster-agent/controllers/managed-gitops/eventloop/repocred.go @@ -64,7 +64,9 @@ func deleteArgoCDSecretLeftovers(ctx context.Context, databaseID string, argoCDN item := list.Items[idx] - l.Info("Deleting Argo CD Secret (leftover) that is missing a DB Entry", "secret", item.Name, "namespace", item.Namespace) + l := l.WithValues("secretName", item.Name, "secretNamespace", item.Namespace) + + l.Info("Deleting Argo CD Secret (leftover) that is missing a DB Entry") // Delete all Argo CD Secret with the corresponding database label (but, there should be only one) // #nosec G601 @@ -72,16 +74,16 @@ func deleteArgoCDSecretLeftovers(ctx context.Context, databaseID string, argoCDN logutil.LogAPIResourceChangeEvent(item.Namespace, item.Name, item, logutil.ResourceDeleted, l) if err != nil { if apierr.IsNotFound(err) { - l.Info("Argo CD Secret (leftover) was already previously deleted", "secret", item.Name, "namespace", item.Namespace) + l.Info("Argo CD Secret (leftover) was already previously deleted") } else { - l.Error(err, errDeletePrivateSecret, "secret", item.Name, "namespace", item.Namespace) + l.Error(err, errDeletePrivateSecret) if firstDeletionErr == nil { firstDeletionErr = err } } } else { - l.Info("Argo CD Secret (leftover) has been successfully deleted", "secret", item.Name, "namespace", item.Namespace) + l.Info("Argo CD Secret (leftover) has been successfully deleted") } } @@ -106,25 +108,27 @@ func processOperation_RepositoryCredentials(ctx context.Context, dbOperation db. return retry, fmt.Errorf("%v: %v", errOperationIDNotFound, crOperation.Name) } - l := opConfig.log. - WithValues("operationRow", dbOperation.Operation_id). - WithName(logutil.LogLogger_managed_gitops) + l := opConfig.log // 2) Retrieve the RepositoryCredentials database row that corresponds to the operation dbRepositoryCredentials, err := opConfig.dbQueries.GetRepositoryCredentialsByID(ctx, dbOperation.Resource_id) if err != nil { + + l := l.WithValues("resource-id", dbOperation.Resource_id) + // If the db row is missing, try to delete the related leftovers (ArgoCD Secret) if db.IsResultNotFoundError(err) { - l.Error(err, errRowNotFound, "resource-id", dbOperation.Resource_id) + l.Error(err, errRowNotFound) return deleteArgoCDSecretLeftovers(ctx, dbOperation.Resource_id, opConfig.argoCDNamespace, opConfig.eventClient, l) } // Something went wrong with the database connection, just retry - l.Error(err, errGenericDB, "resource-id", dbOperation.Resource_id) + l.Error(err, errGenericDB) return retry, err } l = l.WithValues("repositoryCredentialsRow", dbRepositoryCredentials.RepositoryCredentialsID) + l.Info("Retrieved RepositoryCredentials DB row") // 3) Retrieve ArgoCD secret from the cluster. @@ -138,13 +142,15 @@ func processOperation_RepositoryCredentials(ctx context.Context, dbOperation db. l = l.WithValues("secret", argoCDSecret.Name, "namespace", argoCDSecret.Namespace) if err = opConfig.eventClient.Get(ctx, client.ObjectKeyFromObject(argoCDSecret), argoCDSecret); err != nil { + if apierr.IsNotFound(err) { l.Info(errPrivateSecretNotFound) + convertRepoCredToSecret(dbRepositoryCredentials, argoCDSecret) - errCreateArgoCDSecret := opConfig.eventClient.Create(ctx, argoCDSecret, &client.CreateOptions{}) - if errCreateArgoCDSecret != nil { - l.Error(errCreateArgoCDSecret, errPrivateSecretCreate) - return retry, errCreateArgoCDSecret + + if err := opConfig.eventClient.Create(ctx, argoCDSecret, &client.CreateOptions{}); err != nil { + l.Error(err, errPrivateSecretCreate) + return retry, err } logutil.LogAPIResourceChangeEvent(argoCDSecret.Namespace, argoCDSecret.Name, argoCDSecret, logutil.ResourceCreated, l) @@ -158,10 +164,7 @@ func processOperation_RepositoryCredentials(ctx context.Context, dbOperation db. return retry, err } } else { - l.Info("A corresponding Argo CD Private Repository secret has already been existing", - "URL", string(argoCDSecret.Data["url"]), - "username", string(argoCDSecret.Data["username"]), - "SSH Key (length)", len(string(argoCDSecret.Data["ssh"]))) + l.V(logutil.LogLevel_Debug).Info("Not creating Secret as Argo CD Private Repository secret already exists") } // 4. Check if the Argo CD secret has the correct name, and if not, update it with the name from the database. @@ -171,7 +174,7 @@ func processOperation_RepositoryCredentials(ctx context.Context, dbOperation db. isUpdateNeeded := compareClusterResourceWithDatabaseRow(dbRepositoryCredentials, argoCDSecret, l, decodedSecret) if isUpdateNeeded { - l.Info("Syncing with database...") + l.Info("Updating Argo CD Repository Secret") if err = opConfig.eventClient.Update(ctx, argoCDSecret); err != nil { l.Error(err, errUpdatePrivateSecret) return retry, err diff --git a/cluster-agent/controllers/managed-gitops/operation_controller.go b/cluster-agent/controllers/managed-gitops/operation_controller.go index 627783024e..4bb43b1e9a 100644 --- a/cluster-agent/controllers/managed-gitops/operation_controller.go +++ b/cluster-agent/controllers/managed-gitops/operation_controller.go @@ -99,25 +99,30 @@ func (g *garbageCollector) StartGarbageCollector() { func (g *garbageCollector) startGarbageCollectionCycle() { go func() { + ctx := context.Background() + + log := log.FromContext(ctx). + WithName(logutil.LogLogger_managed_gitops). + WithValues(logutil.Log_Component, logutil.Log_Component_Appstudio_Controller) + for { // garbage collect the operations after a specified interval <-time.After(garbageCollectionInterval) - _, _ = sharedutil.CatchPanic(func() error { - ctx := context.Background() - log := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + _, err := sharedutil.CatchPanic(func() error { // get failed/completed operations with non-zero gc interval operations := []db.Operation{} - err := g.db.ListOperationsToBeGarbageCollected(ctx, &operations) - if err != nil { + if err := g.db.ListOperationsToBeGarbageCollected(ctx, &operations); err != nil { log.Error(err, "failed to list operations ready for garbage collection") } g.garbageCollectOperations(ctx, operations, log) return nil }) + + log.Error(err, "error in garbage collector") + } }() } @@ -162,14 +167,18 @@ type removeOperationCRTask struct { } func (r *removeOperationCRTask) PerformTask(ctx context.Context) (bool, error) { + + log := r.log.WithValues("operation_id", r.operation.Spec.OperationID) + if err := r.Delete(ctx, r.operation); err != nil { if errors.IsNotFound(err) { return false, nil } - r.log.Error(err, "failed to delete operation from the cluster", "operation_id", r.operation.Spec.OperationID) + log.Error(err, "failed to delete Operation from the cluster") return true, err } + logutil.LogAPIResourceChangeEvent(r.operation.Namespace, r.operation.Name, r.operation, logutil.ResourceDeleted, r.log) - r.log.Info("successfully garbage collected operation", "operation_id", r.operation.Spec.OperationID) + log.Info("successfully garbage collected Operation") return false, nil } diff --git a/cluster-agent/controllers/util.go b/cluster-agent/controllers/util.go index 640ad163cc..2a5427d059 100644 --- a/cluster-agent/controllers/util.go +++ b/cluster-agent/controllers/util.go @@ -38,7 +38,7 @@ const ( // - If the Application is not deleted after X+2 minutes, return an error func DeleteArgoCDApplication(ctx context.Context, appFromList appv1.Application, eventClient client.Client, log logr.Logger) error { - log = log.WithValues("name", appFromList.Name, "namespace", appFromList.Namespace, "uid", string(appFromList.UID)) + log = log.WithValues("argoCDApplicationName", appFromList.Name, "argoCDApplicationNamespace", appFromList.Namespace, "argoCDApplicationUID", string(appFromList.UID)) log.Info("Attempting to delete Argo CD Application CR") @@ -188,9 +188,9 @@ func DeleteArgoCDApplication(ctx context.Context, appFromList appv1.Application, } if !success { - log.Info("Application was not successfully deleted") + log.Error(nil, "Argo CD Application was not successfully deleted") } else { - log.Info("Application was successfully deleted") + log.Info("Argo CD Application was successfully deleted") } return nil diff --git a/cluster-agent/metrics/argocd/argocd_metrics.go b/cluster-agent/metrics/argocd/argocd_metrics.go index ad968afd17..ecd9089666 100644 --- a/cluster-agent/metrics/argocd/argocd_metrics.go +++ b/cluster-agent/metrics/argocd/argocd_metrics.go @@ -65,7 +65,8 @@ func (m *ReconciliationMetricsUpdater) poll(interval time.Duration) { ctx := context.Background() logger := log.FromContext(ctx). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops). + WithValues(logutil.Log_Component, logutil.Log_Component_ClusterAgent) updater := &reconciliationMetricsUpdater{ client: m.Client, diff --git a/cluster-agent/utils/argocd_login_credentials.go b/cluster-agent/utils/argocd_login_credentials.go index 5f5d9b41a1..492e49bbdf 100644 --- a/cluster-agent/utils/argocd_login_credentials.go +++ b/cluster-agent/utils/argocd_login_credentials.go @@ -127,7 +127,8 @@ type credentialResponse struct { func (cs *CredentialService) credentialHandler(input chan credentialRequest) { log := log.FromContext(context.Background()). - WithName(logutil.LogLogger_managed_gitops) + WithName(logutil.LogLogger_managed_gitops). + WithValues(logutil.Log_Component, logutil.Log_Component_ClusterAgent) credentials := map[string]argoCDCredentials{} diff --git a/cluster-agent/utils/argocd_terminate_app_command.go b/cluster-agent/utils/argocd_terminate_app_command.go index 8516380abe..5489d941f8 100644 --- a/cluster-agent/utils/argocd_terminate_app_command.go +++ b/cluster-agent/utils/argocd_terminate_app_command.go @@ -65,13 +65,16 @@ func terminateOperation(ctx context.Context, appName string, argocdNamespace cor Namespace: argocdNamespace.Name, }, } + + log := log.WithValues("appName", appName, "appNamespace", argocdNamespace.Name) + if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(application), application); err != nil { if apierr.IsNotFound(err) { - log.Info("application '" + appName + "' no longer exists, so exiting terminate operation") + log.Info("application no longer exists, so exiting terminate operation") return nil } - log.Error(err, "unable to retrieve application '"+appName+"' from namespace "+argocdNamespace.Name) + log.Error(err, "unable to retrieve application from namespace") } else { @@ -79,7 +82,7 @@ func terminateOperation(ctx context.Context, appName string, argocdNamespace cor operationPhase := application.Status.OperationState.Phase if operationPhase != "" && operationPhase != common.OperationRunning && operationPhase != common.OperationTerminating { - log.Info("application '" + appName + "' operation is no longer running (or not running).") + log.Info("application operation is no longer running (or not running).") return nil } }