0

I have statefulset mongo-replica, It creates Two replica, I want to set a new label ( COMPANY ) foreach pod (replica) it's value should be the pod's name .e.g.:

in POD mongo-replica-0 -> COMPANY: mongo-replica-0
in POD mongo-replica-1 -> COMPANY: mongo-replica-1 

So, Is there away to do it, automatically in Creating/Restarting pod ?

I know we can do it via kubectl label, but it manual

Net Sim
  • 337
  • 2
  • 6
  • 20

1 Answers1

0

At the time of writing this, there is not such a dedicated tool for this purpose. There are two things that comes to my mind here:

  • Use an initContainer for the Statefulset Pods that uses service account created upon it with appropriate permissions. initContainer will then run a command like kubectl label pod $hostname company=$HOSTNAME"

    This article shows how run kubectl from within a pod with image build, service account and roles creation.


  • Creating some sort of bash script that will run in pod/job and automate this process for you:

 a=$(kubectl get pods -o jsonpath='{.items[*].[metadata.name](http://metadata.name/)}' -l app=$stsname) for n in $a ; do kubectl label pod $n company="$n" --overwrite ; done

  • Create custom mutating webhook/controller that will modify those objects.

Here is good article that describes how to write basic kubernetes mutating admission webhook. Kubernetes official documentaion shares a very good section about dynamic admission control that is worth checking out.

acid_fuji
  • 6,287
  • 7
  • 22