Skip to content

Commit

Permalink
switch to structured logging
Browse files Browse the repository at this point in the history
fix gitlog func

update logging

update tests logging

update logging

update logging

switch to structured logging

fix gitlog func

update logging

update tests logging

update logging

update logging

switch to structured logging

fix gitlog func

update logging

update tests logging

update logging

update logging

Structured logging
  • Loading branch information
pinikomarov committed Nov 8, 2023
1 parent a07d827 commit e82dc49
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 48 deletions.
57 changes: 37 additions & 20 deletions controllers/glance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ import (
type GlanceReconciler 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 *GlanceReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("Glance")
}

//+kubebuilder:rbac:groups=glance.openstack.org,resources=glances,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=glance.openstack.org,resources=glances/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=glance.openstack.org,resources=glances/finalizers,verbs=update
Expand All @@ -89,7 +93,7 @@ type GlanceReconciler struct {

// Reconcile reconcile Glance requests
func (r *GlanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = log.FromContext(ctx)
Log := r.GetLogger(ctx)

// Fetch the Glance instance
instance := &glancev1.Glance{}
Expand All @@ -110,7 +114,7 @@ func (r *GlanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -203,7 +207,9 @@ func (r *GlanceReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *GlanceReconciler) reconcileDelete(ctx context.Context, instance *glancev1.Glance, 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 @@ -270,7 +276,7 @@ func (r *GlanceReconciler) reconcileDelete(ctx context.Context, instance *glance

// 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 All @@ -282,7 +288,9 @@ func (r *GlanceReconciler) 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))

// Define the PVCs objects required by Glance
ctrlResult, err := r.ensurePVC(ctx, helper, instance, serviceLabels)
Expand Down Expand Up @@ -429,7 +437,7 @@ func (r *GlanceReconciler) reconcileInit(
}
if dbSyncjob.HasChanged() {
instance.Status.Hash[glancev1.DbSyncHash] = dbSyncjob.GetHash()
r.Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[glancev1.DbSyncHash]))
Log.Info(fmt.Sprintf("Service '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[glancev1.DbSyncHash]))
}
instance.Status.Conditions.MarkTrue(condition.DBSyncReadyCondition, condition.DBSyncReadyMessage)

Expand All @@ -438,32 +446,38 @@ func (r *GlanceReconciler) reconcileInit(

// run Glance 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
}

func (r *GlanceReconciler) reconcileUpdate(ctx context.Context, instance *glancev1.Glance, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
Log := r.GetLogger(ctx)

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

// TODO: should have minor update tasks if required
// - delete dbsync hash from status to rerun it?

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

func (r *GlanceReconciler) reconcileUpgrade(ctx context.Context, instance *glancev1.Glance, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
Log := r.GetLogger(ctx)

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

// TODO: should have major version upgrade tasks
// -delete dbsync hash from status to rerun it?

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

func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glancev1.Glance, 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))

// Service account, role, binding
rbacRules := []rbacv1.PolicyRule{
Expand Down Expand Up @@ -610,7 +624,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance
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)))
}

// It is possible that an earlier call to update the status has also set
Expand Down Expand Up @@ -642,7 +656,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance
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)))
}

// Mirror external GlanceAPI status' APIEndpoints and ReadyCount to this parent CR
Expand Down Expand Up @@ -675,7 +689,7 @@ func (r *GlanceReconciler) reconcileNormal(ctx context.Context, instance *glance
instance.Status.Conditions.MarkTrue(condition.CronJobReadyCondition, condition.CronJobReadyMessage)
// create CronJob - end

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

Expand Down Expand Up @@ -754,6 +768,7 @@ func (r *GlanceReconciler) generateServiceConfig(
instance *glancev1.Glance,
envVars *map[string]env.Setter,
) error {
Log := r.GetLogger(ctx)
labels := labels.GetLabels(instance, labels.GetGroupLabel(glance.ServiceName), map[string]string{})

ospSecret, _, err := secret.GetSecret(ctx, h, instance.Spec.Secret, instance.Namespace)
Expand All @@ -778,7 +793,7 @@ func (r *GlanceReconciler) generateServiceConfig(
// error
defer func() {
if err := recover(); err != nil {
r.Log.Error(err.(error), "ImageCacheSize not valid: %s")
Log.Error(err.(error), "ImageCacheSize not valid: %s")
}
}()
cacheSize := resource.MustParse(instance.Spec.ImageCacheSize)
Expand All @@ -805,6 +820,7 @@ func (r *GlanceReconciler) ensureRegisteredLimits(
instance *glancev1.Glance,
quota map[string]int,
) error {
Log := r.GetLogger(ctx)

// get admin
var err error
Expand All @@ -826,7 +842,7 @@ func (r *GlanceReconciler) ensureRegisteredLimits(
ResourceName: lName,
DefaultLimit: lValue,
}
_, err = o.CreateOrUpdateRegisteredLimit(r.Log, m)
_, err = o.CreateOrUpdateRegisteredLimit(Log, m)
if err != nil {
return err
}
Expand Down Expand Up @@ -917,6 +933,7 @@ func (r *GlanceReconciler) registeredLimitsDelete(
instance *glancev1.Glance,
quota map[string]int,
) error {
Log := r.GetLogger(ctx)

// get admin
var err error
Expand All @@ -929,12 +946,12 @@ func (r *GlanceReconciler) registeredLimitsDelete(
if err != nil {
return err
}
fetchRegLimits, err := o.ListRegisteredLimitsByServiceID(r.Log, instance.Status.ServiceID)
fetchRegLimits, err := o.ListRegisteredLimitsByServiceID(Log, instance.Status.ServiceID)
if err != nil {
return err
}
for _, l := range fetchRegLimits {
err = o.DeleteRegisteredLimit(r.Log, l.ID)
err = o.DeleteRegisteredLimit(Log, l.ID)
if err != nil {
return err
}
Expand Down
62 changes: 40 additions & 22 deletions controllers/glanceapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ import (
type GlanceAPIReconciler 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 *GlanceAPIReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("GlanceAPI")
}

//+kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=glance.openstack.org,resources=glanceapis/finalizers,verbs=update
Expand All @@ -82,7 +86,7 @@ type GlanceAPIReconciler struct {

// Reconcile reconcile Glance API requests
func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
_ = log.FromContext(ctx)
Log := r.GetLogger(ctx)

// Fetch the GlanceAPI instance
instance := &glancev1.GlanceAPI{}
Expand All @@ -103,7 +107,7 @@ func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -176,7 +180,9 @@ func (r *GlanceAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

// SetupWithManager sets up the controller with the Manager.
func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *GlanceAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {

Log := r.GetLogger(ctx)

// Watch for changes to any CustomServiceConfigSecrets. Global secrets
svcSecretFn := func(o client.Object) []reconcile.Request {
Expand All @@ -189,8 +195,8 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
listOpts := []client.ListOption{
client.InNamespace(namespace),
}
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve API CRs %v")
if err := r.Client.List(ctx, apis, listOpts...); err != nil {
Log.Error(err, "Unable to retrieve API CRs %v")
return nil
}
for _, cr := range apis.Items {
Expand All @@ -200,7 +206,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
Namespace: namespace,
Name: cr.Name,
}
r.Log.Info(fmt.Sprintf("Secret %s is used by Glance CR %s", secretName, cr.Name))
Log.Info(fmt.Sprintf("Secret %s is used by Glance CR %s", secretName, cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
Expand All @@ -220,8 +226,8 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), glanceAPIs, listOpts...); err != nil {
r.Log.Error(err, "Unable to retrieve GlanceAPI CRs %w")
if err := r.Client.List(ctx, glanceAPIs, listOpts...); err != nil {
Log.Error(err, "Unable to retrieve GlanceAPI CRs %w")
return nil
}
for _, cr := range glanceAPIs.Items {
Expand All @@ -230,7 +236,7 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
Namespace: cr.GetNamespace(),
Name: cr.GetName(),
}
r.Log.Info(fmt.Sprintf("NAD %s is used by GlanceAPI CR %s", o.GetName(), cr.GetName()))
Log.Info(fmt.Sprintf("NAD %s is used by GlanceAPI CR %s", o.GetName(), cr.GetName()))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
Expand All @@ -254,7 +260,9 @@ func (r *GlanceAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *GlanceAPIReconciler) reconcileDelete(ctx context.Context, instance *glancev1.GlanceAPI, 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 the finalizer from our KeystoneEndpoint CR
keystoneEndpoint, err := keystonev1.GetKeystoneEndpointWithName(ctx, helper, instance.Name, instance.Namespace)
Expand All @@ -274,7 +282,7 @@ func (r *GlanceAPIReconciler) reconcileDelete(ctx context.Context, instance *gla

// Endpoints are 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 All @@ -285,7 +293,9 @@ func (r *GlanceAPIReconciler) reconcileInit(
helper *helper.Helper,
serviceLabels 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))

//
// create service/s
Expand Down Expand Up @@ -436,32 +446,38 @@ func (r *GlanceAPIReconciler) reconcileInit(
// create keystone endpoints - 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
}

func (r *GlanceAPIReconciler) reconcileUpdate(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
Log := r.GetLogger(ctx)

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

// TODO: should have minor update tasks if required
// - delete dbsync hash from status to rerun it?

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

func (r *GlanceAPIReconciler) reconcileUpgrade(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper) (ctrl.Result, error) {
r.Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
Log := r.GetLogger(ctx)

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

// TODO: should have major version upgrade tasks
// -delete dbsync hash from status to rerun it?

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

func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *glancev1.GlanceAPI, helper *helper.Helper, req ctrl.Request) (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))

configVars := make(map[string]env.Setter)
privileged := false
Expand Down Expand Up @@ -543,7 +559,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *gla
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, can't run GlanceAPI with this config
r.Log.Info("Cinder resource not found. Waiting for it to be deployed")
Log.Info("Cinder resource not found. Waiting for it to be deployed")
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
}
}
Expand Down Expand Up @@ -672,7 +688,7 @@ func (r *GlanceAPIReconciler) reconcileNormal(ctx context.Context, instance *gla
}
// create Deployment - end

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

Expand Down Expand Up @@ -775,6 +791,8 @@ func (r *GlanceAPIReconciler) createHashOfInputHashes(
instance *glancev1.GlanceAPI,
envVars map[string]env.Setter,
) (string, bool, error) {
Log := r.GetLogger(ctx)

var hashMap map[string]string
changed := false
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
Expand All @@ -784,7 +802,7 @@ func (r *GlanceAPIReconciler) createHashOfInputHashes(
}
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
instance.Status.Hash = hashMap
r.Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
}
return hash, changed, nil
}
Loading

0 comments on commit e82dc49

Please sign in to comment.