@@ -19,6 +19,7 @@ package controller
1919
2020import (
2121 "context"
22+ "errors"
2223 "fmt"
2324 "maps"
2425 "time"
@@ -68,6 +69,12 @@ import (
6869 k8s_errors "k8s.io/apimachinery/pkg/api/errors"
6970)
7071
72+ // Static errors for Application Credential handling
73+ var (
74+ ErrACSecretNotFound = errors .New ("ApplicationCredential secret not found" )
75+ ErrACSecretMissingKeys = errors .New ("ApplicationCredential secret missing required keys" )
76+ )
77+
7178type conditionUpdater interface {
7279 Set (c * condition.Condition )
7380 MarkTrue (t condition.Type , messageFormat string , messageArgs ... any )
@@ -849,6 +856,7 @@ const (
849856 tlsAPIInternalField = ".spec.tls.api.internal.secretName"
850857 tlsAPIPublicField = ".spec.tls.api.public.secretName"
851858 topologyField = ".spec.topologyRef.Name"
859+ authAppCredSecretField = ".spec.auth.applicationCredentialSecret" // #nosec G101
852860)
853861
854862var allWatchFields = []string {
@@ -857,6 +865,7 @@ var allWatchFields = []string{
857865 tlsAPIInternalField ,
858866 tlsAPIPublicField ,
859867 topologyField ,
868+ authAppCredSecretField ,
860869}
861870
862871// SetupWithManager sets up the controller with the Manager.
@@ -921,6 +930,18 @@ func (r *PlacementAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
921930 return err
922931 }
923932
933+ // index authAppCredSecretField
934+ if err := mgr .GetFieldIndexer ().IndexField (context .Background (), & placementv1.PlacementAPI {}, authAppCredSecretField , func (rawObj client.Object ) []string {
935+ // Extract the application credential secret name from the spec, if one is provided
936+ cr := rawObj .(* placementv1.PlacementAPI )
937+ if cr .Spec .Auth .ApplicationCredentialSecret == "" {
938+ return nil
939+ }
940+ return []string {cr .Spec .Auth .ApplicationCredentialSecret }
941+ }); err != nil {
942+ return err
943+ }
944+
924945 return ctrl .NewControllerManagedBy (mgr ).
925946 For (& placementv1.PlacementAPI {}).
926947 Owns (& mariadbv1.MariaDBDatabase {}).
@@ -1379,6 +1400,30 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
13791400 ),
13801401 }
13811402
1403+ templateParameters ["UseApplicationCredentials" ] = false
1404+ // Try to get Application Credential for this service
1405+ if instance .Spec .Auth .ApplicationCredentialSecret != "" {
1406+ acSecretObj , _ , err := secret .GetSecret (ctx , h , instance .Spec .Auth .ApplicationCredentialSecret , instance .Namespace )
1407+ if err != nil {
1408+ if k8s_errors .IsNotFound (err ) {
1409+ h .GetLogger ().Info ("ApplicationCredential secret not found, waiting" , "secret" , instance .Spec .Auth .ApplicationCredentialSecret )
1410+ return fmt .Errorf ("%w: %s" , ErrACSecretNotFound , instance .Spec .Auth .ApplicationCredentialSecret )
1411+ }
1412+ h .GetLogger ().Error (err , "Failed to get ApplicationCredential secret" , "secret" , instance .Spec .Auth .ApplicationCredentialSecret )
1413+ return err
1414+ }
1415+ acID , okID := acSecretObj .Data [keystonev1 .ACIDSecretKey ]
1416+ acSecretData , okSecret := acSecretObj .Data [keystonev1 .ACSecretSecretKey ]
1417+ if ! okID || len (acID ) == 0 || ! okSecret || len (acSecretData ) == 0 {
1418+ h .GetLogger ().Info ("ApplicationCredential secret missing required keys" , "secret" , instance .Spec .Auth .ApplicationCredentialSecret )
1419+ return fmt .Errorf ("%w: %s" , ErrACSecretMissingKeys , instance .Spec .Auth .ApplicationCredentialSecret )
1420+ }
1421+ templateParameters ["UseApplicationCredentials" ] = true
1422+ templateParameters ["ACID" ] = string (acID )
1423+ templateParameters ["ACSecret" ] = string (acSecretData )
1424+ h .GetLogger ().Info ("Using ApplicationCredentials auth" , "secret" , instance .Spec .Auth .ApplicationCredentialSecret )
1425+ }
1426+
13821427 // create httpd vhost template parameters
13831428 httpdVhostConfig := map [string ]any {}
13841429 for _ , endpt := range []service.Endpoint {service .EndpointInternal , service .EndpointPublic } {
0 commit comments