美文网首页
pod配置之enableServiceLinks

pod配置之enableServiceLinks

作者: wwq2020 | 来源:发表于2023-05-18 19:00 被阅读0次

背景

在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
        }
    }
        ...
}

相关文章

网友评论

      本文标题:pod配置之enableServiceLinks

      本文链接:https://www.haomeiwen.com/subject/mlqgsdtx.html