How can I get the clusters CIDR in a pod?

8/21/2020

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
kubernetes

1 Answer

8/27/2020

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 andGoogle Kubernetes Engine about CIDR. You can check out this thread for more information.

-- PjoterS
Source: StackOverflow