Home > database >  How do I Reference Service's Property from another Kubernetes Entity?
How do I Reference Service's Property from another Kubernetes Entity?

Time:07-25

Is there any option to reference service's property from another entity, Like Config Map or Deployment? To be more specific I want to put service's name in ConfigMap, not by myself, but rather to link it programmatically.

apiVersion: v1 
kind: ConfigMap 
metadata:
  name: config-map 
  namespace: ConfigMap-Namespace 

Data:
  ServiceName: <referenced-service-name> 

--- 

apiVersion: v1 
kind: Service 
metadata:
 name: service-name           /// that name I want to put in ConfigMap.
 namespace: ConfigMap-Namespace 
spec:
   .... 

Thanks...

CodePudding user response:

Using plain kubectl, there is no way to dynamically fill in content like this. There are very limited exceptions around injecting values into environment variables in Pods (and PodSpecs inside other objects) but a ConfigMap must contain fixed content.

In this example, the Service object name is fixed, and I'd just embed the same fixed string into the ConfigMap.

If you were using a templating engine like Helm then you could call the same template code to render the Service name in both places. For example:

{{- define "service.name" -}}
{{ .Release.Name }}-service
{{- end -}}
---
apiVersion: v1
kind: Service
metadata: 
  name: {{ include "service.name" . }}
...
---
apiVersion: v1
kind: ConfigMap
metadata: { ... }
data:
  serviceName: {{ include "service.name" . }}
  • Related