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

ClusterKeycloakInstance

The ClusterKeycloakInstance resource makes a Keycloak server known to the operator at the cluster level, allowing resources in any namespace to reference it.

Overview

This is the cluster-scoped equivalent of KeycloakInstance. Use it when:

  • You have a central Keycloak server shared across multiple namespaces
  • You want to avoid duplicating instance definitions in each namespace
  • You need cross-namespace realm and client management

Example

apiVersion: keycloak.hostzero.io/v1beta1
kind: ClusterKeycloakInstance
metadata:
  name: central-keycloak
spec:
  baseUrl: https://keycloak.example.com
  credentials:
    secretRef:
      name: keycloak-admin-credentials
      namespace: keycloak-system

With Client Authentication

apiVersion: keycloak.hostzero.io/v1beta1
kind: ClusterKeycloakInstance
metadata:
  name: central-keycloak
spec:
  baseUrl: https://keycloak.example.com
  realm: master
  credentials:
    secretRef:
      name: keycloak-admin-credentials
      namespace: keycloak-system
      usernameKey: admin-user
      passwordKey: admin-password
  client:
    id: admin-cli

Spec

FieldTypeDescriptionRequired
baseUrlstringURL of the Keycloak serverYes
credentials.secretRef.namestringName of the credentials secretYes
credentials.secretRef.namespacestringNamespace of the credentials secretYes
credentials.secretRef.usernameKeystringKey for username in secretNo (default: “username”)
credentials.secretRef.passwordKeystringKey for password in secretNo (default: “password”)
realmstringAdmin realm nameNo (default: “master”)
client.idstringClient ID for authenticationNo
client.secretstringClient secret (if confidential)No
token.secretNamestringSecret to cache access tokensNo

Status

FieldTypeDescription
readybooleanWhether connection to Keycloak is established
versionstringDetected Keycloak server version
statusstringCurrent status (Ready, ConnectionFailed, Error)
messagestringAdditional status information
conditions[]ConditionKubernetes conditions

Behavior

Connection Verification

The operator periodically verifies the connection to Keycloak by:

  1. Authenticating with the provided credentials
  2. Fetching server info to detect the version
  3. Updating the ready status and connection metrics

Secret Reference

Since ClusterKeycloakInstance is cluster-scoped, the namespace field in secretRef is required (unlike the namespaced KeycloakInstance where it defaults to the resource’s namespace).

Client Manager

The operator maintains a pool of authenticated Keycloak clients. When a ClusterKeycloakInstance is created, a client is registered in the pool with a special cluster-scoped key, making it available for all resources that reference it.

Use Cases

Central Keycloak for Multi-Tenant Platform

# Define the central instance once
apiVersion: keycloak.hostzero.io/v1beta1
kind: ClusterKeycloakInstance
metadata:
  name: platform-keycloak
spec:
  baseUrl: https://auth.platform.example.com
  credentials:
    secretRef:
      name: keycloak-admin
      namespace: auth-system
---
# Create cluster-scoped realms for each tenant
apiVersion: keycloak.hostzero.io/v1beta1
kind: ClusterKeycloakRealm
metadata:
  name: tenant-a-realm
spec:
  clusterInstanceRef:
    name: platform-keycloak
  definition:
    realm: tenant-a
    enabled: true

Shared Instance Across Environments

# Credentials in a secure namespace
apiVersion: v1
kind: Secret
metadata:
  name: keycloak-credentials
  namespace: keycloak-secrets
type: Opaque
stringData:
  username: admin
  password: ${KEYCLOAK_ADMIN_PASSWORD}
---
# Cluster instance referencing the secret
apiVersion: keycloak.hostzero.io/v1beta1
kind: ClusterKeycloakInstance
metadata:
  name: shared-keycloak
spec:
  baseUrl: https://keycloak.internal.example.com
  credentials:
    secretRef:
      name: keycloak-credentials
      namespace: keycloak-secrets

Comparison with KeycloakInstance

AspectKeycloakInstanceClusterKeycloakInstance
ScopeNamespacedCluster
Secret namespaceOptional (defaults to same)Required
Accessible fromSame namespace onlyAny namespace
Short namekcickci
Use caseSingle namespaceMulti-namespace/platform

Notes

  • Only one ClusterKeycloakInstance with a given name can exist
  • Deleting the instance will invalidate all resources that reference it
  • The credentials secret must exist before creating the instance
  • The operator requires RBAC permissions to read secrets from the specified namespace