@@ -24,6 +24,7 @@ import (
2424
2525 "github.com/blang/semver"
2626 "github.com/pkg/errors"
27+ oteltrace "go.opentelemetry.io/otel/trace"
2728 corev1 "k8s.io/api/core/v1"
2829 apierrors "k8s.io/apimachinery/pkg/api/errors"
2930 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,6 +48,7 @@ import (
4748 "sigs.k8s.io/cluster-api/feature"
4849 "sigs.k8s.io/cluster-api/internal/contract"
4950 "sigs.k8s.io/cluster-api/internal/util/ssa"
51+ traceutil "sigs.k8s.io/cluster-api/internal/util/trace"
5052 "sigs.k8s.io/cluster-api/util"
5153 "sigs.k8s.io/cluster-api/util/annotations"
5254 "sigs.k8s.io/cluster-api/util/collections"
@@ -76,6 +78,7 @@ type KubeadmControlPlaneReconciler struct {
7678 controller controller.Controller
7779 recorder record.EventRecorder
7880 Tracker * remote.ClusterCacheTracker
81+ TraceProvider oteltrace.TracerProvider
7982
8083 EtcdDialTimeout time.Duration
8184 EtcdCallTimeout time.Duration
@@ -95,6 +98,10 @@ type KubeadmControlPlaneReconciler struct {
9598}
9699
97100func (r * KubeadmControlPlaneReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , options controller.Options ) error {
101+ if r .TraceProvider == nil {
102+ r .TraceProvider = oteltrace .NewNoopTracerProvider ()
103+ }
104+ tr := traceutil .Reconciler (r , r .TraceProvider , "kubeadmcontrolplane" , "KubeadmControlPlane" )
98105 c , err := ctrl .NewControllerManagedBy (mgr ).
99106 For (& controlplanev1.KubeadmControlPlane {}).
100107 Owns (& clusterv1.Machine {}).
@@ -109,7 +116,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg
109116 predicates .ClusterUnpausedAndInfrastructureReady (ctrl .LoggerFrom (ctx )),
110117 ),
111118 ),
112- ).Build (r )
119+ ).Build (tr )
113120 if err != nil {
114121 return errors .Wrap (err , "failed setting up with a controller manager" )
115122 }
@@ -301,6 +308,9 @@ func (r *KubeadmControlPlaneReconciler) initControlPlaneScope(ctx context.Contex
301308}
302309
303310func patchKubeadmControlPlane (ctx context.Context , patchHelper * patch.Helper , kcp * controlplanev1.KubeadmControlPlane ) error {
311+ ctx , span := traceutil .Start (ctx , "patchKubeadmControlPlane" )
312+ defer span .End ()
313+
304314 // Always update the readyCondition by summarizing the state of other conditions.
305315 conditions .SetSummary (kcp ,
306316 conditions .WithConditions (
@@ -332,6 +342,9 @@ func patchKubeadmControlPlane(ctx context.Context, patchHelper *patch.Helper, kc
332342
333343// reconcile handles KubeadmControlPlane reconciliation.
334344func (r * KubeadmControlPlaneReconciler ) reconcile (ctx context.Context , controlPlane * internal.ControlPlane ) (res ctrl.Result , reterr error ) {
345+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.reconcile" )
346+ defer span .End ()
347+
335348 log := ctrl .LoggerFrom (ctx )
336349 log .Info ("Reconcile KubeadmControlPlane" )
337350
@@ -507,6 +520,9 @@ func (r *KubeadmControlPlaneReconciler) reconcileClusterCertificates(ctx context
507520// The implementation does not take non-control plane workloads into consideration. This may or may not change in the future.
508521// Please see https://github.com/kubernetes-sigs/cluster-api/issues/2064.
509522func (r * KubeadmControlPlaneReconciler ) reconcileDelete (ctx context.Context , controlPlane * internal.ControlPlane ) (ctrl.Result , error ) {
523+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.reconcileDelete" )
524+ defer span .End ()
525+
510526 log := ctrl .LoggerFrom (ctx )
511527 log .Info ("Reconcile KubeadmControlPlane deletion" )
512528
@@ -594,6 +610,9 @@ func (r *KubeadmControlPlaneReconciler) ClusterToKubeadmControlPlane(_ context.C
594610// Otherwise, fields would be co-owned by our "old" "manager" and "capi-kubeadmcontrolplane" and then we would not be
595611// able to e.g. drop labels and annotations.
596612func (r * KubeadmControlPlaneReconciler ) syncMachines (ctx context.Context , controlPlane * internal.ControlPlane ) error {
613+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.syncMachines" )
614+ defer span .End ()
615+
597616 patchHelpers := map [string ]* patch.Helper {}
598617 for machineName := range controlPlane .Machines {
599618 m := controlPlane .Machines [machineName ]
@@ -677,6 +696,9 @@ func (r *KubeadmControlPlaneReconciler) syncMachines(ctx context.Context, contro
677696// reconcileControlPlaneConditions is responsible of reconciling conditions reporting the status of static pods and
678697// the status of the etcd cluster.
679698func (r * KubeadmControlPlaneReconciler ) reconcileControlPlaneConditions (ctx context.Context , controlPlane * internal.ControlPlane ) error {
699+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.reconcileControlPlaneConditions" )
700+ defer span .End ()
701+
680702 // If the cluster is not yet initialized, there is no way to connect to the workload cluster and fetch information
681703 // for updating conditions. Return early.
682704 if ! controlPlane .KCP .Status .Initialized {
@@ -706,6 +728,9 @@ func (r *KubeadmControlPlaneReconciler) reconcileControlPlaneConditions(ctx cont
706728//
707729// NOTE: this func uses KCP conditions, it is required to call reconcileControlPlaneConditions before this.
708730func (r * KubeadmControlPlaneReconciler ) reconcileEtcdMembers (ctx context.Context , controlPlane * internal.ControlPlane ) error {
731+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.reconcileEtcdMembers" )
732+ defer span .End ()
733+
709734 log := ctrl .LoggerFrom (ctx )
710735
711736 // If etcd is not managed by KCP this is a no-op.
@@ -758,6 +783,9 @@ func (r *KubeadmControlPlaneReconciler) reconcileEtcdMembers(ctx context.Context
758783}
759784
760785func (r * KubeadmControlPlaneReconciler ) reconcileCertificateExpiries (ctx context.Context , controlPlane * internal.ControlPlane ) error {
786+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.reconcileCertificateExpiries" )
787+ defer span .End ()
788+
761789 log := ctrl .LoggerFrom (ctx )
762790
763791 // Return if there are no KCP-owned control-plane machines.
@@ -828,6 +856,9 @@ func (r *KubeadmControlPlaneReconciler) reconcileCertificateExpiries(ctx context
828856}
829857
830858func (r * KubeadmControlPlaneReconciler ) adoptMachines (ctx context.Context , kcp * controlplanev1.KubeadmControlPlane , machines collections.Machines , cluster * clusterv1.Cluster ) error {
859+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.adoptMachines" )
860+ defer span .End ()
861+
831862 // We do an uncached full quorum read against the KCP to avoid re-adopting Machines the garbage collector just intentionally orphaned
832863 // See https://github.com/kubernetes/kubernetes/issues/42639
833864 uncached := controlplanev1.KubeadmControlPlane {}
@@ -905,6 +936,9 @@ func (r *KubeadmControlPlaneReconciler) adoptMachines(ctx context.Context, kcp *
905936}
906937
907938func (r * KubeadmControlPlaneReconciler ) adoptOwnedSecrets (ctx context.Context , kcp * controlplanev1.KubeadmControlPlane , currentOwner * bootstrapv1.KubeadmConfig , clusterName string ) error {
939+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.adoptOwnedSecrets" )
940+ defer span .End ()
941+
908942 secrets := corev1.SecretList {}
909943 if err := r .Client .List (ctx , & secrets , client .InNamespace (kcp .Namespace ), client.MatchingLabels {clusterv1 .ClusterNameLabel : clusterName }); err != nil {
910944 return errors .Wrap (err , "error finding secrets for adoption" )
@@ -941,6 +975,8 @@ func (r *KubeadmControlPlaneReconciler) adoptOwnedSecrets(ctx context.Context, k
941975
942976// ensureCertificatesOwnerRef ensures an ownerReference to the owner is added on the Secrets holding certificates.
943977func (r * KubeadmControlPlaneReconciler ) ensureCertificatesOwnerRef (ctx context.Context , certificates secret.Certificates , owner metav1.OwnerReference ) error {
978+ ctx , span := traceutil .Start (ctx , "kubeadmcontrolplane.Reconciler.ensureCertificatesOwnerRef" )
979+ defer span .End ()
944980 for _ , c := range certificates {
945981 if c .Secret == nil {
946982 continue
0 commit comments