Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of ConfigMap monitoring #131

Open
vasyakrg opened this issue Mar 3, 2020 · 4 comments
Open

Add example of ConfigMap monitoring #131

vasyakrg opened this issue Mar 3, 2020 · 4 comments
Labels
example The idea for a new example good first issue Good for newcomers
Milestone

Comments

@vasyakrg
Copy link

vasyakrg commented Mar 3, 2020

monitored configMaps too.

{ "configVersion":"v1", "kubernetes":[ { "apiVersion": "events.k8s.io/v1beta1", "kind": "Event", "namespace": { "nameSelector": { "matchNames": ["example-monitor-events"] } }, "fieldSelector": { "matchExpressions": [ { "field": "metadata.namespace", "operator": "Equals", "value": "example-monitor-events" } ] } } ] }

monitored only activity of pods

@diafour
Copy link
Contributor

diafour commented Mar 3, 2020

There was a similar question: #22
Event is a special resource and it is not needed to monitor Added/Modified/Deleted events.

The example of a config:

configVersion: v1
kubernetes:
- name: ConfigMapMonitor
  apiVersion: v1
  kind: ConfigMap
  watchEvent:
  - Added
  - Deleted
  - Modified
  jqFilter: ".data"

P.S. I think it is a good idea for a new example!

@diafour diafour changed the title Monitoring configMaps Add example of ConfigMap monitoring Mar 3, 2020
@diafour diafour added enhancement New feature or request good first issue Good for newcomers example The idea for a new example and removed enhancement New feature or request labels Mar 3, 2020
@kp3642
Copy link

kp3642 commented Jul 28, 2020

Hi @diafour

Thanks for marking this as good first issue . I would love to work on this and file a PR. let me know what exactly you want me to do ? A bit more description will be helpful .

@diafour
Copy link
Contributor

diafour commented Jul 29, 2020

This issue is to create a new example.

There are examples to monitor Pods, Namespaces, Secrets:
https://github.com/flant/shell-operator/tree/master/examples/101-monitor-pods
https://github.com/flant/shell-operator/tree/master/examples/102-monitor-namespaces
https://github.com/flant/shell-operator/tree/master/examples/104-secret-copier
And there is no example to monitor ConfigMap. It will be great to create 107-monitor-configmap example.

I have several scenarios in mind:

  1. Simply copy 101-monitor-pods to 107-monitor-configmap and change configuration.

  2. Also, an advanced example can be added: use ConfigMap modifications to configure additional annotations for Nodes.

@diafour diafour added this to the future milestone Mar 31, 2021
@mac2000
Copy link

mac2000 commented Jul 3, 2021

Config maps can be monitored the same way as in pods example:

oper.yml

---
apiVersion: v1
kind: Namespace
metadata:
  name: oper
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: oper
  namespace: oper
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: oper
  namespace: oper
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: oper
  namespace: oper
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: oper
subjects:
- kind: ServiceAccount
  name: oper
  namespace: oper
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: oper
  namespace: oper
data:
  entrypoint.sh: |
    #!/usr/bin/env bash

    # https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7.1#installation-via-direct-download---alpine-39-and-310
    apk add --no-cache ca-certificates less ncurses-terminfo-base krb5-libs libgcc libintl libssl1.1 libstdc++ tzdata userspace-rcu zlib icu-libs curl
    apk -X https://dl-cdn.alpinelinux.org/alpine/edge/main add --no-cache lttng-ust
    curl -s -L https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell-7.1.3-linux-alpine-x64.tar.gz -o /tmp/powershell.tar.gz
    mkdir -p /opt/microsoft/powershell/7
    tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
    chmod +x /opt/microsoft/powershell/7/pwsh
    ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh

    # https://github.com/flant/shell-operator/blob/master/Dockerfile#L40
    exec /sbin/tini -- /shell-operator start
  oper.ps1: |
    #!/usr/bin/env pwsh

    if ($args[0] -eq '--config') {
      Write-Host '
      configVersion: v1
      kubernetes:
      - apiVersion: v1
        kind: Pod
        executeHookOnEvent: ["Added"]
      - apiVersion: v1
        kind: ConfigMap
        executeHookOnEvent: ["Added", "Deleted", "Modified"]
      '
    } else {
      $items = Get-Content $env:BINDING_CONTEXT_PATH | ConvertFrom-Json
      foreach($item in $items) {
        $event = $item.watchEvent
        $kind = $item.object.kind
        $name = $item.object.metadata.name
        Write-Host "$kind $name $event"
      }
    }
---
apiVersion: v1
kind: Pod
metadata:
  name: oper
  namespace: oper
spec:
  serviceAccountName: oper
  volumes:
  - name: oper
    configMap:
      name: oper
      defaultMode: 0755
  containers:
  - name: oper
    image: flant/shell-operator:latest
    command: 
      - /entrypoint.sh
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: oper
      subPath: oper.ps1
      mountPath: /hooks/oper.ps1
    - name: oper
      subPath: entrypoint.sh
      mountPath: /entrypoint.sh

Now you can run it:

kubectl apply -f oper.yml

And watch what will happen:

kubectl -n oper logs oper | grep '^{' | grep stdout | jq -r ".msg"

ConfigMap ingress-controller-leader-nginx Modified
ConfigMap hello Added
ConfigMap ingress-controller-leader-nginx Modified
ConfigMap ingress-controller-leader-nginx Modified
Pod demo-1625304780-pp66g Added
ConfigMap ingress-controller-leader-nginx Modified
ConfigMap ingress-controller-leader-nginx Modified
ConfigMap hello Deleted

To cleanup, just delete namespace, e.g. kubectl delete ns oper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example The idea for a new example good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants