Skip to content

Commit

Permalink
optimization of log consist (#689)
Browse files Browse the repository at this point in the history
optimization of log consist

Signed-off-by: soulseen <zhuxiaoyang1996@gmail.com>
  • Loading branch information
soulseen committed Jul 14, 2021
1 parent 599e7a1 commit a0db41f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
14 changes: 6 additions & 8 deletions controllers/goharbor/configuration/reconciler.go
Expand Up @@ -6,10 +6,9 @@ import (
"fmt"
"time"

"github.com/go-logr/logr"
goharborv1 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1beta1"
"github.com/goharbor/harbor-operator/controllers"
commonCtrl "github.com/goharbor/harbor-operator/pkg/controller"
"github.com/goharbor/harbor-operator/pkg/factories/application"
"github.com/goharbor/harbor-operator/pkg/harbor"
"github.com/goharbor/harbor-operator/pkg/utils/strings"
"github.com/ovh/configstore"
Expand All @@ -20,7 +19,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/yaml"
Expand All @@ -31,9 +29,10 @@ var pwdFields = []string{"email_password", "ldap_search_password", "uaa_client_s

// New HarborConfiguration reconciler.
func New(ctx context.Context, configStore *configstore.Store) (commonCtrl.Reconciler, error) {
return &Reconciler{
Log: ctrl.Log.WithName(application.GetName(ctx)).WithName("configuration-controller").WithValues("controller", "HarborConfiguration"),
}, nil
r := &Reconciler{}
r.Controller = commonCtrl.NewController(ctx, controllers.HarborCluster, nil, configStore)

return r, nil
}

const (
Expand All @@ -47,8 +46,7 @@ const (

// Reconciler reconciles a configuration configmap.
type Reconciler struct {
client.Client
Log logr.Logger
*commonCtrl.Controller
Scheme *runtime.Scheme
}

Expand Down
34 changes: 10 additions & 24 deletions controllers/goharbor/harborcluster/ctrl_setup.go
Expand Up @@ -3,7 +3,6 @@ package harborcluster
import (
"context"

"github.com/go-logr/logr"
goharborv1 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1beta1"
"github.com/goharbor/harbor-operator/controllers"
"github.com/goharbor/harbor-operator/pkg/builder"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/goharbor/harbor-operator/pkg/cluster/lcm"
"github.com/goharbor/harbor-operator/pkg/config"
commonCtrl "github.com/goharbor/harbor-operator/pkg/controller"
"github.com/goharbor/harbor-operator/pkg/factories/application"
"github.com/goharbor/harbor-operator/pkg/utils/strings"
"github.com/ovh/configstore"
"github.com/pkg/errors"
Expand All @@ -25,7 +23,6 @@ import (
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
)

Expand All @@ -38,21 +35,16 @@ const (
// TODO: Refactor to inherit the common reconciler in future
// Reconciler reconciles a HarborCluster object.
type Reconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme

// In case
ConfigStore *configstore.Store
Name string
Version string
Name string

CacheCtrl lcm.Controller
DatabaseCtrl lcm.Controller
StorageCtrl lcm.Controller
HarborCtrl *harbor.Controller

ctrl *commonCtrl.Controller // TODO: move the Reconcile to pkg/controller.Controller
CacheCtrl lcm.Controller
DatabaseCtrl lcm.Controller
StorageCtrl lcm.Controller
HarborCtrl *harbor.Controller
*commonCtrl.Controller // TODO: move the Reconcile to pkg/controller.Controller
}

// +kubebuilder:rbac:groups=goharbor.io,resources=harborclusters,verbs=get;list;watch
Expand All @@ -69,10 +61,6 @@ type Reconciler struct {
// +kubebuilder:rbac:groups="",resources=persistentvolumeclaims,verbs=get;list;watch;create;update;patch;delete

func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
if err := r.ctrl.SetupWithManager(ctx, mgr); err != nil {
return err
}

concurrentReconcile, err := config.GetInt(r.ConfigStore, config.ReconciliationKey, config.DefaultConcurrentReconcile)
if err != nil {
return errors.Wrap(err, "cannot get concurrent reconcile")
Expand Down Expand Up @@ -134,10 +122,8 @@ func (r *Reconciler) NormalizeName(ctx context.Context, name string, suffixes ..

// New HarborCluster reconciler.
func New(ctx context.Context, configStore *configstore.Store) (commonCtrl.Reconciler, error) {
return &Reconciler{
Version: application.GetVersion(ctx),
ConfigStore: configStore,
Log: ctrl.Log.WithName(application.GetName(ctx)).WithName("controller").WithValues("controller", "HarborCluster"),
ctrl: commonCtrl.NewController(ctx, controllers.HarborCluster, nil, configStore),
}, nil
r := &Reconciler{}
r.Controller = commonCtrl.NewController(ctx, controllers.HarborCluster, nil, configStore)

return r, nil
}
14 changes: 6 additions & 8 deletions controllers/goharbor/harborcluster/harborcluster.go
Expand Up @@ -8,7 +8,6 @@ import (
goharborv1 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1beta1"
"github.com/goharbor/harbor-operator/pkg/cluster/gos"
"github.com/goharbor/harbor-operator/pkg/cluster/lcm"
"github.com/goharbor/harbor-operator/pkg/factories/logger"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -17,8 +16,7 @@ import (

// Reconcile logic of the HarborCluster.
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) { // nolint:funlen
ctx = r.ctrl.PopulateContext(ctx, req)
log := logger.Get(ctx)
ctx = r.PopulateContext(ctx, req)

// Get the harborcluster first
harborcluster := &goharborv1.HarborCluster{}
Expand All @@ -34,20 +32,20 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.

// Check if it is being deleted
if !harborcluster.ObjectMeta.DeletionTimestamp.IsZero() {
log.Info("harbor cluster is being deleted", "name", req.NamespacedName)
r.Log.Info("harbor cluster is being deleted", "name", req.NamespacedName)

return ctrl.Result{}, nil
}

if err := r.ctrl.PrepareStatus(ctx, harborcluster); err != nil {
return r.ctrl.HandleError(ctx, harborcluster, errors.Wrap(err, "cannot prepare owner status"))
if err := r.PrepareStatus(ctx, harborcluster); err != nil {
return r.HandleError(ctx, harborcluster, errors.Wrap(err, "cannot prepare owner status"))
}

// For tracking status
st := newStatus(harborcluster).
WithContext(ctx).
WithClient(r.Client).
WithLog(log)
WithLog(r.Log)

defer func() {
// Execute the status update operation
Expand Down Expand Up @@ -130,7 +128,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.
r.Log.Info("Reconcile is completed")

if harborStatus.Condition.Status == corev1.ConditionTrue {
return ctrl.Result{}, r.ctrl.SetSuccessStatus(ctx, harborcluster)
return ctrl.Result{}, r.SetSuccessStatus(ctx, harborcluster)
}

return ctrl.Result{}, nil
Expand Down

0 comments on commit a0db41f

Please sign in to comment.