Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

switch to structured logging #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions controllers/barbican_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ import (
type BarbicanReconciler struct {
client.Client
Kclient kubernetes.Interface
Log logr.Logger
Scheme *runtime.Scheme
}

// GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
func (r *BarbicanReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("Barbican")
}

//+kubebuilder:rbac:groups=barbican.openstack.org,resources=barbicans,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=barbican.openstack.org,resources=barbicans/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=barbican.openstack.org,resources=barbicans/finalizers,verbs=update
Expand All @@ -81,7 +85,7 @@ type BarbicanReconciler struct {
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *BarbicanReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = log.FromContext(ctx)
Log := r.GetLogger(ctx)

instance := &barbicanv1beta1.Barbican{}
err := r.Client.Get(ctx, req.NamespacedName, instance)
Expand All @@ -101,7 +105,7 @@ func (r *BarbicanReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -174,7 +178,9 @@ func (r *BarbicanReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r
}

func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barbicanv1beta1.Barbican, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name))
Log := r.GetLogger(ctx)

Log.Info(fmt.Sprintf("Reconciling Service '%s'", instance.Name))

serviceLabels := map[string]string{
common.AppSelector: barbican.ServiceName,
Expand All @@ -197,13 +203,13 @@ func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barb
}

if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op)))
Log.Info(fmt.Sprintf("TransportURL %s successfully reconciled - operation: %s", transportURL.Name, string(op)))
}

instance.Status.TransportURLSecret = transportURL.Status.SecretName

if instance.Status.TransportURLSecret == "" {
r.Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name))
Log.Info(fmt.Sprintf("Waiting for TransportURL %s secret to be created", transportURL.Name))
instance.Status.Conditions.Set(condition.FalseCondition(
barbicanv1beta1.BarbicanRabbitMQTransportURLReadyCondition,
condition.RequestedReason,
Expand All @@ -212,7 +218,7 @@ func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barb
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
}

r.Log.Info(fmt.Sprintf("TransportURL secret name %s", transportURL.Status.SecretName))
Log.Info(fmt.Sprintf("TransportURL secret name %s", transportURL.Status.SecretName))
instance.Status.Conditions.MarkTrue(barbicanv1beta1.BarbicanRabbitMQTransportURLReadyCondition, barbicanv1beta1.BarbicanRabbitMQTransportURLReadyMessage)

//
Expand Down Expand Up @@ -310,7 +316,7 @@ func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barb
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
}

// create or update Barbican Worker deployment
Expand All @@ -325,7 +331,7 @@ func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barb
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
}

// create or update Barbican KeystoneListener deployment
Expand All @@ -340,7 +346,7 @@ func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barb
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
r.Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
Log.Info(fmt.Sprintf("Deployment %s successfully reconciled - operation: %s", instance.Name, string(op)))
}

// TODO(dmendiza): Handle API endpoints
Expand All @@ -351,7 +357,8 @@ func (r *BarbicanReconciler) reconcileNormal(ctx context.Context, instance *barb
}

func (r *BarbicanReconciler) reconcileDelete(ctx context.Context, instance *barbicanv1beta1.Barbican, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))

// remove db finalizer first
db, err := mariadbv1.GetDatabaseByName(ctx, helper, instance.Name)
Expand Down Expand Up @@ -434,7 +441,7 @@ func (r *BarbicanReconciler) reconcileDelete(ctx context.Context, instance *barb

// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
r.Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))

return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -464,7 +471,8 @@ func (r *BarbicanReconciler) generateServiceConfig(
envVars *map[string]env.Setter,
serviceLabels map[string]string,
) error {
r.Log.Info("generateServiceConfigMaps - Barbican controller")
Log := r.GetLogger(ctx)
Log.Info("generateServiceConfigMaps - Barbican controller")

// create Secret required for barbican input
labels := labels.GetLabels(instance, labels.GetGroupLabel(barbican.ServiceName), serviceLabels)
Expand Down Expand Up @@ -536,9 +544,10 @@ func (r *BarbicanReconciler) transportURLCreateOrUpdate(
}

func (r *BarbicanReconciler) apiDeploymentCreateOrUpdate(ctx context.Context, instance *barbicanv1beta1.Barbican, helper *helper.Helper) (*barbicanv1beta1.BarbicanAPI, controllerutil.OperationResult, error) {
Log := r.GetLogger(ctx)

r.Log.Info(fmt.Sprintf("Creating barbican API spec. transporturlsecret: '%s'", instance.Status.TransportURLSecret))
r.Log.Info(fmt.Sprintf("database hostname: '%s'", instance.Status.DatabaseHostname))
Log.Info(fmt.Sprintf("Creating barbican API spec. transporturlsecret: '%s'", instance.Status.TransportURLSecret))
Log.Info(fmt.Sprintf("database hostname: '%s'", instance.Status.DatabaseHostname))
apiSpec := barbicanv1beta1.BarbicanAPISpec{
BarbicanTemplate: instance.Spec.BarbicanTemplate,
BarbicanAPITemplate: instance.Spec.BarbicanAPI,
Expand All @@ -554,7 +563,7 @@ func (r *BarbicanReconciler) apiDeploymentCreateOrUpdate(ctx context.Context, in
}

op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error {
r.Log.Info("Setting deployment spec to be apispec")
Log.Info("Setting deployment spec to be apispec")
deployment.Spec = apiSpec

err := controllerutil.SetControllerReference(instance, deployment, r.Scheme)
Expand All @@ -572,9 +581,10 @@ func (r *BarbicanReconciler) apiDeploymentCreateOrUpdate(ctx context.Context, in
}

func (r *BarbicanReconciler) workerDeploymentCreateOrUpdate(ctx context.Context, instance *barbicanv1beta1.Barbican, helper *helper.Helper) (*barbicanv1beta1.BarbicanWorker, controllerutil.OperationResult, error) {
Log := r.GetLogger(ctx)

r.Log.Info(fmt.Sprintf("Creating barbican Worker spec. transporturlsecret: '%s'", instance.Status.TransportURLSecret))
r.Log.Info(fmt.Sprintf("database hostname: '%s'", instance.Status.DatabaseHostname))
Log.Info(fmt.Sprintf("Creating barbican Worker spec. transporturlsecret: '%s'", instance.Status.TransportURLSecret))
Log.Info(fmt.Sprintf("database hostname: '%s'", instance.Status.DatabaseHostname))
workerSpec := barbicanv1beta1.BarbicanWorkerSpec{
BarbicanTemplate: instance.Spec.BarbicanTemplate,
BarbicanWorkerTemplate: instance.Spec.BarbicanWorker,
Expand All @@ -590,7 +600,7 @@ func (r *BarbicanReconciler) workerDeploymentCreateOrUpdate(ctx context.Context,
}

op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error {
r.Log.Info("Setting deployment spec to be workerspec")
Log.Info("Setting deployment spec to be workerspec")
deployment.Spec = workerSpec

err := controllerutil.SetControllerReference(instance, deployment, r.Scheme)
Expand All @@ -609,8 +619,9 @@ func (r *BarbicanReconciler) workerDeploymentCreateOrUpdate(ctx context.Context,

func (r *BarbicanReconciler) keystoneListenerDeploymentCreateOrUpdate(ctx context.Context, instance *barbicanv1beta1.Barbican, helper *helper.Helper) (*barbicanv1beta1.BarbicanKeystoneListener, controllerutil.OperationResult, error) {

r.Log.Info(fmt.Sprintf("Creating barbican KeystoneListener spec. transporturlsecret: '%s'", instance.Status.TransportURLSecret))
r.Log.Info(fmt.Sprintf("database hostname: '%s'", instance.Status.DatabaseHostname))
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Creating barbican KeystoneListener spec. transporturlsecret: '%s'", instance.Status.TransportURLSecret))
Log.Info(fmt.Sprintf("database hostname: '%s'", instance.Status.DatabaseHostname))
keystoneListenerSpec := barbicanv1beta1.BarbicanKeystoneListenerSpec{
BarbicanTemplate: instance.Spec.BarbicanTemplate,
BarbicanKeystoneListenerTemplate: instance.Spec.BarbicanKeystoneListener,
Expand All @@ -626,7 +637,7 @@ func (r *BarbicanReconciler) keystoneListenerDeploymentCreateOrUpdate(ctx contex
}

op, err := controllerutil.CreateOrUpdate(ctx, r.Client, deployment, func() error {
r.Log.Info("Setting deployment spec to be keystonelistenerspec")
Log.Info("Setting deployment spec to be keystonelistenerspec")
deployment.Spec = keystoneListenerSpec

err := controllerutil.SetControllerReference(instance, deployment, r.Scheme)
Expand All @@ -650,7 +661,8 @@ func (r *BarbicanReconciler) reconcileInit(
serviceLabels map[string]string,
serviceAnnotations map[string]string,
) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))

// Service account, role, binding
rbacRules := []rbacv1.PolicyRule{
Expand Down Expand Up @@ -810,7 +822,7 @@ func (r *BarbicanReconciler) reconcileInit(
}
if dbSyncjob.HasChanged() {
instance.Status.Hash[barbicanv1beta1.DbSyncHash] = dbSyncjob.GetHash()
r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[barbicanv1beta1.DbSyncHash]))
Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[barbicanv1beta1.DbSyncHash]))
}
instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage)

Expand All @@ -819,6 +831,6 @@ func (r *BarbicanReconciler) reconcileInit(

// run Barbican db sync - end

r.Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
return ctrl.Result{}, nil
}
Loading
Loading