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

refact: modify logs, add employer's info & set log level #28

Merged
merged 1 commit into from
Jan 17, 2024
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
6 changes: 2 additions & 4 deletions pkg/frame/controller/consister.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ func (r *Consist) diffEmployer(expectEmployer, currentEmployer []IEmployer) (ToC
}
}

// todo, log level
r.logger.Info("employer info",
r.logger.V(5).Info("employer info",
"toCreate", toCreate[:toCreateIdx],
"toUpdate", toUpdate[:toUpdateIdx],
"toDelete", toDelete[:toDeleteIdx],
Expand Down Expand Up @@ -164,8 +163,7 @@ func (r *Consist) diffEmployees(expectEmployees, currentEmployees []IEmployee) (
}
}

// todo, log level
r.logger.Info("employee info",
r.logger.V(5).Info("employee info",
"toCreate", toCreate[:toCreateIdx],
"toUpdate", toUpdate[:toUpdateIdx],
"toDelete", toDelete[:toDeleteIdx],
Expand Down
57 changes: 34 additions & 23 deletions pkg/frame/controller/resourceconsist_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewReconcile(mgr manager.Manager, reconcileAdapter ReconcileAdapter) *Consi
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
adapter: reconcileAdapter,
logger: logf.Log.WithName(reconcileAdapter.GetControllerName()),
logger: logf.Log.WithName(reconcileAdapter.GetControllerName()).V(4),
recorder: recorder,
}
}
Expand Down Expand Up @@ -133,81 +133,92 @@ func (r *Consist) Reconcile(ctx context.Context, request reconcile.Request) (rec
if errors.IsNotFound(err) {
return reconcile.Result{}, nil
}
r.logger.Error(err, "get employer failed")
r.logger.Error(err, fmt.Sprintf("get %s: %s/%s failed", employer.GetObjectKind().GroupVersionKind().Kind,
request.Namespace, request.Name))
return reconcile.Result{}, err
}

// ensure employer-clean finalizer firstly, employer-clean finalizer should be cleaned at the end
updated, err := r.ensureEmployerCleanFlz(ctx, employer)
if err != nil {
r.logger.Error(err, "add employer clean finalizer failed")
r.logger.Error(err, fmt.Sprintf("add employer-clean finalizer for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "ensureEmployerCleanFlzFailed",
"add employer clean finalizer failed: %s", err.Error())
"add employer-clean finalizer failed: %s", err.Error())
return reconcile.Result{}, err
}
if updated {
r.logger.Info("add employer clean finalizer")
r.logger.Info(fmt.Sprintf("add employer-clean finalizer for %s: %s/%s",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeNormal, "ensureEmployerCleanFlzSucceed",
"add employer clean finalizer")
"add employer-clean finalizer")
return reconcile.Result{}, nil
}

isExpectedClean, err := r.ensureExpectedFinalizer(ctx, employer)
if err != nil {
r.logger.Error(err, "ensure employees expected finalizer failed")
r.logger.Error(err, fmt.Sprintf("ensure employees' expected finalizer failed for %s: %s/%s",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "ensureExpectedFinalizerFailed",
"ensure employees expected finalizer failed: %s", err.Error())
"ensure employees' expected finalizer failed: %s", err.Error())
return reconcile.Result{}, err
}

expectEmployer, err := r.adapter.GetExpectedEmployer(ctx, employer)
expectedEmployer, err := r.adapter.GetExpectedEmployer(ctx, employer)
if err != nil {
r.logger.Error(err, "get expect employer failed")
r.logger.Error(err, fmt.Sprintf("get expected employer for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "GetExpectEmployerFailed",
"get expect employer status failed: %s", err.Error())
"get expect employer failed: %s", err.Error())
return reconcile.Result{}, err
}
currentEmployer, err := r.adapter.GetCurrentEmployer(ctx, employer)
if err != nil {
r.logger.Error(err, "get current employer failed")
r.logger.Error(err, fmt.Sprintf("get current employer for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "GetCurrentEmployerFailed",
"get current employer status failed: %s", err.Error())
"get current employer failed: %s", err.Error())
return reconcile.Result{}, err
}
isCleanEmployer, syncEmployerFailedExist, err := r.syncEmployer(ctx, employer, expectEmployer, currentEmployer)
isCleanEmployer, syncEmployerFailedExist, err := r.syncEmployer(ctx, employer, expectedEmployer, currentEmployer)
if err != nil {
r.logger.Error(err, "sync employer status failed")
r.logger.Error(err, fmt.Sprintf("sync employer for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "syncEmployerFailed",
"sync employer status failed: %s", err.Error())
"sync employer failed: %s", err.Error())
return reconcile.Result{}, err
}

expectEmployees, err := r.adapter.GetExpectedEmployee(ctx, employer)
if err != nil {
r.logger.Error(err, "get expect employees failed")
r.logger.Error(err, fmt.Sprintf("get expect employees for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "GetExpectEmployeeFailed",
"get expect employees status failed: %s", err.Error())
"get expect employees failed: %s", err.Error())
return reconcile.Result{}, err
}
currentEmployees, err := r.adapter.GetCurrentEmployee(ctx, employer)
if err != nil {
r.logger.Error(err, "get current employees failed")
r.logger.Error(err, fmt.Sprintf("get current employees for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "GetCurrentEmployeeFailed",
"get current employees status failed: %s", err.Error())
"get current employees failed: %s", err.Error())
return reconcile.Result{}, err
}
isCleanEmployee, syncEmployeeFailedExist, err := r.syncEmployees(ctx, employer, expectEmployees, currentEmployees)
if err != nil {
r.logger.Error(err, "sync employees status failed")
r.logger.Error(err, fmt.Sprintf("sync employees for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "syncEmployeesFailed",
"sync employees status failed: %s", err.Error())
"sync employees failed: %s", err.Error())
return reconcile.Result{}, err
}

if isCleanEmployer && isCleanEmployee && isExpectedClean && !employer.GetDeletionTimestamp().IsZero() {
err = r.cleanEmployerCleanFinalizer(ctx, employer)
if err != nil {
r.logger.Error(err, "clean employer clean-finalizer failed")
r.logger.Error(err, fmt.Sprintf("clean employer clean-finalizer for %s: %s/%s failed",
employer.GetObjectKind().GroupVersionKind().Kind, employer.GetNamespace(), employer.GetName()))
r.recorder.Eventf(employer, corev1.EventTypeWarning, "cleanEmployerCleanFinalizerFailed",
"clean employer clean-finalizer failed: %s", err.Error())
return reconcile.Result{}, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/frame/controller/resourceconsit_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ var _ = Describe("resource-consist-controller", func() {
}
for _, evt := range events.Items {
if evt.Reason == "syncEmployerFailed" &&
evt.Message == "sync employer status failed: syncCreate failed, err: fake err" {
evt.Message == "sync employer failed: syncCreate failed, err: fake err" {
return true
}
}
Expand Down Expand Up @@ -529,7 +529,7 @@ var _ = Describe("resource-consist-controller", func() {
}
for _, evt := range events.Items {
if evt.Reason == "syncEmployeesFailed" &&
evt.Message == "sync employees status failed: syncCreate failed, err: fake err" {
evt.Message == "sync employees failed: syncCreate failed, err: fake err" {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/frame/webhook/resourceconsist_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// AddToMgr is only necessary for controllers following PodOpsLifecycle
func AddToMgr(mgr manager.Manager, adapter WebhookAdapter) error {
server := mgr.GetWebhookServer()
logger := mgr.GetLogger().WithName("webhook")
logger := mgr.GetLogger().WithName("webhook").V(3)

if len(adapter.Name()) == 0 {
logger.Info("Skip registering handlers without a name")
Expand All @@ -49,7 +49,7 @@ func AddToMgr(mgr manager.Manager, adapter WebhookAdapter) error {
}
decoder, _ := admission.NewDecoder(mgr.GetScheme())
server.Register(path, &webhook.Admission{Handler: NewPodResourceConsistWebhook(mgr.GetClient(), decoder, adapter)})
logger.V(3).Info("Registered webhook handler", "path", path)
logger.Info("Registered webhook handler", "path", path)

return nil
}
Expand Down
Loading