Skip to content

Commit

Permalink
Refactor log statements to better use log context
Browse files Browse the repository at this point in the history
  • Loading branch information
jgwest committed Sep 20, 2023
1 parent ad82ba5 commit af617ad
Show file tree
Hide file tree
Showing 46 changed files with 528 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}

Expand All @@ -56,25 +60,36 @@ 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
}

// 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
}
18 changes: 18 additions & 0 deletions backend-shared/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Loading

0 comments on commit af617ad

Please sign in to comment.