@@ -27,6 +27,7 @@ import (
2727 "sigs.k8s.io/cluster-api/util/secret"
2828 "sigs.k8s.io/controller-runtime/pkg/client"
2929 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
30+ "sigs.k8s.io/controller-runtime/pkg/log"
3031
3132 "sigs.k8s.io/cluster-api-provider-azure/azure"
3233 "sigs.k8s.io/cluster-api-provider-azure/azure/scope"
@@ -133,24 +134,25 @@ func (r *azureManagedControlPlaneService) reconcileKubeconfig(ctx context.Contex
133134 }
134135
135136 // PATCH POINT: Inject custom CA certificate data into kubeconfig before storing in secret
136- fmt .Printf ("=== DEBUG: AMCP reconcileKubeconfig() - Processing kubeconfig %d (admin=0, user=1) ===\n " , i )
137- fmt .Printf ("DEBUG: AMCP - Original kubeconfig size: %d bytes\n " , len (kubeConfigData ))
137+ logger := log .FromContext (ctx )
138+ logger .V (4 ).Info ("Processing kubeconfig" , "index" , i , "type" , map [int ]string {0 : "admin" , 1 : "user" }[i ])
139+ logger .V (4 ).Info ("Original kubeconfig size" , "bytes" , len (kubeConfigData ))
138140
139141 if customCACert := getCustomCACertificateForAMCP (r .scope .ClusterName ()); customCACert != nil {
140- fmt . Printf ( "DEBUG: AMCP - Custom CA certificate found (%d bytes), proceeding with COMBINATION \n " , len (customCACert ))
142+ logger . V ( 4 ). Info ( " Custom CA certificate found" , "size_bytes " , len (customCACert ))
141143
142144 // Parse kubeconfig
143145 kubeconfig , err := clientcmd .Load (kubeConfigData )
144146 if err != nil {
145- fmt . Printf ( "DEBUG: AMCP - ERROR: Failed to parse kubeconfig: %v \n " , err )
147+ logger . V ( 4 ). Error ( err , " Failed to parse kubeconfig" )
146148 } else {
147149 patchedCount := 0
148150 for clusterName , clusterInfo := range kubeconfig .Clusters {
149- fmt . Printf ( "DEBUG: AMCP - Processing cluster '%s' \n " , clusterName )
150- fmt . Printf ( "DEBUG: AMCP - Original CA data length: %d bytes\n " , len (clusterInfo .CertificateAuthorityData ))
151+ logger . V ( 4 ). Info ( " Processing cluster" , "name " , clusterName )
152+ logger . V ( 4 ). Info ( " Original CA data length" , " bytes" , len (clusterInfo .CertificateAuthorityData ))
151153
152154 if clusterInfo .CertificateAuthorityData != nil && len (clusterInfo .CertificateAuthorityData ) > 0 {
153- fmt . Printf ( "DEBUG: AMCP - Combining original CA with custom CA\n " )
155+ logger . V ( 4 ). Info ( " Combining original CA with custom CA" )
154156 // Combine: original + newline + custom CA
155157 combinedCA := make ([]byte , 0 , len (clusterInfo .CertificateAuthorityData )+ 1 + len (customCACert ))
156158 combinedCA = append (combinedCA , clusterInfo .CertificateAuthorityData ... )
@@ -163,28 +165,30 @@ func (r *azureManagedControlPlaneService) reconcileKubeconfig(ctx context.Contex
163165 combinedCA = append (combinedCA , customCACert ... )
164166 clusterInfo .CertificateAuthorityData = combinedCA
165167
166- fmt .Printf ("DEBUG: AMCP - Combined CA data length: %d bytes (original: %d + custom: %d)\n " ,
167- len (combinedCA ), len (clusterInfo .CertificateAuthorityData )- len (customCACert )- 1 , len (customCACert ))
168+ logger .V (4 ).Info ("Combined CA data" ,
169+ "total_bytes" , len (combinedCA ),
170+ "original_bytes" , len (clusterInfo .CertificateAuthorityData )- len (customCACert )- 1 ,
171+ "custom_bytes" , len (customCACert ))
168172 } else {
169- fmt . Printf ( "DEBUG: AMCP - No original CA data, using only custom CA\n " )
173+ logger . V ( 4 ). Info ( " No original CA data, using only custom CA" )
170174 // No original CA, just use custom
171175 clusterInfo .CertificateAuthorityData = customCACert
172- fmt . Printf ( "DEBUG: AMCP - Set CA data length: %d bytes\n " , len (clusterInfo .CertificateAuthorityData ))
176+ logger . V ( 4 ). Info ( " Set CA data length" , " bytes" , len (clusterInfo .CertificateAuthorityData ))
173177 }
174178 patchedCount ++
175179 }
176- fmt . Printf ( "DEBUG: AMCP - Combined CA certificates in %d clusters total \n " , patchedCount )
180+ logger . V ( 4 ). Info ( " Combined CA certificates" , "clusters_patched " , patchedCount )
177181
178182 // Write back the modified kubeconfig
179183 if patchedKubeconfig , err := clientcmd .Write (* kubeconfig ); err != nil {
180- fmt . Printf ( "DEBUG: AMCP - ERROR: Failed to write patched kubeconfig: %v \n " , err )
184+ logger . V ( 4 ). Error ( err , " Failed to write patched kubeconfig" )
181185 } else {
182186 kubeConfigData = patchedKubeconfig
183- fmt . Printf ( "DEBUG: AMCP - Successfully wrote combined kubeconfig (%d bytes) \n " , len (kubeConfigData ))
187+ logger . V ( 4 ). Info ( " Successfully wrote combined kubeconfig" , " bytes" , len (kubeConfigData ))
184188 }
185189 }
186190 } else {
187- fmt . Printf ( "DEBUG: AMCP - No custom CA certificate found, keeping original kubeconfig\n " )
191+ logger . V ( 4 ). Info ( " No custom CA certificate found, keeping original kubeconfig" )
188192 }
189193
190194 kubeConfigSecret := r .scope .MakeEmptyKubeConfigSecret ()
@@ -210,7 +214,7 @@ func (r *azureManagedControlPlaneService) reconcileKubeconfig(ctx context.Contex
210214 return errors .Wrap (err , "failed to reconcile kubeconfig secret for cluster" )
211215 }
212216
213- fmt . Printf ( "DEBUG: AMCP - Successfully stored kubeconfig secret '%s' with CA injection\n " , kubeConfigSecret .Name )
217+ logger . V ( 4 ). Info ( " Successfully stored kubeconfig secret with CA injection" , "secret_name " , kubeConfigSecret .Name )
214218 }
215219
216220 // store cluster-info for the cluster with the admin kubeconfig.
@@ -243,35 +247,37 @@ func (r *azureManagedControlPlaneService) reconcileKubeconfig(ctx context.Contex
243247// This function leverages the same certificate that is used for Azure authentication
244248// by checking the global AzSecretCertPool that gets populated during Azure client initialization
245249func getCustomCACertificateForAMCP (clusterName string ) []byte {
246- fmt .Printf ("=== DEBUG: AMCP getCustomCACertificateForAMCP() called for cluster: %s ===\n " , clusterName )
250+ logger := log .FromContext (context .Background ()) // Changed to context.Background() to avoid context leak
251+ logger .V (4 ).Info ("getCustomCACertificateForAMCP called" , "cluster_name" , clusterName )
247252
248253 // Debug the condition checks
249254 isConfigured := azure .IsAzSecretCertConfigured ()
250255 poolNotNil := azure .AzSecretCertPool != nil
251256 dataLength := len (azure .AzSecretCertData )
252257
253- fmt .Printf ("DEBUG: AMCP - azure.IsAzSecretCertConfigured() = %v\n " , isConfigured )
254- fmt .Printf ("DEBUG: AMCP - azure.AzSecretCertPool != nil = %v\n " , poolNotNil )
255- fmt .Printf ("DEBUG: AMCP - len(azure.AzSecretCertData) = %d\n " , dataLength )
258+ logger .V (4 ).Info ("Condition checks" ,
259+ "is_configured" , isConfigured ,
260+ "pool_not_nil" , poolNotNil ,
261+ "data_length" , dataLength )
256262
257263 // Check if we have a certificate in the global AzSecretCertPool
258264 // This is the same certificate pool used for Azure authentication
259265 if azure .IsAzSecretCertConfigured () && azure .AzSecretCertPool != nil {
260- fmt . Printf ( "DEBUG: AMCP - Passed first condition check (IsConfigured && PoolNotNil)\n " )
266+ logger . V ( 4 ). Info ( " Passed first condition check (IsConfigured && PoolNotNil)" )
261267 // Return the raw certificate data stored in AzSecretCertData
262268 // This contains the original PEM data that was used to populate the certificate pool
263269 if len (azure .AzSecretCertData ) > 0 {
264- fmt . Printf ( "DEBUG: AMCP - Found certificate data, returning %d bytes\n " , len (azure .AzSecretCertData ))
265- fmt . Printf ( "DEBUG: AMCP - Certificate data preview (first 100 chars): %s... \n " ,
270+ logger . V ( 4 ). Info ( " Found certificate data, returning" , " bytes" , len (azure .AzSecretCertData ))
271+ logger . V ( 4 ). Info ( " Certificate data preview (first 100 chars)" , "preview " ,
266272 string (azure .AzSecretCertData [:int (math .Min (100 , float64 (len (azure .AzSecretCertData ))))]))
267273 return azure .AzSecretCertData
268274 } else {
269- fmt . Printf ( "DEBUG: AMCP - Certificate data is empty, returning nil\n " )
275+ logger . V ( 4 ). Info ( " Certificate data is empty, returning nil" )
270276 }
271277 } else {
272- fmt . Printf ( "DEBUG: AMCP - Failed condition check - IsConfigured: %v , PoolNotNil: %v \n " , isConfigured , poolNotNil )
278+ logger . V ( 4 ). Info ( " Failed condition check - IsConfigured: false , PoolNotNil: false" )
273279 }
274280
275- fmt . Printf ( "DEBUG: AMCP - getCustomCACertificateForAMCP() returning nil\n " )
281+ logger . V ( 4 ). Info ( " getCustomCACertificateForAMCP returning nil" )
276282 return nil
277283}
0 commit comments