Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #227 from pinikomarov/structured_logging
Browse files Browse the repository at this point in the history
switch to structured logging
  • Loading branch information
openshift-merge-bot[bot] authored Nov 16, 2023
2 parents d3cafdc + f6ab615 commit 49554f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
33 changes: 21 additions & 12 deletions controllers/openstack_ansibleee_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/openstack-k8s-operators/lib-common/modules/storage"
redhatcomv1alpha1 "github.com/openstack-k8s-operators/openstack-ansibleee-operator/api/v1alpha1"
Expand All @@ -56,10 +57,14 @@ const (
type OpenStackAnsibleEEReconciler 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 *OpenStackAnsibleEEReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("OpenStackAnsibleEE")
}

// +kubebuilder:rbac:groups=ansibleee.openstack.org,resources=openstackansibleees,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=ansibleee.openstack.org,resources=openstackansibleees/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=ansibleee.openstack.org,resources=openstackansibleees/finalizers,verbs=update
Expand All @@ -78,6 +83,7 @@ type OpenStackAnsibleEEReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.12.2/pkg/reconcile
func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
Log := r.GetLogger(ctx)

instance, err := r.getOpenStackAnsibleeeInstance(ctx, req)
if err != nil || instance.Name == "" {
Expand All @@ -89,12 +95,12 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
r.Client,
r.Kclient,
r.Scheme,
r.Log,
Log,
)

if err != nil {
// helper might be nil, so can't use util.LogErrorForObject since it requires helper as first arg
r.Log.Error(err, fmt.Sprintf("Unable to acquire helper for OpenStackAnsibleEE %s", instance.Name))
Log.Error(err, fmt.Sprintf("Unable to acquire helper for OpenStackAnsibleEE %s", instance.Name))
return ctrl.Result{}, err
}

Expand All @@ -114,7 +120,7 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R

err := helper.PatchInstance(ctx, instance)
if err != nil {
r.Log.Error(_err, "PatchInstance error")
Log.Error(_err, "PatchInstance error")
_err = err
return
}
Expand Down Expand Up @@ -173,15 +179,15 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
currentJobHash := instance.Status.Hash[ansibleeeJobType]

// Define a new job
jobDef, err := r.jobForOpenStackAnsibleEE(instance, helper, serviceAnnotations)
jobDef, err := r.jobForOpenStackAnsibleEE(ctx, instance, helper, serviceAnnotations)
if err != nil {
return ctrl.Result{}, err
}

configMap := &corev1.ConfigMap{}
err = r.Get(ctx, types.NamespacedName{Name: instance.Spec.EnvConfigMapName, Namespace: instance.Namespace}, configMap)
if err != nil && !errors.IsNotFound(err) {
r.Log.Error(err, err.Error())
Log.Error(err, err.Error())
return ctrl.Result{}, err
} else if err == nil {
addEnvFrom(instance, jobDef)
Expand Down Expand Up @@ -223,17 +229,19 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R

if ansibleeeJob.HasChanged() {
instance.Status.Hash[ansibleeeJobType] = ansibleeeJob.GetHash()
r.Log.Info(fmt.Sprintf("AnsibleEE CR '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[ansibleeeJobType]))
Log.Info(fmt.Sprintf("AnsibleEE CR '%s' - Job %s hash added - %s", instance.Name, jobDef.Name, instance.Status.Hash[ansibleeeJobType]))
}

instance.Status.Conditions.MarkTrue(redhatcomv1alpha1.AnsibleExecutionJobReadyCondition, redhatcomv1alpha1.AnsibleExecutionJobReadyMessage)
instance.Status.JobStatus = redhatcomv1alpha1.JobStatusSucceeded

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

func (r *OpenStackAnsibleEEReconciler) getOpenStackAnsibleeeInstance(ctx context.Context, req ctrl.Request) (*redhatcomv1alpha1.OpenStackAnsibleEE, error) {
Log := r.GetLogger(ctx)

// Fetch the OpenStackAnsibleEE instance
instance := &redhatcomv1alpha1.OpenStackAnsibleEE{}

Expand All @@ -243,22 +251,23 @@ func (r *OpenStackAnsibleEEReconciler) getOpenStackAnsibleeeInstance(ctx context
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
r.Log.Info("OpenStackAnsibleEE resource not found. Ignoring since object must be deleted")
Log.Info("OpenStackAnsibleEE resource not found. Ignoring since object must be deleted")
return &redhatcomv1alpha1.OpenStackAnsibleEE{}, nil
}
// Error reading the object - requeue the request.
r.Log.Error(err, err.Error())
Log.Error(err, err.Error())
return instance, err
}

return instance, nil
}

// jobForOpenStackAnsibleEE returns a openstackansibleee Job object
func (r *OpenStackAnsibleEEReconciler) jobForOpenStackAnsibleEE(
func (r *OpenStackAnsibleEEReconciler) jobForOpenStackAnsibleEE(ctx context.Context,
instance *redhatcomv1alpha1.OpenStackAnsibleEE,
h *helper.Helper,
annotations map[string]string) (*batchv1.Job, error) {
Log := r.GetLogger(ctx)
labels := instance.GetObjectMeta().GetLabels()
var deployIdentifier string

Expand Down Expand Up @@ -300,7 +309,7 @@ func (r *OpenStackAnsibleEEReconciler) jobForOpenStackAnsibleEE(
// Override args list if we are in a debug mode
if instance.Spec.Debug {
args = []string{"sleep", "1d"}
r.Log.Info(fmt.Sprintf("Instance %s will be running in debug mode.", instance.Name))
Log.Info(fmt.Sprintf("Instance %s will be running in debug mode.", instance.Name))
}

podSpec := corev1.PodSpec{
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
setupLog = ctrl.Log.WithName("Setup").WithName("Controllers").WithName("OpenStackAnsibleEE")
)

func init() {
Expand All @@ -70,7 +70,7 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
devMode, err := strconv.ParseBool(os.Getenv("DEV_MODE"))
if err != nil {
devMode = false
devMode = true
}
opts := zap.Options{
Development: devMode,
Expand Down Expand Up @@ -115,7 +115,6 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("OpenStackAnsibleEE"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackAnsibleEE")
os.Exit(1)
Expand Down
1 change: 0 additions & 1 deletion tests/functional/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("OpenStackAnsibleEE"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down

0 comments on commit 49554f9

Please sign in to comment.