Skip to content

Commit

Permalink
logging correlation id (#231)
Browse files Browse the repository at this point in the history
* logging correlation id

* minimize get co&op id

---------

Co-authored-by: peidlu <[email protected]>
  • Loading branch information
lpdnju and lpdnju authored Sep 22, 2023
1 parent f18df01 commit d924bff
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,6 @@ const (
AnnotationClusterInfrastructureReady = "azurestackhci.cluster.sigs.k8s.io/infrastructure-ready"
ValueReady = "true"
AnnotationControlPlaneReady = "azurestackhci.cluster.sigs.k8s.io/control-plane-ready"
AzureOperationIDAnnotationKey = "management.azure.com/operationId"
AzureCorrelationIDAnnotationKey = "management.azure.com/correlationId"
)
18 changes: 18 additions & 0 deletions controllers/azurestackhcicluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (r *AzureStackHCIClusterReconciler) Reconcile(ctx context.Context, req ctrl
return reconcile.Result{}, err
}

log = log.WithValues("operationId", azureStackHCICluster.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey], "correlationId", azureStackHCICluster.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])

// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, azureStackHCICluster.ObjectMeta)
if err != nil {
Expand Down Expand Up @@ -238,6 +240,13 @@ func (r *AzureStackHCIClusterReconciler) deleteOrphanedMachines(clusterScope *sc
return err
}
if machine == nil {
// update correlation id before deletion
infrav1util.CopyCorrelationId(clusterScope.AzureStackHCICluster, azhciMachine)
if err := r.Client.Update(clusterScope.Context, azhciMachine); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "Failed to update AzureStackHCIMachine %s", azhciMachine)
}
}
clusterScope.Info("Deleting Orphaned Machine", "Name", azhciMachine.Name, "AzureStackHCICluster", clusterScope.AzureStackHCICluster.Name)
if err := r.Client.Delete(clusterScope.Context, azhciMachine); err != nil {
if !apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -278,6 +287,7 @@ func (r *AzureStackHCIClusterReconciler) reconcileAzureStackHCILoadBalancer(clus
azureStackHCILoadBalancer.Spec.SSHPublicKey = clusterScope.AzureStackHCILoadBalancer().SSHPublicKey
azureStackHCILoadBalancer.Spec.VMSize = clusterScope.AzureStackHCILoadBalancer().VMSize
azureStackHCILoadBalancer.Spec.Replicas = clusterScope.AzureStackHCILoadBalancer().Replicas
infrav1util.CopyCorrelationId(clusterScope.AzureStackHCICluster, azureStackHCILoadBalancer)
return nil
}

Expand Down Expand Up @@ -326,6 +336,14 @@ func (r *AzureStackHCIClusterReconciler) reconcileDeleteAzureStackHCILoadBalance
}
} else if azureStackHCILoadBalancer.GetDeletionTimestamp().IsZero() {
// If the AzureStackHCILoadBalancer is not already marked for deletion, delete it
// Update correlation id before deletion
infrav1util.CopyCorrelationId(clusterScope.AzureStackHCICluster, azureStackHCILoadBalancer)
if err := r.Client.Update(clusterScope.Context, azureStackHCILoadBalancer); err != nil {
if !apierrors.IsNotFound(err) {
conditions.MarkFalse(clusterScope.AzureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, clusterv1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
return errors.Wrapf(err, "Failed to update AzureStackHCILoadBalancer %s", azureStackHCILoadBalancerName)
}
}
if err := r.Client.Delete(clusterScope.Context, azureStackHCILoadBalancer); err != nil {
if !apierrors.IsNotFound(err) {
conditions.MarkFalse(clusterScope.AzureStackHCICluster, infrav1.NetworkInfrastructureReadyCondition, clusterv1.DeletionFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
Expand Down
2 changes: 2 additions & 0 deletions controllers/azurestackhciloadbalancer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (r *AzureStackHCILoadBalancerReconciler) Reconcile(ctx context.Context, req
return reconcile.Result{}, err
}

logger = logger.WithValues("operationId", azureStackHCILoadBalancer.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey], "correlationId", azureStackHCILoadBalancer.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])

// Fetch the CAPI Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, azureStackHCILoadBalancer.ObjectMeta)
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion controllers/azurestackhcimachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func (r *AzureStackHCIMachineReconciler) Reconcile(ctx context.Context, req ctrl
return reconcile.Result{}, err
}

logger = logger.WithValues("operationId", azureStackHCIMachine.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey], "correlationId", azureStackHCIMachine.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])

// Fetch the Machine.
machine, err := util.GetOwnerMachine(ctx, r.Client, azureStackHCIMachine.ObjectMeta)
if err != nil {
Expand Down Expand Up @@ -304,6 +306,8 @@ func (r *AzureStackHCIMachineReconciler) reconcileVirtualMachineNormal(machineSc

machineScope.AzureStackHCIMachine.Spec.NetworkInterfaces.DeepCopyInto(&vm.Spec.NetworkInterfaces)

infrav1util.CopyCorrelationId(machineScope.AzureStackHCIMachine, vm)

return nil
}

Expand Down Expand Up @@ -371,10 +375,16 @@ func (r *AzureStackHCIMachineReconciler) reconcileVirtualMachineDelete(machineSc
}
} else if vm.GetDeletionTimestamp().IsZero() {
// this means the VM resource was found and has not been deleted
infrav1util.CopyCorrelationId(machineScope.AzureStackHCIMachine, vm)
if err := r.Client.Update(clusterScope.Context, vm); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to update AzureStackHCIVirtualMachine %s", vmName)
}
}
// is this a synchronous call?
if err := r.Client.Delete(clusterScope.Context, vm); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to get AzureStackHCIVirtualMachine %s", vmName)
return errors.Wrapf(err, "failed to delete AzureStackHCIVirtualMachine %s", vmName)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions controllers/azurestackhcivirtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (r *AzureStackHCIVirtualMachineReconciler) Reconcile(ctx context.Context, r
return reconcile.Result{}, err
}

logger = logger.WithValues("operationId", azureStackHCIVirtualMachine.GetAnnotations()[infrav1.AzureOperationIDAnnotationKey], "correlationId", azureStackHCIVirtualMachine.GetAnnotations()[infrav1.AzureCorrelationIDAnnotationKey])

// Create the machine scope
virtualMachineScope, err := scope.NewVirtualMachineScope(scope.VirtualMachineScopeParams{
Logger: &logger,
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ func GetReconcileID(ctx context.Context) types.UID {
}
return reconcileID
}

func CopyCorrelationId(source, target client.Object) {
sourceAnnotations := source.GetAnnotations()
if len(sourceAnnotations) == 0 {
return
}

annotations := target.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[infrav1.AzureCorrelationIDAnnotationKey] = sourceAnnotations[infrav1.AzureCorrelationIDAnnotationKey]
annotations[infrav1.AzureOperationIDAnnotationKey] = sourceAnnotations[infrav1.AzureOperationIDAnnotationKey]
target.SetAnnotations(annotations)

}

0 comments on commit d924bff

Please sign in to comment.