saswatamcode/configmap-operator

Kubernetes Operator which allows you to update ConfigMaps, based on some external URL/filepath which serves the required data.

★ 3Forks 1GoGitHub ↗Compare
kubernetes-operator

README

configmap-operator

Kubernetes Operator which allows you to update ConfigMaps, based on some external URL/filepath which serves the required data.

⚠ This project is made for learning/experimentation purposes and config can change at any time. Ideally, you can replicate similar functionality via other standard configuration practices.

usage: configmap-operator run [<flags>]

Launches ConfigMap Operator

Flags:
  -h, --help                   Show context-sensitive help (also try --help-long
                               and --help-man).
      --version                Show application version.
      --log.level=info         Log filtering level.
      --log.format=clilog      Log format to use.
      --metrics="metrics"      Endpoint for serving metrics.
      --kubeconfig=KUBECONFIG  Path to a kubeconfig. Only required if
                               out-of-cluster.
      --master=MASTER          The address of the Kubernetes API server.
                               Overrides any value in kubeconfig. Only required
                               if out-of-cluster.
      --namespace="default"    The namespace to watch.
      --refresh.interval=10s   The interval after which the ConfigMap will be
                               refreshed.

Getting Started

Example manifests are provided here and are generated with jsonnet!

Spin-up a cluster with kind and create a configmap-operator-demo namespace,

kubectl apply -f examples/manifests/namespace.yaml

Deploy the configmap-operator,

kubectl apply -f examples/manifests/

This creates the ConfigMap below with an empty data field and the annotations configmap-operator-src and configmap-operator-key which will be used by the operator to fill in data.

apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
    configmap-operator-key: prom.yaml
    configmap-operator-src: https://raw.githubusercontent.com/prometheus/prometheus/main/documentation/examples/prometheus.yml
  labels:
    app.kubernetes.io/component: kubernetes-operator
    app.kubernetes.io/instance: configmap-operator
    app.kubernetes.io/name: configmap-operator
  name: example-prom-config
  namespace: configmap-operator-demo

The operator is deployed with the Deployment below and its related RoleBinding and ServiceAccount. It finds ConfigMaps with the above annotations and starts updating it periodically (15s)

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/component: kubernetes-operator
    app.kubernetes.io/instance: configmap-operator
    app.kubernetes.io/name: configmap-operator
  name: configmap-operator
  namespace: configmap-operator-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/component: kubernetes-operator
      app.kubernetes.io/instance: configmap-operator
      app.kubernetes.io/name: configmap-operator
  template:
    metadata:
      labels:
        app.kubernetes.io/component: kubernetes-operator
        app.kubernetes.io/instance: configmap-operator
        app.kubernetes.io/name: configmap-operator
    spec:
      containers:
      - args:
        - run
        - --log.level=info
        - --log.format=json
        - --refresh.interval=15s
        - --namespace=$(NAMESPACE)
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: saswatamcode/configmap-operator
        imagePullPolicy: IfNotPresent
        name: configmap-operator
        ports:
        - containerPort: 9090
        resources: {}
      serviceAccount: configmap-operator-sa

Watch the ConfigMap for changes and see the data field get populated with a prom.yaml sample Prometheus config.

watch kubectl get configmap example-prom-config --namespace configmap-operator-demo -o yaml 

Credits

Initially inspired by cloud-native-skunkworks/cnskunkworks-operator!

Contributors

saswatamcode

Issues