kubernetes-reflector

repository·main·Indexed 23 days ago

https://github.com/emberstack/kubernetes-reflector

A Kubernetes addon that monitors changes to Secret and ConfigMap resources and reflects those changes to mirror resources in the same or other namespaces. It supports automatic mirror creation, manual mirroring via annotations, and integration with cert-manager for mirroring automatically generated secrets.

Tokens
1.9K
Snippets
5
Records
8
Agent score
32%

What's inside kubernetes-reflector

  1. What is Reflector?

    main
    Reflector is a Kubernetes addon that monitors changes to Secret and ConfigMap resources. When a change is detected in a source resource, Reflector automatically reflects those changes to designated mirror resources in the same namespace or in other namespaces.
  2. Enable automatic mirror creation

    main

    Reflector can automatically create and manage mirror resources with the same name as the source in target namespaces.

    Automatic Mirror Annotations

    Add these to the source resource:

    • reflector.v1.k8s.emberstack.com/reflection-auto-enabled: Set to "true" to enable automatic creation. (Note: Requires reflection-allowed to be true).
    • reflector.v1.k8s.emberstack.com/reflection-auto-namespaces: A list of namespaces where mirrors should be automatically created. If omitted, all namespaces are allowed.
    • reflector.v1.k8s.emberstack.com/reflection-auto-namespaces-selector: A Kubernetes label selector to select namespaces for automatic mirrors.

    Lifecycle and Constraints

    • Deletion: Automatic mirrors are deleted if the source is deleted, if reflection/automirroring is disabled, or if the mirror's namespace no longer matches the allowed namespace criteria.
    • Conflicts: Reflector will skip creation if a resource with the same name already exists in the target namespace and will log a warning.
  3. Integrate Reflector with cert-manager

    main

    You can use Reflector to mirror secrets automatically generated by cert-manager.

    Using secretTemplate (cert-manager v1.5+)

    Annotate the Certificate resource's spec.secretTemplate.annotations to permit reflection on the resulting secret.

    Using Ingress annotations (cert-manager v1.15+)

    Annotate an Ingress using the cert-manager.io/secret-template annotation. The value must be a JSON string containing the desired annotations.

    Note: The cert-manager.io/secret-template approach is used to inject annotations into the secret created by the certificate issuance process.

    # Example: Certificate resource
    apiVersion: cert-manager.io/v1
    kind: Certificate
    ...
    spec:
      secretTemplate:
        annotations:
          reflector.v1.k8s.emberstack.com/reflection-allowed: "true"
          reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces: ""
    ...
    
    # Example: Ingress resource
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    ...
    metadata:
      annotations:
        cert-manager.io/cluster-issuer: letsencrypt-prod
        cert-manager.io/secret-template: |
          {"annotations": {"reflector.v1.k8s.emberstack.com/reflection-allowed": "true", "reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces": ""}}
    ...
  4. Install Reflector using Helm

    main

    The recommended way to deploy Reflector is via Helm. You can install it directly from an OCI registry or by adding the Emberstack Helm repository.

    Option 1: Install via OCI registry

    helm upgrade --install reflector oci://ghcr.io/emberstack/helm-charts/reflector

    Option 2: Install via Helm repository

    helm repo add emberstack https://emberstack.github.io/helm-charts
    helm repo update
    helm upgrade --install reflector emberstack/reflector
    $ helm upgrade --install reflector oci://ghcr.io/emberstack/helm-charts/reflector
  5. Install Reflector manually via kubectl

    main

    To deploy Reflector without Helm, you can apply the reflector.yaml file directly from the latest release on GitHub. It is common practice to deploy this into the kube-system namespace.

    $ kubectl -n kube-system apply -f https://github.com/emberstack/kubernetes-reflector/releases/latest/download/reflector.yaml
  6. Configure a mirror Secret or ConfigMap

    main

    To manually create a mirror for a source resource, add the reflects annotation to the mirror object. The value must be in the format <source-namespace>/<source-name>.

    Manual Mirror Annotations

    • reflector.v1.k8s.emberstack.com/reflects: The full name of the source object (e.g., default/my-secret).
    • reflector.v1.k8s.emberstack.com/reflected-version: If you are performing manual changes to a mirror (e.g., via Helm), add this annotation with an empty value ("") to reset the reflected version and allow Reflector to re-sync.

    Data Synchronized

    • Secrets: Reflector copies the data field.
    • ConfigMaps: Reflector copies both data and binaryData fields.
    apiVersion: v1
    kind: Secret
    metadata:
      name: mirror-secret
      annotations:
        reflector.v1.k8s.emberstack.com/reflects: "default/source-secret"
    data:
      ...
  7. Configure source Secret or ConfigMap for reflection

    main

    To allow a Secret or ConfigMap to be mirrored to other namespaces, you must add specific annotations to the source resource.

    Permission Annotations

    • reflector.v1.k8s.emberstack.com/reflection-allowed: Set to "true" to permit reflection.
    • reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces: A comma-separated list of namespaces or regular expressions allowed to receive the reflection. If omitted, all namespaces are allowed.
    • reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces-selector: A Kubernetes label selector (e.g., env=production) to permit reflection only to namespaces matching the selector.

    If both a list and a selector are provided, a namespace matches if it satisfies either condition.

    apiVersion: v1
    kind: Secret
    metadata:
      name: source-secret
      annotations:
        reflector.v1.k8s.emberstack.com/reflection-allowed: "true"
        reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces: "namespace-1,namespace-2,namespace-[0-9]*"
        reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces-selector: "env=production"
    data:
      ...
  8. Configure Reflector via Helm values

    main

    When deploying with Helm, you can customize the behavior of Reflector using the following configuration parameters:

    ParameterDescriptionDefault
    image.repositoryContainer image repositoryemberstack/kubernetes-reflector
    image.tagContainer image tagSame as chart version
    configuration.logging.minimumLevelLogging minimum levelInformation
    configuration.watcher.timeoutMaximum watcher lifetime in seconds``
    configuration.watcher.excludedNamespacesComma-separated list of namespace glob patterns to exclude (supports * and ?). Example: "ephie-*,kube-system,*-temp"``
    configuration.kubernetes.skipTlsVerifySkip TLS verify when connecting to the clusterfalse
    rbac.enabledCreate and use RBAC resourcestrue
    serviceAccount.createCreate ServiceAccounttrue
    resourcesResource limits{}