// Reconcile reads that state of the cluster for a KeycloakUser object and makes changes based on the state read
// and what is in the KeycloakUser.Spec
// TODO(user): Modify this Reconcile function to implement your Controller logic.  This example creates
// a Pod as an example
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
Reconcile reads that state of the cluster for a KeycloakUser object and makes changes based on the state read
and what is in the KeycloakUser.Spec
TODO(user): Modify this Reconcile function to implement your Controller logic.  This example creates
a Pod as an example
Note:
The Controller will requeue the Request to be processed again if the returned error is non-nil or
Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
// Reconcile reads that state of the cluster for a KeycloakUser object and makes changes based on the state read // and what is in the KeycloakUser . Spec // TODO ( user ) : Modify this Reconcile function to implement your Controller logic . This example creates // a Pod as an example // Note : // The Controller will requeue the Request to be processed again if the returned error is non - nil or // Result . Requeue is true otherwise upon completion it will remove the work from the queue .
func (r *ReconcileKeycloakUser) Reconcile(request reconcile.Request) (reconcile.Result, error) {
	reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
	reqLogger.Info("Reconciling KeycloakUser")
	instance := &kc.KeycloakUser{}
	err := r.client.Get(context.TODO(), request.NamespacedName, instance)
	if err != nil {
		if kubeerrors.IsNotFound(err) {
			return reconcile.Result{}, nil
		}
		return reconcile.Result{}, err
	}
	if instance.Spec.RealmSelector == nil {
		log.Info(fmt.Sprintf("user %v/%v has no realm selector and will be ignored", instance.Namespace, instance.Name))
		return reconcile.Result{Requeue: false}, nil
	}
	realms, err := common.GetMatchingRealms(r.context, r.client, instance.Spec.RealmSelector)
	if err != nil {
		return reconcile.Result{}, err
	}
	log.Info(fmt.Sprintf("found %v matching realm(s) for user %v/%v", len(realms.Items), instance.Namespace, instance.Name))
	for _, realm := range realms.Items {
		if realm.Spec.Unmanaged {
			return r.ManageError(instance, errors.Errorf("users cannot be created for unmanaged keycloak realms"))
		}
		keycloaks, err := common.GetMatchingKeycloaks(r.context, r.client, realm.Spec.InstanceSelector)
		if err != nil {
			return r.ManageError(instance, err)
		}
		for _, keycloak := range keycloaks.Items {
			if keycloak.Spec.Unmanaged {
				return r.ManageError(instance, errors.Errorf("users cannot be created for unmanaged keycloak instances"))
			}
			keycloakFactory := common.LocalConfigKeycloakFactory{}
			authenticated, err := keycloakFactory.AuthenticatedClient(keycloak)
			if err != nil {
				return r.ManageError(instance, err)
			}
			log.Info(fmt.Sprintf("got authenticated client for keycloak at %v", keycloak.Status.InternalURL))
			userState := common.NewUserState(keycloak)
			log.Info(fmt.Sprintf("read state for keycloak %v/%v, realm %v/%v",
				keycloak.Namespace,
				keycloak.Name,
				instance.Namespace,
				realm.Spec.Realm.Realm))
			err = userState.Read(authenticated, r.client, instance, realm)
			if err != nil {
				return r.ManageError(instance, err)
			}
			reconciler := NewKeycloakuserReconciler(keycloak, realm)
			desiredState := reconciler.Reconcile(userState, instance)
			actionRunner := common.NewClusterAndKeycloakActionRunner(r.context, r.client, r.scheme, instance, authenticated)
			err = actionRunner.RunAll(desiredState)
			if err != nil {
				return r.ManageError(instance, err)
			}
		}
	}
	return reconcile.Result{Requeue: false}, r.manageSuccess(instance, instance.DeletionTimestamp != nil)
}
func ( r * ReconcileKeycloakUser ) Reconcile ( request reconcile . Request ) ( reconcile . Result , error ) { reqLogger := log . WithValues ( " " , request . Namespace , " " , request . Name ) reqLogger . Info ( " " ) instance := & kc . KeycloakUser { } err := r . client . Get ( context . TODO ( ) , request . NamespacedName , instance ) if err != nil { if kubeerrors . IsNotFound ( err ) { return reconcile . Result { } , nil } return reconcile . Result { } , err } if instance . Spec . RealmSelector == nil { log . Info ( fmt . Sprintf ( " " , instance . Namespace , instance . Name ) ) return reconcile . Result { Requeue : false } , nil } realms , err := common . GetMatchingRealms ( r . context , r . client , instance . Spec . RealmSelector ) if err != nil { return reconcile . Result { } , err } log . Info ( fmt . Sprintf ( " " , len ( realms . Items ) , instance . Namespace , instance . Name ) ) for _ , realm := range realms . Items { if realm . Spec . Unmanaged { return r . ManageError ( instance , errors . Errorf ( " " ) ) } keycloaks , err := common . GetMatchingKeycloaks ( r . context , r . client , realm . Spec . InstanceSelector ) if err != nil { return r . ManageError ( instance , err ) } for _ , keycloak := range keycloaks . Items { if keycloak . Spec . Unmanaged { return r . ManageError ( instance , errors . Errorf ( " " ) ) } keycloakFactory := common . LocalConfigKeycloakFactory { } authenticated , err := keycloakFactory . AuthenticatedClient ( keycloak ) if err != nil { return r . ManageError ( instance , err ) } log . Info ( fmt . Sprintf ( " " , keycloak . Status . InternalURL ) ) userState := common . NewUserState ( keycloak ) log . Info ( fmt . Sprintf ( " " , keycloak . Namespace , keycloak . Name , instance . Namespace , realm . Spec . Realm . Realm ) ) err = userState . Read ( authenticated , r . client , instance , realm ) if err != nil { return r . ManageError ( instance , err ) } reconciler := NewKeycloakuserReconciler ( keycloak , realm ) desiredState := reconciler . Reconcile ( userState , instance ) actionRunner := common . NewClusterAndKeycloakActionRunner ( r . context , r . client , r . scheme , instance , authenticated ) err = actionRunner . RunAll ( desiredState ) if err != nil { return r . ManageError ( instance , err ) } } } return reconcile . Result { Requeue : false } , r . manageSuccess ( instance , instance . DeletionTimestamp != nil ) }
Reconcile reads that state of the cluster for a KeycloakUser object and makes changes based on the state read
and what is in the KeycloakUser.Spec
TODO(user): Modify this Reconcile function to implement your Controller logic.
Reconcile reads that state of the cluster for a KeycloakUser object and makes changes based on the state read and what is in the KeycloakUser . Spec TODO ( user ) : Modify this Reconcile function to implement your Controller logic .
// Fetch the KeycloakUser instance // Request object not found, could have been deleted after reconcile request. // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. // Return and don't requeue // Error reading the object - requeue the request. // If no selector is set we can't figure out which realm instance this user should // be added to. Skip reconcile until a selector has been set. // Find the realms that this user should be added to based on the label selector // Get an authenticated keycloak api client for the instance // Compute the current state of the realm