Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Custom Resource Definitions

The Keycloak Operator provides several Custom Resource Definitions (CRDs) to manage Keycloak resources declaratively.

Resource Hierarchy

KeycloakInstance / ClusterKeycloakInstance
    └── KeycloakRealm / ClusterKeycloakRealm
            ├── KeycloakClient
            │       ├── KeycloakUser (service account, via clientRef)
            │       ├── KeycloakRole (client role)
            │       └── KeycloakProtocolMapper
            ├── KeycloakUser (regular users, via realmRef)
            │       └── KeycloakUserCredential
            ├── KeycloakGroup
            ├── KeycloakClientScope
            │       └── KeycloakProtocolMapper
            ├── KeycloakRole (realm role)
            ├── KeycloakRoleMapping (maps roles to Users/Groups)
            ├── KeycloakComponent (LDAP, key providers, etc.)
            ├── KeycloakIdentityProvider
            └── KeycloakOrganization (requires Keycloak 26+)

Overview

Instance Resources

CRDDescriptionScope
KeycloakInstanceConnection to a Keycloak serverNamespaced
ClusterKeycloakInstanceCluster-scoped Keycloak connectionCluster

Realm Resources

CRDDescriptionParent
KeycloakRealmRealm configurationKeycloakInstance
ClusterKeycloakRealmCluster-scoped realmClusterKeycloakInstance

OAuth & Client Resources

CRDDescriptionParent
KeycloakClientOAuth2/OIDC clientKeycloakRealm
KeycloakClientScopeClient scope configurationKeycloakRealm
KeycloakProtocolMapperToken claim mappersKeycloakClient or KeycloakClientScope

Identity Resources

CRDDescriptionParent
KeycloakUserUser managementKeycloakRealm or KeycloakClient¹
KeycloakUserCredentialUser password managementKeycloakUser
KeycloakGroupGroup managementKeycloakRealm

Role & Access Control

CRDDescriptionParent
KeycloakRoleRealm and client rolesKeycloakRealm or KeycloakClient
KeycloakRoleMappingRole-to-subject mappingsKeycloakUser or KeycloakGroup

Federation & Infrastructure

CRDDescriptionParent
KeycloakComponentLDAP federation, key providersKeycloakRealm
KeycloakIdentityProviderExternal identity providersKeycloakRealm
KeycloakOrganizationOrganization management²KeycloakRealm

¹ KeycloakUser supports clientRef for managing service account users associated with a client
² KeycloakOrganization requires Keycloak 26.0.0 or later

Common Patterns

Definition Field

Most resources include a definition field that accepts the full Keycloak API representation:

spec:
  definition:
    # Full Keycloak API object
    realm: my-realm
    enabled: true
    displayName: My Realm

This provides flexibility to configure any Keycloak property, even those not explicitly modeled in the CRD.

Status Tracking

All resources expose status information:

status:
  ready: true
  message: "Resource synchronized successfully"
  conditions:
    - type: Ready
      status: "True"
      lastTransitionTime: "2024-01-01T00:00:00Z"
      reason: Synchronized
      message: "Resource is in sync with Keycloak"

Finalizers

Resources use finalizers to ensure proper cleanup when deleted:

metadata:
  finalizers:
    - keycloak.hostzero.com/finalizer

Preserving Resources on Deletion

By default, when you delete a Custom Resource, the operator also deletes the corresponding resource in Keycloak. If you want to keep the resource in Keycloak while removing the CR from Kubernetes, use the keycloak.hostzero.com/preserve-resource annotation:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRealm
metadata:
  name: my-realm
  annotations:
    keycloak.hostzero.com/preserve-resource: "true"
spec:
  # ...

When this annotation is set to "true", deleting the CR will:

  • Remove the CR from Kubernetes
  • Keep the resource in Keycloak untouched

This is useful for scenarios like:

  • Migrating management of a resource to a different system
  • Temporarily removing operator control without losing data
  • Testing or debugging without affecting production resources

Note: The annotation value must be exactly "true" (as a string) to preserve the resource. Any other value (or absence of the annotation) will result in normal deletion behavior.

Supported Resources: This annotation works with all resource types except KeycloakInstance and ClusterKeycloakInstance (which don’t manage Keycloak resources directly).

API Version

All CRDs use the keycloak.hostzero.com/v1beta1 API version:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRealm