K8S Read config map via go API
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Reading a ConfigMap from Go means using the Kubernetes client-go library to talk to the API server and fetch the object from a namespace. The overall flow is straightforward: build a client configuration, create a typed clientset, and call the ConfigMaps API for the namespace you care about.
Create the Kubernetes Client
In an external tool or local script, the most common setup is to read kubeconfig from disk.
This reads the ConfigMap named app-config from the default namespace and prints its key-value data.
Read a Specific Key
Most applications do not need the whole object. They need one configuration entry.
ConfigMap.Data stores string keys and string values. If you use binary content, check BinaryData instead.
In-Cluster Code Uses a Different Config Source
If the Go program runs inside Kubernetes as a Pod, use in-cluster configuration instead of a kubeconfig file.
You need this extra import:
This works when the Pod has the right service account credentials and RBAC permissions.
Handle Errors Carefully
A failed Get call can mean different things:
- The ConfigMap does not exist
- The namespace is wrong
- The caller lacks
getpermission - The client cannot authenticate to the API server
In real code, inspect the error instead of panicking immediately.
If you want more control, use Kubernetes error helpers such as apierrors.IsNotFound(err).
RBAC Still Applies
Reading a ConfigMap through the API requires permission. A Pod running inside the cluster does not automatically get read access to every namespace.
A minimal role might look like this:
Then bind that role to the service account used by the Pod.
When API Access Is the Right Choice
Kubernetes also lets Pods consume ConfigMaps as mounted files or environment variables. Reading via the API is useful when:
- You are writing an operator or controller
- You need to read arbitrary namespaces programmatically
- You want to watch for updates and react dynamically
If a normal application only needs static config at startup, mounting the ConfigMap into the Pod may be simpler than calling the API directly.
Common Pitfalls
A common mistake is forgetting the namespace. ConfigMaps are namespaced objects, so looking in the wrong namespace makes a valid ConfigMap appear missing.
Another mistake is using BuildConfigFromFlags inside a Pod. In-cluster code should usually call rest.InClusterConfig() instead.
A third mistake is debugging the client code before checking RBAC. The code can be correct while the service account simply lacks permission to read ConfigMaps.
Summary
- Use
client-goto build a config, create a clientset, and callCoreV1().ConfigMaps(namespace).Get(...). - Use kubeconfig for external tools and
InClusterConfigfor Pods running inside the cluster. - Read values from
ConfigMap.DataorBinaryDatadepending on the content. - Make sure the caller has RBAC permission to
getthe ConfigMap. - Consider mounted files or environment variables if API access is unnecessary.
Related reading
- Kafka Find Controller ID in a cluster using Kraft protocol
- Kafka in Kubernetes - Marking the coordinator dead for group
- Kafka in Kubernetes Cluster- How to publish/consume messages from outside of Kubernetes Cluster
- Kafka inaccessible once inside Kubernetes/Minikube
- Kafka broker constantly ISR shrinking and expanding?
- Kafka check queue size
- Kafka - Consumer group creation with specific offset?
- Kafka - How to use filter and filternot at the same time?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.