Skip to content

nadundesilva/k8s-replicator

Repository files navigation

K8s Replicator

Main Branch Build Vulnerabilities Scan License

Release Docker Image Docker Pulls

Replicator supports copying kubernetes resources across namespaces. This controller was written keeping extensibility and performance in mind. Therefore, it can be extended to any other resource as needed. The following resources are supported by the Kubernetes replicator.

  • Secrets
  • Config Maps
  • Network Policies

How to Use

Prerequisites

The following tools are expected to be installed and ready.

  • Kubectl
  • Operator SDK

The following tools can be either installed on your own or let the installation scripts handle it.

  • OLM to be installed in the cluster OLM can be installed using the operator-sdk
    operator-sdk olm install

How to Setup Operator

Quickstart

Run the following command to apply the controller to your cluster. The <VERSION> should be replaced with the release version to be used (eg:- 0.1.0) and kubectl CLI should be configured pointing to the cluster in which the controller needs to be started.

curl -L https://raw.githubusercontent.com/nadundesilva/k8s-replicator/main/installers/install.sh | bash -s <VERSION>

Manual Installation

  • Make sure all the pre-requisites are installed (including the dependencies which are normally installed by the installation scripts)
  • Install the Operator Bundle using the Operator SDK. The <VERSION> should be replaced with the release version to be used (eg:- 0.1.0) and kubectl CLI should be configured pointing to the cluster in which the controller needs to be started.
    operator-sdk run bundle docker.io/nadunrds/k8s-replicator-bundle:<VERSION>

How to mark a object to be replicated

Use the following label to mark the object to be replicated.

replicator.nadundesilva.github.io/object-type=replicated

All objects with the above label will replicated into all namespaces.

Ignored namespaces

The following namespaces are ignored by default.

  • The namespace in which controller resides
  • Namespaces with the name starting with kube- prefix
  • Namespaces with the label
    replicator.nadundesilva.github.io/namespace-type=ignored

If you want to override this behavior and specifically replicate to a namespace, add the following label

replicator.nadundesilva.github.io/namespace-type=managed

Examples

Examples for the CRDs used by the Operator can be found in the samples directory.

Additional labels/annotations used by the controller

The folloing labels are used by the controller to track the replication of resources.

  • The following label with the value replica is used to mark the replicated objects.
    replicator.nadundesilva.github.io/object-type=replica
  • The following annotation is used to store a replicated resource's source namespace.
    replicator.nadundesilva.github.io/source-namespace=<namespace>

How to Cleanup Operator

Quick Remove

Run the following command to remove the controller from your cluster. Kubectl CLI should be configured pointing to the cluster in which the controller needs to be started.

curl -L https://raw.githubusercontent.com/nadundesilva/k8s-replicator/main/installers/uninstall.sh | bash -s

Manual Removal

Remove the controller from your cluster by running the following command.

operator-sdk cleanup k8s-replicator

How to Extend

This Operator is created with extensibility in mind. To support this, a common interface Replicator was introduced.

type Replicator interface {
 GetKind() string
 AddToScheme(scheme *runtime.Scheme) error

 EmptyObject() client.Object
 EmptyObjectList() client.ObjectList
 ObjectListToArray(client.ObjectList) []client.Object

 Replicate(sourceObject client.Object, targetObject client.Object)
}

The K8s Replicator core uses the methods defined in this interface to get, list, and replicate resources to namespaces. The methods are carefully chosen to ensure that the minimum set of functionalities are defined for each resource separately keeping most of the logic reusable in the Operator core.

You can check the existing implementations of Replicator to get an idea of what needs to be done. However, you need to build the Operator from the source to get the new Replicator up and running. That being said, if you wish to contribute new resource replicators, you are most welcome.

Support

❔ If you need support or have a question about the K8s Replicator, reach out through Discussions.

🐛 If you have found a bug and would like to get it fixed, try opening a Bug Report.

💡 If you have a new idea or want to get a new feature or improvement added to the K8s Replicator, try creating a Feature Request.