shell-operator icon indicating copy to clipboard operation
shell-operator copied to clipboard

Concurrency-Issue /Event-Loss while deploying multiple CR of same group using Helm Chart

Open shreegithub opened this issue 1 year ago • 1 comments

Expected behavior (what you expected to happen):

We have usecases to create Kafka topic on AWS MSK on the fly when any application is deployed within Kubernetes Cluster.

We have written one CR/CRD((Api Group "kafka.abc.io")) having the details of topic information. This CR is deployed as part of the application deployment as Helm Chart.

We have written one hook in operator to listen this CR and do appropriate action to create/update topic based on captured Events(Add/Modify).

When Helm chart deploy multiple CR of same Api Group("kafka.abc.io") then shell-operator should capture all the events for the CR added to the cluster.

Actual behavior (what actually happened):

shell-operator is able to capture "Add" Event of only one CR. Other "Add" Events of different CR are being losses because all the CR are added into K8s cluster at same timestamp by Helm Chart

Steps to reproduce:

Apply the below CR using helm chart :

apiVersion: "kafka.abc.io/v1"
kind: topics
metadata:
  name: topics-1
  namespace: test
spec:
  topicName: "kafka-topic1"
  sharedTopicDep: false
---
apiVersion: "kafka.abc.io/v1"
kind: topics
metadata:
  name: topics-2
  namespace: test
spec:
  topicName: "kafka-topic2"
  sharedTopicDep: false

Environment:

  • Shell-operator version: latest
  • Kubernetes version: 1.28
  • Installation type (kubectl apply, helm chart, etc.): Kubectl apply

Anything else we should know?: Is there any solution to capture the events which got missed due to concurrency?

Script
#!/usr/bin/env bash

if [[ $1 == "--config" ]] ; then cat <<EOF { "configVersion":"v1", "kubernetes":[{ "apiVersion": "kafka.abc.io/v1", "kind": "topics", "executeHookOnEvent":["Added"] }] } EOF else type=$(jq -r '.[0].type' ${BINDING_CONTEXT_PATH})

if [[ $type == "Event" ]] ; then echo "${BINDING_CONTEXT_PATH} @@@@@@@@@@@@@@@@@@@@@@@@@@" name=$(jq -r '.[0].object.metadata.name' ${BINDING_CONTEXT_PATH}) kind=$(jq -r '.[0].object.kind' ${BINDING_CONTEXT_PATH}) echo "${kind}/${name} object is added ###################" fi

fi

Logs
{"binding":"kubernetes","event":"kubernetes","hook":"cr-create-call-topic.sh","level":"info","msg":"Execute hook","queue":"main","task":"HookRun","time":"2024-07-23T11:18:25Z"}
{"binding":"kubernetes","event":"kubernetes","hook":"cr-create-call-topic.sh","level":"info","msg":"/tmp/shell-operator/hook-cr-create-call-topic-sh-binding-context-7a6cdaad-b1a9-4206-977f-8300fe3d456b.json @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@","output":"stdout","queue":"main","task":"HookRun","time":"2024-07-23T11:18:25Z"}
{"binding":"kubernetes","event":"kubernetes","hook":"cr-create-call-topic.sh","level":"info","msg":"topics/topics-1 object is added ###################","output":"stdout","queue":"main","task":"HookRun","time":"2024-07-23T11:18:25Z"}

shreegithub avatar Jul 28 '24 13:07 shreegithub

@shreegithub you followed one of the examples that do

type=$(jq -r '.[0].type' ${BINDING_CONTEXT_PATH})

which is always ofc the 0 item in an array.

It's explained here that this is an array potentially containing multiple bindings, including the ones you're missing: https://flant.github.io/shell-operator/HOOKS.html

For an example of walking over the array rather than taking only the first one: https://github.com/flant/shell-operator/blob/main/examples/102-monitor-namespaces/hooks/namespace-hook.sh#L30

The-Loeki avatar Mar 05 '25 07:03 The-Loeki