Skip to content

Commit

Permalink
unify reconcile action start/end string
Browse files Browse the repository at this point in the history
  • Loading branch information
shulin-sq committed Jun 13, 2024
1 parent 573f147 commit 12e6131
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 56 deletions.
8 changes: 2 additions & 6 deletions pkg/controllers/accesslogpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ func RegisterAccessLogPolicyController(
}

func (r *accessLogPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "accesslogpolicy")
gwlog.AddMetadata(ctx, "name", req.Name)

r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "accesslogpolicy", req.Name)
defer func() {
r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, r.log)
}()

recErr := r.reconcile(ctx, req)
Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,9 @@ func RegisterGatewayController(
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways/finalizers,verbs=update

func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "gateway")
gwlog.AddMetadata(ctx, "name", req.Name)

r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "gateway", req.Name)
defer func() {
r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, r.log)
}()

recErr := r.reconcile(ctx, req)
Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/gatewayclass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ func RegisterGatewayClassController(log gwlog.Logger, mgr ctrl.Manager) error {
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/finalizers,verbs=update

func (r *gatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "gatewayclass")
gwlog.AddMetadata(ctx, "name", req.Name)

r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "gatewayclass", req.Name)
defer func() {
r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, r.log)
}()

gwClass := &gwv1beta1.GatewayClass{}
Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/iamauthpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,9 @@ func RegisterIAMAuthPolicyController(log gwlog.Logger, mgr ctrl.Manager, cloud p
//
// Policy Attachment Spec is defined in [GEP-713]: https://gateway-api.sigs.k8s.io/geps/gep-713/.
func (c *IAMAuthPolicyController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "iamauthpolicy")
gwlog.AddMetadata(ctx, "name", req.Name)

c.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, c.log, "iamauthpolicy", req.Name)
defer func() {
c.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, c.log)
}()

k8sPolicy := &anv1alpha1.IAMAuthPolicy{}
Expand Down
7 changes: 4 additions & 3 deletions pkg/controllers/route_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ func RegisterAllRouteControllers(
}

func (r *routeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "route")
gwlog.AddMetadata(ctx, "name", req.Name)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "route", req.Name)
defer func() {
gwlog.EndReconcileTrace(ctx, r.log)
}()

r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)

Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,9 @@ func RegisterServiceController(
//+kubebuilder:rbac:groups=core,resources=configmaps, verbs=create;delete;patch;update;get;list;watch

func (r *serviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "service")
gwlog.AddMetadata(ctx, "name", req.Name)

r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "service", req.Name)
defer func() {
r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, r.log)
}()

recErr := r.reconcile(ctx, req)
Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/serviceexport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ func RegisterServiceExportController(
//+kubebuilder:rbac:groups=application-networking.k8s.aws,resources=serviceexports/finalizers,verbs=update

func (r *serviceExportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "serviceexport")
gwlog.AddMetadata(ctx, "name", req.Name)

r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "serviceexport", req.Name)
defer func() {
r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, r.log)
}()

recErr := r.reconcile(ctx, req)
Expand Down
7 changes: 2 additions & 5 deletions pkg/controllers/serviceimport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ func RegisterServiceImportController(
//+kubebuilder:rbac:groups=application-networking.k8s.aws,resources=serviceimports/finalizers,verbs=update

func (r *serviceImportReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "serviceimport")
gwlog.AddMetadata(ctx, "name", req.Name)
r.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, r.log, "serviceimport", req.Name)
defer func() {
r.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, r.log)
}()

serviceImport := &anv1alpha1.ServiceImport{}
Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/targetgrouppolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ func RegisterTargetGroupPolicyController(log gwlog.Logger, mgr ctrl.Manager) err
}

func (c *TargetGroupPolicyController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "targetgrouppolicy")
gwlog.AddMetadata(ctx, "name", req.Name)

c.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, c.log, "targetgrouppolicy", req.Name)
defer func() {
c.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, c.log)
}()

tgPolicy := &TGP{}
Expand Down
8 changes: 2 additions & 6 deletions pkg/controllers/vpcassociationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ func RegisterVpcAssociationPolicyController(log gwlog.Logger, cloud pkg_aws.Clou
}

func (c *vpcAssociationPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx = gwlog.NewTrace(ctx)
gwlog.AddMetadata(ctx, "type", "vpcassociationpolicy")
gwlog.AddMetadata(ctx, "name", req.Name)

c.log.Infow(ctx, "reconcile starting", gwlog.GetMetadata(ctx)...)
ctx = gwlog.StartReconcileTrace(ctx, c.log, "vpcassociationpolicy", req.Name)
defer func() {
c.log.Infow(ctx, "reconcile completed", gwlog.GetMetadata(ctx)...)
gwlog.EndReconcileTrace(ctx, c.log)
}()

k8sPolicy := &anv1alpha1.VpcAssociationPolicy{}
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/gwlog/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gwlog

const ReconcileStart = "[ACTION_RECONCILE_START]"
const ReconcileEnd = "[ACTION_RECONCILE_END]"
18 changes: 18 additions & 0 deletions pkg/utils/gwlog/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func GetMetadata(ctx context.Context) []interface{} {

if ctx.Value(metadata) != nil {
for k, v := range ctx.Value(metadata).(*metadataValue).m {
if k == traceID {
// skip since there's a separate method to grab the trace id
continue
}
fields = append(fields, k)
fields = append(fields, v)
}
Expand All @@ -66,3 +70,17 @@ func GetTrace(ctx context.Context) string {
}
return ""
}

func StartReconcileTrace(ctx context.Context, log Logger, k8sresourcetype, name string) context.Context {
ctx = NewTrace(ctx)
AddMetadata(ctx, "type", k8sresourcetype)
AddMetadata(ctx, "name", name)

log.Infow(ctx, ReconcileStart, GetMetadata(ctx)...)

return ctx
}

func EndReconcileTrace(ctx context.Context, log Logger) {
log.Infow(ctx, ReconcileEnd, GetMetadata(ctx)...)
}

0 comments on commit 12e6131

Please sign in to comment.