Skip to content

Commit

Permalink
NO-ISSUE KubeAPI Controllers Debuggability: Add Logger Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Nir Magnezi committed May 17, 2021
1 parent 32486e6 commit 4af778f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/controller/controllers/agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func (r *AgentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
r.Log.WithError(err).Errorf("Failed to get resource %s", req.NamespacedName)
return ctrl.Result{}, client.IgnoreNotFound(err)
}
r.Log = r.Log.WithFields(
logrus.Fields{
"agent": agent.Name,
"namespace": agent.Namespace,
})

if agent.Spec.ClusterDeploymentName == nil {
r.Log.Debugf("ClusterDeploymentName not set in Agent %s. Skipping Reconcile", agent.Name)
return ctrl.Result{Requeue: false}, nil
Expand Down Expand Up @@ -127,6 +133,14 @@ func (r *AgentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return r.updateStatus(ctx, agent, nil, errors.Wrapf(err, errMsg), !k8serrors.IsNotFound(err))
}

r.Log = r.Log.WithFields(
logrus.Fields{
"agent": agent.Name,
"agent namespace": agent.Namespace,
"clusterDeployment": clusterDeployment.Name,
"clusterDeployment namespace": clusterDeployment.Namespace,
})

// Retrieve cluster by ClusterDeploymentName from the database
cluster, err := r.Installer.GetClusterByKubeKey(kubeKey)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions internal/controller/controllers/agentserviceconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func (r *AgentServiceConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, err
}

r.Log = r.Log.WithValues(
"agentServiceConfig", instance.Name,
"agentServiceConfig namespace", instance.Namespace,
)

// We only support one AgentServiceConfig per cluster, and it must be called "agent". This prevents installing
// AgentService more than once in the cluster.
if instance.Name != agentServiceConfigName {
Expand Down Expand Up @@ -322,6 +327,13 @@ func (r *AgentServiceConfigReconciler) ensureIngressCertCM(ctx context.Context,
return err
}

r.Log = r.Log.WithValues(
"agentServiceConfig", instance.Name,
"agentServiceConfig namespace", instance.Namespace,
"configMap", sourceCM.Name,
"configMap namespace", sourceCM.Namespace,
)

cm, mutateFn := r.newIngressCertCM(instance, sourceCM)

if result, err := controllerutil.CreateOrUpdate(ctx, r.Client, cm, mutateFn); err != nil {
Expand Down Expand Up @@ -419,6 +431,13 @@ func (r *AgentServiceConfigReconciler) ensureAssistedCM(ctx context.Context, ins
return err
}

r.Log = r.Log.WithValues(
"agentServiceConfig", instance.Name,
"agentServiceConfig namespace", instance.Namespace,
"route", route.Name,
"route namespace", route.Namespace,
)

serviceURL := &url.URL{Scheme: "https", Host: route.Spec.Host}
cm, mutateFn := r.newAssistedCM(instance, serviceURL)

Expand Down
31 changes: 31 additions & 0 deletions internal/controller/controllers/bmh_agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (r *BMACReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
}
return reconcileComplete{}.Result()
}
r.Log = r.Log.WithFields(
logrus.Fields{
"bareMetalHost": bmh.Name,
"bareMetalHost namespace": bmh.Namespace,
})

// Let's reconcile the BMH
result := r.reconcileBMH(ctx, bmh)
Expand Down Expand Up @@ -416,6 +421,14 @@ func (r *BMACReconciler) reconcileBMH(ctx context.Context, bmh *bmh_v1alpha1.Bar
return reconcileError{client.IgnoreNotFound(err)}
}

r.Log = r.Log.WithFields(
logrus.Fields{
"bareMetalHost": bmh.Name,
"bareMetalHost namespace": bmh.Namespace,
"infraEnv": infraEnv.Name,
"infraEnv namespace": infraEnv.Namespace,
})

if infraEnv.Status.ISODownloadURL == "" {
// the image has not been created yet, try later.
r.Log.Infof("Image URL for InfraEnv (%s/%s) not available yet. Waiting for new reconcile for BareMetalHost %s/%s",
Expand Down Expand Up @@ -489,6 +502,14 @@ func (r *BMACReconciler) reconcileSpokeBMH(ctx context.Context, bmh *bmh_v1alpha
return reconcileError{err}
}

r.Log = r.Log.WithFields(
logrus.Fields{
"bareMetalHost": bmh.Name,
"bareMetalHost namespace": bmh.Namespace,
"clusterDeployment": clusterDeployment.Name,
"clusterDeployment namespace": clusterDeployment.Namespace,
})

// If the cluster is not installed yet, we can't get kubeconfig for the cluster yet.
if !clusterDeployment.Spec.Installed {
r.Log.Infof("ClusterDeployment %s/%s for agent %s/%s is not installed yet", clusterDeployment.Namespace, clusterDeployment.Name, agent.Namespace, agent.Name)
Expand All @@ -508,6 +529,16 @@ func (r *BMACReconciler) reconcileSpokeBMH(ctx context.Context, bmh *bmh_v1alpha
return reconcileError{err}
}

r.Log = r.Log.WithFields(
logrus.Fields{
"bareMetalHost": bmh.Name,
"bareMetalHost namespace": bmh.Namespace,
"clusterDeployment": clusterDeployment.Name,
"clusterDeployment namespace": clusterDeployment.Namespace,
"secret": secret.Name,
"secret namespace": secret.Namespace,
})

spokeClient, err := r.getSpokeClient(secret)
if err != nil {
r.Log.WithError(err).Errorf("failed to create spoke kubeclient")
Expand Down
12 changes: 12 additions & 0 deletions internal/controller/controllers/clusterdeployments_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (r *ClusterDeploymentsReconciler) Reconcile(ctx context.Context, req ctrl.R
r.Log.WithError(err).Errorf("Failed to get resource %s", req.NamespacedName)
return ctrl.Result{Requeue: true}, nil
}
r.Log = r.Log.WithFields(
logrus.Fields{
"clusterDeployment": clusterDeployment.Name,
"clusterDeployment namespace": clusterDeployment.Namespace,
})

// ignore unsupported platforms
if !isSupportedPlatform(clusterDeployment) {
Expand Down Expand Up @@ -124,6 +129,13 @@ func (r *ClusterDeploymentsReconciler) Reconcile(ctx context.Context, req ctrl.R
r.Log.WithError(err).Errorf("Failed to get AgentClusterInstall %s", aciName)
return ctrl.Result{Requeue: true}, err
}
r.Log = r.Log.WithFields(
logrus.Fields{
"clusterDeployment": clusterDeployment.Name,
"clusterDeployment namespace": clusterDeployment.Namespace,
"clusterInstall": clusterInstall.Name,
"clusterInstall namespace": clusterInstall.Namespace,
})

cluster, err := r.Installer.GetClusterByKubeKey(req.NamespacedName)
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand Down
14 changes: 14 additions & 0 deletions internal/controller/controllers/infraenv_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func (r *InfraEnvReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, client.IgnoreNotFound(err)
}

r.Log = r.Log.WithFields(
logrus.Fields{
"infraEnv": infraEnv.Name,
"infraEnv namespace": infraEnv.Namespace,
})

return r.ensureISO(ctx, infraEnv)
}

Expand Down Expand Up @@ -216,6 +222,14 @@ func (r *InfraEnvReconciler) ensureISO(ctx context.Context, infraEnv *aiv1beta1.
return ctrl.Result{Requeue: Requeue}, nil
}

r.Log = r.Log.WithFields(
logrus.Fields{
"infraEnv": infraEnv.Name,
"infraEnv namespace": infraEnv.Namespace,
"clusterDeployment": clusterDeployment.Name,
"clusterDeployment namespace": clusterDeployment.Namespace,
})

// Retrieve cluster from the database
cluster, err := r.Installer.GetClusterByKubeKey(types.NamespacedName{
Name: clusterDeployment.Name,
Expand Down

0 comments on commit 4af778f

Please sign in to comment.