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

KeycloakRole

Identifier field: Set the role name in the spec.name field. It is required and immutable once set. A name inside spec.definition is tolerated only when it matches spec.name; a conflicting value is rejected.

A KeycloakRole manages Keycloak roles. Roles can be either realm-level (shared across all clients) or client-level (specific to a single client).

Specification

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRole
metadata:
  name: my-role
spec:
  # Exactly one of realmRef, clusterRealmRef, or clientRef must be specified
  
  # For realm roles:
  realmRef:
    name: my-realm
  
  # For client roles (the realm comes from the client; do not also set realmRef):
  # clientRef:
  #   name: my-client
  
  # Required: Role definition (Keycloak RoleRepresentation)
  name: admin-role
  definition:
    description: Administrator role

Status

status:
  ready: true
  status: "Ready"
  roleName: "admin-role"
  roleID: "12345678-1234-1234-1234-123456789abc"
  isClientRole: false
  message: "Role synchronized successfully"
  resourcePath: "/admin/realms/my-realm/roles/admin-role"
  instance:
    instanceRef: my-keycloak
  realm:
    realmRef: my-realm
  conditions:
    - type: Ready
      status: "True"
      reason: Synchronized

Examples

Realm Role

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRole
metadata:
  name: my-realm-role
  namespace: keycloak
spec:
  realmRef:
    name: my-realm
  name: admin-role
  definition:
    description: Administrator role with full access
    composite: false

Client Role

The realm is derived from the referenced client, so no realmRef is given:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRole
metadata:
  name: my-client-role
  namespace: keycloak
spec:
  clientRef:
    name: my-client
  name: editor
  definition:
    description: Can edit resources

Parent Reference

A KeycloakRole can belong to one of three parent types:

ReferenceScopeUse Case
realmRefRealm roleShared across all clients in the realm
clusterRealmRefRealm roleFor cluster-scoped realms
clientRefClient roleSpecific to a single client

Note: Exactly one of these must be specified; setting more than one is rejected.

For a client role, use clientRef alone. The realm is taken from the referenced client, which already belongs to exactly one realm, so realmRef and clusterRealmRef must not be combined with clientRef.

Definition Properties

The definition field accepts any valid Keycloak RoleRepresentation:

FieldTypeDescription
namestringRole name (required)
descriptionstringRole description
compositebooleanWhether this is a composite role
clientRolebooleanWhether this is a client role
containerIdstringContainer ID (realm or client ID)
attributesobjectCustom attributes

Status Fields

FieldTypeDescription
readybooleanWhether the role is synchronized
statusstringCurrent status (e.g., “Ready”, “Error”)
messagestringHuman-readable status message
resourcePathstringKeycloak API path for this role
roleIDstringKeycloak internal role ID
roleNamestringThe role name in Keycloak
isClientRolebooleanWhether this is a client role
clientIDstringClient ID (for client roles)
instanceobjectResolved instance reference
realmobjectResolved realm reference
observedGenerationintegerLast observed generation
conditions[]ConditionKubernetes conditions

Short Names

AliasFull Name
kcrkeycloakroles
kubectl get kcr

Notes

  • Role names must be unique within their scope (realm or client)
  • When using clientRef, the role becomes a client role
  • Composite roles can reference other realm or client roles