背景
在pod中有时我们可以看到一些svc的环境变量,如下
image.png
实际上他是kubelet帮我们添加进去的(针对同namespace有clusterip的svc)
我们可以通过enableServiceLinks设置为false来禁止添加这样的环境变量
但是k8s master的svc对应的环境变量无法禁止添加
相关代码
pkg/kubelet/kubelet_pods.go中
func (kl *Kubelet) getServiceEnvVarMap(ns string, enableServiceLinks bool) (map[string]string, error) {
...
for i := range services {
service := services[i]
// ignore services where ClusterIP is "None" or empty
if !v1helper.IsServiceIPSet(service) {
continue
}
serviceName := service.Name
// We always want to add environment variabled for master services
// from the master service namespace, even if enableServiceLinks is false.
// We also add environment variables for other services in the same
// namespace, if enableServiceLinks is true.
if service.Namespace == kl.masterServiceNamespace && masterServices.Has(serviceName) {
if _, exists := serviceMap[serviceName]; !exists {
serviceMap[serviceName] = service
}
} else if service.Namespace == ns && enableServiceLinks {
serviceMap[serviceName] = service
}
}
...
}
网友评论