replikator

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2024 License: MIT Imports: 32 Imported by: 0

README

replikator

codecov

A kubernetes resource replicator.

replikator watches a resource in a namespace, and replicates it to other namespaces.

Usage

replikator --conf CONFIG_DIR --kubeconfig path/to/kubeconfig

Container Image

yankeguo/replikator
ghcr.io/yankeguo/replikator

Mount the kubeconfig file to /root/.kube/config, or setup RBAC for in-cluster authentication

Mount configuration files to /replikator

Configuration File

replikator will watch the configuration directory for changes, and reload the configuration files.

# resource name, required, should be canonical plural
# e.g. 'secrets', 'networking.k8s.io/v1/ingresses', 'apps/v1/deployments'
resource: secrets

# replication source
source:
  # source namespace, required
  namespace: kube-ingress
  # source resource name, required
  name: tls-cluster-wildcard

# replication target
target:
  # target namespace regexp, required
  namespace: .+
  # target resource name, optional, default to source name
  name: "tls-cluster-wildcard"

# modification of the resource, optional
modification:
  # jsonpatch to modify the resource, optional
  jsonpatch:
    - op: remove
      path: /metadata/annotations/replikator/modified

  # javascript code to modify the resource, optional, see below for details
  javascript: |
    resource.metadata.annotations["replikator/modified"] = new Date().toISOString()


# multi-documents YAML are supported
# use --- to separate multiple tasks
---
# another task

Modification

JSONPatch

A list of JSONPatch operations to modify the resource.

A example to remove spec.clusterIP and spec.clusterIPs from a Service resource.

modification:
  jsonpatch:
    - op: remove
      path: /spec/clusterIP
    - op: remove
      path: /spec/clusterIPs
JavaScript

You can use JavaScript to modify the resource, just modify the resource object in place.

A example to remove spec.ports[*].nodePort from a Service resource.

modification:
  javascript: |
    resource.spec.ports.forEach(port => delete port.nodePort)

Examples

In-Cluster Registry Credentials Replication
apiVersion: v1
kind: ServiceAccount
metadata:
  name: replikator
automountServiceAccountToken: true
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: replikator
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["namespaces"]
    verbs: ["list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: replikator
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: replikator
subjects:
  - kind: ServiceAccount
    name: replikator
    namespace: default
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: replikator-config
data:
  replikator.yaml: |
    resource: secrets
    source:
      namespace: default
      name: registry-credentials
    target:
      namespace: .+
---
apiVersion: v1
kind: Service
metadata:
  name: replikator
spec:
  clusterIP: None
  selector:
    app: replikator
  ports:
    - protocol: TCP
      port: 42
      name: placeholder
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: replikator
spec:
  replicas: 1
  serviceName: replikator
  selector:
    matchLabels:
      app: replikator
  template:
    metadata:
      labels:
        app: replikator
    spec:
      serviceAccountName: replikator
      volumes:
        - name: replikator-config
          configMap:
            name: replikator-config
      containers:
        - name: replikator
          image: yankeguo/replikator
          imagePullPolicy: Always
          volumeMounts:
            - name: replikator-config
              mountPath: /replikator

Credits

GUO YANKE, MIT License

Documentation

Index

Constants

View Source
const FieldManagerReplikator = "io.github.yankeguo/replikator"

Variables

View Source
var (
	ErrScriptTimeout = errors.New("script timeout")
)

Functions

func DigestTaskDefinitionsFromDir added in v0.3.0

func DigestTaskDefinitionsFromDir(dir string) (digest string, err error)

DigestTaskDefinitionsFromDir creates digest for TaskDefinitions in dir, for change detection

func EvaluateJavaScriptModification added in v0.2.0

func EvaluateJavaScriptModification(src string, script string) (out string, err error)

EvaluateJavaScriptModification evaluates the javascript modification script on the src, input and output are both JSON string

func ParseGroupVersionResource added in v0.2.0

func ParseGroupVersionResource(s string) (res schema.GroupVersionResource, err error)

ParseGroupVersionResource parse a string to GroupVersionResource

func RetrieveMetadataName added in v0.3.0

func RetrieveMetadataName(obj any) (name string, err error)

RetrieveMetadataName retrieve metadata.name from an object

Types

type Flags

type Flags struct {
	Conf       string
	Kubeconfig struct {
		Path      string
		InCluster bool
	}
}

func ParseFlags

func ParseFlags() (flags Flags, err error)

func (Flags) CreateKubernetesClient added in v0.3.0

func (flags Flags) CreateKubernetesClient() (client *kubernetes.Clientset, dynClient *dynamic.DynamicClient, err error)

type Session added in v0.3.0

type Session struct {
	// contains filtered or unexported fields
}

func (*Session) Do added in v0.3.0

func (s *Session) Do(ctx context.Context, namespace string) (err error)

func (*Session) Run added in v0.3.0

func (s *Session) Run(ctx context.Context)

Run the task until context is done

func (*Session) Watch added in v0.3.0

func (s *Session) Watch(ctx context.Context, triggers chan string)

type SessionList added in v0.3.0

type SessionList []*Session

func (SessionList) Run added in v0.3.0

func (list SessionList) Run(ctx context.Context)

type Task

type Task struct {
	// contains filtered or unexported fields
}

func (*Task) NewSession added in v0.3.0

func (t *Task) NewSession(opts TaskOptions) *Session

NewSession creates a new session for the task with kubernetes client and dynamic client

type TaskDefinition added in v0.2.0

type TaskDefinition struct {
	Resource string `yaml:"resource"`
	Source   struct {
		Namespace string `yaml:"namespace"`
		Name      string `yaml:"name"`
	} `yaml:"source"`
	Target struct {
		Namespace string `yaml:"namespace"`
		Name      string `yaml:"name"`
	} `yaml:"target"`
	Modification struct {
		JSONPatch  []any  `yaml:"jsonpatch"`
		Javascript string `yaml:"javascript"`
	} `yaml:"modification"`
}

TaskDefinition is the definition of a Task

func (TaskDefinition) Build added in v0.2.0

func (def TaskDefinition) Build() (out *Task, err error)

Build creates a Task from TaskDefinition

type TaskDefinitionList added in v0.3.0

type TaskDefinitionList []TaskDefinition

func LoadTaskDefinitionsFromDir added in v0.2.0

func LoadTaskDefinitionsFromDir(dir string) (defs TaskDefinitionList, err error)

LoadTaskDefinitionsFromDir loads TaskDefinitions from dir

func LoadTaskDefinitionsFromFile added in v0.3.0

func LoadTaskDefinitionsFromFile(file string) (defs TaskDefinitionList, err error)

LoadTaskDefinitionsFromFile loads TaskDefinition from file

func (TaskDefinitionList) Build added in v0.3.0

func (defs TaskDefinitionList) Build() (tasks TaskList, err error)

type TaskList added in v0.3.0

type TaskList []*Task

func (TaskList) NewSessions added in v0.3.0

func (list TaskList) NewSessions(opts TaskOptions) (out SessionList)

type TaskOptions added in v0.2.0

type TaskOptions struct {
	Client        *kubernetes.Clientset
	DynamicClient *dynamic.DynamicClient
}

TaskOptions is the options for creating a new session

Directories

Path Synopsis
cmd
replikator command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL