3

How can I use the cluster CIDR (the ip address range containing all pod ip addresses) inside a pod? (Autmoatically, without putting it manually in an environment variable, ConfigMap or anywhere else.)

Exampel of what I would like to:

env:
  - name: CLUSTER_CIDR
    valueFrom: # ??? does a configMap like this exist ??? Or any other source for clusterCidr?
      configMap:
       key: clusterCidr
       name: ...

my best partial solution:

  - name: POD_IP
    valueFrom:
      fieldRef:
        fieldPath: status.podIP

  - name: GUESSED_CLUSTER_CIDR
    value: $(POD_IP)/16

I can find clusterCidr inside the configMap full-cluster-state in namespace kube-system somewhere in the value of key full-cluster-state. But this value is a string containing json, and it looks vendor specific (in currentState.rkeConfig.services.kubeController.clusterCidr). I can not extract part of the the value in deployment.yaml. And I prefer to have a vendor independent solution.

I have not idea where to find ComponentConfig mentioned in related issues and do not even know if it is in alpha still.


related k8s issues (all closed without (clear) fixing):

About finding the CIDR of the cluster manually:

simohe
  • 613
  • 7
  • 20
  • Not sure if I got what you want to achieve. As you have ConfigMap with Cidrs why you wont use it as its described in [docs](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/). Also as you have json output you can open if with YAML output using `-o yaml`. You can also create own configmap from file or some values. – PjoterS Aug 21 '20 at 12:37
  • I extended the question with info why I can not use the mentioned config map and that I look for a vendor independent solution. – simohe Aug 24 '20 at 09:30

1 Answers1

0

Im afraid there is no vendor independent solution for this. Also ComponentConfig is still an alpha feature so there is not enough proper documentation.

However, the best thing right now (even if it's not universal) is to use:

$ kubectl cluster-info dump | grep -m 1 cluster-cidr

Then you can create a new ConfigMap with the cluster CIDR value that was outputted and then refer to it in the pod as in this docs.

Even if the concept is the same, you will have to apply a different approach in different environments. Unfortunately, as of today there is no single solution.

As for the additional information, I have already made a small comparison between Kubeadm and Google Kubernetes Engine about CIDR. You can check out this thread for more information.

PjoterS
  • 12,841
  • 1
  • 22
  • 54
  • What I want is to get the value automatically in the pod. (No manual putting it in an environment variable, ConfigMap or whatever. ) The answer is it does not work currently. (My partial solution GUESSED_CLUSTER_CIDR seems to be the closest of what I want.) – simohe Aug 28 '20 at 17:00