Skip to content

Commit

Permalink
add correlationId in eventhandler of ctrl (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpdnju authored Dec 16, 2023
1 parent f657965 commit 34a3f4d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions controllers/azurestackhcicluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,28 @@ type AzureStackHCIClusterReconciler struct {
func (r *AzureStackHCIClusterReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
WithLogConstructor(r.ConstructLogger).
For(&infrav1.AzureStackHCICluster{}).
Complete(r)
}

func (r *AzureStackHCIClusterReconciler) ConstructLogger(req *reconcile.Request) logr.Logger {
log := r.Log.WithName("")
if req == nil {
return log
}
log = log.WithValues("azureStackHCICluster", req.NamespacedName)
cxt := context.Background()
azureStackHCICluster := &infrav1.AzureStackHCICluster{}
err := r.Get(cxt, req.NamespacedName, azureStackHCICluster)
if err != nil {
log.Error(err, "failed to get azureStackHCICluster")
return log
}
return log.WithValues("operationId", azureStackHCICluster.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey],
"correlationId", azureStackHCICluster.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhciclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhciclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
Expand Down
18 changes: 18 additions & 0 deletions controllers/azurestackhciloadbalancer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (r *AzureStackHCILoadBalancerReconciler) SetupWithManager(mgr ctrl.Manager,
// later we will also want to watch the cluster which owns the LB
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
WithLogConstructor(r.ConstructLogger).
For(&infrav1.AzureStackHCILoadBalancer{}).
Watches(
&source.Kind{Type: &infrav1.AzureStackHCIVirtualMachine{}},
Expand All @@ -65,6 +66,23 @@ func (r *AzureStackHCILoadBalancerReconciler) SetupWithManager(mgr ctrl.Manager,
Complete(r)
}

func (r *AzureStackHCILoadBalancerReconciler) ConstructLogger(req *reconcile.Request) logr.Logger {
log := r.Log.WithName("")
if req == nil {
return log
}
log = log.WithValues("azureStackHCILoadBalancer", req.NamespacedName)
cxt := context.Background()
azureStackHCILoadBalancer := &infrav1.AzureStackHCILoadBalancer{}
err := r.Get(cxt, req.NamespacedName, azureStackHCILoadBalancer)
if err != nil {
log.Error(err, "failed to get azureStackHCILoadBalancer")
return log
}
return log.WithValues("operationId", azureStackHCILoadBalancer.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey],
"correlationId", azureStackHCILoadBalancer.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhciloadbalancers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhciloadbalancers/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch
Expand Down
18 changes: 18 additions & 0 deletions controllers/azurestackhcimachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type AzureStackHCIMachineReconciler struct {
func (r *AzureStackHCIMachineReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
WithLogConstructor(r.ConstructLogger).
For(&infrav1.AzureStackHCIMachine{}).
Watches(
&source.Kind{Type: &clusterv1.Machine{}},
Expand All @@ -76,6 +77,23 @@ func (r *AzureStackHCIMachineReconciler) SetupWithManager(mgr ctrl.Manager, opti
Complete(r)
}

func (r *AzureStackHCIMachineReconciler) ConstructLogger(req *reconcile.Request) logr.Logger {
log := r.Log.WithName("")
if req == nil {
return log
}
log = log.WithValues("azureStackHCIMachine", req.NamespacedName)
cxt := context.Background()
azureStackHCIMachine := &infrav1.AzureStackHCIMachine{}
err := r.Get(cxt, req.NamespacedName, azureStackHCIMachine)
if err != nil {
log.Error(err, "failed to get azureStackHCIMachine")
return log
}
return log.WithValues("operationId", azureStackHCIMachine.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey],
"correlationId", azureStackHCIMachine.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhcimachines,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhcimachines/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch;update;patch
Expand Down
18 changes: 18 additions & 0 deletions controllers/azurestackhcivirtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,28 @@ type AzureStackHCIVirtualMachineReconciler struct {
func (r *AzureStackHCIVirtualMachineReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
WithLogConstructor(r.ConstructLogger).
For(&infrav1.AzureStackHCIVirtualMachine{}).
Complete(r)
}

func (r *AzureStackHCIVirtualMachineReconciler) ConstructLogger(req *reconcile.Request) logr.Logger {
log := r.Log.WithName("")
if req == nil {
return log
}
log = log.WithValues("azureStackHCIVirtualMachine", req.NamespacedName)
cxt := context.Background()
azureStackHCIVirtualMachine := &infrav1.AzureStackHCIVirtualMachine{}
err := r.Get(cxt, req.NamespacedName, azureStackHCIVirtualMachine)
if err != nil {
log.Error(err, "failed to get azureStackHCIVirtualMachine")
return log
}
return log.WithValues("operationId", azureStackHCIVirtualMachine.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey],
"correlationId", azureStackHCIVirtualMachine.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhcivirtualmachines,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azurestackhcivirtualmachines/status,verbs=get;update;patch

Expand Down

0 comments on commit 34a3f4d

Please sign in to comment.