@@ -142,43 +142,54 @@ func postCreateOrUpdateResourceHook(ctx context.Context, scope ManagedClusterSco
142142 The user needs to ensure to provide service principal with admin AAD privileges.
143143*/
144144func reconcileKubeconfig (ctx context.Context , scope ManagedClusterScope , namespace string ) (adminKubeConfigData []byte , userKubeConfigData []byte , err error ) {
145+ fmt .Printf ("=== DEBUG: reconcileKubeconfig() called for cluster: %s ===\n " , scope .ClusterName ())
146+ fmt .Printf ("DEBUG: Namespace: %s\n " , namespace )
147+ fmt .Printf ("DEBUG: IsAADEnabled: %v\n " , scope .IsAADEnabled ())
148+ fmt .Printf ("DEBUG: AreLocalAccountsDisabled: %v\n " , scope .AreLocalAccountsDisabled ())
149+
145150 if scope .IsAADEnabled () {
151+ fmt .Printf ("DEBUG: AAD is enabled, getting user kubeconfig data\n " )
146152 if userKubeConfigData , err = getUserKubeconfigData (ctx , scope , namespace ); err != nil {
153+ fmt .Printf ("DEBUG: ERROR - Failed to get user kubeconfig: %v\n " , err )
147154 return nil , nil , errors .Wrap (err , "error while trying to get user kubeconfig" )
148155 }
156+ fmt .Printf ("DEBUG: Got user kubeconfig data: %d bytes\n " , len (userKubeConfigData ))
149157 }
150158
151159 if scope .AreLocalAccountsDisabled () {
160+ fmt .Printf ("DEBUG: Local accounts disabled, using user kubeconfig with token path\n " )
152161 userKubeconfigWithToken , err := getUserKubeConfigWithToken (ctx , userKubeConfigData , scope )
153162 if err != nil {
163+ fmt .Printf ("DEBUG: ERROR - Failed to get user kubeconfig with token: %v\n " , err )
154164 return nil , nil , errors .Wrap (err , "error while trying to get user kubeconfig with token" )
155165 }
166+ fmt .Printf ("DEBUG: Successfully got user kubeconfig with token: %d bytes\n " , len (userKubeconfigWithToken ))
156167 return userKubeconfigWithToken , userKubeConfigData , nil
157168 }
158169
170+ fmt .Printf ("DEBUG: Using admin kubeconfig path (local accounts enabled)\n " )
159171 asoSecret := & corev1.Secret {}
172+ secretName := adminKubeconfigSecretName (scope .ClusterName ())
173+ fmt .Printf ("DEBUG: Looking for ASO admin kubeconfig secret: %s/%s\n " , namespace , secretName )
174+
160175 err = scope .GetClient ().Get (
161176 ctx ,
162177 client.ObjectKey {
163178 Namespace : namespace ,
164- Name : adminKubeconfigSecretName ( scope . ClusterName ()) ,
179+ Name : secretName ,
165180 },
166181 asoSecret ,
167182 )
168183 if err != nil {
184+ fmt .Printf ("DEBUG: ERROR - Failed to get ASO admin kubeconfig secret: %v\n " , err )
169185 return nil , nil , errors .Wrap (err , "failed to get ASO admin kubeconfig secret" )
170186 }
171- adminKubeConfigData = asoSecret .Data [secret .KubeconfigDataName ]
172187
173- // PATCH POINT: Inject custom CA certificate data into admin kubeconfig
174- // This allows patching CA certificates for admin kubeconfig retrieved from ASO
175- if adminKubeConfigData != nil && len (adminKubeConfigData ) > 0 {
176- if patchedAdminConfig , err := patchKubeconfigWithCustomCA (adminKubeConfigData , scope .ClusterName ()); err == nil {
177- adminKubeConfigData = patchedAdminConfig
178- }
179- // Note: We could log the error but not fail the reconciliation if patching fails
180- }
188+ adminKubeConfigData = asoSecret .Data [secret .KubeconfigDataName ]
189+ fmt .Printf ("DEBUG: Retrieved admin kubeconfig from ASO secret: %d bytes\n " , len (adminKubeConfigData ))
181190
191+ fmt .Printf ("DEBUG: reconcileKubeconfig() completed - admin: %d bytes, user: %d bytes\n " ,
192+ len (adminKubeConfigData ), len (userKubeConfigData ))
182193 return adminKubeConfigData , userKubeConfigData , nil
183194}
184195
@@ -215,60 +226,16 @@ func getUserKubeConfigWithToken(ctx context.Context, userKubeConfigData []byte,
215226 auth .Exec = nil
216227 }
217228
218- // PATCH POINT: Inject custom CA certificate data here
219- // This is where you could add logic to replace the certificate-authority-data
220- // with your custom CA certificate
221- if customCACert := getCustomCACertificate (); customCACert != nil {
222- for _ , cluster := range config .Clusters {
223- cluster .CertificateAuthorityData = customCACert
224- }
225- }
226-
227229 kubeconfig , err := clientcmd .Write (* config )
228230 if err != nil {
229231 return nil , errors .Wrap (err , "error while trying to marshal new user kubeconfig with token" )
230232 }
231233 return kubeconfig , nil
232234}
233235
234- // getCustomCACertificate returns custom CA certificate data if available
235- // This function leverages the same certificate that is used for Azure authentication
236- // by checking the global AzSecretCertPool that gets populated during Azure client initialization
237- func getCustomCACertificate () []byte {
238- // Check if we have a certificate in the global AzSecretCertPool
239- // This is the same certificate pool used for Azure authentication
240- if azure .IsAzSecretCertConfigured () && azure .AzSecretCertPool != nil {
241- // Return the raw certificate data stored in AzSecretCertData
242- // This contains the original PEM data that was used to populate the certificate pool
243- if len (azure .AzSecretCertData ) > 0 {
244- return azure .AzSecretCertData
245- }
246- }
247-
248- return nil
249- }
250-
251- // patchKubeconfigWithCustomCA patches kubeconfig data with custom CA certificate
252- func patchKubeconfigWithCustomCA (kubeconfigData []byte , clusterName string ) ([]byte , error ) {
253- customCACert := getCustomCACertificate ()
254- if customCACert == nil {
255- return kubeconfigData , nil // No custom CA, return original
256- }
257-
258- config , err := clientcmd .Load (kubeconfigData )
259- if err != nil {
260- return nil , errors .Wrap (err , "failed to load kubeconfig for CA patching" )
261- }
262-
263- // Replace CA data in all clusters
264- for _ , cluster := range config .Clusters {
265- cluster .CertificateAuthorityData = customCACert
266- }
267-
268- patchedKubeconfig , err := clientcmd .Write (* config )
269- if err != nil {
270- return nil , errors .Wrap (err , "failed to write patched kubeconfig" )
236+ func min (a , b int ) int {
237+ if a < b {
238+ return a
271239 }
272-
273- return patchedKubeconfig , nil
240+ return b
274241}
0 commit comments